• No results found

Discrete PID controllers

In document Advanced Control using MATLAB (Page 165-170)

The PID controller

4.5 Discrete PID controllers

To implement a PID controller as given in Eqn.4.1on a computer, one must first discretise or approximate the continuous controller equation. If the error at time t = kT is ǫk, then the contin-uous expression

The integral in Eqn.4.17is approximated in Eqn.4.18by the rectangular rule and the derivative is approximated as a first order difference, although other discretisations are possible. Normally the sample time, T , used by the controller is much faster than the dominant time constants of the process so the approximation is satisfactory and the discrete PID controller is, to all intents and purposes, indistinguishable from a continuous controller.

It is considerably more convenient to consider the discrete PID controller as a rational polynomial expression in z. Taking z-transforms of the discrete position form of the PID controller, Eqn.4.18, we get

This shows that the transfer function of the PID controller is GPID(z) = Kc

Eqn.4.19is the discrete approximation to Eqn.4.2and the approximation is reasonable provided the sample time, T , is sufficiently short. A block diagram of this discrete approximation is

Kc T

-which we could further manipulate this using (block diagram algebra rules) to use only delay elements to give

Kc T

There are other alternatives for a discrete PID controller depending on how we approximate the integral part. For example we could use a backward difference,

ukT = u(k−1)T

(which is not to be recommended due to stability problems), or the trapezoidal approximation.

ukT = u(k−1)T

We can insert any one of these alternatives to give the overall discrete z-domain PID controller, although the trapeziodal scheme is the most accurate and therefore the preferred implementation.

A SIMULINKdiscrete PID controller with sample time T using the trapeziodal approximation of Eqn.4.21is given in Fig.4.12. Note that now, as opposed to the continuous time implementation of the PID controller, the derivative and integral gain values are now a function of sample time, T . While continuous versions of PID controllers exist in SIMULINK, discrete versions simulate much faster.

4.5.1 Discretising continuous PID controllers

The easiest way to generate a discrete PID controller is to simply call the MATLABstandard PID pidstdwith a trailing argument to indicate that you want a discrete controller. Since the default

u

Figure 4.12: A discrete PID controller in SIMULINK using a trapezoidal approximation for the integral with sample time T following Eqn.4.21. This controller block is used in the simulation presented in Fig.4.13.

discretisation strategy is using the forward Euler, it would be prudent to use explicitly state the stable backward Euler option for both the integration and differentiation.

Listing 4.2: Constructing a discrete (filtered) PID controller

>> C = pidstd(1,2,3,100,0.1, ...

'IFormula','BackwardEuler','DFormula','BackwardEuler') Discrete-time PIDF controller in standard form:

4

Velocity form of the PID controller

Eqn.4.18is called the position form implementation of the PID controller. An alternative form is the velocity form which is obtained by subtracting the previous control input uk−1 from the current input ukto get

∆uk= uk− uk−1 The velocity form in Eqn.4.23has three advantages over the position form: (see [191, pp636-637]), namely it requires no need for initialisation, (the computer does not need to know the current u), no integral windup problems, and the algorithm offers some protection against computer failure in that if the computer crashes, the input remains at the previous, presumably reasonable, value.

One drawback, however, is that it should not be used for P or PD controllers since the controller is unable to maintain the reference value.

Fig.4.13shows a SIMULINKblock diagram of a pressure control scheme on a headbox of a paper machine. The problem was that the pressure sensor was very slow, only delivering a new pres-sure reading every 5 seconds. The digital PID controller however was running with a frequency of 1 Hz. The control engineer in this application faced with poor closed loop performance, was concerned that the pressure sensor was too slow and therefore should be changed. In Fig.4.13, the plant is a continuous transfer function while the discrete PID controller runs with sample period of T = 1 second, and the pressure sensor is modelled with a zeroth-order hold with T = 5 seconds. Consequently the simulation is a mixed continuous/discrete example, with more than one discrete sample time.

pressure sensor 200s +30s+12

3.5

headbox Signal

Generator

Scope error ctrl output

Discrete PID

Figure 4.13: Headbox control with a slow pressure transducer. The discrete PID controller was given in Fig.4.12.

Fig.4.14shows the controlled results. Note how the pressure signal to the controller lags behind the true pressure, but the controller still manages to control the plant.

4.5.2 Simulating a PID controlled response in Matlab

We can simulate a PID controlled plant in MATLABby writing a small script file that calls a gen-eral PID controller function. The PID controller is written in the discrete velocity form following Eqn.4.23in the MATLABfunction filepidctr.mshown in listing4.3.

Listing 4.3: A simple PID controller function [u,e] = pidctr(err,u,dt,e,pidt)

% [u,e] = pidctr(err,u,dt,e,pidt)

%PID controller

4 %err: = ǫ, current error, u = u(t), current manipulated variable

%dt = T sample time, e: row vector of past 3 errors

%pidt = [Kc, 1/τI, τd] = tuning constants

k = pidt(1); rs = pidt(2); td2=pidt(3)/dt;

9 e = [err,e(1:2)]; %error shift register du = k*[1+rs*dt+td2,-1-2*td2,td2]*e';

u = du + u; %Update control value unew= uold+ ∆u return

This simple PID controller function is a very naive implementation of a PID controller without any of the necessary modifications common in robust commercial industrial controllers as de-scribed in section4.4.

0 50 100 150

−4

−2 0 2 4

u

time [s]

−1.5

−1

−0.5 0 0.5 1 1.5 2

y & setpoint

y y sampled setpoint

Figure 4.14: Headbox control with a slow pressure transducer measurement sample time of T = 5 while the control sample time is T = 1. Upper: The pressure setpoint (dotted), the actual pressure and the sampled-and-held pressure fed back to the PID controller. Lower: The controller output.

The plant to be controlled for this simulation is

Gp(q−1) = q−d 1.2q−1

1− 0.25q−1− 0.5q−2 (4.24)

where the sample time is T = 2 seconds and the dead time is d = 3 sample time units, and the setpoint is a long period square wave. For this simulation, we will try out the PID controller with tuning constants of Kc= 0.3, 1/τi= 0.2 and τd= 0.1. How I arrived at these tuning constants is discussed later in §4.6.

The MATLABsimulation using the PID function from Listing4.3is given by the following script file:

a=[0.25,0.5];b=1.2;theta=[a b]'; dead = 3; %Plant G(q), Eqn.4.24.

3 dt = 2.0; t = dt*[0:300]'; yspt = square(t/40); %time vector & setpoint y = zeros(size(yspt));u=zeros(size(y)); %initialise y(t) & u(t)

pid = [0.3, 0.2, 0.1]; %PID tuning constants: Kc= 0.3, 1/τI= 0.2, τd= 0.1 e = zeros(1,3); % initialise error integral

8 for i=3+dead:length(y)

X = [y(i-1), y(i-2), u(i-1-dead)]; %collect i/o

y(i) = X*theta; %system prediction

err = yspt(i) - y(i); %current error

[u(i),e] = pidctr(err,u(i-1),dt,e,pid); %PID controller from Listing4.3.

13 end % for

plot(t,yspt,'--',t,[y,u]) %Plot results in Fig.4.15.

Figure4.15shows the controlled response of this simulation. Note how I have plotted the input (lower plot of Fig. 4.15) as a series of horizontal lines that show that the input is actually a piecewise zeroth-order hold for this discrete case using thestairsfunction.

Figure 4.15: The output (up-per solid), setpoint (up(up-per dashed) and discretised input (lower) of a PID controlled process.

−2

−1 0 1 2

PID control

y & setpoint

0 50 100 150 200

−1

−0.5 0 0.5 1

Time

Input

4.5.3 Controller performance as a function of sample time

Given that the discrete PID controller is an approximation to the continuous controller, we must expect a deterioration in performance with increasing sample time. Our motivation to use coarse sampling times is to reduce the computational load. Fig.4.16compares the controlled response of the continuous plant,

Gp(s) = s + 3

(s + 4)(τ2s2+ 2ζτ s + 1)

with τ = 4, ζ = 0.4 given the same continuous controller settings at different sampling rates.

Note that the reset and derivative controller settings for the discrete controller are functions of time, and must be adjusted accordingly. Fig.4.16shows that the controller performance improves as the sampling time is decreased and converges to the continuous case. However if the sampling time is too small, the discrete PID controller is then suspectable to numerical problems.

In document Advanced Control using MATLAB (Page 165-170)