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 :