+ Post New Thread
Results 1 to 17 of 17
  1. #1
    Junior Member level 1
    Points: 699, Level: 5

    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 0-800
    if(duty_cycle>800){
    	duty_cycle = 800;
    }
    else if (duty_cycle<0){
    	duty_cycle = 0;
    }
    
    htim1.Instance->CCR1 = duty_cycle;

  2. #2
    Advanced Member level 1
    Points: 2,290, Level: 11
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    444
    Helped
    43 / 43
    Points
    2,290
    Level
    11

    Re: Embedded PI Controller

    There is a C PID library here which you can refer.

    https://www.edaboard.com/showthread....king-correctly



  3. #3
    Super Moderator
    Points: 252,756, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,024
    Helped
    13395 / 13395
    Points
    252,756
    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?



  4. #4
    Junior Member level 1
    Points: 699, Level: 5

    Join Date
    Nov 2015
    Posts
    17
    Helped
    0 / 0
    Points
    699
    Level
    5

    Re: Embedded PI Controller

    Quote Originally Posted by FvM View Post
    The PI algorithm is basically correct. How are you reading speed, can the measurement keep up with the 100 Hz controller sample rate?
    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).



    •   AltAdvertisment

        
       

  5. #5
    Advanced Member level 2
    Points: 4,580, Level: 16

    Join Date
    Feb 2014
    Posts
    611
    Helped
    214 / 214
    Points
    4,580
    Level
    16

    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 anti-windup being a key feature.


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  6. #6
    Super Moderator
    Points: 252,756, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,024
    Helped
    13395 / 13395
    Points
    252,756
    Level
    100

    Re: Embedded PI Controller

    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.


    1 members found this post helpful.

  7. #7
    Junior Member level 1
    Points: 699, Level: 5

    Join Date
    Nov 2015
    Posts
    17
    Helped
    0 / 0
    Points
    699
    Level
    5

    Re: Embedded PI Controller

    Quote Originally Posted by FvM View Post
    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.
    So do you suggest to lower the controller sample rate?



  8. #8
    Advanced Member level 2
    Points: 4,580, Level: 16

    Join Date
    Feb 2014
    Posts
    611
    Helped
    214 / 214
    Points
    4,580
    Level
    16

    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.

    •   AltAdvertisment

        
       

  9. #9
    Junior Member level 1
    Points: 699, Level: 5

    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=300-500 = -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.



  10. #10
    Super Moderator
    Points: 70,834, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,484
    Helped
    3296 / 3296
    Points
    70,834
    Level
    64

    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.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  11. #11
    Advanced Member level 2
    Points: 4,580, Level: 16

    Join Date
    Feb 2014
    Posts
    611
    Helped
    214 / 214
    Points
    4,580
    Level
    16

    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 0-800 but would only clamp the output to 0-800 (as you're doing). Then within the if-then-else 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 feed-forward) is keeping the final output positive.
    Last edited by asdf44; 18th November 2018 at 22:19.



  12. #12
    Super Moderator
    Points: 70,834, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,484
    Helped
    3296 / 3296
    Points
    70,834
    Level
    64

    Re: Embedded PI Controller

    Hi,

    in my opinion wouldn't clamp the I term to 0-800 but would only clamp the output to 0-800
    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
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  13. #13
    Super Moderator
    Points: 28,658, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,532
    Helped
    1081 / 1081
    Points
    28,658
    Level
    41
    Blog Entries
    6

    Re: Embedded PI Controller

    You need to limit the I value. In both directions to meaningfull values.
    Changing the math could shift values of the output if compared to the expected standard PI model, once this would change the PI model itself. In fact, it is usual to apply a saturation function to the error value, but this will somewhat change overall PI model, althought most often with a negligible impact. What I see here is the lack of a functional simulation of the whole process, which could give a better clue of the fine adjustements necessary to tune the control dynamics.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  14. #14
    Super Moderator
    Points: 252,756, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    44,024
    Helped
    13395 / 13395
    Points
    252,756
    Level
    100

    Re: Embedded PI Controller

    Yes, integrator limits are mandatory for anti-windup. 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.



  15. #15
    Junior Member level 1
    Points: 699, Level: 5

    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.



    •   AltAdvertisment

        
       

  16. #16
    Advanced Member level 2
    Points: 4,580, Level: 16

    Join Date
    Feb 2014
    Posts
    611
    Helped
    214 / 214
    Points
    4,580
    Level
    16

    Re: Embedded PI Controller

    Quote Originally Posted by KlausST View Post
    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
    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 feed-forward 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.



  17. #17
    Super Moderator
    Points: 70,834, Level: 64
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    14,484
    Helped
    3296 / 3296
    Points
    70,834
    Level
    64

    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'
    Now I understand. Makes sense.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--