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;
The PI algorithm is basically correct. How are you reading speed, can the measurement keep up with the 100 Hz controller sample rate?
Why? integral is keeping the duty cycle.I thought if the error is zero the duty would be zero
Why? integral is keeping the duty cycle.
Problem with speed measurement is the dead time. Below 1000 RPM, the update rate is lower than the controller sample rate.
From my experience this won't work.in my opinion wouldn't clamp the I term to 0-800 but would only clamp the output to 0-800
You need to limit the I value. In both directions to meaningfull values.
Hi,
From my experience this won't work.
You need to limit the I value. In both directions to meaningfull values.
Consider this situation.
The setpoint is 100%
But due to heavy load - even at full duty cycle - it does not get the desired RPM.
The speed is continously too low, causing the I part to increase.
Only time or a variable overflow limits the value of I.
So a 16 bit value may become 32,000 ....
Or even more: 32765, 32766, 32767, (overflow) -32768, -32767, -32766 ....
And suddenly the duty cycle becomes 0 and the motor stops....and nobody knows why .... maybe after hours of running ...
Klaus
Now I understand. Makes sense.if (integrator_input>0 & saturated_pos) 'don't run integrator'
if (integrator_input<0 & saturated_neg) 'don't run integrator'
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?