You are on page 1of 58

# 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
Pr ocess
u t ( )
)
u
k
y t
(
u t ( )
y
k
Sampler Hold
Comput er
u
k
y
k
t
t
t
y t ( )
t
D-A A-D

Mix of continuous-time and discrete-time signals
Networked control systems
u
k
u
k
k
y
k
y
Communicat ion net wor k
Comput er
Pr ocess
y(t)
.
.
.
.
.
u(t)
t
.
and
D−A
Hold
Sampler
A−D
and
y(t) u(t)
.
.
.
.
t
.
.
.
.
.
.
.
.
t
t

Extra delay, possibly lost packets
Sampling
Process
A/D D/A Algorithm
Computer
u
y
A/D
DA-converter acts as a hold device
Normally, zero-order-hold is used = piecewise constant control
signals
Aliasing
0 5 10
−1
0
1
Time
ω
s
=

h
= sampling frequency
ω
N
=
ω
s
2
= Nyquist frequency
Frequencies above the Nyquist frequency are folded and appear as
low-frequency signals.
The fundamental alias for a frequency f
1
is given by
f = [( f
1
÷ f
N
) mod ( f
s
) − f
N
[
Above: f
1
= 0.9, f
s
= 1, f
N
= 0.5, f = 0.1
Anti-aliasing ﬁlter
Analog low-pass ﬁlter that eliminates all frequencies above the
Nyquist frequency

Analog ﬁlter
– 2-6th order Bessel or Butterworth ﬁlter
– Difﬁculties with changing h (sampling interval)

Analog + digital ﬁlter
– Fixed, fast sampling with ﬁxed analog ﬁlter
– Downsampling using digital LP-ﬁlter
– Control algorithm at the lower rate
– Easy to change sampling interval
The ﬁlter may have to be included in the control design
Example – Preﬁltering
0 10 20 30
−1
0
1
(a)
0 10 20 30
−1
0
1
(b)
0 10 20 30
−1
0
1
Time
(c)
0 10 20 30
−1
0
1
Time
(d)
ω
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
G(s) =
k
Js
2
Continuous time controller
U(s) =
bK
a
U
c
(s) − K
s ÷ b
s ÷ a
Y(s)
Discrete time controller (continuous time design + discretization)
u(t
k
) = K(
b
a
u
c
(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
u:=K*(b/a*uc-y+x)
dout(u)
x:=x+h*((a-b)*y-a*x)
Algor it hm
Clock
Sampling period h = 0.2]ω
0
0 5 10
0
1
O
u
t
p
u
t
0 5 10
−0.5
0
0.5
Time (ω
0
t )
I
n
p
u
t
1
Increased sampling period
a) h = 0.5]ω
0
b) h = 1.08]ω
0
0 5 10
0
1
(a)
O
u
t
p
u
t
0 5 10
0
1
(b)
O
u
t
p
u
t
0 5 10 15
−0.5
0
0.5
Time (ω
0
t )
I
n
p
u
t
0 5 10 15
−0.5
0
0.5
Time (ω
0
t )
I
n
p
u
t
1
Better performance?
0
u(t
k
) = t
0
u
c
(t
k
) ÷ t
1
u
c
(t
k−1
) − s
0
y(t
k
) − s
1
y(t
k−1
) − r
1
u(t
k−1
)
0 5 10
0
1
P
o
s
i
t
i
o
n
0 5 10
0
0.5
V
e
l
o
c
i
t
y
0 5 10
−0.5
0
0.5
Time (ω
0
t )
I
n
p
u
t
1
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
Algor it hm Pr ocess
Clock
A-D D-A
Comput er

y(t )

u(t )
y(t
k
)
{ }

u(t
k
) { }
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
D-A
Clock
Syst em A-D

{u (t
k
)}

y (t
k
) { }

y(t )

u (t )
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
dt
= Ax(t) ÷ Bu(t)
y(t) = Cx(t) ÷ Du(t)
Solve the system equation:
x(t) = e
A(t−t
k
)
x(t
k
) ÷

t
t
k
e
A(t−s
/
)
Bu(s
/
) ds
/
= e
A(t−t
k
)
x(t
k
) ÷

t
t
k
e
A(t−s
/
)
ds
/
Bu(t
k
) (u const.)
= e
A(t−t
k
)
x(t
k
) ÷

t−t
k
0
e
As
ds Bu(t
k
) (variable change)
= Φ(t, t
k
)x(t
k
) ÷ Γ(t, t
k
)u(t
k
)
1
Periodic sampling
Assume periodic sampling, i.e. t
k
= kh. Then
x(kh ÷ h) = Φx(kh) ÷ Γu(kh)
y(kh) = Cx(kh) ÷ Du(kh)
where
Φ = e
Ah
Γ =

h
0
e
As
ds B
Time-invariant linear system!
1
Example: Sampling of inverted pendulum
dx
dt
=

0 1
1 0

x ÷

0
1

u
y =

1 0

x
We get
Φ = e
Ah
=

cosh h sinh h
sinh h cosh h

Γ =

h
0

sinh s
cosh s

ds =

cosh h −1
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
Φ = e
Ah
Γ
0
=

h−τ
0
e
As
ds B
Γ
1
= e
A(h−τ )

τ
0
e
As
ds B
We get one extra state

u(kh − h)

in the sampled system
2
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.
1
1
2
Digital control design
Similar to analog control design, but

– 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 difﬁcult 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ω):
10
−1
10
0
10
−2
10
−1
10
0
10
1
F
ö
r
s
t
ä
r
k
n
i
n
g
10
−1
10
0
−250
−200
−150
−100
−50
F
a
s
ω
c
ϕ
m

ω
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
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
y
u
h = 0.1,
ω
c
h = 0.28
h = 0.3,
ω
c
h = 0.78
h = 0.5,
ω
c
h = 1.12

Large ω
c
h may seem OK, but beware!
– Digital design assuming perfect model
– Controller perfectly synchronized with initial disturbance
2
Pendulum with non-synchronized disturbance
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
0 5
−1
0
1
2
0 5
−20
−10
0
10
Time
y
u
h = 0.1,
ω
c
h = 0.28
h = 0.3,
ω
c
h = 0.78
h = 0.5,
ω
c
h = 1.12
2
Accounting for the anti-aliasing ﬁlter
Assume we also have a second-order Butterworth anti-aliasing
ﬁlter with a gain of 0.1 at the Nyquist frequency. The ﬁlter 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
Algor it hm
Clock

u kh ( ) { }

y kh ( ) { }

H ( z ) ≈ G(s )

y (t ) u (t )
A-D D-A
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)
dt
~
x(t
k÷1
) − x(t
k
)
h
s
/
=
z−1
h
Backward Difference:
dx(t)
dt
~
x(t
k
) − x(t
k−1
)
h
s
/
=
z−1
zh
Tustin:
dx(t)
dt
÷
dx(t
k÷1
)
dt
2
~
x(t
k÷1
) − x(t
k
)
h
s
/
=
2
h
z−1
z÷1
3
Stability of approximations
How is the continuous-time stability region (left half plane) mapped?
For war d differ ences Backwar d differ ences Tust in
3
Discretization example
Controller designed in continuous-time:
U(s) =
b
s ÷ a
E(s)
Discretization using Forward Euler (s
/
=
z−1
h
):
u(k) =
b
(z −1)]h ÷ a
e(k)
(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
imply that the closed loop system is stable, though)
3
Controller Synthesis
Process Model
G(s)
˙ x = Ax +Bu
y = Cx +Du
• Loop shaping
• Pole placement
• PID
• ….
Discretize the Controller
• Euler
• Tustin
• ….
Difference Equation
Software algorithm
Discretize the process
• e.g. ZOH Sampling
x(k + 1) = Φx(k) +Γu(k)
y(k) = Cx(k) +Du(k)
• Pole placement
• LQG
• ….
Session outline

Sampled-data control

Discretization of continuous-time controllers

Implementation of PID Controllers
3
PID Algorithm
Textbook Algorithm:
u(t) = K(e(t) ÷
1
T
I
t

e(τ )dτ ÷ T
D
de(t)
dt
)
U(s) = K(E(s) ÷
1
sT
I
E(s) ÷ T
D
sE(s))
= P ÷ I ÷ D
3
Algorithm Modiﬁcations
Modiﬁcations 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 modiﬁcation is used:
sT
D
~
sT
D
1 ÷ sT
D
]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.
D(s) =
sT
D
1 ÷ sT
D
]N
(γ Y
sp
(s) − Y(s))
Often, γ = 0 in process control, γ = 1 in servo control
3
Setpoint weighting
An advantage to also use weighting on the setpoint.
u = K(y
sp
− y)
replaced by
u = K(β y
sp
− 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
U(s) = K(β y
r
− y ÷
1
sT
I
E(s) −
T
D
s
1 ÷ sT
D
]N
Y(s))
Modiﬁcations:

Setpoint weighting (β) in the proportional term improves set-
point response

Limitation of the derivative gain (low-pass ﬁlter) 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.
0 10 20
0
0.5
1
1.5
2
Output y and yref
0 10 20
−0.2
0
0.2
Control variable u
4
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-ﬁlter
with a time constant T
r
.
4
Tracking
Σ
Act uat or
–y
v
u
– +
K Σ
Σ

e = r − y

1
T
t

K
T
i

KT
d
s

1
s

e
s
Act uat or
– +
Σ
Σ
Σ

e = r − y

KT
d
s
K

1
s

1
T
t

K
T
i
–y

e
s
Act uat or
model
4
Tracking
0 10 20 30
0
0.5
1
0 10 20 30
−0.05
0.05
0.15
0 10 20 30
−0.8
−0.4
0
4
Discretization
P-part:
u
P
(k) = K(β y
sp
(k) − y(k))
4
Discretization
I-part:
I(t) =
K
T
I
t

0
e(τ )dτ ,
dI
dt
=
K
T
I
e

Forward difference
I(t
k÷1
) − I(t
k
)
h
=
K
T
I
e(t
k
)
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):
D = K
sT
D
1 ÷ sT
D
]N
(−Y(s))
T
D
N
dD
dt
÷ D = −KT
D
dy
dt

Forward difference (unstable for small T
D
)
4
Discretization, cont.
D-part:

Backward difference
T
D
N
D(t
k
) − D(t
k−1
)
h
÷ D(t
k
)
= −KT
D
y(t
k
) − y(t
k−1
)
h
D(t
k
) =
T
D
T
D
÷ Nh
D(t
k−1
)

KT
D
N
T
D
÷ Nh
(y(t
k
) − y(t
k−1
))
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), at
high frequencies, i.e., no roll-off at high frequencies.
An alternative is to instead of having a low pass ﬁlter only on the
derivative part use a second-order low-pass ﬁlter on the measured
signal before it enters a PID controller with a pure derivative part.
Y
f
(s) =
1
T
2
f
s
2
÷1.4T
f
s ÷1
Y(s)
U(s) = K(β Y
re f
(s) − Y
f
(s) ÷
1
T
I
s
(Y
re f
(s) − Y
f
(s)) − T
D
sY
f
(s))
5
Class SimplePID
public class SimplePID {
private double u,e,v,y;
private double K,Ti,Td,Beta,Tr,N,h;
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);
}
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;
}
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);
// Timing Code
}
}
}
5
5

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.
5

Session outline
• Sampled-data control • Discretization of continuous-time controllers • Implementation of PID Controllers

Sampled-data control systems
u (t )

y (t )

t u (t ) Hold uk uk D-A Computer Process

y (t ) Sampler yk A-D yk

t

t

t

• Mix of continuous-time and discrete-time signals

Networked control systems

..... .
u(t) t

y(t) u(t) Process t D−A and Hold Sampler and A−D y(t)

..... .
uk t

Communication network uk Computer yk

..... .
yk t

• Extra delay, possibly lost packets

Sampling
Computer A/D Algorithm D/A u Process y

A/D

DA-converter acts as a hold device Normally, zero-order-hold is used signals piecewise constant control

The fundamental alias for a frequency f1 is given by f = ( f1 + f N ) mod ( fs ) − f N Above: f1 = 0.9. f N = 0.1 .Aliasing 1 0 −1 0 5 Time 10 ωs = ωN = 2π h = sampling frequency = Nyquist frequency ωs 2 Frequencies above the Nyquist frequency are folded and appear as low-frequency signals. f = 0.5. fs = 1.

Anti-aliasing ﬁlter Analog low-pass ﬁlter that eliminates all frequencies above the Nyquist frequency • Analog ﬁlter – 2-6th order Bessel or Butterworth ﬁlter – Difﬁculties with changing h (sampling interval) • Analog + digital ﬁlter – – – – Fixed. fast sampling with ﬁxed analog ﬁlter Downsampling using digital LP-ﬁlter Control algorithm at the lower rate Easy to change sampling interval The ﬁlter may have to be included in the control design .

ω N = 0.25 .5.9. ω alias = 0.Example – Preﬁltering (a) 1 0 −1 0 (c) 1 0 −1 0 10 Time 20 30 10 20 30 (d) 1 0 −1 0 10 Time 20 30 (b) 1 0 −1 0 10 20 30 ω d = 0.1 6th order Bessel with ω B = 0.

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 s+b bK Uc (s) − K Y (s) U (s) = a s+a Discrete time controller (continuous time design + discretization) x(t k + h) = x(t k) + h ((a − b) y(t k) − ax(t k)) b u(t k) = K ( a uc (t k) − y(t k) + x(t k)) 1 .

5 0 5 10 0 5 Time (ω0t) 10 1 .5 Input 0 −0.2/ω 0 Output 1 Clock Algorithm 0 0.Disk drive example y: = adin(in2) u:=K*(b/a*uc-y+x) dout(u) x:=x+h*((a-b)*y-a*x) Sampling period h = 0.

5 Input 0 −0.08/ω 0 (a) Output 1 (b) Output 0 5 10 1 0 0.Increased sampling period a) h = 0.5 0 5 10 0 5 10 Time (ω0t) 15 0 5 10 Time (ω0t) 15 1 .5 0 0.5 Input 0 −0.5/ω 0 b) h = 1.

5 5 10 Input 0 −0.5 0 5 Time (ω0t) 10 1 .Better performance? Dead-beat control h = 1.5 0 0 0.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 ) Position 1 0 0 5 10 Velocity 0.

However. long sampling periods also have problems • open loop between the samples • disturbance and reference changes that occur between samples will remain undetected until the next sample 1 .

Sampled control theory Computer Clock A-D {y(t k )} {u(t k )} Algorithm D-A u(t) Process y(t ) 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) D-A System y(t) A-D { y (tk )} Zero-order-hold sampling of a system • Let the inputs be piecewise constant • Look at the sampling points only • Solve the system equation 1 .

tk) x(tk) + Γ(t.) eAs ds Bu(t k) (variable change) = eA(t−tk) x(tk) + = eA(t−tk) x(tk) + tk t−tk 0 = Φ(t. tk)u(tk) 1 .Sampling a continuous-time system Process: dx = Ax(t) + Bu(t) dt y(t) = Cx(t) + Du(t) Solve the system equation: x(t) = eA(t−tk) x(t k) + t tk t eA(t−s ) Bu(s ) ds eA(t−s ) ds Bu(t k) (u const.

Then x( kh + h) = Φ x( kh) + Γ u( kh) y( kh) = Cx( kh) + Du( kh) where Φ = eAh Γ= 0 h eAs ds B Time-invariant linear system! 1 .Periodic sampling Assume periodic sampling.e. tk = kh. i.

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 ⎪ Ah ⎪ Φ=e =⎪ ⎭ ⎩ sinh h cosh h ⎧ ⎫ ⎫ ⎧ h ⎪ sinh s ⎪ ⎪ ⎪ ds = ⎪ cosh h − 1 ⎪ ⎪ ⎪ Γ= ⎩ ⎭ ⎭ ⎩ cosh s sinh h 0 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 Γ0 = 0 h−τ eAs ds B τ 0 Γ 1 = eA(h−τ ) eAs ds B We get one extra state u( kh − h) in the sampled system 2 .

e. • In discrete time the stability region is the unit circle.Stability region • In continuous time the stability region is the complex left half plane. the system is stable if all the poles are in the left half plane. i. 1 1 2 ..

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 difﬁcult to compute • The sampling interval h is a new design parameter 2 .Digital control design Similar to analog control design.

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 .

ϕ m = phase margin • We should have ω s ≫ 2ω c 2 .Typical loop transfer function L(iω ) = P(iω ) C(iω ): 10 1 Förstärkning 10 0 ωc 10 −1 10 −2 10 −1 10 0 −50 −100 −150 ϕm 10 Frekvens [rad/s] 0 Fas −200 −250 −1 10 • ω c = cross-over frequency.

Then 0. 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○ .Sampling interval rule of thumb A sample-and-hold (S&H) circuit can be approximated by a delay of h/2.15 < ω c h < 0.5 This corresponds to a Nyquist frequency about 6 to 20 times larger than the crossover frequency 2 .

1. but beware! – Digital design assuming perfect model – Controller perfectly synchronized with initial disturbance 2 . ω c h = 0.12 y 0 −1 10 0 0 5 0 5 0 5 u −10 −20 0 Time 5 0 Time 5 0 Time 5 • Large ω c h may seem OK. ω c h = 0. ω c h = 1.28 2 1 0 −1 10 0 −10 −20 h = 0.78 2 1 0 −1 10 0 −10 −20 h = 0.Example: control of inverted pendulum 2 1 h = 0.3.5.

ω c h = 0.12 2 1 2 1 0 −1 10 0 −10 −20 2 1 0 −1 10 0 −10 −20 y 0 −1 10 0 0 5 0 5 0 5 u −10 −20 0 Time 5 0 Time 5 0 Time 5 2 .5.3.1.78 h = 0.28 h = 0.Pendulum with non-synchronized disturbance h = 0. ω c h = 0. ω c h = 1.

Accounting for the anti-aliasing ﬁlter Assume we also have a second-order Butterworth anti-aliasing ﬁlter with a gain of 0.4ω c h. The ﬁlter gives an additional phase margin loss of 1. Then 0.05 < ω c h < 0.1 at the Nyquist frequency. Again assume we can accept a phase loss of 5○ to 15○ .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 .

i..Discretization of continuous-time controllers Basic idea: Reuse the analog design H ( z) ≈ G (s) u(t) A-D {u ( kh )} Algorithm { y ( kh )} y (t) D-A Clock Want to get: • A/D + Algorithm + D/A Methods: • Approximate s. H ( z) = G (s ) • Other discretization methods (Matlab) G ( s) 3 .e.

Approximation methods Forward Difference (Euler’s method): dx(t) dt x(t k+1 ) − x(t k) h s = z−1 h Backward Difference: dx(t) dt x(t k) − x(t k−1 ) h s = z−1 zh Tustin: dx(t) dt tk + dx(dt+1 ) x(t k+1 ) − x(t k) 2 h 2 z s = h z−1 +1 3 .

Stability of approximations How is the continuous-time stability region (left half plane) mapped? Forward differences Backward differences Tustin 3 .

e. i.. though) .Discretization example Controller designed in continuous-time: b E(s) U (s) = s+a Discretization using Forward Euler (s = z−1 ): h b e( k) u( 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. 0 < h < 2/a (does not 3 imply that the closed loop system is stable.

x = Ax + Bu ˙ y = Cx + Du Discretize the process • e. Discretize the Controller • Euler • Tustin • ….g.Controller Synthesis Process Model G(s) Control Design in Continuous-Time • Loop shaping • Pole placement • PID • …. Difference Equation Software algorithm . ZOH Sampling x(k + 1) = Φx(k) + Γu(k) y(k) = Cx(k) + Du(k) Control Design in Discrete-Time • Pole placement • LQG • ….

Session outline • Sampled-data control • Discretization of continuous-time controllers • Implementation of PID Controllers 3 .

PID Algorithm Textbook Algorithm: u(t) = K ( e(t) + 1 TI t e(τ )dτ + TD de(t) ) dt U (s) = K ( E(s) + 1 sTI E(s) + TD sE(s)) = P + I + D 3 .

Algorithm Modiﬁcations Modiﬁcations 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. often 10 − 20 3 . therefore the following modiﬁcation is used: sTD sTD 1 + sTD / N N = maximum derivative gain.

γ = 1 in servo control 3 . Derivative part applied on part of the setpoint or only on the measurement signal.Derivative weighting The setpoint is often constant for long periods of time Setpoint often changed in steps → D-part becomes very large. γ = 0 in process control. sTD D (s) = (γ Ysp(s) − Y (s)) 1 + sTD / N Often.

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 .Setpoint weighting An advantage to also use weighting on the setpoint.

A better algorithm 1 TD s Y (s)) U (s) = K (β yr − y + E(s) − sTI 1 + sTD / N Modiﬁcations: • Setpoint weighting (β ) in the proportional term improves setpoint response • Limitation of the derivative gain (low-pass ﬁlter) to avoid derivation of measurement noise • Derivative action only on y to avoid bumps for step changes in the reference signal 4 .

When the control signal saturates the integral part will integrate up to a very large value.2 0 −0. Problems for controllers with integration.Control Signal Limitations All actuators saturate. This may cause large overshoots.5 1 0.5 0 0 Control variable u 0.2 0 10 20 10 20 4 . When the control signal saturates the integral part will continue to grow – integrator (reset) windup. 2 Output y and yref 1.

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 .

4 .e.. i.g. e.Tracking • when the control signal saturates.. the re-computation is done dynamically. the integral is recomputed so that its new value gives a control signal at the saturation limit • to avoid resetting the integral due to. through a LP-ﬁlter with a time constant Tr . measurement noise.

Tracking –y KTds Actuator e = r− y K Σ 1 s 1 Tt v u K Ti Σ – Σ es + –y KT d s Actuator model Actuator e = r− y K K Ti 1 s 1 Tt Σ – Σ + es Σ 4 .

15 0.05 0 10 20 30 0 10 20 30 0 −0.8 0 10 20 30 4 .5 0 0.4 −0.Tracking 1 0.05 −0.

Discretization P-part: u P ( k) = K (β ysp( k) − y( k)) 4 .

= e dt TI • 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.Discretization I-part: K I ( t) = TI t 0 K dI e(τ )dτ . 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 .

cont.Discretization. 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−1 ) D (t k) = TD + Nh K TD N ( y(tk) − y(tk−1 )) − TD + Nh 4 .

5 .umin).Discretization Tracking: v := P + I + D. I := I + (K*h/Ti)*e + (h/Tr)*(u .umax.v). u := sat(v.

u = sat(v.y) + I + D.bd * (y .v).y. // A-D conversion e = yref .yold).put(u). yold = y ad and bd are precalculated parameters given by the backward difference approximation of the D-term.umax. // D-A conversion I = I + (K*h/Ti)*e + (h/Tr)*(u . D = ad * D . 5 .get(). Execution time for CalculateOutput can be minimized even further.umin)} uOut.PID code PID-controller with anti-reset windup y = yIn. v = K*(beta*yref .

a high frequencies.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 . i.. 1 Y (s) Y f (s) = 2 2 T f s + 1.e. no roll-off at high frequencies.Alternative PID Implementation The PID controller described so far has constant gain. An alternative is to instead of having a low pass ﬁlter only on the derivative part use a second-order low-pass ﬁlter on the measured signal before it enters a PID controller with a pure derivative part. K (1 + N ).

double nBeta. double nN. ad = Td / (Td + N*h).h.nh). double nTr.Beta.N. double nN. double NTd. Td = nTd. h = nh.y. private double ad.Td. Ti = nTi. double nh) { K = nK. double nBeta.I. Beta = nBeta. } public void updateParameters(double nK.nTr. double nTi.nTi.nBeta. bd = K*ad*N. double nTi. double NTd.e.Tr. } 5 . Tr = nTr N = nN. private double D.nN. private double K.yOld.bd. double nh) { updateParameters(nK.Class SimplePID public class SimplePID { private double u. public SimplePID(double nK.v.nTd. double nTr.Ti.

D = ad*D .public double calculateOutput(double yref.v).y) + I + D. e = yref . } public void updateState(double u) { I = I + (K*h/Ti)*e + (h/Tr)*(u . } } 5 . yOld = y.y. v = K*(Beta*yref . return v.bd*(y .yOld). double newY) { y = newY.

// Timing Code } } } 5 .0.y). } public void run() { // Other stuff while (true) { y = getY().1).0. yref = getYref(): u = pid.5.Extract from Regul public class Regul extends Thread { private SimplePID pid. pid.1.updateState(u). u = limit(u). setU(u).10. public Regul() { pid = new SimplePID(1.10.calculateOutput(yref.