# Lead-lag position control of a hydraulic actuator

This example ilustrates the design of a lead-lag compensator for the position control of a hydraulic actuator. For the theory, see lecture 9 of the TUD course Control Engineering (WB2207).

## Overview of the design procedure

` To design the controller, follow the steps outlined in Lecture 9:`
``` Lead compensator
1) Determine the desired crossover frequency wc.
2) Find out how much extra phase the lead compensator must add at wc and calculate alpha and Tlead.
3) Compute the overall controller gain K such that wc becomes the crossover frequency.
4) Check whether the bandwidth specs are met, if not, revise the choice of wc.```
``` Lag compensator
1) Determine the DC gain beta of the lag compensator to meet the steady-state requirements.
2) Choose Tlag between 2/wc and 10/wc
3) Check whether all the control specs are met, if not, revise.```

## Design specifications

```wb = 40;                                            % desired closed-loop bandwidth of at least 40 rad/s
PMd = 60;                                           % desired phase margin of at least 60 degrees
errd = 0.01;                                        % steady-state error while tracking a ramp reference of at most 0.01 m
```

## Enter open-loop transfer function in Matlab

```s = tf('s');                                        % define the 's' operator as a transfer function
G = 5574416/(s*(s + 25)*(s^2  + 91.53*s + 8068));   % define the open-loop transfer function
zpk(G)                                              % check whether it is what we intended using the zero/pole/gain form
```
```
Zero/pole/gain:
5574416
-------------------------------
s (s+25) (s^2  + 91.53s + 8068)

```

## Determine the desired crossover frequency

We start the design by defining the desired crossover frequency as half of the bandwith (in the end we will check the actual closed-loop bandwidth and we may need to increase wc if the bandwidth turns out to be too low):

```wc = wb/2;
```

## Find out how much extra phase the lead compensator must add at wc and calculate alpha and Tlead

```% Calculate the phase of G at the desired crossover frequency wc
[M,P] = bode(G,wc);
% The phase P at wc and the phase margin are
P
PM = P + 180
```
```P =

-142.0868

PM =

37.9132

```
```% As the desired phase margin is 60 degrees, the lead compensator must add
% about 22 degrees. We will add 5 degrees extra to account for the phase drop by the lag
% compensator that we will include later on:
Phimax = PMd - PM + 5
```
```Phimax =

27.0868

```
```% Given Phimax, calculate first alpha and then Tlead
alpha = (1 - sin(Phimax*pi/180)) / (1 + sin(Phimax*pi/180));
```
```alpha =

0.3742

0.0817

```
```% Construct the lead compensator transfer function
```
```
Transfer function:
0.08173 s + 1
-------------
0.03059 s + 1

```
```% Compare the Bode plot of G (blue) and of the loop-transfer G*Clead (green)
figure(1); clf;
```
```% Calculate the magnitude and phase at the desired crossover frequency wc
```
```Mlead =

1.8051

-115.0000

```

## Compute the overall controller gain K such that wc becomes the crossover frequency

As seen above, the phase Plead of the loop transfer G*Clead is the intended 115 degrees (yielding the phase margin of 65 degrees). The magnitude, however, is not equal to 1 (0 dB), meaning that wc is not the crossover frequency. We will now calculate the overall gain K, so that the magnitude of the loop transfer function G*Clead*K equals 1 and therefore wc will be the crossover frequency (K will, of course, not modify the phase):

```K = 1/Mlead
```
```K =

0.5540

```
```% Just a brief check, to make sure that the phase margin is as desired
% and that wc is indeed the crossover frequency:
```
```Mlead =

1

-115.0000

```

## Check whether the bandwidth specs are met

```% Given the lead compensator, calculate the closed-loop transfer function and its bandwidth:
bandwidth(Gc)
```
```ans =

43.2393

```
```% This is slightly above the desired 40 rad/s, so we are happy with this
```

## Determine the DC gain beta of the lag compensator

The final step is to check whether we need a lag compensator to meet the maximum steady-state error specification. The loop-transfer function is a type-one system; therefore the steady-state error for a ramp input is 1/Kv, with Kv the "velocity" constant:

```err = 1/dcgain(s*G*Clead*K)
```
```err =

0.0653

```
```% We can see that the error is about 0.06 m, six times more than allowed.
% This result can be confirmed by the following simulation:
t = [0:0.01:1]';                                    % time vector
r = t;                                              % ramp reference
y = lsim(Gc,r,t);                                   % simulate the closed-loop system
figure(2); clf;                                     % open a new figure
subplot(211); plot(t,r,t,y); grid;                  % plot the reference and the output
subplot(212); plot(t,r-y); grid;                    % plot the error
```
```% To calculate the gain of the lag compensator, divide the current steady-state error by the desired one:
beta = err/errd
```
```beta =

6.5315

```

## Choose Tlag between 2/wc and 10/wc

To be on the safe side, let us choose a fairly large Tlag (a slow "integrator"):

```Tlag = 10/wc
```
```Tlag =

0.5000

```
```% and construct the lag compensator transfer function
Clag = beta*(Tlag*s + 1) / (beta*Tlag*s + 1)
```
```
Transfer function:
3.266 s + 6.532
---------------
3.266 s + 1

```

## Check whether all the control specs are met

```Gc = feedback(G*Clead*Clag*K,1);
wb = bandwidth(Gc)
PM = P + 180
```
```wb =

43.6314

PM =

60.1666

err =

0.0100

```

## Simulation: tracking a sinusoidal input

Finally, let us plot the closed-loop step response and let us check how the controller tracks a sinusoidal reference with a frequency well below the bandwidth:

```figure(3); clf;                                     % open a new figure
step(Gc);                                           % step response
t = [0:0.01:2]';                                    % time vector
r = 0.1*sin(4*t);                                   % sinusoidal reference with w = 4 rad/s
y = lsim(Gc,r,t);                                   % simulate the closed-loop system
figure(4); clf;                                     % open a new figure
subplot(211); plot(t,r,t,y); grid;                  % plot the reference and the output
subplot(212); plot(t,r-y); grid;                    % plot the error
```