@@ -187,19 +187,11 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
187
187
// if yes throw an error and return 0
188
188
// either the current sense is not connected or the current is
189
189
// 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 )){
193
191
SIMPLEFOC_DEBUG (" CS: Err too low current, rise voltage!" );
194
192
return 0 ; // measurement current too low
195
193
}
196
194
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
-
203
195
// now we have to determine
204
196
// 1) which pin correspond to which phase of the bldc driver
205
197
// 2) if the currents measured have good polarity
@@ -244,7 +236,6 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
244
236
_swap (offset_ia, offset_ib);
245
237
_swap (gain_a, gain_b);
246
238
_swap (c_a.b , c_a.b );
247
- _swap (c_b.a , c_b.b ); // for the next phase of alignment
248
239
phases_switched = true ; // signal that pins have been switched
249
240
break ;
250
241
case 2 : // phase C is the max current
@@ -254,7 +245,6 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
254
245
_swap (offset_ia, offset_ic);
255
246
_swap (gain_a, gain_c);
256
247
_swap (c_a.a , c_a.c );
257
- _swap (c_b.a , c_b.c ); // for the next phase of alignment
258
248
phases_switched = true ;// signal that pins have been switched
259
249
break ;
260
250
}
@@ -276,15 +266,13 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
276
266
_swap (offset_ia, offset_ib);
277
267
_swap (gain_a, gain_b);
278
268
_swap (c_a.b , c_a.b );
279
- _swap (c_b.a , c_b.b ); // for the next phase of alignment
280
269
phases_switched = true ; // signal that pins have been switched
281
270
}else if (_isset (pinA) && !_isset (pinC)){
282
271
SIMPLEFOC_DEBUG (" CS: Switch A-(C)NC" );
283
272
_swap (pinA, pinC);
284
273
_swap (offset_ia, offset_ic);
285
274
_swap (gain_a, gain_c);
286
275
_swap (c_a.b , c_a.c );
287
- _swap (c_b.a , c_b.c ); // for the next phase of alignment
288
276
phases_switched = true ; // signal that pins have been switched
289
277
}
290
278
}
@@ -294,6 +282,13 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
294
282
//
295
283
// In either case A is done, now we have to check the phase B and C
296
284
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
+
297
292
// check the phase B
298
293
// find the highest magnitude in c_b
299
294
// 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){
386
381
// 4 - success but pins reconfigured and gains inverted
387
382
int CurrentSense::alignStepperDriver (float voltage, StepperDriver* stepper_driver){
388
383
389
- int exit_flag = 1 ;
384
+ bool phases_switched = 0 ;
385
+ bool phases_inverted = 0 ;
390
386
391
387
if (_isset (pinA)){
392
388
// set phase A active to high and B to low
@@ -410,10 +406,11 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
410
406
_swap (offset_ia, offset_ib);
411
407
_swap (gain_a, gain_b);
412
408
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
414
410
}else if (c.a < 0 ){
415
411
SIMPLEFOC_DEBUG (" CS: Inv A" );
416
412
gain_a *= -1 ;
413
+ phases_inverted = true ; // signal that pins have been inverted
417
414
}
418
415
}
419
416
@@ -432,11 +429,18 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
432
429
if (c.b < 0 ){
433
430
SIMPLEFOC_DEBUG (" CS: Inv B" );
434
431
gain_b *= -1 ;
432
+ phases_inverted = true ; // signal that pins have been inverted
435
433
}
436
434
}
437
435
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 ;
440
444
return exit_flag;
441
445
}
442
446
0 commit comments