SlideShare a Scribd company logo
PID controller implementation in
STM32F407 microcontroller
By Omar Bouzourraa
May 2017
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 low_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
The RC circuit
This figure illustrates how voltage (in blue) across the
capacitor depends on PWM signal duty-cycle
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.
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.
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.
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 low_pass filter to reduce all
idesirable high frequency noise effect on system.
The easiest low_pass filter that can ever exist is a first-order low_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.
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)
But that’s 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;
}
Let’s look at results !
This curve illustrates System response for a constant target = 1.5V
Ki = 15 Kp = -1 Kd = 0
Let’s 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
Let’s check some things !
Now, I’ll use only proportional term . Let’s see results together !
Kp = 1 Kp = 2
Kp = 5
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 low_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 :
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 :
In Matlab :
Theoritical curve is perfectly like the experimental one
Let’s disturb our system !
To do this, I simply brought a poteniometer connected as follow:
Let’s keep the last PID prameters and try
You can simply notice when perturbation is applied and
system reaction
Find here the link to a complete
project
https://github.com/EmbeddedLover/PID_Digital
_Implementation/find/master
Thank you !
For your attention

More Related Content

Similar to Control project

PID controller in control systems
PID controller in control systemsPID controller in control systems
PID controller in control systems
khalaf Gaeid
 
Introduction to control system 2
Introduction to control system 2Introduction to control system 2
Introduction to control system 2
turna67
 
UNIT-V.ppt
UNIT-V.pptUNIT-V.ppt
Control systems diagraming
Control systems diagramingControl systems diagraming
Control systems diagraming
Nuwematsiko Chris
 
Pid control by Adarsh singh
Pid control  by Adarsh singhPid control  by Adarsh singh
Pid control by Adarsh singh
Adarsh Singh
 
Lead-lag controller
Lead-lag controllerLead-lag controller
Lead-lag controller
Chiramathe Nami
 
Chapter 10-pid-1
Chapter 10-pid-1Chapter 10-pid-1
Chapter 10-pid-1
mohamed maghraby
 
Control tutorials for matlab and simulink introduction pid controller desig...
Control tutorials for matlab and simulink   introduction pid controller desig...Control tutorials for matlab and simulink   introduction pid controller desig...
Control tutorials for matlab and simulink introduction pid controller desig...
ssuser27c61e
 
Closed-loop control system modelling_pid.pptx
Closed-loop control system modelling_pid.pptxClosed-loop control system modelling_pid.pptx
Closed-loop control system modelling_pid.pptx
KadiriIbrahim2
 
1578385.ppt
1578385.ppt1578385.ppt
1578385.ppt
MuhammadMubeen58
 
476 293
476 293476 293
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
Mathankumar S
 
Control system unit(1)
Control system unit(1)Control system unit(1)
Control system unit(1)
Mohammed Waris Senan
 
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
TaraGonzales5
 
Week 14 pid may 24 2016 pe 3032
Week  14 pid  may 24 2016 pe 3032Week  14 pid  may 24 2016 pe 3032
Week 14 pid may 24 2016 pe 3032
Charlton Inao
 
Projt1111
Projt1111Projt1111
Projt1111
Ankit Biswas
 
Control system basics, block diagram and signal flow graph
Control system basics, block diagram and signal flow graphControl system basics, block diagram and signal flow graph
Control system basics, block diagram and signal flow graph
SHARMA NAVEEN
 
REPORT
REPORTREPORT
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEMPERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
ijccmsjournal
 
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEMPERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
ijccmsjournal
 

Similar to Control project (20)

PID controller in control systems
PID controller in control systemsPID controller in control systems
PID controller in control systems
 
Introduction to control system 2
Introduction to control system 2Introduction to control system 2
Introduction to control system 2
 
UNIT-V.ppt
UNIT-V.pptUNIT-V.ppt
UNIT-V.ppt
 
Control systems diagraming
Control systems diagramingControl systems diagraming
Control systems diagraming
 
Pid control by Adarsh singh
Pid control  by Adarsh singhPid control  by Adarsh singh
Pid control by Adarsh singh
 
Lead-lag controller
Lead-lag controllerLead-lag controller
Lead-lag controller
 
Chapter 10-pid-1
Chapter 10-pid-1Chapter 10-pid-1
Chapter 10-pid-1
 
Control tutorials for matlab and simulink introduction pid controller desig...
Control tutorials for matlab and simulink   introduction pid controller desig...Control tutorials for matlab and simulink   introduction pid controller desig...
Control tutorials for matlab and simulink introduction pid controller desig...
 
Closed-loop control system modelling_pid.pptx
Closed-loop control system modelling_pid.pptxClosed-loop control system modelling_pid.pptx
Closed-loop control system modelling_pid.pptx
 
1578385.ppt
1578385.ppt1578385.ppt
1578385.ppt
 
476 293
476 293476 293
476 293
 
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
Biomedical Control Systems - Time Response Analysis (Short Questions & Answers)
 
Control system unit(1)
Control system unit(1)Control system unit(1)
Control system unit(1)
 
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
179529572-ROOT-LOCUS-Control-system-Design-ppt.ppt
 
Week 14 pid may 24 2016 pe 3032
Week  14 pid  may 24 2016 pe 3032Week  14 pid  may 24 2016 pe 3032
Week 14 pid may 24 2016 pe 3032
 
Projt1111
Projt1111Projt1111
Projt1111
 
Control system basics, block diagram and signal flow graph
Control system basics, block diagram and signal flow graphControl system basics, block diagram and signal flow graph
Control system basics, block diagram and signal flow graph
 
REPORT
REPORTREPORT
REPORT
 
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEMPERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
 
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEMPERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
PERFORMANCE COMPARISON OF TWO CONTROLLERS ON A NONLINEAR SYSTEM
 

Recently uploaded

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
shivani5543
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
Madan Karki
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 

Recently uploaded (20)

ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
john krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptxjohn krisinger-the science and history of the alcoholic beverage.pptx
john krisinger-the science and history of the alcoholic beverage.pptx
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 

Control project

  • 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 low_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 low_pass filter to reduce all idesirable high frequency noise effect on system. The easiest low_pass filter that can ever exist is a first-order low_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.
  • 9. 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)
  • 10. But that’s 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; }
  • 11. Let’s look at results ! This curve illustrates System response for a constant target = 1.5V Ki = 15 Kp = -1 Kd = 0
  • 12. Let’s 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
  • 13. Let’s check some things ! Now, I’ll use only proportional term . Let’s see results together ! Kp = 1 Kp = 2 Kp = 5
  • 14. 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 low_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 :
  • 15. 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 :
  • 16. In Matlab : Theoritical curve is perfectly like the experimental one
  • 17. Let’s disturb our system ! To do this, I simply brought a poteniometer connected as follow:
  • 18. Let’s keep the last PID prameters and try You can simply notice when perturbation is applied and system reaction
  • 19. Find here the link to a complete project https://github.com/EmbeddedLover/PID_Digital _Implementation/find/master
  • 20. Thank you ! For your attention