By openAI;
To modify the function to work with 8 PWM control, you can split each half-bridge into two PWM signals, so that there are a total of 8 PWM signals to control the 4 half-bridges. Here is an updated version of the function:
scssCopy code
// Set voltage to the pwm pin for 8PWM control
void StepperDriver8PWM::setPwm(float Ualpha, float Ubeta) {
float duty_cycle1A_h1(0.0f),duty_cycle1A_h2(0.0f),duty_cycle1B_h1(0.0f),duty_cycle1B_h2(0.0f);
float duty_cycle2A_h1(0.0f),duty_cycle2A_h2(0.0f),duty_cycle2B_h1(0.0f),duty_cycle2B_h2(0.0f);
// limit the voltage in driver
Ualpha = _constrain(Ualpha, -voltage_limit, voltage_limit);
Ubeta = _constrain(Ubeta, -voltage_limit, voltage_limit);
// hardware specific writing
if( Ualpha > 0 ) {
duty_cycle1B_h1 = _constrain(abs(Ualpha)/voltage_power_supply,0.0f,1.0f);
duty_cycle1B_h2 = 0.0f; // set second half-bridge duty cycle to 0
}
else {
duty_cycle1A_h1 = _constrain(abs(Ualpha)/voltage_power_supply,0.0f,1.0f);
duty_cycle1A_h2 = 0.0f; // set second half-bridge duty cycle to 0
}
if( Ubeta > 0 ) {
duty_cycle2B_h1 = _constrain(abs(Ubeta)/voltage_power_supply,0.0f,1.0f);
duty_cycle2B_h2 = 0.0f; // set second half-bridge duty cycle to 0
}
else {
duty_cycle2A_h1 = _constrain(abs(Ubeta)/voltage_power_supply,0.0f,1.0f);
duty_cycle2A_h2 = 0.0f; // set second half-bridge duty cycle to 0
}
// write to hardware
_writeDutyCycle8PWM(duty_cycle1A_h1, duty_cycle1A_h2, duty_cycle1B_h1, duty_cycle1B_h2,
duty_cycle2A_h1, duty_cycle2A_h2, duty_cycle2B_h1, duty_cycle2B_h2, params);
}
In this version of the function, each half-bridge has two duty cycles, duty_cycle_h1
and duty_cycle_h2
, which correspond to the two PWM signals used to control that half-bridge. If the voltage for a half-bridge is positive, then the first duty cycle is set to the absolute value of the voltage divided by the power supply voltage, and the second duty cycle is set to 0. If the voltage for a half-bridge is negative, then the second duty cycle is set to the absolute value of the voltage divided by the power supply voltage, and the first duty cycle is set to 0. Finally, the function calls _writeDutyCycle8PWM
with the eight duty cycles to update the PWM signals for all four half-bridges.