|
| 1 | +#include "LowsideCurrentSense.h" |
| 2 | +// LowsideCurrentSensor constructor |
| 3 | +// - shunt_resistor - shunt resistor value |
| 4 | +// - gain - current-sense op-amp gain |
| 5 | +// - phA - A phase adc pin |
| 6 | +// - phB - B phase adc pin |
| 7 | +// - phC - C phase adc pin (optional) |
| 8 | +LowsideCurrentSense::LowsideCurrentSense(float _shunt_resistor, float _gain, int _pinA, int _pinB, int _pinC){ |
| 9 | + pinA = _pinA; |
| 10 | + pinB = _pinB; |
| 11 | + pinC = _pinC; |
| 12 | + |
| 13 | + shunt_resistor = _shunt_resistor; |
| 14 | + amp_gain = _gain; |
| 15 | + volts_to_amps_ratio = 1.0 /_shunt_resistor / _gain; // volts to amps |
| 16 | + // gains for each phase |
| 17 | + gain_a = volts_to_amps_ratio; |
| 18 | + gain_b = volts_to_amps_ratio; |
| 19 | + gain_c = volts_to_amps_ratio; |
| 20 | +} |
| 21 | + |
| 22 | +// Lowside sensor init function |
| 23 | +void LowsideCurrentSense::init(){ |
| 24 | + // configure ADC variables |
| 25 | + _configureADCLowSide(pinA,pinB,pinC); |
| 26 | + // calibrate zero offsets |
| 27 | + calibrateOffsets(); |
| 28 | +} |
| 29 | +// Function finding zero offsets of the ADC |
| 30 | +void LowsideCurrentSense::calibrateOffsets(){ |
| 31 | + // find adc offset = zero current voltage |
| 32 | + offset_ia =0; |
| 33 | + offset_ib= 0; |
| 34 | + offset_ic= 0; |
| 35 | + // read the adc voltage 1000 times ( arbitrary number ) |
| 36 | + for (int i = 0; i < 1000; i++) { |
| 37 | + offset_ia += _readADCVoltageLowSide(pinA); |
| 38 | + offset_ib += _readADCVoltageLowSide(pinB); |
| 39 | + if(_isset(pinC)) offset_ic += _readADCVoltageLowSide(pinC); |
| 40 | + _delay(1); |
| 41 | + } |
| 42 | + // 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 / 1000.0; |
| 46 | +} |
| 47 | + |
| 48 | +// read all three phase currents (if possible 2 or 3) |
| 49 | +PhaseCurrent_s LowsideCurrentSense::getPhaseCurrents(){ |
| 50 | + PhaseCurrent_s current; |
| 51 | + current.a = (_readADCVoltageLowSide(pinA) - offset_ia)*gain_a;// amps |
| 52 | + current.b = (_readADCVoltageLowSide(pinB) - offset_ib)*gain_b;// amps |
| 53 | + current.c = (!_isset(pinC)) ? 0 : (_readADCVoltageLowSide(pinC) - offset_ic)*gain_c; // amps |
| 54 | + return current; |
| 55 | +} |
| 56 | +// Function synchronizing current sense with motor driver. |
| 57 | +// for in-line sensig no such thing is necessary |
| 58 | +int LowsideCurrentSense::driverSync(BLDCDriver *driver){ |
| 59 | + _driverSyncLowSide(); |
| 60 | + return 1; |
| 61 | +} |
| 62 | + |
| 63 | +// Function aligning the current sense with motor driver |
| 64 | +// if all pins are connected well none of this is really necessary! - can be avoided |
| 65 | +// returns flag |
| 66 | +// 0 - fail |
| 67 | +// 1 - success and nothing changed |
| 68 | +// 2 - success but pins reconfigured |
| 69 | +// 3 - success but gains inverted |
| 70 | +// 4 - success but pins reconfigured and gains inverted |
| 71 | +int LowsideCurrentSense::driverAlign(BLDCDriver *driver, float voltage){ |
| 72 | + //gain_a *= -1; |
| 73 | + //gain_b *= -1; |
| 74 | + //gain_c *= -1; |
| 75 | + |
| 76 | + /* |
| 77 | + int exit_flag = 1; |
| 78 | + if(skip_align) return exit_flag; |
| 79 | +
|
| 80 | + // set phase A active and phases B and C down |
| 81 | + driver->setPwm(voltage, 0, 0); |
| 82 | + _delay(2000); |
| 83 | + PhaseCurrent_s c = getPhaseCurrents(); |
| 84 | + // read the current 100 times ( arbitrary number ) |
| 85 | + for (int i = 0; i < 100; i++) { |
| 86 | + PhaseCurrent_s c1 = getPhaseCurrents(); |
| 87 | + c.a = c.a*0.6 + 0.4*c1.a; |
| 88 | + c.b = c.b*0.6 + 0.4*c1.b; |
| 89 | + c.c = c.c*0.6 + 0.4*c1.c; |
| 90 | + _delay(3); |
| 91 | + } |
| 92 | + driver->setPwm(0, 0, 0); |
| 93 | + // align phase A |
| 94 | + float ab_ratio = fabs(c.a / c.b); |
| 95 | + float ac_ratio = c.c ? fabs(c.a / c.c) : 0; |
| 96 | + if( ab_ratio > 1.5 ){ // should be ~2 |
| 97 | + gain_a *= _sign(c.a); |
| 98 | + }else if( ab_ratio < 0.7 ){ // should be ~0.5 |
| 99 | + // switch phase A and B |
| 100 | + int tmp_pinA = pinA; |
| 101 | + pinA = pinB; |
| 102 | + pinB = tmp_pinA; |
| 103 | + gain_a *= _sign(c.b); |
| 104 | + exit_flag = 2; // signal that pins have been switched |
| 105 | + }else if(_isset(pinC) && ac_ratio < 0.7 ){ // should be ~0.5 |
| 106 | + // switch phase A and C |
| 107 | + int tmp_pinA = pinA; |
| 108 | + pinA = pinC; |
| 109 | + pinC= tmp_pinA; |
| 110 | + gain_a *= _sign(c.c); |
| 111 | + exit_flag = 2;// signal that pins have been switched |
| 112 | + }else{ |
| 113 | + // error in current sense - phase either not measured or bad connection |
| 114 | + return 0; |
| 115 | + } |
| 116 | +
|
| 117 | + // set phase B active and phases A and C down |
| 118 | + driver->setPwm(0, voltage, 0); |
| 119 | + _delay(200); |
| 120 | + c = getPhaseCurrents(); |
| 121 | + // read the current 50 times |
| 122 | + for (int i = 0; i < 100; i++) { |
| 123 | + PhaseCurrent_s c1 = getPhaseCurrents(); |
| 124 | + c.a = c.a*0.6 + 0.4*c1.a; |
| 125 | + c.b = c.b*0.6 + 0.4*c1.b; |
| 126 | + c.c = c.c*0.6 + 0.4*c1.c; |
| 127 | + _delay(3); |
| 128 | + } |
| 129 | + driver->setPwm(0, 0, 0); |
| 130 | + float ba_ratio = fabs(c.b/c.a); |
| 131 | + float bc_ratio = c.c ? fabs(c.b / c.c) : 0; |
| 132 | + if( ba_ratio > 1.5 ){ // should be ~2 |
| 133 | + gain_b *= _sign(c.b); |
| 134 | + }else if( ba_ratio < 0.7 ){ // it should be ~0.5 |
| 135 | + // switch phase A and B |
| 136 | + int tmp_pinB = pinB; |
| 137 | + pinB = pinA; |
| 138 | + pinA = tmp_pinB; |
| 139 | + gain_b *= _sign(c.a); |
| 140 | + exit_flag = 2; // signal that pins have been switched |
| 141 | + }else if(_isset(pinC) && bc_ratio < 0.7 ){ // should be ~0.5 |
| 142 | + // switch phase A and C |
| 143 | + int tmp_pinB = pinB; |
| 144 | + pinB = pinC; |
| 145 | + pinC = tmp_pinB; |
| 146 | + gain_b *= _sign(c.c); |
| 147 | + exit_flag = 2; // signal that pins have been switched |
| 148 | + }else{ |
| 149 | + // error in current sense - phase either not measured or bad connection |
| 150 | + return 0; |
| 151 | + } |
| 152 | +
|
| 153 | + // if phase C measured |
| 154 | + if(_isset(pinC)){ |
| 155 | + // set phase B active and phases A and C down |
| 156 | + driver->setPwm(0, 0, voltage); |
| 157 | + _delay(200); |
| 158 | + c = getPhaseCurrents(); |
| 159 | + // read the adc voltage 500 times ( arbitrary number ) |
| 160 | + for (int i = 0; i < 50; i++) { |
| 161 | + PhaseCurrent_s c1 = getPhaseCurrents(); |
| 162 | + c.c = (c.c+c1.c)/50.0; |
| 163 | + } |
| 164 | + driver->setPwm(0, 0, 0); |
| 165 | + gain_c *= _sign(c.c); |
| 166 | + } |
| 167 | +
|
| 168 | + if(gain_a < 0 || gain_b < 0 || gain_c < 0) exit_flag +=2; |
| 169 | + // exit flag is either |
| 170 | + // 0 - fail |
| 171 | + // 1 - success and nothing changed |
| 172 | + // 2 - success but pins reconfigured |
| 173 | + // 3 - success but gains inverted |
| 174 | + // 4 - success but pins reconfigured and gains inverted |
| 175 | +
|
| 176 | + return exit_flag; |
| 177 | + */ |
| 178 | + return 1; |
| 179 | +} |
0 commit comments