CMU
UM


Example: State-space design for the inverted pendulum

Open-loop poles
LQR design
Observer design

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.

This problem can be solved using the control-law for full state feedback. This type of controller has two added elements: the control law and the estimator. The schematic of the state-space design elements is shown below.

Open-loop poles

In this problem r(t) represents the force applied to the cart; this force will be simulated by an impulse to model the cart receiving a bump. The 4 states represent the position and velocity of the cart and the angle and angular velocity of the pendulum. The output y(t) contains both the position of the cart and the angle of the pendulum. We want to design a controller so that when an impulse input is given to the system, the pendulum should be displaced, but eventually return to zero (i.e. the vertical). Although we will focus our design on balancing the pendulum, the cart will also be moving.

The first step in designing this type of controller is to determine the open-loop poles of the system. Enter the following lines of code into a m-file (or a '.m' file located in the same directory as Matlab):

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

     p = eig(A)
The Matlab command window should output the following text as a result:
As you can see, there is one right-half-plane pole at 5.5651. This should confirm your intuition that the system is unstable in open loop.

LQR design

The next step in the design process is to assume that we have full-state feedback (i.e. that we can measure all four states), and find the vector K which determines the feedback control law. This can be done in a number of ways. If you know the desired closed-loop poles, you can use the place or acker command. Another option is to use the lqr function; this will give you the optimal controller (under certain assumptions; consult your textbook for more details). The lqr function allows you to choose two parameters, R and Q, which will balance the relative importance of the input and state in the cost function that you are trying to optimize. The simplest case is to assume R=1, and Q=C'*C. You may notice that we are using both outputs (the pendulum's angle and the cart's position). Essentially, the lqr method allows for the control of both outputs. In this case, it is pretty easy to do. The controller can be tuned by changing the nonzero elements in the Q matrix to get a desirable response. To find the structure of Q, enter the following into the Matlab command window:

You should see the following in the command window:
The element in the 1,1 position will be used to weight the cart's position and the element in the 3,3 position will be used to weight the pendulum's angle. The input weighting R will remain at 1. Now that we know what the Q matrix should look like we can experiment to find the K matrix that will give us a good controller. We will go ahead and find the K matrix and plot the response all in one step so that changes can be made in the control and be seen automatically in the response. Enter the following text into your m-file:
You should get the following impulse response plot:

The curve in purple represents the pendulum's angle, in radians. As you can see, this plot is still not satisfactory. The pendulum moves about 0.25 radians away from the vertical, which is 0.2 radians too far. The settling time is about right at 4 seconds. The yellow curve is the cart's position (in meters). At first it is bumped a little (about 0.1 m), but then it moves a relatively long way in the other direction to stop the pendulum from tipping over. Go back to your m-file and change the x and y variables to see if you can get a better response. You will find that increasing x makes the settling time go down, and lowers the angle the pendulum moves. Using x=50,000 and y=100,000, the following impulse response was reached:

You may have noted that if you increased x and y even higher, you could improve the response even more. The reason this plot was chosen was because it satisfied the design requirements while keeping x and y as small as possible. In this problem, x and y have been used to describe the relative weight of the tracking error in the cart's position and pendulum's angle versus the control effort. The higher x and y are, the more control effort is used, but the smaller the tracking error. The system response has the pendulum's angle moving about 0.05 radians away from the vertical, and a 0.75 second settling time.

This response is good, but was found assuming full-state feedback, which most likely will not be a valid assumption. To compensate for this, we will next design a full-order estimator to estimate those states that are not measured.

Observer design

To begin, we must first find the controller poles. To do this copy the following code to the end of your m-file:

If you changed the weighting factors x and y above to x=50,000 and y=100,000, you should see the following poles in the Matlab command window:

We want to design estimator poles that are about 4-10 times as fast as slowest pole, say at -10. We will use the place command in Matlab to find the L vector (note that acker would also work). Remember that the place command cannot have all the desired poles at the same location. Delete from the impulse command on and enter the following text to the end of your m-file to find the L matrix:

We are using both outputs (the angle of the pendulum and the position of the cart) to design the observer. The system is not observable using only the angle of the pendulum as output; you can check this in Matlab by computing rank(obsv(A,C(2,:))). This should make sense to you: if you can only measure the angle of the pendulum, you cannot determine what the position of the cart will be.

You should see the following in the Matlab window:

Now we will combine the control-law design with the estimator design to get the compensator. The response should be similar to the one from the control-law design. To set up the compensator, delete your impulse command and copy the following code to the end of your m-file:

After running this m-file, you should output the following impulse response plot:

This response is about the same as before. The settling time is now about 1 second and the pendulum does not ever move more than 0.05 radians away from the vertical. Both of the design requirements have been met with the minimum amount of control effort, so no more iteration is needed. The cart position is stabilized as well, albeit a bit more slowly. As you can see, it is much easier to control multi-input or multi-output systems with the state space method than with any other of the methods.


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


State Space 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, 12/16/96 dmt