Computer Implementation of Control Systems
Karl-Erik Årzen, Anton Cervin
Session outline
• Sampled-data control
• Discretization of continuous-time controllers
• Implementation of PID Controllers
Sampled-data control systems
u (t ) y (t )
t t
u (t ) y (t )
Process
Hold Sampler
uk yk
uk D-A Computer A-D yk
t t
• Mix of continuous-time and discrete-time signals
Networked control systems
. .
u(t)
. ... u(t) y(t)
y(t)
Process
t t
D−A Sampler
and and
Hold A−D
Communication network
. .
uk
. ...
yk
. ...
uk
Computer
yk
. .
t t
• Extra delay, possibly lost packets
Sampling
Computer u
y
A/D Algorithm D/A Process
AD-converter acts as sampler
A/D
DA-converter acts as a hold device
Normally, zero-order-hold is used piecewise constant control
signals
Aliasing
1
−1
0 5 10
Time
2π
ωs = h
= sampling frequency
ωs
ωN = 2
= Nyquist frequency
Frequencies above the Nyquist frequency are folded and appear as
low-frequency signals.
The fundamental alias for a frequency f1 is given by
f = ( f1 + f N ) mod ( fs ) − f N
Above: f1 = 0.9, fs = 1, f N = 0.5, f = 0.1
Anti-aliasing filter
Analog low-pass filter that eliminates all frequencies above the
Nyquist frequency
• Analog filter
– 2-6th order Bessel or Butterworth filter
– Difficulties with changing h (sampling interval)
• Analog + digital filter
– Fixed, fast sampling with fixed analog filter
– Downsampling using digital LP-filter
– Control algorithm at the lower rate
– Easy to change sampling interval
The filter may have to be included in the control design
Example – Prefiltering
(a) (b)
1 1
0 0
−1 −1
0 10 20 30 0 10 20 30
(c) (d)
1 1
0 0
−1 −1
0 10 20 30 0 10 20 30
Time Time
ω d = 0.9, ω N = 0.5, ω alias = 0.1
6th order Bessel with ω B = 0.25
Design approaches
Digital controllers can be designed in two different ways:
• Discrete-time design – sampled control theory
– Sample the continuous system
– Design a digital controller for the sampled system
∗ Z-transform domain
∗ state-space domain
• Continuous time design + discretization
– Design a continuous controller for the continuous system
– Approximate the continuous design
– Use fast sampling
Disk drive example
Control of the arm of a disk drive
k
G (s) =
Js2
Continuous time controller
bK s+b
U (s) = Uc (s) − K Y (s)
a s+a
Discrete time controller (continuous time design + discretization)
u(t k) = K ( ab uc (t k) − y(t k) + x(t k))
x(t k + h) = x(t k) + h ((a − b) y(t k) − ax(t k))
1
Disk drive example
y: = adin(in2) Clock
u:=K*(b/a*uc-y+x)
dout(u) Algorithm
x:=x+h*((a-b)*y-a*x)
Sampling period h = 0.2/ω 0
1
Output
0
0 5 10
0.5
Input
−0.5
1
0 5 10
Time (ω0t)
Increased sampling period
a) h = 0.5/ω 0 b) h = 1.08/ω 0
(a) (b)
Output
Output
1 1
0 0
0 5 10 0 5 10
0.5 0.5
Input
Input
0 0
−0.5 −0.5
0 5 10 15 0 5 10 15
Time (ω0t) Time (ω0t)
1
Better performance?
Dead-beat control h = 1.4/ω 0
u(t k) = t0 uc (t k) + t1 uc (t k−1 ) − s0 y(t k) − s1 y(t k−1 ) − r1 u(t k−1 )
1
Position
0
0 5 10
Velocity
0.5
0 5 10
0.5
Input
−0.5
0 5 10 1
Time (ω0t)
However, long sampling periods also have problems
• open loop between the samples
• disturbance and reference changes that occur between sam-
ples will remain undetected until the next sample
1
Sampled control theory
Computer
Clock
{y(t k )} {u(t k )} u(t) y(t )
A-D Algorithm D-A Process
Basic idea: look at the sampling instances only
• System theory analogous to continuous-time systems
• Better performance can be achieved
• Potential problem with intersample behaviour
1
Sampling of systems
Look at the system from the point of view of the computer
Clock
{u(t k )} u(t) y(t) { y (tk )}
D-A System A-D
Zero-order-hold sampling of a system
• Let the inputs be piecewise constant
• Look at the sampling points only
• Solve the system equation
1
Sampling a continuous-time system
Process:
dx
= Ax(t) + Bu(t)
dt
y(t) = Cx(t) + Du(t)
Solve the system equation:
t
A(t−tk ) A(t−s )
x ( t) = e x(t k) + e Bu(s ) ds
tk
t
A(t−tk ) A(t−s )
=e x(t k) + e ds Bu(t k) (u const.)
tk
t−tk
= eA(t−tk) x(tk) + eAs ds Bu(t k) (variable change)
0
= Φ(t, tk) x(tk) + Γ(t, tk)u(tk) 1
Periodic sampling
Assume periodic sampling, i.e. tk = kh. Then
x( kh + h) = Φ x( kh) + Γ u( kh)
y( kh) = Cx( kh) + Du( kh)
where
Φ = eAh
h
Γ= eAs ds B
0
Time-invariant linear system!
1
Example: Sampling of inverted pendulum
⎧ ⎫ ⎧ ⎫
dx ⎪ 0 1⎪ ⎪ 0⎪
=⎪⎩ ⎭x+⎪
⎪ ⎩ ⎪ ⎭u
dt 1 0 1
⎧ ⎫
y = ⎩1 0⎭x
We get
⎧ ⎫
⎪
⎪ cosh h sinh h ⎪
⎪
Φ=e =⎩
Ah
⎭
sinh h cosh h
h⎧ ⎫ ⎧ ⎫
⎪ sinh s ⎪ ⎪ cosh h − 1 ⎪
Γ= ⎪
⎩ ⎪ ⎪
⎭ ds = ⎩ ⎪
⎭
0 cosh s sinh h
Several ways to calculate Φ and Γ. Matlab
1
Sampling a system with a time delay
Sampling the system
dx(t)
dt
= Ax(t) + Bu(t − τ ), τ ≤h
we get the discrete-time system
x( kh + h) = Φ x( kh) + Γ 0 u( kh) + Γ 1 u( kh − h)
where
Φ = eAh
h−τ
Γ0 = eAs ds B
0
τ
Γ 1 = eA(h−τ ) eAs ds B
0
2
We get one extra state u( kh − h) in the sampled system
Stability region
• In continuous time the stability region is the complex left half
plane, i.e., the system is stable if all the poles are in the left
half plane.
• In discrete time the stability region is the unit circle.
2
Digital control design
Similar to analog control design, but
• Z-transform instead of Laplace transform
– zX ( z) x(t k+1 )
– z−1 X ( z) x(t k−1 )
• Poles are placed within the unit circle
• The frequency response is more difficult to compute
• The sampling interval h is a new design parameter
2
Choice of sampling interval
Nyquist’s sampling theorem:
“We must sample at least twice as fast as the highest
frequency we are interested in”
• What frequencies are we interested in?
2
Typical loop transfer function L(iω ) = P(iω ) C(iω ):
1
10
0
10
ωc
Förstärkning
−1
10
−2
10
−1 0
10 10
−50
−100
ϕm
Fas
−150
−200
−250
−1 0
10 10
Frekvens [rad/s]
• ω c = cross-over frequency, ϕ m = phase margin
• We should have ω s ≫ 2ω c
2
Sampling interval rule of thumb
A sample-and-hold (S&H) circuit can be approximated by a delay of
h/2.
G S&H (s) e−sh/2
This will decrease the phase margin by
arg G S&H (iω c ) = arg e−iω c h/2 = −ω c h/2
Assume we can accept a phase loss between 5○ and 15○ . Then
0.15 < ω c h < 0.5
This corresponds to a Nyquist frequency about 6 to 20 times larger
than the crossover frequency
2
Example: control of inverted pendulum
h = 0.1, h = 0.3, h = 0.5,
ω c h = 0.28 ω c h = 0.78 ω c h = 1.12
2 2 2
1 1 1
y
0 0 0
−1 −1 −1
0 5 0 5 0 5
10 10 10
0 0 0
u
−10 −10 −10
−20 −20 −20
0 5 0 5 0 5
Time Time Time
• Large ω c h may seem OK, but beware!
– Digital design assuming perfect model
2
– Controller perfectly synchronized with initial disturbance
Pendulum with non-synchronized disturbance
h = 0.1, h = 0.3, h = 0.5,
ω c h = 0.28 ω c h = 0.78 ω c h = 1.12
2 2 2
1 1 1
y
0 0 0
−1 −1 −1
0 5 0 5 0 5
10 10 10
0 0 0
u
−10 −10 −10
−20 −20 −20
0 5 0 5 0 5
Time Time Time
2
Accounting for the anti-aliasing filter
Assume we also have a second-order Butterworth anti-aliasing
filter with a gain of 0.1 at the Nyquist frequency. The filter gives an
additional phase margin loss of 1.4ω c h.
Again assume we can accept a phase loss of 5○ to 15○ . Then
0.05 < ω c h < 0.14
This corresponds to a Nyquist frequency about 23 to 70 times
larger than the crossover frequency
2
Session outline
• Sampled-data control
• Discretization of continuous-time controllers
• Implementation of PID Controllers
2
Discretization of continuous-time controllers
Basic idea: Reuse the analog design
H ( z) ≈ G (s)
u(t) {u ( kh )} { y ( kh )} y (t)
A-D Algorithm D-A
Clock
Want to get:
• A/D + Algorithm + D/A G (s)
Methods:
• Approximate s, i.e., H ( z) = G (s )
• Other discretization methods (Matlab) 3
Approximation methods
Forward Difference (Euler’s method):
dx(t) x(t k+1 ) − x(t k)
dt h
s = z−1
h
Backward Difference:
dx(t) x(t k) − x(t k−1 )
dt h
s = z−1
zh
Tustin:
dx(t)
+ dx(dt
tk+1 )
x(t k+1 ) − x(t k)
dt
2 h
s = h2 zz−
3
1
+1
Stability of approximations
How is the continuous-time stability region (left half plane) mapped?
Forward differences Backward differences Tustin
3
Discretization example
Controller designed in continuous-time:
b
U (s) = E(s)
s+a
z−1
Discretization using Forward Euler (s = h
):
b
u( k) = e( k)
( z − 1)/h + a
( z − 1 + ha)u( k) = bhe( k)
u( k + 1) = (1 − ha)u( k) + bhe( k)
u( k) = (1 − ha)u( k − 1) + bhe( k − 1)
Controller stable if −1 < (1 − ha) < 1, i.e., 0 < h < 2/a (does not 3
imply that the closed loop system is stable, though)
Controller Synthesis
Process Model
ẋ = Ax + Bu
G(s) y = Cx + Du
Control Design in Continuous-Time Discretize the process
• Loop shaping • e.g. ZOH Sampling
• Pole placement x(k + 1) = Φx(k) + Γu(k)
• PID
• …. y(k) = Cx(k) + Du(k)
Discretize the Controller Control Design in Discrete-Time
• Euler • Pole placement
• Tustin • LQG
• …. • ….
Difference Equation
Software algorithm
Session outline
• Sampled-data control
• Discretization of continuous-time controllers
• Implementation of PID Controllers
3
PID Algorithm
Textbook Algorithm:
t
u(t) = K ( e(t) + 1
TI
e(τ )dτ + TD dedt(t) )
U (s) = K ( E(s) + 1
sTI
E(s) + TD sE(s))
= P + I + D
3
Algorithm Modifications
Modifications are needed to make the controller practically useful
• Limitations of derivative gain
• Derivative weighting
• Setpoint weighting
3
Limitations of derivative gain
We do not want to apply derivation to high frequency measurement
noise, therefore the following modification is used:
sTD
sTD
1 + sTD / N
N = maximum derivative gain, often 10 − 20
3
Derivative weighting
The setpoint is often constant for long periods of time
Setpoint often changed in steps → D-part becomes very large.
Derivative part applied on part of the setpoint or only on the mea-
surement signal.
sTD
D (s) = (γ Ysp(s) − Y (s))
1 + sTD / N
Often, γ = 0 in process control, γ = 1 in servo control
3
Setpoint weighting
An advantage to also use weighting on the setpoint.
u = K ( ysp − y)
replaced by
u = K (β ysp − y)
0≤β ≤1
A way of introducing feedforward from the reference signal (position
a closed loop zero)
Improved set-point responses.
3
A better algorithm
1 TD s
U (s) = K (β yr − y + E(s) − Y (s))
sTI 1 + sTD / N
Modifications:
• Setpoint weighting (β ) in the proportional term improves set-
point response
• Limitation of the derivative gain (low-pass filter) to avoid deriva-
tion of measurement noise
• Derivative action only on y to avoid bumps for step changes in
the reference signal
4
Control Signal Limitations
All actuators saturate.
Problems for controllers with integration.
When the control signal saturates the integral part will continue to grow –
integrator (reset) windup.
When the control signal saturates the integral part will integrate up to a
very large value. This may cause large overshoots.
2 Output y and yref
1.5
0.5
0
0 10 20
Control variable u
0.2
−0.2
4
0 10 20
Anti-Reset Windup
Several solutions exist:
• controllers on velocity form (not discussed here))
• limit the setpoint variations (saturation never reached)
• conditional integration (integration is switched off when the
control is far from the steady-state)
• tracking (back-calculation)
4
Tracking
• when the control signal saturates, the integral is recomputed so
that its new value gives a control signal at the saturation limit
• to avoid resetting the integral due to, e.g., measurement noise,
the re-computation is done dynamically, i.e., through a LP-filter
with a time constant Tr .
4
Tracking
–y
KTds
Actuator
e = r− y v u
K Σ
K – +
Σ 1 Σ
Ti s
es
1
Tt
–y
KT d s
Actuator
model Actuator
e = r− y
K Σ
K 1 – +
Ti
Σ Σ
s
1 es 4
Tt
Tracking
0.5
0
0 10 20 30
0.15
0.05
−0.05
0 10 20 30
−0.4
−0.8
0 10 20 30 4
Discretization
P-part:
u P ( k) = K (β ysp( k) − y( k))
4
Discretization
I-part:
t
K dI K
I ( t) = e(τ )dτ , = e
TI dt TI
0
• Forward difference
I (t k+1 ) − I (t k) K
= e(t k)
h TI
I(k+1) := I(k) + (K*h/Ti)*e(k)
The I-part can be precalculated in UpdateStates
• Backward difference
The I-part cannot be precalculated, i(k) = f(e(k))
• Others 4
Discretization
D-part (assume γ = 0):
sTD
D=K (− Y (s))
1 + sTD / N
TD dD dy
+ D = − K TD
N dt dt
• Forward difference (unstable for small TD )
4
Discretization, cont.
D-part:
• Backward difference
TD D (t k) − D (t k−1 )
+ D (t k)
N h
y(t k) − y(t k−1 )
= − K TD
h
TD
D (t k) = D (t k−1 )
TD + Nh
K TD N
− ( y(tk) − y(tk−1 ))
TD + Nh
4
Discretization
Tracking:
v := P + I + D;
u := sat(v,umax,umin);
I := I + (K*h/Ti)*e + (h/Tr)*(u - v);
5
PID code
PID-controller with anti-reset windup
y = yIn.get(); // A-D conversion
e = yref - y;
D = ad * D - bd * (y - yold);
v = K*(beta*yref - y) + I + D;
u = sat(v,umax,umin)}
uOut.put(u); // D-A conversion
I = I + (K*h/Ti)*e + (h/Tr)*(u - v);
yold = y
ad and bd are precalculated parameters given by the backward
difference approximation of the D-term.
Execution time for CalculateOutput can be minimized even further.
5
Alternative PID Implementation
The PID controller described so far has constant gain, K (1 + N ), a
high frequencies, i.e., no roll-off at high frequencies.
An alternative is to instead of having a low pass filter only on the
derivative part use a second-order low-pass filter on the measured
signal before it enters a PID controller with a pure derivative part.
1
Y f (s) = 2 2 Y (s)
T f s + 1.4T f s + 1
1
U (s) = K (β Yre f (s) − Y f (s) + ( Yre f (s) − Y f (s)) − TD sY f (s))
TI s
5
Class SimplePID
public class SimplePID {
private double u,e,v,y;
private double K,Ti,Td,Beta,Tr,N,h;
private double ad,bd;
private double D,I,yOld;
public SimplePID(double nK, double nTi, double NTd,
double nBeta, double nTr, double nN, double nh) {
updateParameters(nK,nTi,nTd,nBeta,nTr,nN,nh);
}
public void updateParameters(double nK, double nTi, double NTd,
double nBeta, double nTr, double nN, double nh) {
K = nK; Ti = nTi; Td = nTd; Beta = nBeta;
Tr = nTr
N = nN;
h = nh;
ad = Td / (Td + N*h);
bd = K*ad*N;
}
5
public double calculateOutput(double yref, double newY) {
y = newY;
e = yref - y;
D = ad*D - bd*(y - yOld);
v = K*(Beta*yref - y) + I + D;
return v;
}
public void updateState(double u) {
I = I + (K*h/Ti)*e + (h/Tr)*(u - v);
yOld = y;
}
5
Extract from Regul
public class Regul extends Thread {
private SimplePID pid;
public Regul() {
pid = new SimplePID(1,10,0,1,10,5,0.1);
}
public void run() {
// Other stuff
while (true) {
y = getY();
yref = getYref():
u = pid.calculateOutput(yref,y);
u = limit(u);
setU(u);
pid.updateState(u);
// Timing Code
}
}
5
}
Task Models
5
Further reading
• B. Wittenmark, K. J. Åström, K.-E. Årzén: “Computer Control:
An Overview.” IFAC Professional Brief, 2002.
(Available at http://www.control.lth.se)
• K. J. Åström, B. Wittenmark: Computer-Controlled Systems,
Third Ed. Prentice Hall, 1997.
• K. J. Åström, Tore Hägglund: Advanced PID Control. The
Instrumentation, Systems, and Automation Society, 2005.