@@ -177,11 +177,17 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
177177 bool phases_switched = 0 ;
178178 bool phases_inverted = 0 ;
179179
180-
180+ float center = driver->voltage_limit /2.0 ;
181+
181182 // set phase A active and phases B and C down
182- bldc_driver->setPwm (voltage, 0 , 0 );
183+ // 300 ms of ramping
184+ for (int i=0 ; i < 100 ; i++){
185+ bldc_driver->setPwm (voltage/100.0 *((float )i) , 0 , 0 );
186+ _delay (3 );
187+ }
183188 _delay (500 );
184189 PhaseCurrent_s c_a = readAverageCurrents ();
190+ bldc_driver->setPwm (0 , 0 , 0 );
185191 // check if currents are to low (lower than 100mA)
186192 // TODO calculate the 100mA threshold from the ADC resolution
187193 // if yes throw an error and return 0
@@ -191,6 +197,7 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
191197 SIMPLEFOC_DEBUG (" CS: Err too low current, rise voltage!" );
192198 return 0 ; // measurement current too low
193199 }
200+
194201
195202 // now we have to determine
196203 // 1) which pin correspond to which phase of the bldc driver
@@ -284,7 +291,11 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
284291
285292
286293 // set phase B active and phases A and C down
287- bldc_driver->setPwm (0 , voltage, 0 );
294+ // 300 ms of ramping
295+ for (int i=0 ; i < 100 ; i++){
296+ bldc_driver->setPwm (0 , voltage/100.0 *((float )i), 0 );
297+ _delay (3 );
298+ }
288299 _delay (500 );
289300 PhaseCurrent_s c_b = readAverageCurrents ();
290301 bldc_driver->setPwm (0 , 0 , 0 );
@@ -384,53 +395,67 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
384395 bool phases_switched = 0 ;
385396 bool phases_inverted = 0 ;
386397
387- if (_isset (pinA)){
388- // set phase A active to high and B to low
389- stepper_driver->setPwm (voltage, 0 );
390- _delay (500 );
391- PhaseCurrent_s c = readAverageCurrents ();
392- // disable the phases
393- stepper_driver->setPwm (0 , 0 );
394- if (fabs (c.a ) < 0 .1f && fabs (c.b ) < 0 .1f ){
395- SIMPLEFOC_DEBUG (" CS: Err too low current!" );
396- return 0 ; // measurement current too low
397- }
398- // align phase A
399- // check if measured current a is positive and invert if not
400- // check if current b is around zero and if its not
401- // check if current a is near zero and if it is invert them
402- if (fabs (c.a ) < fabs (c.b )){
403- SIMPLEFOC_DEBUG (" CS: Switch A-B" );
404- // switch phase A and B
405- _swap (pinA, pinB);
406- _swap (offset_ia, offset_ib);
407- _swap (gain_a, gain_b);
408- gain_a *= _sign (c.b );
409- phases_switched = true ; // signal that pins have been switched
410- }else if (c.a < 0 ){
411- SIMPLEFOC_DEBUG (" CS: Inv A" );
412- gain_a *= -1 ;
413- phases_inverted = true ; // signal that pins have been inverted
414- }
398+ if (!_isset (pinA) || !_isset (pinB)){
399+ SIMPLEFOC_DEBUG (" CS: Pins A & B not specified!" );
400+ return 0 ;
415401 }
416402
417- if (_isset (pinB)){
418- // set phase B active and phases A and C down
419- stepper_driver->setPwm (voltage, 0 );
420- _delay (500 );
421- PhaseCurrent_s c = readAverageCurrents ();
422- stepper_driver->setPwm (0 , 0 );
423- if (fabs (c.a ) < 0 .1f && fabs (c.b ) < 0 .1f ){
424- SIMPLEFOC_DEBUG (" CS: Err too low current!" );
425- return 0 ; // measurement current too low
426- }
427- // align phase A
428- // check if measured current a is positive and invert if not
429- if (c.b < 0 ){
430- SIMPLEFOC_DEBUG (" CS: Inv B" );
431- gain_b *= -1 ;
432- phases_inverted = true ; // signal that pins have been inverted
433- }
403+ // set phase A active and phases B down
404+ // ramp 300ms
405+ for (int i=0 ; i < 100 ; i++){
406+ stepper_driver->setPwm (voltage/100.0 *((float )i), 0 );
407+ _delay (3 );
408+ }
409+ _delay (500 );
410+ PhaseCurrent_s c = readAverageCurrents ();
411+ // disable the phases
412+ stepper_driver->setPwm (0 , 0 );
413+ if (fabs (c.a ) < 0 .1f && fabs (c.b ) < 0 .1f ){
414+ SIMPLEFOC_DEBUG (" CS: Err too low current!" );
415+ return 0 ; // measurement current too low
416+ }
417+ // align phase A
418+ // 1) only one phase can be measured so we first measure which ADC pin corresponds
419+ // to the phase A by comparing the magnitude
420+ if (fabs (c.a ) < fabs (c.b )){
421+ SIMPLEFOC_DEBUG (" CS: Switch A-B" );
422+ // switch phase A and B
423+ _swap (pinA, pinB);
424+ _swap (offset_ia, offset_ib);
425+ _swap (gain_a, gain_b);
426+ phases_switched = true ; // signal that pins have been switched
427+ }
428+ // 2) check if measured current a is positive and invert if not
429+ if (c.a < 0 ){
430+ SIMPLEFOC_DEBUG (" CS: Inv A" );
431+ gain_a *= -1 ;
432+ phases_inverted = true ; // signal that pins have been inverted
433+ }
434+
435+ // at this point the driver's phase A is aligned with the ADC pinA
436+ // and the pin B should be the phase B
437+
438+ // set phase B active and phases A down
439+ // ramp 300ms
440+ for (int i=0 ; i < 100 ; i++){
441+ stepper_driver->setPwm (0 , voltage/100.0 *((float )i));
442+ _delay (3 );
443+ }
444+ _delay (500 );
445+ c = readAverageCurrents ();
446+ stepper_driver->setPwm (0 , 0 );
447+
448+ // phase B should be aligned
449+ // 1) we just need to verify that it has been measured
450+ if (fabs (c.a ) < 0 .1f && fabs (c.b ) < 0 .1f ){
451+ SIMPLEFOC_DEBUG (" CS: Err too low current!" );
452+ return 0 ; // measurement current too low
453+ }
454+ // 2) check if measured current a is positive and invert if not
455+ if (c.b < 0 ){
456+ SIMPLEFOC_DEBUG (" CS: Inv B" );
457+ gain_b *= -1 ;
458+ phases_inverted = true ; // signal that pins have been inverted
434459 }
435460
436461 // construct the return flag
0 commit comments