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 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 pendulum's 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:

• 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 its 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:

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

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

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

then substitute into the second equation, and re-arrange. 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:

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

num = [m*l/q  0];
den = [1  b*(i+m*l^2)/q  -(M+m)*m*g*l/q  -b*m*g*l/q];
pend=tf(num,den)
```
```
Transfer function:
4.545 s
----------------------------------
s^3 + 0.1818 s^2 - 31.18 s - 4.455
```
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(pend,t)
axis([0 1 0 60])
```
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.

Although the output amplitude increases past 60 radians (10 revolutions), the model is only valid for small . In actuality, the pendulum will stop rotating when it hits the cart (=90 degree).

### 2. 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 matrices
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]
pend=ss(A,B,C,D);
T=0:0.05:10;
U=0.2*ones(size(T));
[Y,T,X]=lsim(pend,U,T);
plot(T,Y)
axis([0 2 0 100])
```
You should see the following output after running the m-file:

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. Several example controllers are included with these tutorials; select from below the one you would like to use.

Note: The solutions shown in the PID, root locus and frequency response 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 in these examples, 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.

Modeling Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball & Beam

Inverted Pendulum Examples
Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

Tutorials
MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples