Electronics > Projects, Designs, and Technical Stuff
Designing an Embedded PI Controller for Speed Control
(1/2) > >>
XaviPacheco:
Hello,

I will appreciate your guidance in this. 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'm confused about the PI output and the duty cycle relationship. How is it? This is what I have:


--- Code: ---int error=0;
int target_RPM=0;
int duty_cycle=0;
int Kp = 0;
int integral = 0;
int current_RPM = 0;
int Ki = 0;

--- End code ---

I plan to execute the following PID loop every 100 ms with an interrupt.


--- Code: ---//Get current RPM
current_RPM = read_RPM();

//Get the error
error = target_RPM - current_RPM;

//Calculate the integral
integral = integral+error;

//Calculate the Control Variable
duty_cycle = (Kp*error) + (Ki*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;

--- End code ---

I still don't have the PI constants. First, I want to make sure that the algorithm is correct. Also, I know I need anti-wind up for the integral variable, but still not sure how to implement it.
german77:
Your code will work. I usually limit the integral error to the maximum posible pwm.  with something like this.

if( integral *Ki > 800)
integral = 800 / Ki;
else if( integral *Ki < -800)
integral = -800 / Ki;

It's not perfect as you can see a recover delay if it does reach this limit. But at least it doesn't go to the roof.

Another approach that may give better results depending on the system by reducing the recovery delay. Is to do this.

if(integral*Ki +(Kp * error)>800)
integral = (800 -(Kp * error))/Ki;
XaviPacheco:
Good. I will try it. Is it correct to do the PID algorithm in 100 ms loop or less? Also, I'm trying to understand better how the code works. Does the duty cycle actually gets adjusted with good PI constants according to the RPM error?
german77:
The timing depends a lot of the kind of system. Usually I use 5ms timings or lower. The more updates per second the more stable the system becomes.

Yes the duty cycle actually gets adjusted until the rpm error is almost cero. It's very easy to see once you test it. I usually set the Kp first and increase it's value until the error becomes very low but without overshoot. Then I increment the Ki to remove any error. Kd often removes oscillation of the system.
XaviPacheco:
What values have you obtained doing manual tuning?
Navigation
Message Index
Next page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod