Skip to content

Commit cf70cce

Browse files
committed
added the contering posibility
1 parent 9aee1aa commit cf70cce

File tree

6 files changed

+25
-21
lines changed

6 files changed

+25
-21
lines changed

src/BLDCMotor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int BLDCMotor::alignCurrentSense() {
202202
SIMPLEFOC_DEBUG("MOT: Align current sense.");
203203

204204
// align current sense and the driver
205-
exit_flag = current_sense->driverAlign(voltage_sensor_align);
205+
exit_flag = current_sense->driverAlign(voltage_sensor_align, modulation_centered);
206206
if(!exit_flag){
207207
// error in current sense - phase either not measured or bad connection
208208
SIMPLEFOC_DEBUG("MOT: Align error!");

src/StepperMotor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ int StepperMotor::alignCurrentSense() {
159159
SIMPLEFOC_DEBUG("MOT: Align current sense.");
160160

161161
// align current sense and the driver
162-
exit_flag = current_sense->driverAlign(voltage_sensor_align);
162+
exit_flag = current_sense->driverAlign(voltage_sensor_align, modulation_centered);
163163
if(!exit_flag){
164164
// error in current sense - phase either not measured or bad connection
165165
SIMPLEFOC_DEBUG("MOT: Align error!");

src/common/base_classes/CurrentSense.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void CurrentSense::disable(){
133133
// 3 - success but gains inverted
134134
// 4 - success but pins reconfigured and gains inverted
135135
// IMPORTANT, this function can be overriden in the child class
136-
int CurrentSense::driverAlign(float voltage){
136+
int CurrentSense::driverAlign(float voltage, bool modulation_centered){
137137

138138
int exit_flag = 1;
139139
if(skip_align) return exit_flag;
@@ -142,9 +142,9 @@ int CurrentSense::driverAlign(float voltage){
142142

143143
// check if stepper or BLDC
144144
if(driver_type == DriverType::Stepper)
145-
return alignStepperDriver(voltage, (StepperDriver*)driver);
145+
return alignStepperDriver(voltage, (StepperDriver*)driver, modulation_centered);
146146
else
147-
return alignBLDCDriver(voltage, (BLDCDriver*)driver);
147+
return alignBLDCDriver(voltage, (BLDCDriver*)driver, modulation_centered);
148148
}
149149

150150

@@ -172,22 +172,23 @@ PhaseCurrent_s CurrentSense::readAverageCurrents(int N) {
172172
// 2 - success but pins reconfigured
173173
// 3 - success but gains inverted
174174
// 4 - success but pins reconfigured and gains inverted
175-
int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
175+
int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver, bool modulation_centered){
176176

177177
bool phases_switched = 0;
178178
bool phases_inverted = 0;
179179

180-
float center = driver->voltage_limit/2.0;
180+
float zero = 0;
181+
if(modulation_centered) zero = driver->voltage_limit/2.0;
181182

182183
// set phase A active and phases B and C down
183184
// 300 ms of ramping
184185
for(int i=0; i < 100; i++){
185-
bldc_driver->setPwm(voltage/100.0*((float)i) , 0, 0);
186+
bldc_driver->setPwm(voltage/100.0*((float)i)+zero , zero, zero);
186187
_delay(3);
187188
}
188189
_delay(500);
189190
PhaseCurrent_s c_a = readAverageCurrents();
190-
bldc_driver->setPwm(0, 0, 0);
191+
bldc_driver->setPwm(zero, zero, zero);
191192
// check if currents are to low (lower than 100mA)
192193
// TODO calculate the 100mA threshold from the ADC resolution
193194
// if yes throw an error and return 0
@@ -293,12 +294,12 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
293294
// set phase B active and phases A and C down
294295
// 300 ms of ramping
295296
for(int i=0; i < 100; i++){
296-
bldc_driver->setPwm(0, voltage/100.0*((float)i), 0);
297+
bldc_driver->setPwm(zero, voltage/100.0*((float)i)+zero, zero);
297298
_delay(3);
298299
}
299300
_delay(500);
300301
PhaseCurrent_s c_b = readAverageCurrents();
301-
bldc_driver->setPwm(0, 0, 0);
302+
bldc_driver->setPwm(zero, zero, zero);
302303

303304
// check the phase B
304305
// find the highest magnitude in c_b
@@ -390,10 +391,13 @@ int CurrentSense::alignBLDCDriver(float voltage, BLDCDriver* bldc_driver){
390391
// 2 - success but pins reconfigured
391392
// 3 - success but gains inverted
392393
// 4 - success but pins reconfigured and gains inverted
393-
int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_driver){
394+
int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_driver, bool modulation_centered){
394395

395396
bool phases_switched = 0;
396397
bool phases_inverted = 0;
398+
399+
float zero = 0;
400+
if(modulation_centered) zero = driver->voltage_limit/2.0;
397401

398402
if(!_isset(pinA) || !_isset(pinB)){
399403
SIMPLEFOC_DEBUG("CS: Pins A & B not specified!");
@@ -403,13 +407,13 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
403407
// set phase A active and phases B down
404408
// ramp 300ms
405409
for(int i=0; i < 100; i++){
406-
stepper_driver->setPwm(voltage/100.0*((float)i), 0);
410+
stepper_driver->setPwm(voltage/100.0*((float)i), zero);
407411
_delay(3);
408412
}
409413
_delay(500);
410414
PhaseCurrent_s c = readAverageCurrents();
411415
// disable the phases
412-
stepper_driver->setPwm(0, 0);
416+
stepper_driver->setPwm(zero, zero);
413417
if (fabs(c.a) < 0.1f && fabs(c.b) < 0.1f ){
414418
SIMPLEFOC_DEBUG("CS: Err too low current!");
415419
return 0; // measurement current too low
@@ -438,12 +442,12 @@ int CurrentSense::alignStepperDriver(float voltage, StepperDriver* stepper_drive
438442
// set phase B active and phases A down
439443
// ramp 300ms
440444
for(int i=0; i < 100; i++){
441-
stepper_driver->setPwm(0, voltage/100.0*((float)i));
445+
stepper_driver->setPwm(zero, voltage/100.0*((float)i)+zero);
442446
_delay(3);
443447
}
444448
_delay(500);
445449
c = readAverageCurrents();
446-
stepper_driver->setPwm(0, 0);
450+
stepper_driver->setPwm(zero, zero);
447451

448452
// phase B should be aligned
449453
// 1) we just need to verify that it has been measured

src/common/base_classes/CurrentSense.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class CurrentSense{
6868
*
6969
* IMPORTANT: Default implementation provided in the CurrentSense class, but can be overriden in the child classes
7070
*/
71-
virtual int driverAlign(float align_voltage);
71+
virtual int driverAlign(float align_voltage, bool modulation_centered = false);
7272

7373
/**
7474
* Function rading the phase currents a, b and c
@@ -130,11 +130,11 @@ class CurrentSense{
130130
/**
131131
* Function used to align the current sense with the BLDC motor driver
132132
*/
133-
int alignBLDCDriver(float align_voltage, BLDCDriver* driver);
133+
int alignBLDCDriver(float align_voltage, BLDCDriver* driver, bool modulation_centered);
134134
/**
135135
* Function used to align the current sense with the Stepper motor driver
136136
*/
137-
int alignStepperDriver(float align_voltage, StepperDriver* driver);
137+
int alignStepperDriver(float align_voltage, StepperDriver* driver, bool modulation_centered);
138138
/**
139139
* Function used to read the average current values over N samples
140140
*/

src/current_sense/GenericCurrentSense.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ PhaseCurrent_s GenericCurrentSense::getPhaseCurrents(){
5454
// returns flag
5555
// 0 - fail
5656
// 1 - success and nothing changed
57-
int GenericCurrentSense::driverAlign(float voltage){
57+
int GenericCurrentSense::driverAlign(float voltage, bool modulation_centered){
5858
_UNUSED(voltage) ; // remove unused parameter warning
5959
int exit_flag = 1;
6060
if(skip_align) return exit_flag;

src/current_sense/GenericCurrentSense.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GenericCurrentSense: public CurrentSense{
2020
// CurrentSense interface implementing functions
2121
int init() override;
2222
PhaseCurrent_s getPhaseCurrents() override;
23-
int driverAlign(float align_voltage) override;
23+
int driverAlign(float align_voltage, bool modulation_centered) override;
2424

2525

2626
PhaseCurrent_s (*readCallback)() = nullptr; //!< function pointer to sensor reading

0 commit comments

Comments
 (0)