Example: Modeling an Inverted Pendulum in Simulink

Problem setup and design requirements
Force analysis and system equation setup
Building the model
Open-loop response
Extracting a linearized model
Implementing PID control
Closed-loop response

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 = 0 (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 0). 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

In this example, we will implement a PID controller which can only be applied to a single-input-single-output (SISO) system,so we will be only interested in the control of the pendulums angle. Therefore, none of the design criteria deal with the cart's position. 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:

Force analysis and system equation setup

Below are the two Free Body Diagrams of the system.

This system is tricky to model in Simulink because of the physical constraint (the pin joint) between the cart and pendulum which reduces the degrees of freedom in the system. Both the cart and the pendulum have one degree of freedom (X and theta, respectively). We will then model Newton's equation for these two degrees of freedom.

It is necessary, however, to include the interaction forces N and P between the cart and the pendulum in order to model the dynamics. The inclusion of these forces requires modeling the x and y dynamics of the pendulum in addition to its theta dynamics. In the MATLAB tutorial pendulum modeling example the interaction forces were solved for algebraically. Generally, we would like to exploit the modeling power of Simulink and let the simulation take care of the algebra. Therefore, we will model the additional x and y equations for the pendulum.
However, xp and yp are exact functions of theta. Therefore, we can represent their derivatives in terms of the derivatives of theta.
These expressions can then be substituted into the expressions for N and P. Rather than continuing with algebra here, we will simply represent these equations in Simulink.

Simulink can work directly with nonlinear equations, so it is unnecessary to linearize these equations as it was in the MATLAB tutorials.

Building the Model in Simulink

First, we will model the states of the system in theta and x. We will represent Newton's equations for the pendulum rotational inertia and the cart mass.

Now, we will add in two of the forces acting on the cart.

Now, we will apply the forces N and P to both the cart and the pendulum. These forces contribute torques to the pendulum with components "N L cos(theta) and P L sin(theta)". Therefore, we need to construct these components.

Now that the pendulum components are available, we can apply the forces N and P. We will assume we can generate these forces, and just draw them coming from nowhere for now.

Next, we will represent the force N and P in terms of the pendulum's horizontal and vertical accelerations from Newton's laws.

Now, we will begin to produce the signals which contribute to d2/dt2(xp) and d2/dt2(yp).

Now, we will generate the terms d2/dt2(theta)*lsin(theta) and d2/dt2(theta)*lcos(theta).

Now, we will generate the terms (d/dt(theta))^2*lsin(theta) and (d/dt(theta))^2*lcos(theta).

Finally, we will connect these signals to produce the pendulum acceleration signals. In addition, we will create the system outputs x and theta.

Now, save this model as pend.mdl.

Open-loop response

To generate the open-loop response, it is necessary to contain this model in a subsystem block.

Now, we will apply a unit impulse force input, and view the pendulum angle and cart position. An impulse can not be exactly simulated, since it is an infinite signal for an infinitesimal time with time integral equal to 1. Instead, we will use a pulse generator to generate a large but finite pulse for a small but finite time. The magnitude of the pulse times the length of the pulse will equal 1.

We now need to set an appropriate simulation time to view the response.

You can download a version of the system here. Before running it, it is necessary to set the physical constants. Enter the following commands at the MATLAB prompt. Now, start the simulation (select Start from the Simulation menu or hit Ctrl-t). If you look at the MATLAB prompt, you will see some error messages concerning algebraic loops. Due to the algebraic constraint in this system, there are closed loops in the model with no dynamics which must be resolved completely at each time step before dynamics are considered. In general, this is not a problem, but often algebraic loops slow down the simulation, and can cause real problems if discontinuities exist within the loop (such as saturation, sign functions, etc.)

Open both Scopes and hit the autoscale buttons. You will see the following for theta (left) and x (right).

Notice that the pendulum swings all the way around due to the impact, and the cart travels along with a jerky motion due to the pendulum. These simulations differ greatly from the MATLAB open loop simulations because Simulink allows for fully nonlinear systems.

Extracting the linearized model into MATLAB

Since MATLAB can't deal with nonlinear systems directly, we cannot extract the exact model from Simulink into MATLAB. However, a linearized model can be extracted. This is done through the use of In and Out Connection blocks and the MATLAB function linmod. In the case of this example, will use the equivalent command linmod2, which can better handle the numerical difficulties of this problem.

To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as pend.mdl. In this model, one input, F (the force on the cart) and two outputs, theta (pendulum angle from vertical) and x (position of the cart), were defined. When linearizing a model, it is necessary to choose an operating point about which to linearize. By default, the linmod2 command linearizes about a state vector of zero and zero input. Since this is the point about which we would like to linearize, we do not need to specify any extra arguments in the command. Since the system has two outputs, we will generate two transfer functions.

At the MATLAB prompt, enter the following commands

You will see the following output (along with algebraic loop error messages) providing a state-space model, two transfer function numerators, and one transfer function denominator (both transfer functions share the same denominator). To verify the model, we will generate an open-loop response. At the MATLAB command line, enter the following commands. You should get the following response for the angle of the pendulum.

Note that this is identical to the impulse response obtained in the MATLAB tutorial pendulum modeling example. Since it is a linearized model, however, it is not the same as the fully-nonlinear impulse response obtained in Simulink.

Implementing PID control

In the pendulum PID control example, a PID controller was designed with proportional, integral, and derivative gains equal to 100, 1, and 20, respectively. To implement this, we will start with our open-loop model of the inverted pendulum. And add in both a control input and the disturbance impulse input to the plant.

Now, we will feed back the angle output.

Now, we will insert a PID controller.

You can download our version of the closed-loop system here

Closed-loop response

We can now simulate the closed-loop system. Be sure the physical parameters are set (if you just ran the open-loop response, they should still be set.) Start the simulation, double-click on the Theta scope and hit the autoscale button. You should see the following response:

This is identical to the closed-loop response obtained in the MATLAB tutorials. Note that the PID controller handles the nonlinear system very well because the angle is very small (.04 radians).


Simulink Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

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

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