CMU
UM


Example: Solution to the Inverted Pendulum Problem Using PID Control

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

The transfer function of the plant for this problem is given below:

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 Representation

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. The transfer function found from the Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab by inputting the numerator and denominator as vectors. Create an m-file (or a '.m' file located in the same directory as Matlab) and copy the following text to model the transfer function:

Your output should be:

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 (whether your using the transfer function from the Laplace transforms or from the state-space representation):


Note: Non-standard Matlab commands used in this example are highlighted in green.

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 if the pole/zero at the origin that was cancelled is added back in. So the transfer function from X to F can be simplified to:

Now that we have the transfer function for the entire system, let's take a look at the response. First we need the transfer function for the cart's position. To get this we need to go back to the laplace transforms of the system equations and find the transfer function from X(s) to U(s). Below is this transfer function:

For more about the Laplace transform please refer to the inverted pendulum modeling page.

The pole/zero at the origin cancelled out of the transfer function for Phi, has been put back in. So that now den1 = den2, making calculations easier. Now, create a new m-file and run it in the command window:

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 Feedback

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.


PID 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

Tutorials

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

8/12/97 CJC