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