Example: Modeling an Inverted Pendulum

## 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.2(kg)
b = friction of the cart = 0.1(N/m/sec)
I = inertia of the pendulum = 0.006(kg*m^2)
l = length to the pendulum's center of mass = 0.3(m)
F = impulse force applied to cart

For this problem, we are only interested in the control of the pendulum's position (all of these tutorials focus on the single-input, single-output case for control systems). Therefore, none of the design criteria deal with the cart's position. The system starts at equilibrium, and experiences an impulse force of 1N. The pendulum should return to its equilibrium upright position within 5 seconds, and never move more than 0.05 radians away from the vertical.

The design requirements for this system are:

• Settling time of less than 5 seconds.
• Pendulum angle never more than 0.05 radians from the vertical.

## 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:

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:

Now, after a little algebra, the equations can be put into 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 this problem, only the pendulum's position will be controlled (multiple-output systems are beyond the scope of this tutorial). We will ignore the first row of the output for now; occasionally we will return to it and show what happens to the x position of the cart.

## Matlab representation and the open-loop response

Below we show how this problem would be set up using Matlab. 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, the transfer function, and a plot of the velocity response to an impulse force 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]

[num,den] = ss2tf(A,B,C,D)

impulse(A,B,C(2,:),D(2))
axis([0 20 0 2])
```
Notice that in the impulse command, only the second row of the C and D matrices are used. Again, this is because the pendulum's angle, and not the cart's position, is the output of interest. You should see the following text in the command window of Matlab:
```

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

D =
0
0

num =
0   -0.0000    1.8182    0.0000  -44.5455
0    0.0000    4.5455    0.0000         0

den =
1.0000    0.1818  -31.1818   -4.4545         0
```
You should also get the following velocity response plot:

As you can see, 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. It is obvious 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.

Note: The solutions shown in these examples may not yield a workable controller for the inverted pendulum problem. As stated previously, when we put this problem into the single-input, single-output framework, we ignored the x position of the cart. The pendulum can be stabilized in an inverted position if the x position is constant or if the cart moves at a constant velocity (no acceleration). Where possible, we will show what happens to the cart's position when our controller is implemented on the system. We emphasize that the purpose of these examples is to demonstrate design and analysis techniques using Matlab; not to actually control an inverted pendulum.

User Survey
Your anonymous answers to the following questions will help us to improve future versions of these tutorials.
 How useful was this example? Very useful Useful Somewhat useful Not very useful A waste of time How was the level of the example? Too simple About right Too difficult How much time did you spend on it? Less than 30 mins. 30 - 60 mins. More than 60 mins. How much of the Matlab code did you run? None Some Most

We would like to hear about suggestions you have for improvement, difficulties you had with the tutorials, errors that you found, or any other comments that you have. This feedback is anonymous.