+ Post New Thread
Results 1 to 17 of 17

18th November 2018, 02:03 #1
 Join Date
 Nov 2015
 Posts
 17
 Helped
 0 / 0
 Points
 699
 Level
 5
Embedded PI Controller
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:
Code: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 0800 if(duty_cycle>800){ duty_cycle = 800; } else if (duty_cycle<0){ duty_cycle = 0; } htim1.Instance>CCR1 = duty_cycle;

18th November 2018, 10:41 #2
Re: Embedded PI Controller
There is a C PID library here which you can refer.
https://www.edaboard.com/showthread....kingcorrectly

18th November 2018, 10:48 #3
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 44,940
 Helped
 13664 / 13664
 Points
 257,500
 Level
 100
Re: Embedded PI Controller
The PI algorithm is basically correct. How are you reading speed, can the measurement keep up with the 100 Hz controller sample rate?

18th November 2018, 14:17 #4
 Join Date
 Nov 2015
 Posts
 17
 Helped
 0 / 0
 Points
 699
 Level
 5
Re: Embedded PI Controller
For the speed measurement I'm using a 6 pulses per rev reflective sensor. The range of speed I want is from 0 to 3000 RPM. How do I know if the measurement can keep up with the sample rate?
Also, I would like to understand better the code. For example, if the setpoint is reached and the error is zero or near zero, does the routine keep the duty cycle? I thought if the error iz zero the duty would be zero, and the motor would be off, which is not intended (and that's the case of position control, not speed control).

Advertisement

18th November 2018, 15:44 #5
 Join Date
 Feb 2014
 Posts
 763
 Helped
 251 / 251
 Points
 5,409
 Level
 17
Re: Embedded PI Controller
When the error is zero the integral term stays unchanged (and p is zero) so the output stays at the same value.
That’s what the I term does for you. P only would mean zero error gives zero output.
A position control PI still might not go to zero if some holding force was required to hold a given position.
The pi looks good with antiwindup being a key feature.
1 members found this post helpful.

18th November 2018, 15:47 #6
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 44,940
 Helped
 13664 / 13664
 Points
 257,500
 Level
 100
Re: Embedded PI Controller
I thought if the error is zero the duty would be zero
Problem with speed measurement is the dead time. Below 1000 RPM, the update rate is lower than the controller sample rate.
1 members found this post helpful.

18th November 2018, 18:19 #7
 Join Date
 Nov 2015
 Posts
 17
 Helped
 0 / 0
 Points
 699
 Level
 5

18th November 2018, 20:05 #8
 Join Date
 Feb 2014
 Posts
 763
 Helped
 251 / 251
 Points
 5,409
 Level
 17
Re: Embedded PI Controller
I see the bigger problem is the general loop bandwidth issues with the low feedback rate.
For example if you want 100hz loop bandwidth but you're only getting 10 updates per second at low RPMs that will pose a fundamental challenge (output oscillations or thrashing on your power stage for example). But only you know whether you need good performance at low RPM's. If you don't try to regulate there it may not matter.
I wouldn't lower the PI sample rate because it doesn't solve that problem. Having the PI 'oversample' is fine. The key parameter here is the PI corner frequency and having a PI sample rate about 10X that corner frequency
1 members found this post helpful.

Advertisement

18th November 2018, 21:12 #9
 Join Date
 Nov 2015
 Posts
 17
 Helped
 0 / 0
 Points
 699
 Level
 5
Re: Embedded PI Controller
I will try it to see what happens. I have another basic question about understanding the code. If my setpoint is 500 RPM, and then I lower it to 300 RPM, then the error would be e=300500 = 200, a negative error. Here again is the I term which compensates for the negative error? As the duty cycle should be between 0 and 800.

18th November 2018, 22:08 #10
Awards:
 Join Date
 Apr 2014
 Posts
 15,339
 Helped
 3491 / 3491
 Points
 75,658
 Level
 67
Re: Embedded PI Controller
Hi,
The P part will cause immediate reaction on your input change.
It will be multiplied with a value smaller than 1 I assume.
Lets assume it is multiplied with 0.5 then the P output becomes 100.
But still there is the I output. You may consider it to be unchanged for the first response.
So if the I ouput was 400 befor the setponit change... then the overall output will be:
400 + (100) = 300 (for the duty cycle value)
But for sure you should prevent the duty cycle value to become negative.
KlausPlease don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.

18th November 2018, 22:12 #11
 Join Date
 Feb 2014
 Posts
 763
 Helped
 251 / 251
 Points
 5,409
 Level
 17
Re: Embedded PI Controller
Well immediately following that step the P term will reduce by 200*kp and the I term will decrease at a rate proportional to ki. The bottom line is that both terms will contribute to the PWM output reducing causing the motor to slow.
This is the nature of a PI. Each term contributes a response that causes different behaviors over time. The P term causes instant changes while the I term corrects error over a longer period of time. All fancy control loop theory boils down to 'how will the system respond over time'.
Note that a slightly improved implementation in my opinion wouldn't clamp the I term to 0800 but would only clamp the output to 0800 (as you're doing). Then within the ifthenelse that clamps the final output to 0/800 generate max_saturated and min_saturated signals that are used to stop the integrator from 'winding up'.
Because it's theoretically ok to have a negative integrator term if at the same time a positive P term (or other term such as a feedforward) is keeping the final output positive.Last edited by asdf44; 18th November 2018 at 22:19.

18th November 2018, 22:39 #12
Awards:
 Join Date
 Apr 2014
 Posts
 15,339
 Helped
 3491 / 3491
 Points
 75,658
 Level
 67
Re: Embedded PI Controller
Hi,
in my opinion wouldn't clamp the I term to 0800 but would only clamp the output to 0800
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 ...
KlausPlease don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.

18th November 2018, 23:20 #13
 Join Date
 Nov 2006
 Location
 Brazil
 Posts
 8,783
 Helped
 1109 / 1109
 Points
 29,460
 Level
 41
 Blog Entries
 9
Re: Embedded PI Controller
You need to limit the I value. In both directions to meaningfull values.
Part of the world that you live in, You are the part that you're giving ( Renaissance )

18th November 2018, 23:26 #14
 Join Date
 Jan 2008
 Location
 Bochum, Germany
 Posts
 44,940
 Helped
 13664 / 13664
 Points
 257,500
 Level
 100
Re: Embedded PI Controller
Yes, integrator limits are mandatory for antiwindup. It can be implemented in different ways. Simple PI controllers often use the "velocity algorithm" where integrator and controller output are identical, so it can be easily limited.
As for the speed sensor dead time, it tends to cause control loop instability at low RPM. It can be handled by reducing the controller gain at low speed.

Advertisement

18th November 2018, 23:41 #15
 Join Date
 Nov 2015
 Posts
 17
 Helped
 0 / 0
 Points
 699
 Level
 5
Re: Embedded PI Controller
Thanks for your suggestions, I will try the code and report back.

18th November 2018, 23:48 #16
 Join Date
 Feb 2014
 Posts
 763
 Helped
 251 / 251
 Points
 5,409
 Level
 17
Re: Embedded PI Controller
I simply think you didn't follow the second part of what I said.
When its detected that less than 0 or more than 800 was requested you use that knowledge to prevent windup. The point is that in a more generalized approach the modulator and the error amplifier are distinct. The modulator knows the limits of the system and tells the eamp things are 'saturated_pos' or 'saturated_neg'. The eamp then uses that knowledge to prevent windup in its accumulator(s).
if (integrator_input>0 & saturated_pos) 'don't run integrator'
if (integrator_input<0 & saturated_neg) 'don't run integrator'
Again this is relevant if for example you have a feedforward term bypassing the eamp. Now the eamp may need to contribute a positive or negative signal.
Or you may have multiple integrators/polls in a TypeII or TypeIII system where the values of the intermediary accumulators don't map directly to the output.

19th November 2018, 00:18 #17
Awards:
 Join Date
 Apr 2014
 Posts
 15,339
 Helped
 3491 / 3491
 Points
 75,658
 Level
 67
Re: Embedded PI Controller
Hi,
if (integrator_input>0 & saturated_pos) 'don't run integrator'
if (integrator_input<0 & saturated_neg) 'don't run integrator'
KlausPlease don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.
+ Post New Thread
Please login