You are on page 1of 8

Outline PID Control: From Algorithm to Code

Karl-Erik rzen The basic algorithm Algorithm modications Discretization Mode handling Code

PID Control

PID Algorithm

Textbook Algorithm: The oldest controller type The most widely used
Pulp & Paper 86% Steel 93% Oil reneries 93% U (s) = K ( E(s) + u(t)

K ( e(t)

1 TI

e( )d +

TD de(t) ) dt

1 sTI

E(s)

+ TD sE(s))

Much to learn!! =
3

D
4

Proportional Term u
umax u0 umin e0 e0 e
1.5 1

Properties of P-Control
Set point and measured variable Kc=5

Kc=2 0.5 0 0 6 Control variable 4 2 Kc=5 Kc=2 Kc=1 Kc=1 5 10 15 20

Proportionalband

umax u = K e + u0 umin

e > e0

0 2 0 5 10

e0 < e < e0
e < e0

15

20

stationary error increased K means faster speed, increased noise sensitivity, worse stability
5 6

Errors with P-control


Control signal: Error:
e= u = K e + u0 u u0 K

Integral Term

u = K e + u0 u=K
e

e+

1 Ti

e(t)dt

(PI)

Error removed if: 1. K equals innity 2. u0 = u Solution: Automatic way to obtain u0


+
t

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

Automatic Reset
ub Kc e 1 1+sT i
1.5 1 0.5 0 0

Properties of PI-Control
Set point and measured variable Ti=1 Ti=2 Ti=5

U = KE+

Ti= 5 Control variable 10 15 20

1 U 1 + sTi 1 1 + sTi 1 sTi (1 )U = U= U 1 + sTi 1 + sTi 1 + sti 1 )E U = K (1 + sTi

2 Ti=2 1

Ti=1

Ti=5 Ti= 5 10 15 20

0 0

removes stationary error


9

smaller TI implies worse stability, faster steady-state error 10 removal

Prediction

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

A PI-controller contains no prediction The same control signal is obtained for both these cases:
e e

e(t)

e(t + Td)

tid

P:
I P t tid t I P tid

u(t) = K e(t) u(t) = K e(t) + Td de(t) dt K e(t + Td )

PD:

Td = Prediction horizon
11 12

Properties of PD-Control
Set point and measured variable Td=0.1 1 0.5 0 0 6 Control variable Td=0.1 Td=0.5 4 Td=2 2 0 2 0 5 10 15 20 Td=0.5 Td=2

Outline
The basic algorithm

10

15

20

Algorithm modications Discretization Mode handling Code

TD too small, no inuence TD too large, decreased performance In industrial practice the D-term is often turned off.
13 14

Algorithm Modications
Modications are needed to make the controller practically useful Limitations of derivative gain Derivative weighting Setpoint weighting

Limitations of derivative gain

We do not want to apply derivation to high frequency measurement noise, therefore the following modication is used:
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 Setpoint often changed in steps D-part becomes very large. Derivative part applied on part of the setpoint or only on the measurement signal.
D (s) = sTD ( Ysp(s) Y (s)) 1 + sTD / N

u = K ( ysp y)

replaced by
0 1

u = K ( ysp y)

A way of introducing feedforward from the reference signal (position a closed loop zero) Improved set-point responses.

Often, = 0 in process control, = 1 in servo control


17

18

Setpoint weighting
1.5 Set point and measured variable beta=1 1 0.5 0 0 3 Control variable 2 1 beta=0 0 0 20 40 60 beta=0.5 beta=0

How to introduce the reference: PID


u = K ( ysp y + d ( ysp y)d + TD ( ysp y)) = dt Z 1 de K (e + ed + TD )+ TI dt dy K ( 1) ysp + TD K ( 1) sp dt
K1 K2

1 TI

20

40

60

K1 ysp + K2 d ysp /dt ysp


+

PID

GP

beta=1 beta=0.5

Feedforward + Feedback

19

20

Two-degree-of-freedom controller
disturbance rejection
closed loop poles feedback
Gff
ysp
+

A better algorithm

servo performance closed loop zeros feedforward

Gfb

GP

U (s) = K ( yr y +

1 TD s Y (s)) E(s) sTI 1 + sTD / N

Modications: Setpoint weighting ( ) in the proportional term improves set-point 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
21 22

Y = G P ( G f f Ysp + G f b ( Ysp Y )) Y= G P (G f f + G f b) Ysp 1 + GP G f b

G f f modies the zeros but not the poles

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 1 0.5 0 0 Control variable u 0.2 0 0.2 0 10 20 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)

23

24

Tracking
y

Tracking
KTds Actuator e = r y K

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

1 s 1 Tt

K Ti

es

KT d s

Actuator model

Actuator

e = r y

K K Ti 1 s 1 Tt

+
es

25

26

Tracking
1 0.5 0 0.15 0.05 0.05 0 10 20 30

Outline
The basic algorithm Algorithm modications Discretization Mode handling Code

10

20

30

0 0.4 0.8 0 10 20 30

27

28

Discretization
I-part:

Discretization
I ( t) = K TI
t

e( )d ,
0

dI K = e dt TI

P-part: u P ( k) = K ( ysp( k) y( k))

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
29 30

Discretization
D-part (assume = 0): sTD D=K ( Y (s)) 1 + sTD / N TD dD dy + D = K TD N dt dt

Discretization, cont.
D-part:

Backward difference
TD D (t k) D (t k1 ) + D (t k) N h y(t k) y(t k1 ) h TD D (t k1 ) D (t k) = TD + Nh K TD N ( y(tk) y(tk1 )) TD + Nh

Forward difference (unstable for small TD )

= K TD

31

32

Discretization
The basic algorithm
Tracking:

Outline

Algorithm modications Discretization Mode handling Code

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

33

34

Bumpless Transfers
Avoid bumps in control signal when changing operating mode (manual - auto - manual) changing parameters changing between different controllers
Changing operating mode
+

Bumpless Mode Changes


1 Tt 1 Tm

y sp y

1 s PD M 1 s

Key Issue: Make sure that the controller states have the correct values, i.e., the same values before and after the change

K Ti

u A

1 Tt

35

36

Bumpless parameter changes


A change in a parameter when in stationarity should not result in a bump in the control signal. For example: v := P + I + D; I := I +(K*h/Ti)*e; 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

Bumpless parameter changes

More involved situation when setpoint weighting is used. The quantity P + I should be invariant to parameter changes.
Inew = Iold + K old ( old ysp y) K new( new ysp y)

38

Changing Controllers
Controller1 Process Controller2 Switch

Outline
The basic algorithm Algorithm modications Discretization

Similar to changing between manual and auto Let the controllers run in parallel 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 CPUtime)

Mode handling Code

39

40

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

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.
1 Y (s) 2 T f s2 + 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 Y f (s) =

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.


41

42

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; }
43

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

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

You might also like