Skip to content

Commit 48d38d3

Browse files
committed
FEAT merge with dev
2 parents 42ce551 + 644389b commit 48d38d3

19 files changed

+676
-77
lines changed

keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ StepperDriver KEYWORD1
1818
PIDController KEYWORD1
1919
LowPassFilter KEYWORD1
2020
InlineCurrentSense KEYWORD1
21+
LowsideCurrentSense KEYWORD1
2122
CurrentSense KEYWORD1
2223
Commander KEYWORD1
2324
StepDirListener KEYWORD1
@@ -48,6 +49,9 @@ LPF_current_q KEYWORD2
4849
LPF_current_d KEYWORD2
4950
P_angle KEYWORD2
5051
LPF_angle KEYWORD2
52+
lpf_a KEYWORD2
53+
lpf_b KEYWORD2
54+
lpf_c KEYWORD2
5155

5256
MotionControlType KEYWORD1
5357
TorqueControlType KEYWORD1

src/common/defaults.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@
4343
// align voltage
4444
#define DEF_VOLTAGE_SENSOR_ALIGN 3.0f //!< default voltage for sensor and motor zero alignemt
4545
// low pass filter velocity
46-
#define DEF_VEL_FILTER_Tf 0.005f //!< default velocity filter time constant
46+
#define DEF_VEL_FILTER_Tf 0.005 //!< default velocity filter time constant
47+
48+
// current sense default parameters
49+
#define DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf 0.0f //!< default currnet sense per phase low pass filter time constant

src/current_sense/InlineCurrentSense.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ void InlineCurrentSense::calibrateOffsets(){
5050
// read all three phase currents (if possible 2 or 3)
5151
PhaseCurrent_s InlineCurrentSense::getPhaseCurrents(){
5252
PhaseCurrent_s current;
53-
current.a = (_readADCVoltageInline(pinA) - offset_ia)*gain_a;// amps
54-
current.b = (_readADCVoltageInline(pinB) - offset_ib)*gain_b;// amps
55-
current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageInline(pinC) - offset_ic)*gain_c; // amps
53+
current.a = lpf_a(_readADCVoltageInline(pinA) - offset_ia)*gain_a;// amps
54+
current.b = lpf_b(_readADCVoltageInline(pinB) - offset_ib)*gain_b;// amps
55+
current.c = (!_isset(pinC)) ? 0 : lpf_c(_readADCVoltageInline(pinC) - offset_ic)*gain_c; // amps
5656
return current;
5757
}
5858
// Function synchronizing current sense with motor driver.

src/current_sense/InlineCurrentSense.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include "Arduino.h"
55
#include "../common/foc_utils.h"
66
#include "../common/time_utils.h"
7+
#include "../common/defaults.h"
78
#include "../common/base_classes/CurrentSense.h"
9+
#include "../common/lowpass_filter.h"
810
#include "hardware_api.h"
911

1012

@@ -33,8 +35,13 @@ class InlineCurrentSense: public CurrentSense{
3335
float gain_b; //!< phase B gain
3436
float gain_c; //!< phase C gain
3537

36-
private:
38+
// per phase low pass fileters
39+
LowPassFilter lpf_a{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current A low pass filter
40+
LowPassFilter lpf_b{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current B low pass filter
41+
LowPassFilter lpf_c{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current C low pass filter
3742

43+
private:
44+
3845
// hardware variables
3946
int pinA; //!< pin A analog pin for current measurement
4047
int pinB; //!< pin B analog pin for current measurement

src/current_sense/LowsideCurrentSense.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ void LowsideCurrentSense::init(){
2727
calibrateOffsets();
2828
}
2929
// Function finding zero offsets of the ADC
30-
void LowsideCurrentSense::calibrateOffsets(){
30+
void LowsideCurrentSense::calibrateOffsets(){
31+
const int calibration_rounds = 1000;
32+
3133
// find adc offset = zero current voltage
32-
offset_ia =0;
33-
offset_ib= 0;
34-
offset_ic= 0;
34+
offset_ia = 0;
35+
offset_ib = 0;
36+
offset_ic = 0;
3537
// read the adc voltage 1000 times ( arbitrary number )
36-
for (int i = 0; i < 1000; i++) {
38+
for (int i = 0; i < calibration_rounds; i++) {
3739
_startADC3PinConversionLowSide();
3840
offset_ia += _readADCVoltageLowSide(pinA);
3941
offset_ib += _readADCVoltageLowSide(pinB);
@@ -47,12 +49,11 @@ void LowsideCurrentSense::calibrateOffsets(){
4749
}
4850

4951
// read all three phase currents (if possible 2 or 3)
50-
PhaseCurrent_s LowsideCurrentSense::getPhaseCurrents(){
5152
PhaseCurrent_s current;
5253
_startADC3PinConversionLowSide();
53-
current.a = (_readADCVoltageLowSide(pinA) - offset_ia)*gain_a;// amps
54-
current.b = (_readADCVoltageLowSide(pinB) - offset_ib)*gain_b;// amps
55-
current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageLowSide(pinC) - offset_ic)*gain_c; // amps
54+
current.a = lpf_a(_readADCVoltageLowSide(pinA) - offset_ia)*gain_a;// amps
55+
current.b = lpf_b(_readADCVoltageLowSide(pinB) - offset_ib)*gain_b;// amps
56+
current.c = (!_isset(pinC)) ? 0 : lpf_b(_readADCVoltageLowSide(pinC) - offset_ic)*gain_c; // amps
5657
return current;
5758
}
5859
// Function synchronizing current sense with motor driver.

src/current_sense/LowsideCurrentSense.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include "Arduino.h"
55
#include "../common/foc_utils.h"
66
#include "../common/time_utils.h"
7+
#include "../common/defaults.h"
78
#include "../common/base_classes/CurrentSense.h"
89
#include "../common/base_classes/FOCMotor.h"
10+
#include "../common/lowpass_filter.h"
911
#include "hardware_api.h"
1012

1113

@@ -34,6 +36,11 @@ class LowsideCurrentSense: public CurrentSense{
3436
float gain_b; //!< phase B gain
3537
float gain_c; //!< phase C gain
3638

39+
// per phase low pass fileters
40+
LowPassFilter lpf_a{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current A low pass filter
41+
LowPassFilter lpf_b{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current B low pass filter
42+
LowPassFilter lpf_c{DEF_LPF_PER_PHASE_CURRENT_SENSE_Tf}; //!< current C low pass filter
43+
3744
private:
3845

3946
// hardware variables

src/current_sense/hardware_api.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ float _readADCVoltageLowSide(const int pinA);
4242
* function syncing the Driver with the ADC for the LowSide Sensing
4343
*/
4444
void _driverSyncLowSide();
45+
46+
47+
void _startADC3PinConversionLowSide();
48+
4549
#endif
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "../hardware_api.h"
2+
3+
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__)
4+
5+
#define _ADC_VOLTAGE 5.0
6+
#define _ADC_RESOLUTION 1024.0
7+
8+
// adc counts to voltage conversion ratio
9+
// some optimizing for faster execution
10+
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
11+
12+
#ifndef cbi
13+
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
14+
#endif
15+
#ifndef sbi
16+
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
17+
#endif
18+
19+
// adc counts to voltage conversion ratio
20+
// some optimizing for faster execution
21+
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
22+
23+
// function reading an ADC value and returning the read voltage
24+
float _readADCVoltageInline(const int pinA){
25+
uint32_t raw_adc = analogRead(pinA);
26+
return raw_adc * _ADC_CONV;
27+
}
28+
// function reading an ADC value and returning the read voltage
29+
void _configureADCInline(const int pinA,const int pinB,const int pinC){
30+
pinMode(pinA, INPUT);
31+
pinMode(pinB, INPUT);
32+
if( _isset(pinC) ) pinMode(pinC, INPUT);
33+
34+
// set hight frequency adc - ADPS2,ADPS1,ADPS0 | 001 (16mhz/2) | 010 ( 16mhz/4 ) | 011 (16mhz/8) | 100(16mhz/16) | 101 (16mhz/32) | 110 (16mhz/64) | 111 (16mhz/128 default)
35+
// set divisor to 8 - adc frequency 16mhz/8 = 2 mhz
36+
// arduino takes 25 conversions per sample so - 2mhz/25 = 80k samples per second - 12.5us per sample
37+
cbi(ADCSRA, ADPS2);
38+
sbi(ADCSRA, ADPS1);
39+
sbi(ADCSRA, ADPS0);
40+
}
41+
42+
43+
// function reading an ADC value and returning the read voltage
44+
float _readADCVoltageLowSide(const int pinA){
45+
return _readADCVoltageInline(pinA);
46+
}
47+
// Configure low side for generic mcu
48+
// cannot do much but
49+
void _configureADCLowSide(const int pinA,const int pinB,const int pinC){
50+
pinMode(pinA, INPUT);
51+
pinMode(pinB, INPUT);
52+
if( _isset(pinC) ) pinMode(pinC, INPUT);
53+
}
54+
55+
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "../hardware_api.h"
2+
3+
#if defined(__arm__) && defined(__SAM3X8E__)
4+
5+
#define _ADC_VOLTAGE 3.3
6+
#define _ADC_RESOLUTION 1024.0
7+
8+
// adc counts to voltage conversion ratio
9+
// some optimizing for faster execution
10+
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )
11+
12+
// function reading an ADC value and returning the read voltage
13+
float _readADCVoltageInline(const int pinA){
14+
uint32_t raw_adc = analogRead(pinA);
15+
return raw_adc * _ADC_CONV;
16+
}
17+
// function reading an ADC value and returning the read voltage
18+
void _configureADCInline(const int pinA,const int pinB,const int pinC){
19+
pinMode(pinA, INPUT);
20+
pinMode(pinB, INPUT);
21+
if( _isset(pinC) ) pinMode(pinC, INPUT);
22+
}
23+
24+
25+
// function reading an ADC value and returning the read voltage
26+
float _readADCVoltageLowSide(const int pinA){
27+
return _readADCVoltageInline(pinA);
28+
}
29+
// Configure low side for generic mcu
30+
// cannot do much but
31+
void _configureADCLowSide(const int pinA,const int pinB,const int pinC){
32+
pinMode(pinA, INPUT);
33+
pinMode(pinB, INPUT);
34+
if( _isset(pinC) ) pinMode(pinC, INPUT);
35+
}
36+
37+
38+
#endif

0 commit comments

Comments
 (0)