0% found this document useful (0 votes)
549 views

Inverted Pendulum With MATLAB

The document describes an inverted pendulum system mounted on a motorized cart. The objective is to balance the inverted pendulum by applying a force to the cart. There are two design requirements: 1) the pendulum returns to an upright position within 5 seconds after a disturbance, and 2) the pendulum angle never moves more than 0.05 radians from vertical. The document derives the nonlinear and linearized equations of motion for the system and represents the system using transfer functions and state-space models in MATLAB.

Uploaded by

Nofar Gofer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
549 views

Inverted Pendulum With MATLAB

The document describes an inverted pendulum system mounted on a motorized cart. The objective is to balance the inverted pendulum by applying a force to the cart. There are two design requirements: 1) the pendulum returns to an upright position within 5 seconds after a disturbance, and 2) the pendulum angle never moves more than 0.05 radians from vertical. The document derives the nonlinear and linearized equations of motion for the system and represents the system using transfer functions and state-space models in MATLAB.

Uploaded by

Nofar Gofer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Inverted Pendulum

Problem setup and design requirements

The system in this example consists of an inverted pendulum mounted to a motorized cart. The inverted

pendulum system is an example commonly found in control system textbooks and research literature. Its

popularity derives in part from the fact that it is unstable without control, that is, the pendulum will simply

fall over if the cart isn't moved to balance it. Additionally, the dynamics of the system are nonlinear. The

objective of the control system is to balance the inverted pendulum by applying a force to the cart that

the pendulum is attached to. A real-world example that relates directly to this inverted pendulum system

is the attitude control of a booster rocket at takeoff.

In this case we will consider a two-dimensional problem where the pendulum is contrained to move in

the vertical plane shown in the figure below. For this system, the control input is the force that moves

the cart horizontally and the outputs are the angular position of the pendulum and the horizontal

position of the cart .

For this example, let's assume the following quantities:

(M) mass of the cart 0.5 kg


(m) mass of the pendulum 0.2 kg

(b) coefficient of friction for cart 0.1 N/m/sec

(l) length to pendulum center of mass 0.3 m

(I) mass moment of inertia of the pendulum 0.006 kg.m^2

(F) force applied to the cart

(x) cart position coordinate

(theta) pendulum angle from vertical (down)

For the PID, root locus, and frequency response sections of this problem, we will be interested only in

the control of the pendulum's position. This is because the techniques used in these sections are best-

suited for single-input, single-output (SISO) systems. Therefore, none of the design criteria deal with the

cart's position. We will, however, investigate the controller's effect on the cart's position after the

controller has been designed. For these sections, we will design a controller to restore the pendulum to

a vertically upward position after it has experienced an impulsive "bump" to the cart. Specifically, the

design criteria are that the pendulum return to its upright position within 5 seconds and that the

pendulum never move more than 0.05 radians away from vertical after being disturbed by an impulse of

magnitude 1 Nsec. The pendulum will initially begin in the vertically upward equilibrium, = .

In summary, the design requirements for this system are:

 Settling time for of less than 5 seconds

 Pendulum angle never more than 0.05 radians from the vertical

Employing state-space design techniques, we are more readily able to address a multi-output system. In

our case, the inverted pendulum system is single-input, multi-output (SIMO). Therefore, for the state-

space 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 in this section, we will command a 0.2-
meter step in the cart's desired position. Under these conditions, it is desired that the cart achieve its

commanded position within 5 seconds and have a rise time under 0.5 seconds. It is also desired that the

pendulum settle to its vertical position in under 5 seconds, and further, that the pendulum angle not

travel more than 20 degrees (0.35 radians) way from the vertically upward position.

In summary, the design requirements for the inverted pendulum state-space example are:

 Settling time for and of less than 5 seconds

 Rise time for of less than 0.5 seconds

 Pendulum angle never more than 20 degrees (0.35 radians) from the

vertical

 Steady-state error of less than 2% for and

Force analysis and system equations

Below are the free-body diagrams of the two elements of the inverted pendulum system.

Summing the forces in the free-body diagram of the cart in the horizontal direction, you get the following

equation of motion.

(1)
Note that you can also sum the forces in the vertical direction for the cart, but no useful information

would be gained.

Summing the forces in the free-body diagram of the pendulum in the horizontal direction, you get the

following expression for the reaction force .

(2)

If you substitute this equation into the first equation, you get one of the two governing equations for this

system.

(3)

To get the second equation of motion for this system, sum the forces perpendicular to the pendulum.

Solving the system along this axis greatly simplifies the mathematics. You should get the following

equation.

(4)

To get rid of the and terms in the equation above, sum the moments about the centroid of the

pendulum to get the following equation.

(5)

Combining these last two expressions, you get the second governing equation.

(6)

Since the analysis and control design techniques we will be employing in this example apply only to

linear systems, this set of equations needs to be linearized. Specifically, we will linearize the equations

about the vertically upward equillibrium position, = , and will assume that the system stays within a

small neighborhood of this equillbrium. This assumption should be reasonably valid since under control

we desire that the pendulum not deviate more than 20 degrees from the vertically upward position. Let

represent the deviation of the pedulum's position from equilibrium, that is, = + . Again presuming a

small deviation ( ) from equilibrium, we can use the following small angle approximations of the

nonlinear functions in our system equations:


(7)

(8)

(9)

After substiting the above approximations into our nonlinear governing equations, we arrive at the two

linearized equations of motion. Note has been substituted for the input .

(10)

(11)

1. Transfer Function

To obtain the transfer functions of the linearized system equations, we must first take the Laplace

transform of the system equations assuming zero initial conditions. The resulting Laplace transforms are

shown below.

(12)

(13)

Recall that a transfer function represents the relationship between a single input and a single output at a

time. To find our first transfer function for the output and an input of we need to eliminate

from the above equations. Solve the first equation for .

(14)

Then substitute the above into the second equation.

(15)

Rearranging, the transfer function is then the following

(16)

where,
(17)

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 the following.

(18)

Second, the transfer function with the cart position as the output can be derived in a similar

manner to arrive at the following.

(19)

2. State-Space

The linearized equations of motion from above can also be represented in state-space form if they are

rearranged into a series of first order differential equations. Since the equations are linear, they can then

be put into the standard matrix form shown below.

(20)

(21)

The matrix has 2 rows because both the cart's position and the pendulum's position are part of the

output. Specifically, the cart's position is the first element of the output and the pendulum's deviation

from its equilibrium position is the second element of .

MATLAB representation

1. Transfer Function

We can represent the transfer functions derived above for the inverted pendulum system within

MATLAB employing the following commands. Note that you can give names to the outputs (and inputs)
to differentiate between the cart's position and the pendulum's position. Running this code in the

command window produces the output shown below.

M = 0.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;

s = tf('s');

P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q - ((M

+ m)*m*g*l)*s^2/q - b*m*g*l*s/q);

P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q -

b*m*g*l/q);

sys_tf = [P_cart ; P_pend];


inputs = {'u'};

outputs = {'x'; 'phi'};

set(sys_tf,'InputName',inputs)

set(sys_tf,'OutputName',outputs)

sys_tf

sys_tf =

From input "u" to output...

4.182e-06 s^2 - 0.0001025

x: ---------------------------------------------------------

2.3e-06 s^4 + 4.182e-07 s^3 - 7.172e-05 s^2 - 1.025e-05 s

1.045e-05 s

phi: -----------------------------------------------------

2.3e-06 s^3 + 4.182e-07 s^2 - 7.172e-05 s - 1.025e-05


Continuous-time transfer function.

2. State-Space

We can also represent the system using the state-space equations. The following additional MATLAB

commands create a state-space model of the inverted pendulum and produce the output shown below

when run in the MATLAB command window. Again note that the names of the inputs, outputs, and

states can be specified to make the model easier to understand.

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];

states = {'x' 'x_dot' 'phi' 'phi_dot'};

inputs = {'u'};

outputs = {'x'; 'phi'};

sys_ss =

ss(A,B,C,D,'statename',states,'inputname',inputs,'outputname',outputs)

sys_ss =
a =

x x_dot phi phi_dot

x 0 1 0 0

x_dot 0 -0.1818 2.673 0

phi 0 0 0 1

phi_dot 0 -0.4545 31.18 0

b =

x 0

x_dot 1.818

phi 0

phi_dot 4.545

c =

x x_dot phi phi_dot

x 1 0 0 0
phi 0 0 1 0

d =

x 0

phi 0

Continuous-time state-space model.

The above state-space model can also be converted into transfer function form employing the tf

command as shown below. Conversely, the transfer function model can be converted into state-space

form using the ss command.

sys_tf = tf(sys_ss)

sys_tf =

From input "u" to output...

1.818 s^2 - 44.55

x: --------------------------------------
s^4 + 0.1818 s^3 - 31.18 s^2 - 4.455 s

4.545 s + 2.539e-16

phi: ----------------------------------

s^3 + 0.1818 s^2 - 31.18 s - 4.455

Continuous-time transfer function.

Now will design a controller for the inverted pendulum system using the root locus design method. In

the design process we will assume a single-input, single-output plant as described by the following

transfer function. Otherwise stated, we will attempt to control the pendulum's angle without regard for

the cart's position.

(1)

where,

(2)

More specifically, the controller will attempt to maintain the pendulum vertically upward when the cart is

subjected to a 1-Nsec impulse. Under these conditions, the design criteria are:

 Settling time of less than 5 seconds

 Pendulum should not move more than 0.05 radians away from the

vertical
System structure

The structure of the controller for this problem is a little different than the standard control problems you

may be used to. Since we are attempting to control the pendulum's position, which should return to the

vertical after the initial disturbance, the reference signal we are tracking should be zero. This type of

situation is often referred to as a regulator problem. The external force applied to the cart can be

considered as an impulsive disturbance. The schematic for this problem is depicted below.

You may find it easier to analyze and design for this system if we first rearrange the schematic as

follows.

The resulting transfer function for the closed-loop system from an input of force to an output of

pendulum angle is then determined to be the following.

(3)
Before we begin designing our controller, we first need to define our plant within MATLAB. Create a new

m-file and type in the following commands to create the plant model (refer to the main problem for the

details of getting these commands).

M = 0.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;

s = tf('s');

P_pend = (m*l*s/q)/(s^3 + (b*(I + m*l^2))*s^2/q - ((M + m)*m*g*l)*s/q -

b*m*g*l/q);

Root locus design

We will now begin to design a controller for our system employing a root locus design method. We can

use the MATLAB command rlocus for generating the root locus plots. Adding the following commands

to your m-file and running it in the MATLAB command window will create the root locus plot shown

below. This plot displays all possible closed-loop pole locations as a simple proportional control gain

is varied from 0 to infinity. The root locus is the same whether the multiplicative gain is in the

forward or feedback path of the closed-loop system.

rlocus(P_pend)
title('Root Locus of Plant (under Proportional Control)')

As you can see, one of the branches of the root locus is entirely in the right-half of the complex -plane.

This means that no matter the choice of gain , there will always be a closed-loop pole in the right-half

plane making the system's impulse response unstable.

To solve this problem, we need to add a pole at the origin (an integrator) via the controller to cancel the

plant zero at the origin. This addition will produce two closed-loop poles in the right-half plane. In our

subsequent design we can then modify our controller to draw these poles into the left-half plane, thereby

stabilizing the closed-loop system. Modifying your m-file with the following commands and re-running in

the MATLAB command window will produce the root locus plot shown below.

C = 1/s;

rlocus(C*P_pend)
title('Root Locus with Integral Control')

Let's also examine the locations of the system's open-loop poles and zeros so that we may begin to

think about how to draw the root locus branches into the left-half plane. Entering the following

commands into the MATLAB command window will generate the following output.

zeros = zero(C*P_pend)

poles = pole(C*P_pend)

zeros =

poles =

0
5.5651

-5.6041

-0.1428

As you can see, there are four poles and only one zero. This means that the root locus will have three

asymptotes: one along the real axis in the negative direction, and the other two at 120 degree angles to

this one.

This configuration is also unsatisfactory because we still have branches of the root locus that are

entirely in the right-half complex plane. In general, we can pull the branches of our root locus to the left

in the complex plane by adding zeros to our system. Adding a zero to our controller will reduce the

number of asymptotes from three to two. These two asymptotes will be parallel to the imaginary axis

and will intersect the real axis at the location calculated from the following expression.

(4)

Therefore, for our system as described so far, we have the following assuming a minimum-phase zero

(negative).

(5)

Based on the above, the farthest we can pull the asymptotes to the left in the complex plane is

approximately -0.1 for a negligibly small zero. Recall that 2% settling time can be estimated from the

following equation.

(6)

Therefore, dominant closed-loop poles with real parts that approach -0.1 will not be sufficient to meet

the 5 second settling time that we require.


PID control

In the above discussion we demonstrated that adding a zero to our integral controller could pull the

branches of the root locus to the left in the complex plane, but we were not able to the pull the dominant

branches far enough to the left. A possible solution is to add yet another zero. If we place both zeros on

the negative real axis between the two plant poles, then the two branches in the right-half plane will be

pulled into the left-half plane and will terminate at these two zeros. Let's specifically evaluate the root

locus for a controller with an integrator and zeros at -3 and -4. Note that this controller is actually a PID

controller. We can create this controller within MATLAB using the zpk command which creates a model

by specifying the zeros, poles, and gain of the system. Modifying your m-file with the following

commands and re-running will produce the root locus plot shown below.

z = [-3 -4];

p = 0;

k = 1;

C = zpk(z,p,k);

rlocus(C*P_pend)

title('Root Locus with PID Controller')


Examining the above root locus helps us to determine whether or not our given requirements can be

met. Specifically, since it is desired that the settling time of the system be less than 5 seconds, the real

parts of our dominant closed-loop poles should be less than approximately -4/5 = -0.8. In other words,

our dominatnt closed-loop poles should be located in the complex -plane to the left of a vertical line at

. Inspection of the above shows that this is possible. Since it is also desired that the pendulum

not move more than 0.05 radians away from vertical, we also want to ensure that the closed-loop

system has sufficient damping. Placing the dominant closed-loop poles near the real axis will increase

the system's damping (small ).

To find the gain corresponding to a specific point on the root locus, we can use the rlocfind

command. Specifically, enter the command [k,poles] = rlocfind(C*P_pend) in the MATLAB

command window.

Then go to the plot and select a point on the root locus on left side of the loop, close to the real axis as

shown below with the small + marks. Selecting these poles will ensure that the system settles

sufficiently fast and, hopefully, that it has sufficient damping.


After doing this, you should see an output like the following in the MATLAB command window.

Select a point in the graphics window

selected_point =

-3.5367 + 0.7081i
k =

20.2396

poles =

-85.1333

-3.5232 + 0.7086i

-3.5232 - 0.7086i

Note that the values returned in your MATLAB command window may not be exactly the same, but they

should at least have the same order of magnitude.

Then we can check the impulse response of our closed-loop system to see if our requirements are

actually met for a gain of approximately 20. Add the following commands to your m-file and re-run to

generate a closed-loop impulse response like the one shown below.

K = 20;

T = feedback(P_pend,K*C);

impulse(T)
title('Response of Pendulum Angle to an Impulse Disturbance under PID

Control');

Examination of the above demonstrates that all of the given requirements are met.

What happens to the cart's position?

At the beginning of this page, a block diagram for the inverted pendulum system was given. The

diagram was not entirely complete. The block representing the response of the cart's position was not

included because that variable is 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 full system block diagram as shown in the following figure.
Rearranging, we get the following block diagram.

In the above, the block is the controller designed for maintaining the pendulum vertical. The

closed-loop transfer function from an input force applied to the cart to an output of cart position is,

therefore, given by the following.

(7)

Referring to the Inverted Pendulum: System Modeling page, the transfer function for is defined

as follows.

(8)

where,
(9)

Adding the following commands to your m-file (presuming and are still defined) will

generate the response of the cart's position to the same impulsive disturbance we have been

considering.

P_cart = (((I+m*l^2)/q)*s^2 - (m*g*l/q))/(s^4 + (b*(I + m*l^2))*s^3/q - ((M

+ m)*m*g*l)*s^2/q - b*m*g*l*s/q);

T2 = feedback(1,P_pend*C)*P_cart;

t = 0:0.01:8.5;

impulse(T2, t);

title('Response of Cart Position to an Impulse Disturbance under PID

Control');
As you can see, the cart's position goes unstable for this impulse disturbance. Therefore, although the

PID controller stabilizes the angle of the pendulum, this design would not be feasible to implement on an

actual physical system.

You might also like