You are on page 1of 45

Inverted Pendulum Model

Contents
Problem setup and design requirements....................................................................2

Force analysis and system equations......................................................................3

1. Transfer Function................................................................................................. 5

2. State-Space.......................................................................................................... 6

Matlab representation and the open-loop response...................................................6

1. Transfer Function................................................................................................. 6

1. State-Space.......................................................................................................... 8

Solution to the Inverted Pendulum Problem Using PID Control.................................10

Open-loop Representation..................................................................................... 10

Closed-loop transfer function................................................................................11

Adding the PID controller....................................................................................... 12

What happens to the cart's position?............................................................................. 14

Lead-lag controller................................................................................................ 17

What happens to the cart's position?............................................................................. 20

Transfer Function................................................................................................ 21

Solution to the Inverted Pendulum Problem Using Frequency Response Method.....24

Open-loop Representation..................................................................................... 25

Closed-loop response with no compensation........................................................25

Closed-loop response with compensation.............................................................28

What happens to the cart's position?............................................................................. 34

Transfer Function................................................................................................ 35

State-space design for the inverted pendulum.........................................................37

Open-loop poles................................................................................................. 38

LQR design......................................................................................................... 39

Adding the reference input................................................................................. 41


Inverted Pendulum Model
Observer design................................................................................................. 43

Modeling an Inverted Pendulum


Inverted Pendulum Model

Problem setup and design requirements

The cart with an inverted pendulum, shown below, is "bumped" with an impulse
force, F. Determine the dynamic equations of motion for the system, and linearize
about the pendulum's angle, theta = Pi (in other words, assume that pendulum does
not move more than a few degrees away from the vertical, chosen to be at an angle of
Pi). Find a controller to satisfy all of the design requirements given below.

For this example, let's assume that

M mass of the cart 0.5 kg


m mass of the pendulum 0.5 kg
b friction of the cart 0.1 N/m/sec
l length to pendulum center of mass 0.3 m
I inertia of the pendulum 0.006 kg*m^2
F force applied to the cart
x cart position coordinate
theta pendulum angle from vertical

For the PID, root locus, and frequency response sections of this problem we will be
only interested in the control of the pendulums position. This is because the
techniques used in these tutorials can only be applied for a single-input-single-output
(SISO) system. Therefore, none of the design criteria deal with the cart's position. For
these sections we will assume that the system starts at equilibrium, and experiences an
impulse force of 1N. The pendulum should return to its upright position within 5
seconds, and never move more than 0.05 radians away from the vertical.

The design requirements for this system are:


Inverted Pendulum Model

Settling time of less than 5 seconds.

Pendulum angle never more than 0.05 radians from the vertical.

However, with the state-space method we are more readily able to deal with a multi-
output system. Therefore, for this section of the Inverted Pendulum example we will
attempt to control both the pendulum's angle and the cart's position. To make the
design more challenging we will be applying a step input to the cart. The cart should
achieve it's desired position within 5 seconds and have a rise time under 0.5 seconds.
We will also limit the pendulum's overshoot to 20 degrees (0.35 radians), and it should
also settle in under 5 seconds.

The design requirements for the Inverted Pendulum state-space example are:

Settling time for x and theta of less than 5 seconds.

Rise time for x of less than 0.5 seconds.

Overshoot of theta less than 20 degrees (0.35 radians).

Force analysis and system equations

Below are the two Free Body Diagrams of the system.

Summing the forces in the Free Body Diagram of the cart in the horizontal direction,
you get the following equation of motion:
Inverted Pendulum Model

Note that you could also sum the forces in the vertical direction, but no useful
information would be gained.

Summing the forces in the Free Body Diagram of the pendulum in the horizontal
direction, you can get an equation for N:

If you substitute this equation into the first equation, you get the first equation of
motion for this system:

(1)

To get the second equation of motion, sum the forces perpendicular to the pendulum.
Solving the system along this axis ends up saving you a lot of algebra. You should get
the following equation:

To get rid of the P and N terms in the equation above, sum the moments around the
centroid of the pendulum to get the following equation:

Combining these last two equations, you get the second dynamic equation:

(2)

Since Matlab can only work with linear functions, this set of equations should be
linearized about theta = Pi. Assume that theta = Pi + ( represents a small angle
from the vertical upward direction). Therefore, cos(theta) = -1, sin(theta) = -, and
(d(theta)/dt)^2 = 0. After linearization the two equations of motion become (where u
represents the input):
Inverted Pendulum Model

1. Transfer Function

To obtain the transfer function of the linearized system equations analytically, we


must first take the Laplace transform of the system equations. The Laplace transforms
are:

NOTE: When finding the transfer function initial conditions are assumed to be zero.

Since we will be looking at the angle Phi as the output of interest, solve the first
equation for X(s),

then substituting into the second equation:

Re-arranging, the transfer function is:

where,

From the transfer function above it can be seen that there is both a pole and a zero at
the origin. These can be canceled and the transfer function becomes:
Inverted Pendulum Model

2. State-Space

After a little algebra, the linearized system equations equations can also be
represented in state-space form:

The C matrix is 2 by 4, because both the cart's position and the pendulum's position
are part of the output. For the state-space design problem we will be controlling a
multi-output system so we will be observing the cart's position from the first row of
output and the pendulum's with the second row.

Matlab representation and the open-loop response

1. Transfer Function
The transfer function found from the Laplace transforms can be set up using Matlab
by inputting the numerator and denominator as vectors. Create an m-file and copy the
following text to model the transfer function:
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input


Inverted Pendulum Model

num = [m*l/q 0]
den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]
Your output should be:

num =
4.5455 0

den =
1.0000 0.1818 -31.1818 -4.4545
To observe the system's velocity response to an impulse force applied to the cart add
the following lines at the end of your m-file:
t=0:0.01:5;
impulse(num,den,t)
axis([0 1 0 60])

Note: Matlab commands from the control system toolbox are highlighted in red.

You should get the following velocity response plot:

As you can see from the plot, the response is entirely unsatisfactory. It is not stable in
open loop. You can change the axis to see more of the response if you need to
convince yourself that the system is unstable.
Inverted Pendulum Model

1. State-Space

Below, we show how the problem would be set up using Matlab for the state-space
model. If you copy the following text into a m-file (or into a '.m' file located in the
same directory as Matlab) and run it, Matlab will give you the A, B, C, and D matrices
for the state-space model and a plot of the response of the cart's position and
pendulum angle to a step input of 0.2 m applied to the cart.
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

p = i*(M+m)+M*m*l^2; %denominator for the A and B matricies


A = [0 1 0 0;
0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;
0 0 0 1;
0 -(m*l*b)/p m*g*l*(M+m)/p 0]
B = [ 0;
(i+m*l^2)/p;
0;
m*l/p]
C = [1 0 0 0;
0 0 1 0]
D = [0;
0]

T=0:0.05:10;
U=0.2*ones(size(T));
[Y,X]=lsim(A,B,C,D,U,T);
plot(T,Y)
axis([0 2 0 100])
You should see the following output after running the m-file:

A =
0 1.0000 0 0
0 -0.1818 2.6727 0
0 0 0 1.0000
0 -0.4545 31.1818 0

B =
0
1.8182
0
4.5455

C =
1 0 0 0
0 0 1 0
Inverted Pendulum Model
D =
0
0

The blue line represents the cart's position and the green line represents the
pendulum's angle. It is obvious from this plot and the one above that some sort of
control will have to be designed to improve the dynamics of the system. Four example
controllers are included with these tutorials: PID, root locus, frequency response, and
state space. Select from below the one you would like to use.

Solution to the Inverted Pendulum Problem Using PID


Control

The transfer function of the plant for this problem is given below:

where,
Inverted Pendulum Model

The design criteria (with the pendulum receiving a 1N impulse force from the cart)
are:

Settling time of less than 5 seconds.

Pendulum should not move more than 0.05 radians away from the vertical.

Open-loop Representation

The first thing to do when using PID control in Matlab is to find the transfer function
of the system and to check to see if it makes sense. The transfer function found from
the Laplace transforms for the output Phi (the pendulum's angle) can be set up using
Matlab by inputting the numerator and denominator as vectors. Create an m-file (or a
'.m' file located in the same directory as Matlab) and copy the following text to model
the transfer function:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0]
den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]
Your output should be:

num =
4.5455 0

den =
1.0000 0.1818 -31.1818 -4.4545
Closed-loop transfer function

The control of this problem is a little different than the standard control problems you
may be used to. Since we are trying to control the pendulum's position, which should
return to the vertical after the initial disturbance, the reference signal we are tracking
Inverted Pendulum Model

should be zero. The force applied to the cart can be added as an impulse disturbance.
The schematic for this problem should look like the following.

It will be easier to determine the appropriate transfer function to enter into Matlab if
we first rearrange the schematic as follows:

Now, we can find the closed-loop transfer function.

Adding the PID controller

This closed-loop transfer function can be modeled in Matlab by copying the following
code to the end of your m-file (whether your using the transfer function from the
Laplace transforms or from the state-space representation):

kd = 1;
k = 1;
ki = 1;
numPID = [kd k ki];
denPID = [1 0];
numc = conv(num,denPID)
denc = polyadd(conv(denPID,den),conv(numPID,num))
Inverted Pendulum Model

Note: Non-standard Matlab commands used in this example are highlighted in green.

The function polyadd is not in the Matlab toolbox. You will have to copy it to a new
m-file to use it. This transfer function assumes that both derivative and integral
control will be needed along with proportional control. This does not have to be the
case. If you wanted to start with PI control, just remove the kd term from numPID. If
you wanted to start with PD control, just remove the ki term from numPID and
change denPID to equal[1]. Assuming you do not change the PID control, you should
get the following closed-loop numerator and denominator in the Matlab command
window:
numc =
4.5455 0 0

denc =
1.0000 4.7273 -26.6363 0.0910 0
Now we can begin the actual control of this system. First let's see what the impulse
response looks like with the numbers we already have. Enter the following code to the
end of your m-file:
t=0:0.01:5;
impulse(numc,denc,t)
axis([0 1.5 0 40])
You should get the following velocity response plot from the impulse disturbance:

This response is still not stable. Let's start by increasing the proportional control to the
system. Increase the k variable to see what effect it has on the response. If you
Inverted Pendulum Model

set k=100, and set the axis to axis([0, 2.5, -0.2, 0.2]), you should get the following
velocity response plot:

The settling time is acceptable at about 2 seconds. Since the steady-state error has
already been reduced to zero, no more integral control is needed. You can remove the
integral gain constant to see for yourself that the small integral control is needed. The
overshoot is too high, so that must be fixed. To alleviate this problem, increase
the kd variable. With kd=20, you should get a satisfactory result. You should now see
the following velocity response plot:
Inverted Pendulum Model

As you can see, the overshoot has been reduced so that the pendulum does not move
more than 0.05 radians away from the vertical. All of the design criteria have been
met, so no further iteration is needed.

What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given.
The diagram was not entirely complete. The block representing the the position was
left out because that variable was not being controlled. It is interesting though, to see
what is happening to the cart's position when the controller for the pendulum's angle is
in place. To see this we need to consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:


Inverted Pendulum Model

The feedback loop represents the controller we have designed for the pendulum's. The
transfer function from the cart's position to the impulse force, with the PID feedback
controller which we designed, is given as follows:

Recall that den1=den2 if the pole/zero at the origin that was cancelled is added back
in. So the transfer function from X to F can be simplified to:

Now that we have the transfer function for the entire system, let's take a look at the
response. First we need the transfer function for the cart's position. To get this we need
to go back to the laplace transforms of the system equations and find the transfer
function from X(s) to U(s). Below is this transfer function:

where,
Inverted Pendulum Model

The pole/zero at the origin cancelled out of the transfer function for Phi, has been put
back in. So that now den1 = den2, making calculations easier. Now, create a new m-
file and run it in the command window:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];


den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];


den2 = den1

kd = 20;
k = 100;
ki = 1;
numPID = [kd k ki];
denPID = [1 0];

numc = conv(num2,denPID);
denc = polyadd(conv(denPID,den2),conv(numPID,num1));
t=0:0.01:5;
impulse(numc,denc,t)
Inverted Pendulum Model

As you can see, the cart moves in the negative direction with a constant velocity. So
although the PID controller stabilizes the angle of the pendulum, this design would
not be feasible to implement on an actual physical system.

Lead-lag controller

The solution to this problem is to add another pole far to the left of the other poles and
zeros. To keep the right number of asymptotes, another zero should be added as well.
The placement of the added pole and zeros is not important except that the pole
should be relatively large and the zeros should be relatively small.

Try the m-file below to see what effect the poles and zeros have on the root locus.
The polyadd function needs to be copied to the directory you are running Matlab in.
M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0];


den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q];

z1 = 3;
p1 = 0;
z2 = 4;
p2 = 50;

numlag = [1 z1];
denlag = [1 p1];
numlead = [1 z2];
denlead = [1 p2];

num3 = conv(conv(num, numlead), numlag);


den3 = conv(conv(den, denlead), denlag);

rlocus(num3,den3)
sigrid(0.92)
axis([-50 50 -50 50])
figure
rlocus(num3,den3)
sigrid(0.92)
axis([-10 10 -10 10])
[k,poles]=rlocfind(num3,den3)
figure

numc = conv(conv(num,denlead),denlag);
Inverted Pendulum Model
denc =polyadd(k*num3,den3);
impulse(numc,denc)
axis([0 2 -0.05 0.05])
The poles and zeros were found by trial and error. The only things to keep in mind
was that one pole had to be at the origin, the other had to be far to the left, and the two
zeros had to be small. Furthermore, I found that if the two zeros were close together
and to the right of the farthest left plant pole, the response was better. You should see
first the following root locus plot with the zeros and poles listed above:

The second plot should be of the same root locus magnified a little so that the root
locus around the origin can be seen.
Inverted Pendulum Model

When prompted to pick a location on the root locus, chose a spot on the multiple roots
just before they return to the real axis. Your velocity response to the impulse
disturbance should look similar to the following:
Inverted Pendulum Model

The response now meets all of the requirements, so no further iteration is needed.

What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given.
The diagram was not entirely complete. The block representing the the position was
left out because that part was not being controlled. It would be interesting though, to
see what is happening to the cart's position when the controller for the pendulum's
angle is in place. To see this, we need to consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

The feedback loop represents the controller we have designed for the pendulum. The
transfer function from the cart's position to the impulse force, with the feedback
controller which we designed, is given as follows:

Recall that den1=den2 if the pole/zero at the origin that was canceled is added back in.
So the transfer function from X to F can be simplified to:
Inverted Pendulum Model
Transfer Function
Now that we have the transfer function for the entire system, let's take a look at the
response. First we need the transfer function for the cart's position. To get this we need
to go back to the laplace transforms of the system equations and find the transfer
function from X(s) to U(s). Below is this transfer function:

where,

The pole/zero at the origin canceled out of the transfer function for Phi, has been put
back in. So that now den1 = den2, making calculations easier. Now, create a new m-
file and run it in the command window:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];


den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];


den2 = den1

z1 = 3;
p1 = 0;
z2 = 4;
p2 = 50;

numlag = [1 z1];
denlag = [1 p1];
numlead = [1 z2];
denlead = [1 p2];
Inverted Pendulum Model
num3 = conv(conv(num1, numlead), numlag);
den3 = conv(conv(den1, denlead), denlag);

subplot(1,1,1);rlocus(num3,den3)
axis([-10 10 -10 10])
[k,poles]=rlocfind(num3,den3)

numc = conv(conv(num1,denlead),denlag);
denc = polyadd(k*num3,den3);
t=0:0.01:6;
subplot(2,1,1);
impulse(numc,denc,t)
axis([0 6 -0.05 0.05])
num4 = conv(num2,den3);
den4 = polyadd(conv(den1,den3),k*conv(den1,num3));
subplot(2,1,2);
impulse(num4,den4,t)
axis([0 6 -0.1 0.1])

If you select the point on the root locus you selected before (near the real axis), you
should see the following plot:
Inverted Pendulum Model

The top curve represents the pendulum's angle, and the bottom curve represents the
cart's position. As you can see, the cart moves, is stabilized at near zero for almost five
seconds, and then goes unstable. It is possible that friction (which was neglected in the
modeling of this problem) will actually cause the cart's position to be stabilized. Keep
in mind that if this is in fact true, it is due more to luck than to anything else, since the
cart's position was not included in the control design.

Solution to the Inverted Pendulum Problem Using


Frequency Response Method

The transfer function of the plant for this problem is given below:

where,

Note: There is a pole/zero cancellation in this transfer function. In previous examples


these were removed from the transfer function. However, in this example they will be
left in for reasons that will become clear later.

The design criteria (with the pendulum receiving a 1N impulse force from the cart)
are:
Inverted Pendulum Model

Settling time of less than 5 seconds.

Pendulum should not move more than 0.05 radians away from the vertical.

Open-loop Representation

The frequency response method uses the bode command in Matlab to find the
frequency response for a system described by a transfer function in bode form.

The transfer function found from the Laplace transforms for the output Phi (the
pendulum's angle) can be set up using Matlab by inputting the numerator and
denominator as vectors. Create an m-file (or a '.m' file located in the same directory as
Matlab) and copy the following text to model the transfer function:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0 0]
den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0]
Your output should be:

num =
4.5455 0 0

den =
1.0000 0.1818 -31.1818 -4.4545 0
Closed-loop response with no compensation

We will now design an inverted pendulum controller for an impulse force using
Nyquist diagrams (we cannot use Bode plots because the system is unstable in open
loop). Let's begin by looking at the block diagram for this system:
Inverted Pendulum Model

If you try to model this system in Matlab, you will have all sorts of problems. The best
way to use Matlab to solve this problem is to first change the schematic to something
that can be modeled much more easily. Rearranging the picture, you can get the
following new schematic:

Now we can begin our design. First, we will look at the poles and zeros of this
function:

x = roots(num)
y = roots(den)

x =
0
0

y =
0
-5.6041
5.5651
-0.1428
As you already know, we have a pole-zero cancellation at the origin, as well as one
positive, real pole in the right-half plane. This means that we will need one anti-
clockwise encirclement of -1 in order to have a stable closed-loop system (Z = P + N;
P = 1, N = -1). The following m-file will be very useful for designing our controller.
Please note that you will need the function polyadd.m to run this m-file. Copy and
paste the function from your browser to a m-file in your directory (make sure the
function command starts in the first column of the m-file).

Note: Non-standard Matlab commands used in this example are highlighted in green.

function[ ] = pend()
Inverted Pendulum Model

clf
figure(1)
clf

%define TF
num = [4.5455 0 0];
den =[1.0000 0.1818 -31.1818 -4.4545 0];
figure(1)

%ask user for controller


numc = input('numc?.........');
denc = input('denc?.........');
k = input('K?............');

%view compensated system bode


bode(k*conv(numc,num), conv(denc,den))
%view compensated system nyquist
figure(2)
subplot (2,1,1)
nyquist(k*conv(numc,num), conv(denc,den))
%view compensated CL system impulse response
subplot(2,1,2)
clnum = conv(num,denc);
temp1 = k*conv(numc,num);
temp2 = conv(denc, den);
clden = polyadd(temp1,temp2);
impulse (clnum,clden)

Note: Matlab commands from the control system toolbox are highlighted in red.

With this m-file we will now view the uncompensated system's Nyquist diagram by
setting the controller numerator, denominator and gain equal to one. Enter pend at the
command prompt, and enter 1 for numc, denc, and K. You should see the following
plots in your screen:
numc?.........1
denc?.........1
K?............1
Inverted Pendulum Model

Closed-loop response with compensation

The system is unstable in closed loop (no encirclements of -1). Our first step will be to
add an integrator to cancel the extra zero at the origin (we will then have two poles
and two zeros at the origin). Use the pend command again.
Inverted Pendulum Model
numc?.........1
denc?.........[1 0]
K?............1

Notice that the nyquist diagram encircles the -1 point in a clockwise fashion. Now we
have two poles in the right-half plane (Z= P + N = 1 + 1). We need to add phase in
order to get an anti-clockwise encirclement. We will do this by adding a zero to our
controller. For starters, we will place this zero at -1.
Inverted Pendulum Model

numc?.........[1 1]
denc?.........[1 0]
K?............1

as you can see, this wasn't enough phase. The encirclement around -1 is still
clockwise. We are going to need to add a second zero.
numc?.........conv([1 1],[1 1])
Inverted Pendulum Model
denc?.........[1 0]
K?............1

We still have one clockwise encirclement of the -1 point. However, if we add some
gain, we can make the system stable by shifting the nyquist plot to the left, moving the
anti-clockwise circle around -1, so that N = -1. help impulse
numc?.........conv([1 1],[1 1])
Inverted Pendulum Model
denc?.........[1 0]
K?............10

As you can see, the system is now stable. We can now concentrate on improving the
response. We can modify the poles of the controller in order to do this. We have to
keep in mind that small poles (close to the origin) will affect the response at small
frequencies, while larger poles (farther from the origin) will affect the response at
high frequencies. When designing via frequency response, we are interested in
Inverted Pendulum Model

obtaining simple plots, since they will be easier to manipulate in order to achieve our
design goal. Therefore, we will use these concepts to 'flatten' the frequency response
(bode plot). At the same time, you will notice that the nyquist diagram will take an
oval shape.

If we try different combinations of poles and gains, we can get a very reasonable
response. (Enter the command axis([0 5 -0.05 0.1]) after the pend command.)

numc?.........conv([1 1.1],[1 5])


denc?.........[1 0]
K?............10
Inverted Pendulum Model

Our response has met our design goals. Feel free to vary the parameters and observe
what happens.
What happens to the cart's position?

At the beginning on this solution page, the block diagram for this problem was given.
The diagram was not entirely complete. The block representing the the position was
left out because that variable was not being controlled. It is interesting though, to see
what is happening to the cart's position when the controller for the pendulum's angle is
in place. To see this we need to consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:


Inverted Pendulum Model

The feedback loop represents the controller we have designed for the pendulum. The
transfer function from the cart's position to the impulse force, with the frequency
response feedback controller which we designed, is given as follows:

Recall that den1=den2 (the two transfer functions G1 and G2 differ in numerator
alone), so the transfer function from X to F can be simplified to:

Transfer Function
Now that we have the transfer function for the entire system, let's take a look at the
response. First we need the transfer function for the cart's position. To get this we need
to go back to the laplace transforms of the system equations and find the transfer
function from X(s) to U(s). Below is this transfer function:

where,
Inverted Pendulum Model

The pole/zero at the origin canceled out of the transfer function for Phi, has been put
back in. So that now den1 = den2, making calculations easier. Now, create a new m-
file and run it in the command window:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];


den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];


den2 = den1;

k = 10;
numcontroller = conv([1 1.1],[1 5]);
dencontroller = [1 0];
numc = conv(numx,dencontroller);
denc = polyadd(conv(dencontroller,den),k*conv(numcontroller,num));
t=0:0.01:100;
impulse(numc,denc,t)

As you can see, the cart moves in the negative direction and stabilizes at about -0.18
meters. This design might work pretty well for the actual controller, assuming that the
Inverted Pendulum Model

cart had that much room to move in. Keep in mind, that this was pure luck. We were
not trying to design to stabilize the cart's position, and the fact that we have is a
fortunate side effect.

State-space design for the inverted pendulum

The state equations for this problem are:

The design criteria for this system with the cart receiving a 0.2 m step input are as
follows:

Settling time for x and theta of less than 5 seconds.

Rise time for x of less than 1 second.

Overshoot of theta less than 20 degrees (0.35 radians).

Steady-state error within 2%.

As you may have noticed if you went through some of the other inverted pendulum
examples the design criteria for this example are different. In the other other examples
we were dealing with an impulse and not a step input. Also, we were only concerned
with the pendulums angle and disregarded the cart's position in the design of the
controller. However, for an inverted pendulum it is unrealistic to consider just the
single output system. Using state-space methods it is relatively simple to work with a
multi-output system, so in this example we will design a controller with both the
pendulum angle and the cart position in mind.
Inverted Pendulum Model

This problem can be solved using full state feedback. The schematic of this type of
control system is shown below:

Open-loop poles

In this problem R represents the commanded step input to the cart. The 4 states
represent the position and velocity of the cart and the angle and angular velocity of the
pendulum. The output y contains both the position of the cart and the angle of the
pendulum. We want to design a controller so that when an step input is given to the
system, the pendulum should be displaced, but eventually return to zero (i.e. the
vertical) and the cart should move to it's new commanded position. To view the
system's open-loop response please refer to the inverted pendulum modeling Page

The first step in designing this type of controller is to determine the open-loop poles
of the system. Enter the following lines of code into a m-file (or a '.m' file located in
the same directory as Matlab):
M = 0.5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

p = i*(M+m)+M*m*l^2; %denominator
A = [0 1 0 0;
0 -(i+m*l^2)*b/p (m^2*g*l^2)/p 0;
0 0 0 1;
0 -(m*l*b)/p m*g*l*(M+m)/p 0];
B = [0; (i+m*l^2)/p; 0; m*l/p];
C = [1 0 0 0;
0 0 1 0];
D = [0;0];

p = eig(A)
The Matlab command window should output the following text as a result:
Inverted Pendulum Model
p =
0
-0.1428
5.5651
-5.6041
As you can see, there is one right-half-plane pole at 5.5651. This should confirm your
intuition that the system is unstable in open loop.
LQR design

The next step in the design process is to assume that we have full-state feedback (i.e.
that we can measure all four states), and find the vector K which determines the
feedback control law. This can be done in a number of ways. If you know the desired
closed-loop poles, you can use the place or acker command. Another option is to use
the lqr function; this will give you the optimal controller (under certain assumptions;
consult your textbook for more details). The lqr function allows you to choose two
parameters, R and Q, which will balance the relative importance of the input and state
in the cost function that you are trying to optimize. The simplest case is to assume
R=1, and Q=C'*C. You may notice that we are using both outputs (the pendulum's
angle and the cart's position). Essentially, the lqr method allows for the control of both
outputs. In this case, it is pretty easy to do. The controller can be tuned by changing
the nonzero elements in the Q matrix to get a desirable response.

Note: Matlab commands from the control system toolbox are highlighted in red.

To find the structure of Q, enter the following into the Matlab command window:

C'*C
You should see the following in the command window:
ans =

1 0 0 0
0 0 0 0
0 0 1 0
0 0 0 0
The element in the 1,1 position will be used to weight the cart's position and the
element in the 3,3 position will be used to weight the pendulum's angle. The input
weighting R will remain at 1. Now that we know what the Q matrix should look like
we can experiment to find the K matrix that will give us a good controller. We will go
ahead and find the K matrix and plot the response all in one step so that changes can
be made in the control and be seen automatically in the response. Enter the following
text into your m-file:
Inverted Pendulum Model
x=1;
y=1;
Q=[x 0 0 0;
0 0 0 0;
0 0 y 0;
0 0 0 0];
R = 1;
K = lqr(A,B,Q,R)
Ac = [(A-B*K)];
Bc = [B];
Cc = [C];
Dc = [D];

T=0:0.01:5;
U=0.2*ones(size(T));
[Y,X]=lsim(Ac,Bc,Cc,Dc,U,T);
plot(T,Y)
legend('Cart','Pendulum')
You should get the following value for K and a response plot:
K =

-1.0000 -1.6567 18.6854 3.4594

The curve in green represents the pendulum's angle, in radians and the curve in blue
represents the cart's position in meters. As you can see, this plot is not satisfactory.
The pendulum and cart's overshoot appear fine, but their settling times need
improvement and the cart's rise time needs to go down. As I'm sure you have noticed
the cart is not near the desired location but has in fact moved in the other direction.
This error will be dealt with in the next section and right now we will focus on the
settling and rise times. Go back to your m-file and change the x and y variables to see
if you can get a better response. You will find that increasing x makes the settling and
Inverted Pendulum Model

rise times go down, and lowers the angle the pendulum moves. Using x=5000 and
y=100, the following value of K and step response were found:

K =

-70.7107 -37.8345 105.5298 20.9238

You may have noted that if you increased x and y even higher, you could improve the
response even more. The reason this plot was chosen was because it satisfied the
design requirements while keeping x and y as small as possible. In this problem, x and
y have been used to describe the relative weight of the tracking error in the cart's
position and pendulum's angle versus the control effort. The higher x and y are, the
more control effort is used, but the smaller the tracking error. The system response has
a settling time under 2 seconds.

Adding the reference input

Now we want to get rid of the steady-state error. In contrast to the other design
methods, where we feedback the output and compare it to the reference input to
compute an error, with a full-state feedback controller we are feeding back all the
states. We need to compute what the steady-state value of the states should be,
multiply that by the chosen gain K, and use a new value as our reference for
computing the input. This can be done by adding a constant gain Nbar after the
reference. The schematic below shows this relationship:
Inverted Pendulum Model

Nbar can be found using the user-defined function rscale (copy it to the directory that
your m-file is in). Delete the lsim line and copy the following to your m-file and run it
to view the step response with Nbar added.

Cn=[1 0 0 0];
Nbar=rscale(A,B,Cn,0,K)
Bcn=[Nbar*B];
[Y,X]=lsim(Ac,Bcn,Cc,Dc,U,T);
plot(T,Y)
legend('Cart','Pendulum')

Note: Non-standard matlab commands are highlighted in green.

A different C had to be used because the rscale function will not work for multiple
outputs. However, the Nbar found is correct, as you can see from the output below:

Nbar =

-70.7107
Inverted Pendulum Model

Now, the steady-state error is within our limits, the rise and settling times are met and
the pendulum's overshoot is within range of the design criteria.

Observer design

This response is good, but was found assuming full-state feedback, which most likely
will not be a valid assumption. To compensate for this, we will next design a full-
order estimator to estimate those states that are not measured. A schematic of this kind
of system is shown below, without Nbar:

To begin, we must first find the controller poles. To do this copy the following code to
the end of your m-file:

p = eig(Ac)
Inverted Pendulum Model

If you changed the weighting factors x and y above to x=5000 and y=100, you should
see the following poles in the Matlab command window:
p =

-8.4910 + 7.9283i
-8.4910 - 7.9283i
-4.7592 + 0.8309i
-4.7592 - 0.8309i
We want to design estimator poles that are about 4-10 times as fast as slowest pole,
say at -40. We will use the place command in Matlab to find the L vector (note
that acker would also work). Remember that theplace command cannot have all the
desired poles at the same location. Delete from the lsim command on and enter the
following text to the end of your m-file to find the L matrix:
P = [-40 -41 -42 -43];
L = place(A',C',P)'
We are using both outputs (the angle of the pendulum and the position of the cart) to
design the observer. The system is not observable using only the angle of the
pendulum as output; you can check this in Matlab by
computing rank(obsv(A,C(2,:))). This should make sense to you: if you can only
measure the angle of the pendulum, you cannot determine what the position of the cart
will be.

You should see the following in the Matlab window:

L =

1.0e+03 *

0.0826 -0.0010
1.6992 -0.0402
-0.0014 0.0832
-0.0762 1.7604

Now we will combine the control-law design with the estimator design to get the
compensator. The response should be similar to the one from the control-law design.
To set up the compensator copy the following code to the end of your m-file:
Ace = [A-B*K B*K;
zeros(size(A)) (A-L*C)];
Bce = [ B*Nbar;
zeros(size(B))];
Cce = [Cc zeros(size(Cc))];
Dce = [0;0];
T = 0:0.01:5;
U = 0.2*ones(size(T));
[Y,X] = lsim(Ace,Bce,Cce,Dce,U,T);
Inverted Pendulum Model
plot(T,Y)
legend('Cart','Pendulum')
After running this m-file, you should output the following step response simulation
plot:

This response is about the same as before. All of the design requirements have been
met with the minimum amount of control effort, so no more iteration is needed.

As you can see, it is much easier to control multi-input or multi-output systems with
the state space method than with any other of the methods.

You might also like