@@ -187,19 +187,11 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
187187 // if yes throw an error and return 0
188188 // either the current sense is not connected or the current is
189189 // too low for calibration purposes (one should raise the motor.voltage_sensor_align)
190- if ((_isset (pinA) && fabs (c_a.a ) < 0 .1f )
191- || (_isset (pinB) && fabs (c_a.b ) < 0 .1f )
192- || (_isset (pinC) && fabs (c_a.c ) < 0 .1f )){
190+ if ((fabs (c_a.a ) < 0 .1f ) && (fabs (c_a.b ) < 0 .1f ) && (fabs (c_a.c ) < 0 .1f )){
193191 SIMPLEFOC_DEBUG (" CS: Err too low current, rise voltage!" );
194192 return 0 ; // measurement current too low
195193 }
196194
197- // set phase B active and phases A and C down
198- bldc_driver->setPwm (0 , voltage, 0 );
199- _delay (500 );
200- PhaseCurrent_s c_b = readAverageCurrents ();
201- bldc_driver->setPwm (0 , 0 , 0 );
202-
203195 // now we have to determine
204196 // 1) which pin correspond to which phase of the bldc driver
205197 // 2) if the currents measured have good polarity
@@ -244,7 +236,6 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
244236 _swap (offset_ia, offset_ib);
245237 _swap (gain_a, gain_b);
246238 _swap (c_a.b , c_a.b );
247- _swap (c_b.a , c_b.b ); // for the next phase of alignment
248239 phases_switched = true ; // signal that pins have been switched
249240 break ;
250241 case 2 : // phase C is the max current
@@ -254,7 +245,6 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
254245 _swap (offset_ia, offset_ic);
255246 _swap (gain_a, gain_c);
256247 _swap (c_a.a , c_a.c );
257- _swap (c_b.a , c_b.c ); // for the next phase of alignment
258248 phases_switched = true ;// signal that pins have been switched
259249 break ;
260250 }
@@ -276,15 +266,13 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
276266 _swap (offset_ia, offset_ib);
277267 _swap (gain_a, gain_b);
278268 _swap (c_a.b , c_a.b );
279- _swap (c_b.a , c_b.b ); // for the next phase of alignment
280269 phases_switched = true ; // signal that pins have been switched
281270 }else if (_isset (pinA) && !_isset (pinC)){
282271 SIMPLEFOC_DEBUG (" CS: Switch A-(C)NC" );
283272 _swap (pinA, pinC);
284273 _swap (offset_ia, offset_ic);
285274 _swap (gain_a, gain_c);
286275 _swap (c_a.b , c_a.c );
287- _swap (c_b.a , c_b.c ); // for the next phase of alignment
288276 phases_switched = true ; // signal that pins have been switched
289277 }
290278 }
@@ -294,6 +282,13 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
294282 //
295283 // In either case A is done, now we have to check the phase B and C
296284
285+
286+ // set phase B active and phases A and C down
287+ bldc_driver->setPwm (0 , voltage, 0 );
288+ _delay (500 );
289+ PhaseCurrent_s c_b = readAverageCurrents ();
290+ bldc_driver->setPwm (0 , 0 , 0 );
291+
297292 // check the phase B
298293 // find the highest magnitude in c_b
299294 // and make sure it's around 2 (1.5 at least) times higher than the other two
@@ -386,7 +381,8 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
386381// 4 - success but pins reconfigured and gains inverted
387382int CurrentSense::alignStepperDriver (float voltage, StepperDriver* stepper_driver){
388383
389- int exit_flag = 1 ;
384+ bool phases_switched = 0 ;
385+ bool phases_inverted = 0 ;
390386
391387 if (_isset (pinA)){
392388 // set phase A active to high and B to low
@@ -410,10 +406,11 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
410406 _swap (offset_ia, offset_ib);
411407 _swap (gain_a, gain_b);
412408 gain_a *= _sign (c.b );
413- exit_flag = 2 ; // signal that pins have been switched
409+ phases_switched = true ; // signal that pins have been switched
414410 }else if (c.a < 0 ){
415411 SIMPLEFOC_DEBUG (" CS: Inv A" );
416412 gain_a *= -1 ;
413+ phases_inverted = true ; // signal that pins have been inverted
417414 }
418415 }
419416
@@ -432,11 +429,18 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
432429 if (c.b < 0 ){
433430 SIMPLEFOC_DEBUG (" CS: Inv B" );
434431 gain_b *= -1 ;
432+ phases_inverted = true ; // signal that pins have been inverted
435433 }
436434 }
437435
438- // add 2 if pin gains negative
439- if (gain_a < 0 || gain_b < 0 ) exit_flag +=2 ;
436+ // construct the return flag
437+ // if success and nothing changed return 1
438+ // if the phases have been switched return 2
439+ // if the gains have been inverted return 3
440+ // if both return 4
441+ uint8_t exit_flag = 1 ;
442+ if (phases_switched) exit_flag += 1 ;
443+ if (phases_inverted) exit_flag += 2 ;
440444 return exit_flag;
441445}
442446
0 commit comments