Skip to content

Commit 37fd4af

Browse files
authored
Merge pull request #69 from sDessens/random_fixes
random fixes related to current sensing
2 parents dce7906 + dd73d88 commit 37fd4af

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/common/base_classes/CurrentSense.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ float CurrentSense::getDCCurrent(float motor_electrical_angle){
1717
i_alpha = current.a;
1818
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
1919
}else{
20-
i_alpha = 2*(current.a - (current.b - current.c))/3.0;
21-
i_beta = _2_SQRT3 *( current.b - current.c );
20+
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
21+
float mid = (1.f/3) * (current.a + current.b + current.c);
22+
float a = current.a - mid;
23+
float b = current.b - mid;
24+
i_alpha = a;
25+
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
2226
}
2327

2428
// if motor angle provided function returns signed value of the current
@@ -44,9 +48,13 @@ DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
4448
// if only two measured currents
4549
i_alpha = current.a;
4650
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
47-
}else{
48-
i_alpha = 0.6666667*(current.a - (current.b - current.c));
49-
i_beta = _2_SQRT3 *( current.b - current.c );
51+
} else {
52+
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
53+
float mid = (1.f/3) * (current.a + current.b + current.c);
54+
float a = current.a - mid;
55+
float b = current.b - mid;
56+
i_alpha = a;
57+
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
5058
}
5159

5260
// calculate park transform

src/current_sense/InlineCurrentSense.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ void InlineCurrentSense::init(){
2828
}
2929
// Function finding zero offsets of the ADC
3030
void InlineCurrentSense::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
offset_ia += _readADCVoltage(pinA);
3840
offset_ib += _readADCVoltage(pinB);
3941
if(_isset(pinC)) offset_ic += _readADCVoltage(pinC);
4042
_delay(1);
4143
}
4244
// calculate the mean offsets
43-
offset_ia = offset_ia / 1000.0;
44-
offset_ib = offset_ib / 1000.0;
45-
if(_isset(pinC)) offset_ic = offset_ic / 500.0;
45+
offset_ia = offset_ia / calibration_rounds;
46+
offset_ib = offset_ib / calibration_rounds;
47+
if(_isset(pinC)) offset_ic = offset_ic / calibration_rounds;
4648
}
4749

4850
// read all three phase currents (if possible 2 or 3)

src/current_sense/InlineCurrentSense.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class InlineCurrentSense: public CurrentSense{
2929
// ADC measuremnet gain for each phase
3030
// support for different gains for different phases of more commonly - inverted phase currents
3131
// this should be automated later
32-
int gain_a; //!< phase A gain
33-
int gain_b; //!< phase B gain
34-
int gain_c; //!< phase C gain
32+
float gain_a; //!< phase A gain
33+
float gain_b; //!< phase B gain
34+
float gain_c; //!< phase C gain
3535

3636
private:
3737

0 commit comments

Comments
 (0)