2018-11-17 04:57 PM
I'm designing an embedded PI controller using a STM32 Nucleo board. In the PWM configuration, a 0 means 0% duty and 800 means 100% duty. The objetive is to regulate the speed of a PMDC motor, i.e, to keep the RPM under load variations. I'm not sure yet if this would work. Does the code keep the RPM when the output has reached the setpoint, so error signal is zero? Tell me your suggestions. I still don't have the PI gains, I just want to make sure the algorithm is correct. This is what I have:
int error=0;
int target_RPM=0;
int current_RPM = 0;
int duty_cycle=0;
float Kp = 0;
float integral = 0;
float Ki = 0;
while {
//Main program
}
//10 ms LOOP with TIM6 interrupt
//Get current RPM
current_RPM = read_RPM();
//Get the error
error = target_RPM - current_RPM;
//Calculate the integral
integral = integral + (Ki*error);
if (integral > 800) // check for integral windup and correct for upper limit
integral = 800;
if (integral < 0)//check for integral windup and correct for lower limit
integral = 0;
//Calculate the Control Variable
duty_cycle = (Kp*error) + integral;
//Limit the Control Variable to within 0-800
if(duty_cycle>800){
duty_cycle = 800;
}
else if (duty_cycle<0){
duty_cycle = 0;
}
htim1.Instance->CCR1 = duty_cycle;