CMU
UM


Example: Solution to the Inverted Pendulum Problem Using PID Control

Open-loop transfer function
Closed-loop transfer function
Adding the PID controller
What happens to the cart's position?

The state equations for this problem are:

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

To see how this problem was originally set up, consult the inverted pendulum modeling page.

Open-loop transfer function

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. In this problem, we are given the state-space equations for the system. This set of equations can be changed to a transfer function by creating the following m-file (or a '.m' file located in the same directory as Matlab):

By running this m-file, the numerator and denominator of the transfer function should be shown in the Matlab command window. You should see the following result:
The first thing to notice is that the numerator has two rows; one row represents the cart's position and the other represents pendulum's angle. For this problem, only the pendulum's angle is of interest, and we will ignore the first row for now.

The next thing to notice is that in the second row of the numerator, the first and last elements are 0, while the second and fourth element are 0.0000. If you look closely at each of these elements (try format long e), you will find that they are not exactly zero, but in fact a very small number. These numbers are not really supposed to be part of the transfer function, and should be eliminated. Click here, and look at how to use the ss2tf command to see why. The numerator can be corrected by adding the following line of code to the end of your m-file:

The transfer function numerator now reflects only the output of interest, and the zeros at infinity will not cause errors.

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

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

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:

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 (the two transfer functions differ in numerator alone), so the transfer function from X to F can be simplified to:

Now that we have the transfer function, let's take a look at the response. Create a new m-file that looks like the following:

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.


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.

If you would like to receive a response, send your comments or questions to ctm-feedback@umich.edu


PID Examples
Cruise Control | DC Motor | Bus Suspension | Inverted Pendulum

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

Tutorials

Basics | Modeling | PID | Root Locus | Frequency Response | State Space | Examples

8/28/96 JDP