Module2 - mtrl - Control
Tools
We will use Matlab and its Control System Toolbox so make sure you have it available (it is not available in the virtual machine). If you already have a good knowledge of control, you can skip through the background material quickly and go directly to the material on convex optimization below.
Background
If you have no or only little experience of control you might want to start with watching the introduction videos 1 and 2 (out of 46) in this series of control lectures. Links to an external site.
Make sure you understand the fundamental difference between open loop control and feedback control. The main need for the use of feedback control is the presence of uncertainty (disturbances, imperfect models etc...). Without uncertainty there would be little need for feedback. In the safe world of simulations, where you can assume perfect knowledge of system dynamics and where there are no disturbances you can easily miss the importance. You however quickly notice if you, for some strange reason, want to move from theory to practice.
If you want to start by trying out things yourself, then spend some time (remember your time budget so do not get stuck in all the fun in there) on some of these control challenges Links to an external site.. You could check out the "Cruise Control", "Inverted Pendulum" and "Rocket Landing" problems, we will return some of these later. (Don't get stuck here though, even if you think it is fun...)
Watching the videos above is enough to continue, but if you want even more background before going further, we recommend reading Chapter 1 in the book Feedback Systems, by Åström and Murray. Links to an external site. (Karl Johan Åström is professor emeritus in Lund, Richard Murray is professor at Caltech, and member of the advisory board of WASP)
A control design is typically a trade-off between competing objectives. To do a good design one needs to understand the existing trade-offs and then find a good compromise. The conclusion can be that the objectives are impossible to achieve because of fundamental limitations, and that the only solution is to redesign the system, for instance adding new sensors getting a stronger motor. A good design method should provide the user with appropriate "tuning knobs" for investigating and understanding the trade-offs.
We will start by doing a control design using the most popular method there is: the PID controller, which has three tuning knobs, that often are tuned by hand.
If you want more background on PID control before we start, you can watch these videos
PID intro
Links to an external site.PID examples
Links to an external site.
Design example 1 - Height control of a helicopter (with PID control)
Download the Matlab code height_PID.m
Download height_PID.m and step through it in matlab, while going through the following text.
We will start by going through a control design of a simple system. A dynamical model of the height control of e.g. the Crazyflie is given by
m¨h(t)=f(t)−mg
Here h(t) denotes the height, which we want to follow a certain goal
r(t) (also called reference), for instance
r(t)=1 means we want the height to be 1 meter. The signal
f(t) is the vertical force which we can control by adjusting the rotor speed. The parameter
g is the constant of gravity. To hover at a constant height we clearly need
f(t)=mg.
To analyze the system we will write it in so called state-space form. The state vector is chosen as x=[h˙h] and the resulting system is
˙x(t)=Ax(t)+Bu(t)=[0100]x(t)+[0k]u(t)y(t)=Cx(t)+Du(t)=[10]x(t)
where we have defined a new control signal u(t):=f(t)−mg and put
k:=1/m. The choice of output signal
y reflects that we are interested in the height. We assume here that the height can be measured somehow, for instance by a pressure sensor or by a positioning system.
Start matlab and define the state-space process (with unit mass k=1) by
k=1; A = [0 1 ; 0 0]; B=[0 ; k]; C=[1 0]; D=0;
P = ss(A,B,C,D);
The process P can also be represented as a so called transfer function. In matlab it is calculated by writing
>> tf(P)
ans =
1
---
s^2
The transfer function of the process is hence P(s)=1s2.
The operator s corresponds to derivation and its inverse
1sto integration. The process
P(s) above is therefore often called a double-integrator since its effect is to integrate the input signal
u(t) twice to produce the output
y(t). We will avoid using transfer functions in the presentation below and understanding of these will not be required.
We will here control the process with a PID controller of the form
u(t)=kpe(t)+ki∫t0e(τ)dτ+kdde(t)dt
Here e(t)=r(t)−y(t) is the control error and
r(t) is the wanted height, it is called a reference signal.
The PID controller consists of three parts:
- a proportional part corresponding to the present error,
- an integral part describing error history,
- a derivative part describing the current error trend.
Reasons for the popularity of PID control is that the three tuning knobs (kp,ki,kd) are easy to understand and can often be tuned manually even without deep process knowledge, and that the control structure is often sufficient for acceptable performance.
The transfer function of the PID controller is
C(s)=kp+kis+kds
In practice one often uses a filter on the derivative part, to reduce the impact of noise, therefore we will change the last term to kds1+sTf. Define the controller by
s = tf('s'); % defines the operator s
kp=1; ki=0; kd=0; Tf=0; % only P-part
C = kp + ki/s + kd*s/(1+s*Tf); % PID with filter
We will now use the controller U=C(s)(R−Y) on the process
Y=P(s)U, as illustrated in the figure
Let's simulate a step response, assuming the reference r(t) goes from 0 to 1 at time t=0 :
Gcl = feedback(P*C,1); % y = Gcl r
Tsim = 10; % simulation time
step(Gcl, Tsim); % step response
which gives the following disappointing result
The height fluctuates between 0 and 2 instead of approaching the reference height 1 :-(
Try other values of kp and see how the result is changed.
kp = 10; C=kp % try both increasing and decreasing kp
Gcl = feedback(P*C,1);
step(Gcl, Tsim);
(Remember your conclusions here, since it will be asked for in the quiz.)
We get better results by using the following PID controller parameters
kp=3; ki=1; kd=3; Tf=0.1; %
C = kp + ki/s + kd*s/(1+s*Tf); %
Gcl = feedback(P*C,1); % calculate y
Gu = feedback(C,P); % calculate u
subplot(211); step(Gcl, Tsim); % output y
subplot(212); step(Gu,Tsim) % input u
The height now converges in 3 seconds, with a maximum input corresponding to an acceleration of 3g. We also notice an overshoot of 25 percent.
Further experiments would show that there is a compromise between speed of the height controller and the control signal amplitude. (Decreasing settling time to half increases the control signal amplitude a factor 4.)
We should also remember that our simple model does not capture imperfections such as
- control signal saturation (rotor speed is limited and negative rotor speed might be impossible),
- measurement errors (the height measurement might be inaccurate),
- uncertainty in process parameters (we assumed we knew exactly the amount of thrust the motors generate, we also assumed the mass was known),
- unmodelled dynamics (we assumed the thrust could be changed immediately without any lag).
The simulation results are therefore optimistic. Experience (read "repeated failures") teaches you what effects you can safely neglect in controller design.
Optimization based controller design
Many design methods are formulated as optimization of certain goal criteria. It is however not possible to capture all requirements on the controlled system in a single criterion. The goal criterion should therefore not be viewed as something given beforehand. The parameters in the goal criterion give you design knobs you can change to investigate the design space and to understand the system and the design trade-offs.
The main design time is typically spent on iterating
- trying different parameters in the goal criterion
- calculating the optimal controller (done by a tool)
- evaluating the resulting performance in various aspects
We will not be interested here in the mathematical details on how 2. is performed.
Linear Quadratic Control
This design method has been popular since the 60s and is covered in many control courses. The basic variant is called LQR, and is based on a number of optimistic assumptions
- a linear system model
˙x(t)=Ax(t)+Bu(t) describes the system
- all elements in the state-space vector
x can be measured
- all these measurements are available without noise to the same computational unit without communication delay
- there are no limitations on the control signal
u
- ... some more
The goal criterion is in LQR given by
min
Here Q,R,N are matrices with design parameters giving different weights to combinations of elements in x and u. The criterion reflects a situation where
x=0 (and
u=0) is the ideal situation.
Often the cross-term is left out, i.e. N=0 is used. Also it is popular to use diagonal matrices for
Q and
R
If we have a good guess of typical acceptable sizes of the different components of x and
u, then a good starting point is to guess diagonal elements of Q and R as
Q_{ii} \sim \frac{1}{x_{i,typ}^2}, \quad \textrm{and } \quad R_{jj}\sim \frac{1}{u_{j,typ}^2}
Increasing Q_{ii}will result in a controller that will try to decrease the error in component
x_i. Increasing
R_{jj} will give a controller that doesn't use control signal
j so much.
The optimal controller has the form
u(t) = -Kx(t)
where the feedback matrix K is calculated in matlab by the command
K=lqr(sys,Q,R,N)
Design Example 1 continued - height control (with LQR)
Download the file height_lqr.m Download height_lqr.m that does LQR design for the same double integrator example as before.
Using a diagonal Q matrix we have two tuning knobs in our design,
Q_{11} and
Q_{22} (if you only have one control signal you can always assume
R=1.) If we try equal values
Q_{11}=Q_{22}=q and set
q=0.1, 10, 1000 we get the following results
To follow the change in reference value we have here added a term to the control signal u(t) = k_r r(t) - Kx(t) with the scalar k_r chosen to get the correct output level in stationarity.
The results for q=1000 are similar to what we had with the PID-controller earlier (without the overshoot).
Exercise: Try improving this design by changing Q using the file height_lqr.m
Download height_lqr.m. (Hint: Increasing q further does not give a significantly faster step response, it only increases the size of the control signal.) Try to find a tuning of
Q giving results such as in the following figure. This is asked for in the quiz.
Notice that we with the fastest of the three designs now have a settling time of about 1 second, no overshoot, but still the same size of control signal as before.
LQG design
It is only in small toy examples that the full state space vector x(t) is known. Instead only some partial measurements of
x(t), often corrupted by noise, are available. With linear measurements we have
\begin{equation*}
y(t)=Cx(t)+ v(t)
\end{equation*}, where
C is a known matrix and
v is measurement noise.
A common approach is to design a state estimator that takes information about historical known values of y(t) and
u(t) and constructs a state-estimate
\hat x(t) from a model of the system.
In linear quadratic Gaussian (LQG) control an optimal such state estimate is calculated under the assumption that all disturbance and error signals are Gaussian with known variance matrices. If our system is given by
\begin{align*}
\dot x(t) &= Ax(t) + Bu(t) + Gw(t) \\
y(t) &= Cx(t) + v(t)
\end{align*}
where noise covariance matrices are
E(ww^T) = W, \quad E(vv^T)=V
then the optimal filter to obtain \hat x(t) is given by the Kalman filter
\dot{ \hat x} = A\hat x + Bu + L(y-C\hat x)
where the Kalman gain matrix L can be calculated by the Matlab command
L = lqe(A,G,C,W,V)
In reality the W and V matrices are not known (and the noise might not be Gaussian either). The matrices W and V (and perhaps also G) are instead treated as tuning knobs used to investigate different properties of the resulting controller and the closed loop system.
The controller that minimizes the expected value of the LQR goal criterion is obtained by inserting the state estimator \hat x(t) in the previous formula for the LQR controller, giving
u(t) = -K\hat x(t)
Design Example - Double Inverted Pendulums (with LQG)
The file pend01.m Download pend01.m illustrates LQG-design on a slightly more advanced problem than before. Two pendulums are mounted on a common cart that can move horizontally as illustrated in the picture
Additional: If you are interested, a derivation of the dynamics is available in this note Download in this note (which you need some math to understand).
The task is to balance both pendulums, i.e. get \theta_1 = \theta_2=0, by moving the position
x(t) of the cart. It is assumed the position of the cart and the angles of the pendulums can be measured but not the cart velocity or the angular velocities. The input is the force
u(t) on the cart. The LQG controller is calculated by the following lines of code in the file
Q = C'*C;
R = 1e-4;
[K,S]=lqr(A,B,Q,R);
G = B;
H = 0*C*B;
QN = 1;
RN = diag([1e-3 1e-3 1e-3]);
syse = ss(A,[B G],C,[D H]);
[kest,L]=kalman(syse,QN,RN);
reg = -lqgreg(kest,K);
Remark: The file pend01.m Download pend01.m generates several additional plots that you shouldn't try to understand, unless you are a control student. These plots illustrate the behavior of the system at different frequencies and can be used to understand the design better, such as the robustness against disturbances and modeling errors.
The following movie (generated by the file plotit.m
Download plotit.m) illustrates how the resulting control system handles a situation where the initial condition is close to the goal, but with the two pendulums leaning slightly in different directions (\theta_1(0) = 1 \textrm{ degree}, \quad \theta_2(0) = -2 \textrm{ degrees}).
It is interesting to note that both pendulums can be balanced by the same cart. Using control theory it can be proved that this impossible if the pendulums have the same length.
Controller design by Convex Optimization
Download the CVX addon to Matlab available at this home page Links to an external site.(choose the "standard bundle" and follow the installation instructions.)
In the LQG method we formulated the control design problem as a minimization of an integral of a quadratic function of states and control. As long as the goal function is a convex function of the states and control one can find the control signal by convex optimization.
We will demonstrate the convex optimization method on the Rocket Landing Problem (that was part of the control challenges Links to an external site. which we linked at the top of this page).
If you are interested, a derivation of the used Rocket dynamics is given in this note Download this note.
The CVX software defines a language for "disciplined convex programming". Following the language syntax guarantees that the resulting optimization problem can be solved by convex optimization.
After installing CVX, download the code rocketcvx.m
Download rocketcvx.m that uses convex optimization to find a control signal that takes the rocket from a (known) initial condition to landing at the platform; at the start the rocket is 200 meter above the landing platform and has a horizontal position error of 20 meter. It finds a control signal trajectory u(t) for the last T=10 seconds of operation that lands the rocket on the platform, i.e. giving the correct final state
x(T). There are two control signal
u_1 and
u_2 describing sideways and upwards thrust respectively.
The optimization criterion is chosen, somewhat randomly, as minimization of \max_t \left(|u_1(t)|\right) + \left(\int_0^T |u_2(t)|^2 dt\right)^{1/2}and we also enforce hard limits on sideways thrust
u_{1,min}\leq u_1(t) \leq u_{1,max}
Furthermore we enforce u_1(t) = 0 during the last 3 seconds of operation, to encourage the rocket to do a final vertical approach.
cvx_begin
n = length(x1);
variables x(n,T) u(2,T-1);
minimize(max(abs(u(1,:))) + norm(u(2,:)));
subject to
x(:,1) == x1;
for t = 1 : T-1
x(:,t+1) == Phi*x(:,t) + Gamma*u(:,t);
u1min <= u(1,t) <= u1max
end
for t = T-30 : T-1 % forces the rocket do a purely vertical approach
u(1,t) == 0
end
x(:,T) == xfinal;
cvx_end
The result should look something like the following movie
Spend some time studying the code so you roughly understand how it works. Also experiment with changing some parameters, such as the initial position, the bounds on lateral thrust or the design of the final vertical approach.
There are many other uses of convex optimization and the CVX software you just installed, for instance in signal processing, statistics, machine learning, .... For further study see the CVX example page Links to an external site.and consult the CVX documentation. Links to an external site.
Additional: CVXGEN - generate efficient C-code for Quadratic Programming problems
We will demonstrate how to generate fast C-code that solves convex QP-problems with some hundred or so optimization variables within a millisecond. We will use a web-service that transforms a CVX-like description of your optimization problem and automatically generates C-code that can be used from within matlab, or can be embedded into your realtime production code.
If you want to generate code yourself, you will need to follow the instructions at cvxgen.com Links to an external site.. Note that CVXGEN is only available for free to academic, non-commercial users with an academic email address, and you will need that to request for an academic license.
The work flow is shown in the series of cvxgen screenshots at this page. Links to an external site.
The tool can be used to generate support vector machines, solve so called Lasso problems used in statistical modeling, etc. Here we will demonstrate it by implementing a so called Model Predictive Controller (MPC).
Model predictive control is based on solving repeated optimization problems using so called receding horizon control. The MPC method is explained in this video
Links to an external site.
To generate code for an MPC controller for the double integrator we took the lines of code in the file inputtocvxgen.m Download inputtocvxgen.m and pasted it into the cvxgen webservice (after requesting an academic license). Following the workflow on the cvxgen home page we ended up with a number of files, including the solver codes cvxsolve (matlab-code), csolve.c (C-code) and csolve.mexmaci64 (mex-code to use the C code within matlab on a Mac computer).
The matlab solver was then bench-marked against the generated C-solver in the file test_cvxgen.m Download test_cvxgen.m which implements a simple MPC controller for the double integrator. The running time for solving the optimization problem in matlab with cvxsolve.m was about 3 seconds per time step and using the csolve.mexmaci64 it was 50 microseconds (a speedup of about 60000 !).
If you think it might be useful to be able to generate fast embedded C-code for quadratic optimization problems you might want to redo the example presented above. (You will not have to use this method in the control of the Crazyflie.)
Additional: Evaluation of control design (Gang of Four)
After designing the control system (or better, while doing it) we should try to understand its performance. A good complement to time-domain simulation is to study the systems also in the frequency domain. We will describe the so called Gang of Four figure.
The GoF contain four different amplitude curves as a function of frequency. For a process P(s) and a controller C(s) one puts s=i\omega and plots the amplitude of the following transfer functions as a function of frequency
\omega
\begin{align*}
&\textrm{Sensitivity function } S = \frac{1}{1+PC} \\
&\textrm{Load Sensitivity function } PS = \frac{1}{1+PC} \\
&\textrm{Noise Sensitivity function } CS = \frac{C}{1+PC} \\
&\textrm{Complementary Sensitivity function } T= \frac{PC}{1+PC}
\end{align*}
Read the first 3 pages of Chapter 12 in Åström Murray
Links to an external site. to get an explanation of what these functions illustrate. For instance a low value of the sensitivity function S means that the closed loop system attenuates disturbances well, and a high amplitude on
CS(i\omega) at a certain frequency
\omega means that measurement noise at this frequency will have a large effect on the control signal (which is bad).
Transformation to discrete time
Transformation of a continuous time system sysc = ss(A,B,C,D)
\begin{align*}
\dot x(t) &= Ax(t) + Bu(t) \\
y(t)&= Cx(t) + Du(t)
\end{align*}
to discrete time with sample time h
\begin{align*}
x(kh+h) &= \Phi x(kh) + \Gamma u(kh) \\
y(kh) &= Cx(kh) +Du(kh)
\end{align*}
can be done by the commands
sysd = c2d(sys,h)
[Phi, Gamma,C,D] = ssdata(sysd)
Words of wisdom from the master