Skip to content

random fixes related to current sensing #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/common/base_classes/CurrentSense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ float CurrentSense::getDCCurrent(float motor_electrical_angle){
i_alpha = current.a;
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
}else{
i_alpha = 2*(current.a - (current.b - current.c))/3.0;
i_beta = _2_SQRT3 *( current.b - current.c );
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
float mid = (1.f/3) * (current.a + current.b + current.c);
float a = current.a - mid;
float b = current.b - mid;
i_alpha = a;
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
}

// if motor angle provided function returns signed value of the current
Expand All @@ -44,9 +48,13 @@ DQCurrent_s CurrentSense::getFOCCurrents(float angle_el){
// if only two measured currents
i_alpha = current.a;
i_beta = _1_SQRT3 * current.a + _2_SQRT3 * current.b;
}else{
i_alpha = 0.6666667*(current.a - (current.b - current.c));
i_beta = _2_SQRT3 *( current.b - current.c );
} else {
// signal filtering using identity a + b + c = 0. Assumes measurement error is normally distributed.
float mid = (1.f/3) * (current.a + current.b + current.c);
float a = current.a - mid;
float b = current.b - mid;
i_alpha = a;
i_beta = _1_SQRT3 * a + _2_SQRT3 * b;
}

// calculate park transform
Expand Down
16 changes: 9 additions & 7 deletions src/current_sense/InlineCurrentSense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ void InlineCurrentSense::init(){
}
// Function finding zero offsets of the ADC
void InlineCurrentSense::calibrateOffsets(){
const int calibration_rounds = 1000;

// find adc offset = zero current voltage
offset_ia =0;
offset_ib= 0;
offset_ic= 0;
offset_ia = 0;
offset_ib = 0;
offset_ic = 0;
// read the adc voltage 1000 times ( arbitrary number )
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < calibration_rounds; i++) {
offset_ia += _readADCVoltage(pinA);
offset_ib += _readADCVoltage(pinB);
if(_isset(pinC)) offset_ic += _readADCVoltage(pinC);
_delay(1);
}
// calculate the mean offsets
offset_ia = offset_ia / 1000.0;
offset_ib = offset_ib / 1000.0;
if(_isset(pinC)) offset_ic = offset_ic / 500.0;
offset_ia = offset_ia / calibration_rounds;
offset_ib = offset_ib / calibration_rounds;
if(_isset(pinC)) offset_ic = offset_ic / calibration_rounds;
}

// read all three phase currents (if possible 2 or 3)
Expand Down
6 changes: 3 additions & 3 deletions src/current_sense/InlineCurrentSense.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class InlineCurrentSense: public CurrentSense{
// ADC measuremnet gain for each phase
// support for different gains for different phases of more commonly - inverted phase currents
// this should be automated later
int gain_a; //!< phase A gain
int gain_b; //!< phase B gain
int gain_c; //!< phase C gain
float gain_a; //!< phase A gain
float gain_b; //!< phase B gain
float gain_c; //!< phase C gain

private:

Expand Down