Sie sind auf Seite 1von 8

Outline

PID Control: From Algorithm to Code • The basic algorithm


• Algorithm modifications
Karl-Erik Årzen
• Discretization
• Mode handling
• Code

1
2

PID Control PID Algorithm

Textbook Algorithm:
t
• The oldest controller type u(t) = K ( e(t) + 1
e(τ )dτ + TD dedt(t) )
TI
• The most widely used
– Pulp & Paper 86%
– Steel 93% U (s) = K ( E(s) + 1
E(s) + TD sE(s))
sTI
– Oil refineries 93%
• Much to learn!!
= P + I + D

3 4

Proportional
u
Term Properties of P-Control
Set point and measured variable
1.5
umax Kc=5

1
u0 Kc=2
0.5
Kc=1
umin 0
e 0 5 10 15 20
– e0 e0
6 Control variable
Proportionalband 4 Kc=5

2
Kc=2
⎧ 0 Kc=1


⎪umax e > e0 −2

0 5 10 15 20
u = K e + u0 − e0 < e < e0



umin e < − e0
• stationary error
• increased K means faster speed, increased noise sensitiv-
ity, worse stability
5 6
Errors with P-control Integral Term

Control signal:
u = K e + u0
u = K e + u0
Error:  
u − u0 1
u = K e+ e(t)dt (PI)
e= Ti
K e

Error removed if:


1. K equals infinity
+
2. u0 = u
t
Solution: Automatic way to obtain u0 –

Stationary error present → edt increases → u increases → y
increases → the error is not stationary
7 8

Automatic Reset Properties of PI-Control


Set point and measured variable

1 1.5
Ti=1
Ti=2
ub 1+sT i 1
Ti=5
0.5
Kc e u Ti=∞

Σ 0
0 5 10 15 20
Control variable

2 Ti=1
1
U = KE+ U Ti=2

1 + sTi 1
Ti=5
1 1 + sTi − 1 sTi
(1 − )U = U=
Ti=∞
U 0
1 + sTi 1 + sTi 1 + sti 0 5 10 15 20

1
U = K (1 + )E
sTi
• removes stationary error
• smaller TI implies worse stability, faster steady-state error
9 10
removal

Prediction Derivative Part

Reglerfel
de(t)
e(t) + Td
dt

A PI-controller contains no prediction


e(t)
e(t + Td)
The same control signal is obtained for both these cases:
e e
tid

P:
I I u(t) = K e(t)
P P PD: 
de(t)
t tid t tid u(t) = K e(t) + Td  K e(t + Td )
dt
Td = Prediction horizon
11 12
Properties of PD-Control Outline
Set point and measured variable
Td=0.1
1
Td=0.5

0.5
Td=2
• The basic algorithm
0
0 5 10 15 20
• Algorithm modifications
6 Control variable
Td=0.1 • Discretization
4 Td=0.5

• Mode handling
Td=2
2

−2
• Code
0 5 10 15 20

• TD too small, no influence


• TD too large, decreased performance

In industrial practice the D-term is often turned off.

13 14

Algorithm Modifications Limitations of derivative gain

Modifications are needed to make the controller practically


useful

• Limitations of derivative gain We do not want to apply derivation to high frequency measure-
• Derivative weighting ment noise, therefore the following modification is used:
• Setpoint weighting
sTD
sTD 
1 + sTD / N
N = maximum derivative gain, often 10 − 20

15 16

Derivative weighting Setpoint weighting

An advantage to also use weighting on the setpoint.


The setpoint is often constant for long periods of time
u = K ( ysp − y)
Setpoint often changed in steps → D-part becomes very large.
replaced by
Derivative part applied on part of the setpoint or only on the u = K (β ysp − y)
measurement signal.
0≤β ≤1
sTD A way of introducing feedforward from the reference signal
D (s) = (γ Ysp(s) − Y (s)) (position a closed loop zero)
1 + sTD / N
Often, γ = 0 in process control, γ = 1 in servo control Improved set-point responses.

17 18
Setpoint weighting How to introduce the reference: PID
Z
1 d
u = K (β ysp − y + ( ysp − y)dτ + TD (γ ysp − y)) =
TI dt
1.5 Set point and measured variable Z
1 de
beta=1 K (e + edτ + TD )+
TI dt
1
dy
K (β − 1) ysp + TD K (γ − 1) sp
0.5
beta=0.5
beta=0

dt
K1 K2

0
0 20 40 60 K1 ysp + K2 d ysp /dt
3 Control variable ysp y
+ PID + GP

2
beta=1
beta=0.5
1
beta=0 Feedforward + Feedback
0
0 20 40 60

19 20

Two-degree-of-freedom controller A better algorithm


• disturbance rejection
– closed loop poles Gff
– feedback 1 TD s
ysp y U (s) = K (β yr − y + E(s) − Y (s))
• servo performance +

Gfb + GP sTI 1 + sTD / N
– closed loop zeros
Modifications:
– feedforward
• Setpoint weighting (β ) in the proportional term improves
Y = G P ( G f f Ysp + G f b ( Ysp − Y ))
set-point response
G P (G f f + G f b)
Y= Ysp
1 + GP G f b • Limitation of the derivative gain (low-pass filter) to avoid
G f f modifies the zeros but not the poles derivation of measurement noise
• Derivative action only on y to avoid bumps for step
changes in the reference signal

21 22

Control Signal Limitations Anti-Reset Windup


All actuators saturate.
Several solutions exist:
Problems for controllers with integration.
• controllers on velocity form (not discussed here))
When the control signal saturates the integral part will continue to
grow – integrator (reset) windup. • limit the setpoint variations (saturation never reached)

When the control signal saturates the integral part will integrate up
• conditional integration (integration is switched off when the
control is far from the steady-state)
to a very large value. This may cause large overshoots.
2 Output y and yref

1.5
• tracking (back-calculation)
1

0.5

0
0 10 20
Control variable u

0.2

−0.2

0 10 20

23 24
Tracking Tracking

–y
KTds

• when the control signal saturates, the integral is recom- Actuator

puted so that its new value gives a control signal at the e = r− y


K Σ
v u

saturation limit
K – +
Σ 1 Σ
• to avoid resetting the integral due to, e.g., measurement Ti s

es
noise, the re-computation is done dynamically, i.e., through 1
Tt
a LP-filter with a time constant Tr .
–y
KT d s
Actuator
model Actuator
e = r− y
K Σ

K 1 – +
Ti
Σ Σ
s

1 es
Tt

25 26

Tracking Outline

1 • The basic algorithm


0.5
• Algorithm modifications
0
0 10 20 30 • Discretization
0.15
• Mode handling
0.05 • Code
−0.05
0 10 20 30

−0.4

−0.8
0 10 20 30

27 28

Discretization Discretization
I-part:
t
K dI K
I ( t) = e(τ )dτ , = e
TI dt TI
0

P-part: • Forward difference


I (t k+1 ) − I (t k) K
= e(t k)
u P ( k) = K (β ysp( k) − y( 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
29 30
Discretization Discretization, cont.
D-part (assume γ = 0): D-part:
sTD • Backward difference
D=K (− Y (s))
1 + sTD / N
TD D (t k) − D (t k−1 )
TD dD dy + D (t k)
+ D = − K TD N h
N dt dt
y(t k) − y(t k−1 )
• Forward difference (unstable for small TD ) = − K TD
h
TD
D (t k) = D (t k−1 )
TD + Nh
K TD N
− ( y(tk) − y(tk−1 ))
TD + Nh

31 32

Discretization Outline

• The basic algorithm

Tracking: • Algorithm modifications


• Discretization
v := P + I + D;
• Mode handling
u := sat(v,umax,umin);
I := I + (K*h/Ti)*e + (h/Tr)*(u - v); • Code

33 34

Bumpless Transfers Bumpless Mode Changes


Avoid bumps in control signal when
1
Σ
• changing operating mode (manual - auto - manual) Tt

+

• changing parameters +

1
Σ
1
Tm s
• changing between different controllers
y sp
PD
Changing operating mode y
M
e K 1 u
Key Issue: Make sure that the controller states have the correct Ti
Σ s Σ
A
values, i.e., the same values before and after the change – +
Σ
1
Tt

35 36
Bumpless parameter changes Bumpless parameter changes

A change in a parameter when in stationarity should not result


in a bump in the control signal.
More involved situation when setpoint weighting is used. The
For example: quantity P + I should be invariant to parameter changes.
v := P + I + D;
I := I +(K*h/Ti)*e; Inew = Iold + K old (β old ysp − y) − K new(β new ysp − y)

or

v := P + (K/Ti)*I + D;
I := I + h*e;

The latter results in a bump in u if K or Ti are changed.


37 38

Changing Controllers Outline


Controller1

Process

Controller2 Switch
• The basic algorithm
• Algorithm modifications
• Discretization
Similar to changing between manual and auto • Mode handling
Let the controllers run in parallel • Code
Let the controller that is not active track the one that is active.
Alternatively, execute only the active controller and initialize the
new controller to its correct value when switching (saves CPU-
time)

39 40

PID code Alternative PID Implementation


PID-controller with anti-reset windup
The PID controller described so far has constant gain, K (1 +
y = yIn.get(); // A-D conversion N ), at high frequencies, i.e., no roll-off at high frequencies.
e = yref - y;
An alternative is to instead of having a low pass filter only on
D = ad * D - bd * (y - yold);
v = K*(beta*yref - y) + I + D;
the derivative part use a second-order low-pass filter on the
u = sat(v,umax,umin)} measured signal before it enters a PID controller with a pure
uOut.put(u); // D-A conversion derivative part.
I = I + (K*h/Ti)*e + (h/Tr)*(u - v);
yold = y 1
Y f (s) = Y (s)
T f2 s2 + 1.4T f s + 1
ad and bd are precalculated parameters given by the back-
ward difference approximation of the D-term. 1
U (s) = K (β Yre f (s) − Y f (s) + ( Yre f (s) − Y f (s)) − TD sY f (s))
TI s
Execution time for CalculateOutput can be minimized even
further.
41 42
Class SimplePID
public class SimplePID {
private double u,e,v,y; public double calculateOutput(double yref, double newY) {
private double K,Ti,Td,Beta,Tr,N,h;
private double ad,bd; y = newY;
private double D,I,yOld; e = yref - y;
D = ad*D - bd*(y - yOld);
public SimplePID(double nK, double nTi, double NTd, v = K*(Beta*yref - y) + I + D;
double nBeta, double nTr, double nN, double nh) { return v;
updateParameters(nK,nTi,nTd,nBeta,nTr,nN,nh); }
}
public void updateState(double u) {
public void updateParameters(double nK, double nTi, double NTd,
double nBeta, double nTr, double nN, double nh) { I = I + (K*h/Ti)*e + (h/Tr)*(u - v);
K = nK; Ti = nTi; Td = nTd; Beta = nBeta; yOld = y;
Tr = nTr }
N = nN;
h = nh; }
ad = Td / (Td + N*h);
bd = K*ad*N;
}

43 44

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
}
}
}
45