@@ -15,6 +15,7 @@ StepperDriver4PWM::StepperDriver4PWM(int ph1A,int ph1B,int ph2A,int ph2B,int en1
1515  voltage_power_supply = DEF_POWER_SUPPLY;
1616  voltage_limit = NOT_SET;
1717  pwm_frequency = NOT_SET;
18+   inverse_power_supply = 1 .0f  / voltage_power_supply;
1819
1920}
2021
@@ -52,10 +53,12 @@ int StepperDriver4PWM::init() {
5253  //  sanity check for the voltage limit configuration
5354  if ( !_isset (voltage_limit) || voltage_limit > voltage_power_supply) voltage_limit =  voltage_power_supply;
5455
56+   inverse_power_supply = 1 .0f  / voltage_power_supply;
57+ 
5558  //  Set the pwm frequency to the pins
5659  //  hardware specific function - depending on driver and mcu
5760  params = _configure4PWM (pwm_frequency, pwm1A, pwm1B, pwm2A, pwm2B);
58-   initialized = (params!=SIMPLEFOC_DRIVER_INIT_FAILED);   
61+   initialized = (params!=SIMPLEFOC_DRIVER_INIT_FAILED);
5962  return  params!=SIMPLEFOC_DRIVER_INIT_FAILED;
6063}
6164
@@ -77,14 +80,14 @@ void StepperDriver4PWM::setPwm(float Ualpha, float Ubeta) {
7780  Ubeta = _constrain (Ubeta, -voltage_limit, voltage_limit);
7881  //  hardware specific writing
7982  if ( Ualpha > 0  )
80-     duty_cycle1B = _constrain (abs (Ualpha)/voltage_power_supply ,0 .0f ,1 .0f );
83+     duty_cycle1B = _constrain (abs (Ualpha) * inverse_power_supply ,0 .0f ,1 .0f );
8184  else 
82-     duty_cycle1A = _constrain (abs (Ualpha)/voltage_power_supply ,0 .0f ,1 .0f );
85+     duty_cycle1A = _constrain (abs (Ualpha) * inverse_power_supply ,0 .0f ,1 .0f );
8386
8487  if ( Ubeta > 0  )
85-     duty_cycle2B = _constrain (abs (Ubeta)/voltage_power_supply ,0 .0f ,1 .0f );
88+     duty_cycle2B = _constrain (abs (Ubeta) * inverse_power_supply ,0 .0f ,1 .0f );
8689  else 
87-     duty_cycle2A = _constrain (abs (Ubeta)/voltage_power_supply ,0 .0f ,1 .0f );
90+     duty_cycle2A = _constrain (abs (Ubeta) * inverse_power_supply ,0 .0f ,1 .0f );
8891  //  write to hardware
8992  _writeDutyCycle4PWM (duty_cycle1A, duty_cycle1B, duty_cycle2A, duty_cycle2B, params);
9093}
0 commit comments