Author Topic: Designing an Embedded PI Controller for Speed Control  (Read 844 times)

0 Members and 1 Guest are viewing this topic.

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
Designing an Embedded PI Controller for Speed Control
« on: November 17, 2018, 12:54:49 am »
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: [Select]
int error=0;
int target_RPM=0;
int duty_cycle=0;
int Kp = 0;
int integral = 0;
int current_RPM = 0;
int Ki = 0;

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

Code: [Select]
//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;

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.
« Last Edit: November 17, 2018, 01:00:00 am by XaviPacheco »
 

Offline german77

  • Regular Contributor
  • *
  • Posts: 72
  • Country: mx
Re: Designing an Embedded PI Controller for Speed Control
« Reply #1 on: November 17, 2018, 01:47:21 am »
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;
 
The following users thanked this post: XaviPacheco

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
Re: Designing an Embedded PI Controller for Speed Control
« Reply #2 on: November 17, 2018, 01:57:22 am »
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?
 

Offline german77

  • Regular Contributor
  • *
  • Posts: 72
  • Country: mx
Re: Designing an Embedded PI Controller for Speed Control
« Reply #3 on: November 17, 2018, 03:00:49 am »
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.
 

Offline XaviPachecoTopic starter

  • Regular Contributor
  • *
  • Posts: 243
  • Country: do
Re: Designing an Embedded PI Controller for Speed Control
« Reply #4 on: November 17, 2018, 03:03:49 am »
What values have you obtained doing manual tuning?
 

Offline jbb

  • Super Contributor
  • ***
  • Posts: 1145
  • Country: nz
Re: Designing an Embedded PI Controller for Speed Control
« Reply #5 on: November 17, 2018, 04:32:03 am »
Haven’t reviewed the code, but for best results you should run the fast control loop - which sets the PWM rate - at the PWM frequency.

So 10 kHz switching -> 100us cycle rate,
20 kHz switching -> 50us cycle rate etc.

As previously discussed in another thread, you will get much better[/i] results by using this fast loop to control measured motor current to track a current reference.

Then add a second “outer” control loop which takes actual speed and speed reference to produce the current reference. By limiting the outer control loop output (with anti windup in the PI controller) you can naturally limit he motor current to safe levels.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf