Computer Implementation of Control Systems: Karl-Erik Årzen, Anton Cervin
Computer Implementation of Control Systems: Karl-Erik Årzen, Anton Cervin
t t
u (t ) y (t )
Process
Hold Sampler
uk yk
uk D-A Computer A-D yk
t t
. .
u(t)
Process
t t
D−A Sampler
and and
Hold A−D
Communication network
. .
uk
. ...
yk
. ...
uk
Computer
yk
. .
t t
−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
• 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
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
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)
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
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
Clock
x( kh + h) = Φ x( kh) + Γ u( kh)
y( kh) = Cx( kh) + Du( kh)
where
Φ = eAh
h
Γ= eAs ds B
0
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
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
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:
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]
1 1 1
y
0 0 0
−1 −1 −1
0 5 0 5 0 5
10 10 10
0 0 0
u
1 1 1
y
0 0 0
−1 −1 −1
0 5 0 5 0 5
10 10 10
0 0 0
u
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
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
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
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
3
Limitations of derivative gain
sTD
sTD
1 + sTD / N
3
Derivative weighting
sTD
D (s) = (γ Ysp(s) − Y (s))
1 + sTD / N
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
4
Tracking
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
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;
y = newY;
e = yref - y;
D = ad*D - bd*(y - yOld);
v = K*(Beta*yref - y) + I + D;
return v;
}
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);
}
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