PID Controller Implementation in STM32F407 Microcontroller

PID Controller Implementation in STM32F407 Microcontroller
Slide Note
Embed
Share

"Learn how to implement a PID controller in STM32F407 microcontroller, transitioning from differential equations to digital implementation for real system control. Explore the working principles of PID, its impact on an RC circuit, and the significance of proportional, derivative, and integral terms in control systems. Discover the digital control process using timers for signal sampling in the microcontroller environment."

  • STM32F407
  • PID controller
  • Microcontroller
  • Control system
  • Digital implementation

Uploaded on Mar 18, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. PID controller implementation in STM32F407 microcontroller By Omar Bouzourraa May 2017

  2. The PID is one of the most famous algorithms for mainly linear dynamic systems control. In this quick presentation, I will explain briefely, how you can pass from differential equations to digital implementation on microcontroller for real system control. Dynamic system to deal with is an RC circuit. The goal of control is to maintain a voltage in a constant value even when external noise or perturbation is affecting our circuit. RC circuit is a very simple by-pass filter wich can be controlled by a PWM signal in order to get a constant voltage across the capacitor. voltage across the capacitor is the mean PWM voltage. Uc = Duty_Cycle*PWM_Voltage_Amplitude

  3. The RC circuit This figure illustrates how voltage (in blue) across the capacitor depends on PWM signal duty-cycle

  4. How it works ? The PID controller is based on 3 terms : Proportionnal, Derivative and Integral. In an infinite loop, system output is measured , sampled with a fixed frequency and error between desired and obtained values, is computed every sampling period. Propotionnal term is useful to boost system and make it reach desired value as fast as possible, but it can never make system really reach the desired value! : as its name indicates, the prportionnal action is a simple multiplication operation between error and a constant coefficient. Integral term is used to make error null in steady state : It s a simple mathematical integration of the error. Derivative term : Is the derivative of the error and it s mainly useful to reduce overshoot.

  5. What does it really mean ? When target is defined, system error is maximal as output is null at the beginning, so in order to start as fast as possible, error is multiplied by a constant value Kp and so we have the first part of our controller output. When the system accelerates to reach the target, it can depass it (imagine you are racing and try to stop when you reach a line on the ground, but you cannot, as your inertia prevents you. So you cross the line!). In this situation, you can simply notice that the error when system is trying to reach target for the first time, decreases over time and consequently its derivative becomes negative, so when it is added to the equation of the command, it brakes the system! To have more control flexibility, error derivative is multiplied by a coefficient. When using integral term, current command depends on all measured previous errors added together which makes the system error in steady state null! And as a result proportional and derivative terms have no longer effect, but they intervenes when just a little disturbance on system occurs.

  6. Digital Control All measured signals must be sampled to be processed by the microcontroller. So, I propose to use a timer as a time base in interrupt mode. Every programmed period Ts, program jumps to the timer handler to update system command. How integral and derivative actions are made in digital control ? Error_Derivative < --- (Error_new Error_old)/Ts Error_Integral < --- Error_Integral + Ts*Error_new It s a direct application of mathematical derivative and integral : you can notice that Ts was considered as the infinitesimal time used in physical expressions and it s clear that for an infinitesimal time, for linear systems like in our case, the main system change is infinitesimal also.

  7. Derivative action and noise filtering In real dynamic systems, mesure is always affected by noise whith arbitrary variation, so when derivating the error, noise will be amplified and system output can be totally different from what it must be. So, we should add a by-pass filter to reduce all idesirable high frequency noise effect on system. The easiest by-pass filter that can ever exist is a first-order by-pass filter! Wich transfer function is as follow : F(p) = 1/( *p + 1) You can use matlab to obtain quickly its presentation in discrete domain by the following lines of code.

  8. How can I implement it in my STM32 microcontroller ? Using the discrete transfer function, you can obtain a recursive equation that relates input to output. In our case, the recursive equation is : S(n) = a*S(n 1) + b*U(n 1); While a = 0.995; b = 0.004988; S : is the filter output (derivative term filtered) U : is the filter input (derivative term not filetered yet)

  9. But thats not all ! Every time your program jumps to the main sampling handler related to a timer interrupt, it should update command, so current variables such as S(n),U(n) must be updated by the current measured or computed values, and also S(n -1) ,U(n-1) should be updated by the values of S(n),U(n) just after the main update instruction is already processed. In our case : If(Ts elapsed) { U_new = Update(U_new); S_new = a*S_old + b*U_old; S_old = S_new; U_old = U_new; }

  10. Lets look at results ! This curve illustrates System response for a constant target = 1.5V Ki = 15 Kp = -1 Kd = 0

  11. Lets make things better ! I intentially wanted system to make a noticeable overshoot . Now, I ll eliminate overshoot using derivative term. Ki = 15 Kp = -1 and Kd = 1 You can notice a huge difference now ! When adding derivative term

  12. Lets check some things ! Now, I ll use only proportional term . Let s see results together ! Kp = 1 Kp = 2 Kp = 5

  13. So you can see that precision is improved when incresing Kp value but it cannot be eliminated ! Now let s try something else ! C = 47 F and R = 1KOhm so time constant = 0.047s Ki,Kp and Kd are now unknowen values. You can find in the expression of the derivative term, the existance of a by-pass filter which time constant is 1s. So after a simple comutation I find the transfer function of my controlled system which is the following one :

  14. The caracteristic polynom is : I want my system to behave like the system having the following caracteristic polynom : So : Ki = 2 , Kp = -1 ,Kd = 1.953 So I obtain on the screen of my oscilloscope, this curve :

  15. In Matlab : Theoritical curve is perfectly like the experimental one

  16. Lets disturb our system ! To do this, I simply brought a poteniometer connected as follow:

  17. Lets keep the last PID prameters and try You can simply notice when perturbation is applied and system reaction

  18. Find here the link to a complete project https://github.com/EmbeddedLover/PID_Digital _Implementation/find/master

  19. Thank you ! For your attention

More Related Content