Upload
montoya-santiago
View
222
Download
0
Embed Size (px)
Citation preview
8/12/2019 Control of an Inverted Pendulum System-Libre
1/34
Controlling an inverted pendulum using full state feedback controller
Tsegazeab Shishaye
[ID:2012420012]
Northwestern Polytechnical University, Xian,China
Abstract
In this paper an inverted pendulum is presented using state space modeling method. And full state feedback controller is
developed using pole placement and LQR (Linear Quadratic Regulation) methods. After that, tracking problem is
addressed by designing a steady state error controller. Then, considering the reality conditions, first assuming by some of
the state variables are measurable, a reduced state observer is designed and then for a worst scenario a full order state
observer is designed. Finally, the state feedback controller and the state observer are summed up to give a pre-
compensator and an overall steady state error controller is added to that new system. All mathematical modeling arepresented clearly and simulations together with their analysis were done using MATLAB software. For clear view on
what is going on with the control method and the system, an animation GUI is also presented.
8/12/2019 Control of an Inverted Pendulum System-Libre
2/34
Introduction
As a typical control system, the control of an inverted pendulum is excellent in testing and evaluating different control
methods. 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
but The fundamental principles within this control system can be found in many industrial applications, such as stability
control of walking robots, vibration control of launching platform for shuttles etc
Problem formulation
The cart which a slim stick is fasted on can move along
a smooth track under the force, a controller needs to bedesigned such that the one stage inverted pendulum
can be steadily balanced at its vertical position
after some disturbance.
Given parameters:M - Mass of the cart 0.5 kg
m - Mass of the pendulum 0.5 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
- Pendulum angle from vertical, and
8/12/2019 Control of an Inverted Pendulum System-Libre
3/34
#Task-1
- Linearizing the non-linear equation of motion of the system around =0 and- Finding the associated state space model of the system
Taking the system equations of motion of the system from above,
System modeling
Since the analysis (state space model) and control design techniques that I will employ in this problem apply only to linear
systems, these equations need to be linearized. Specifically, I will linearize the equations about the vertically upward
equilibrium position, = 0, and will assume that the system stays within a small neighborhood of this equilibrium.
Linearization
Here, instead of using the linearization method (Taylor method for small perturbations), I will use simple approximation
around = 0. This assumption is reasonably valid since under control it is desired that the pendulum not deviate more
than 20 degrees (0.35 radians) from the vertically upward position.
So, for small ,
= , And
= 1, (According to the above diagram will lie in the first quadrant. so, the Cosine function is positive)Also dropping all the non-linear components (= 0) the above equations will become:( + ) + + = .. (3)
( + ) =mx .. (4)Solving for
and
independently from equation (3) and equation (4) respectively gives,
= + . (5) = + . (6)Substituting equation (5) in (4) and equation (6) in (3) it yields,
= ()() + () ()
8/12/2019 Control of an Inverted Pendulum System-Libre
4/34
= () ()() + () State-space model
Now, let = , = , = = , = , = = , = , = , the four state equations will be,
=
= (+)( + ) + + (+) ( + ) + = = ( + ) + (+)( + ) + + + ( + ) + State space model of an LTI system is
= + = + So, substituting the above equations in to the matrices properly will give the following state space representation.
=
=
0 1 0 0()() 0 0 ()0 0 0 1() 0 0 ()()
+
0 ()0()
. (7)
= = 0 0 1 01 0 0 0 +
00 . (8)In MATLAB,
=(,,,) % to find the state space model=() % to find the transfer function of the system (for both outputs)N.B.
Examining the results, especially the numerators, in the transfer functions of each output solved from the state space
model, there exists some terms with very small coefficients. These terms should actually be zero, they only show up due
to numerical round-off errorsthat accumulate in the conversion algorithms that MATLAB employs. This can be checked
8/12/2019 Control of an Inverted Pendulum System-Libre
5/34
by solving the transfer functions of the cart and the pendulum independently from the motion equations using Laplace
transform by assuming zero initial conditions.
#Tsak-2
- Simulating the dynamic behavior of the system under impulse force and step force- Analyzing the stability, controllability and observability condition of the system
The design requirements for the Inverted Pendulum project are:
Settling time for and of less than 5 seconds. Rise time for of less than 0.5 seconds. Overshoot of less than 20 degrees (0.35 radians).
System analysis
1. Open-loop impulse response of the systemExamining on how the system responds to a 1-Nsec impulsive force applied to the cart, I found the following result.
Fig.1. open-loop impulse response of an inverted pendulum system
From the plot, the response is unsatisfactory. Both outputs never settle, the angle of the pendulum goes to several
hundred radians in a clockwise direction though it should be less than 0.35 rad. And the cart goes to the right infinitely.
So, this system is unstable in an open loop condition when there is a small impulsive force applied to the cart.
0
20
40
60
80From: u
To:x
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1-500
-400
-300
-200
-100
0
To:theta
Open-Loop Impulse Response
Time (sec)
Amplitude
8/12/2019 Control of an Inverted Pendulum System-Libre
6/34
2. Open-loop step response of the systemHere also, it can be seen from the outputs that the system is unstable under 1-Newton step input applied to the cart. The
outputs are found by using the command of MATLAB which can be employed to simulate the response of LTImodels to arbitrary inputs.
The plot, fig.2, shows that the responses of the system to a step force are unstable.
Fig.2. open-loop step response of an inverted pendulum
3. Stability of the systemTo check stability means to analyze whether the open-loop system (without any control) is stable. That has partly done by
the above simulations under the impulse and step forces. But as per the definition, the eigenvalues of the system state
matrix, A, can determine the stability. That is equivalent to finding the poles of the transfer function of the system. The
eigenvalues of the A matrix are the values of s where( ) = . A system is stable if all its poles have lied in theleft-half of the s-plane.
poles = eig(A)
Poles =
0
7.1430
-7.2220
-0.1000
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-50
-40
-30
-20
-10
0
10
20
30
40
50Open-Loop Step Response
x
theta
8/12/2019 Control of an Inverted Pendulum System-Libre
7/34
Or it can be shown by pole-zero mapping of the system as below using MATLAB command
()
Fig.3. Pole-zero mapping of the system
As can be seen from the output, there is one pole on the right-half plane at 7.1430. This confirms the intuition that thesystem is unstablein open loop.
4. Controllability of the systemA system is controllableif there exists a control input ()that transfers any state of the system to zero in finite time. Itcan be shown that an LTI system is controllable if and only if its controllability matrix, CO, has full rank. i.e. if() = , where nis the number of states.So, the necessary and sufficient condition for controllability of the system is:
() =[ ] = Using the following MATLAB command, the controllability matrix of the system is:
RankCO = rank ((,))RankCO= 4
So, since the controllability matrix has full rank, the system is controllable!
5. Observability of the system
-8 -6 -4 -2 0 2 4 6 8-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1Pole-zero mapping of the system
Real Axis
ImaginaryAxis
8/12/2019 Control of an Inverted Pendulum System-Libre
8/34
In cases where all the state variables of a system may not be directly measurable, it is necessary to estimate the values of
the unknown internal state variables using only the available system outputs.
Conceptually, a system is observableif the initial state, x(t_0), can be determined from the system output, y(t), over some
finite time t0< t < tf.
Since this system is linearized to be LTI system, the system is observable if and only if the Observability matrix, OB, has
full rank (i.e. if () = , where nis the number of states).So, necessary and sufficient condition for Observability is:
() =
= So, in MATLAB using the command
=((, ))RankOB=
4
So, since the observability matrix has full rank, the system is observable!
#Task-3
- Designing a state feedback controller to stabilize the system by improving performance of the system using poleplacementand linear quadratic regulator(LQR) methods
- If all the states are not measurable, designing a faster stable full-order state observer for state feedback controller.- If only , are measurable, designing a reduced-order state observer for state feedback controller.- To make design more challenging, applying a step input to the cart and yet achieving the following design
requirements,
o Settling time for and of less than 5 seconds.o Rise time for of less than 0.5 seconds.o Overshoot of less than 20 degrees (0.35 radians).
Designing full-state feedback controller
8/12/2019 Control of an Inverted Pendulum System-Libre
9/34
Here the main purpose is to design a controller so that when a step reference is given to the system, the pendulum should
be displaced, but eventually return to zero (i.e. vertical) and the cart should move to its new commanded position.
Design procedure:
Checking if the pair (,)is controllable. Constructing equations that will govern the controller dynamics Placing the eigenvalues of the controller matrix in a desired position by finding an arbitrary vector state feedback
control gain vector assuming that all of the state variables are measurable. This can be accomplished usingeither of the two methods pole placement methodand LQR (Linear Quadratic Regulation) method.
So, it has been checked before that the system is controllable. i.e. the pair (,)is controllable.Since, the state of the system is to be to be feedback as an input, the controller dynamics will be:
=
= + ( )= ( ) + = 1- Using pole placement method
This method depends on the performance criteria, such as rise time, settling time, and overshoot used in the design.
The design requirements are,
Settling time, ,for both outputs should be less than 5 seconds. And %Overshoot, %OS, of the angle of the pendulum should be less than 20 (0.35 radians).Design procedures for pole placement:
1. Using time domain specifications to locate dominant poles roots of + + = . This is done byusing the following formulas and finding the dominant poles at
= (%/)(%/)
= , valid up to ~ 0.7, and = 1 = =
2. Then placing rest of poles so they are much faster than the dominant second order behavior.
8/12/2019 Control of an Inverted Pendulum System-Libre
10/34
Typically, keeping the same damped frequency and then moving the real part to make them fasterthan the real part of the dominant poles so that the transient response of the real poles of the system will
decay exponentially to insignificance at the settling time generated by the second order pair.
While taking care of moving the poles too far to the left because it takes a lot of control effect (needslarge actuating signal)
#procedure 1
Given % =2 0(0.35 rad) and =5, calculating the parameters as follow:=0.456,
=1.78rad/sec, = = 62.870, =
= -0.811,
= 1.584So, the dominant polesare:
=> - 1 = -0.811+j1.584 and -0.811-j1.584 (which are complex conjugates)
Fig.4. Dominant poles and guide for desired poles allocation
8/12/2019 Control of an Inverted Pendulum System-Libre
11/34
#procedure 2
Using MATLAB software, the following are the gain
vectors for different sets of desired poles.
Test -1K1 = [-12.4835 -1.2055 -0.2423 -0.4731]
Desired poles_1 = [-2.433 -1.622 -0.8111.584j],by making the remaining poles 2 and 3times faster than thereal part of the dominant poles.
Test 2K2 = [-21.0855 -3.2251 -2.0191 -1.8811]
Desired poles_2 = [-8.11 -4.055 -0.811 1.584j],by making the remaining poles 5 and 10 times faster thanthe real part of the dominant poles.
Test - 3K3 = [-40.2010 -5.9361 -6.7842 -4.8694]
Desired poles_3 = [-11.354 -9.732 -0.8111.584j],by making the remaining poles 10 and 12times faster than
the real part of the dominant poles.
Note:
As can be seen from the respective plots of the system step
response for each calculated gain vectors as per the desired
pole locations, System design requirements are satisfied in
all the three tests. And, the system response tends to be
faster when the real poles go farther to the left from the real
part of the dominant poles. A more faster response can be
found by moving the real poles deeper in to the left half
side of the s-plane, but it requires a larger actuating signal
which in turn brings larger control effort.
0 1 2 3 4 5 6 7 8 9 10-1
-0.8
-0.6
-0.4
-0.2
0
0.2
c
artposition(m)
Step Response using pole placement-1st test
0 1 2 3 4 5 6 7 8 9 10-0.06
-0.04
-0.02
0
0.02
0.04
0.06
penduluma
ngle(radians)
0 1 2 3 4 5 6 7 8 9 10-0.15
-0.1
-0.05
0
0.05
cartposition(m)
Step Response using pole placement-2nd test
0 1 2 3 4 5 6 7 8 9 10-0.02
-0.01
0
0.01
0.02
penduluma
ngle(radians)
0 1 2 3 4 5 6 7 8 9 10-0.04
-0.03
-0.02
-0.01
0
0.01
cartposition(m)
Step Response using pole placement-3rd test
0 1 2 3 4 5 6 7 8 9 10-6
-4
-2
0
2
4x 10
-3
penduluma
ngle(radians)
8/12/2019 Control of an Inverted Pendulum System-Libre
12/34
2- Using Linear Quadratic Regulator (LQR)This approach is to place the pole locations so that the closed-loop system optimizes the cost function given by
= [ ()() + ()()] Where:
- is the state costwith weight - is the control costwith weight
Therefore, LQR selects closed-loop poles that balance between state errors and control effort.
Design procedures for LQR:
1.
Selecting design parameter matrices Qand R Easily, starting with = = 1and increasing the weight on the matrix Q This is done supposing that the cost = ( + )is to be minimized. If so, = , = = =
2. Solving the algebraicRiccatiequation for P, (done using MATLAB software) + + =
3. Finding the state variable feedback using
=
4. Simulating results using MATLAB command and observing5. Verifying design, if it seen to be unsatisfactory, trying again with different weighting matrices Qand RAll the above procedures are completed easily in MATLB as follow:
>> Q = C'*C;>> Q(1,1) = 10;>> Q(3,3) = 100>> R = 1>> K = lqr(A,B,Q,R)>> Ac = A-B*K; %control matrix
>> system_c = ss(Ac,B,C,D);And all the results can be seen below.
Q =80 0 0 00 0 0 00 0 400 00 0 0 0, R = 1K = [-38.1423 -6.2197 -10.0000 -7.7060]
0 1 2 3 4 5 6 7 8 9 10-0.02
0
0.02
cartposition(m)
Step Response with LQR Control
0 1 2 3 4 5 6 7 8 9 10-5
0
5x 10
-3
penduluma
ngle(radians)
8/12/2019 Control of an Inverted Pendulum System-Libre
13/34
Note:
- The reason this weighting was cho
magnitude of more would mak
control effort generally correspond
can be seen clearly by varying the
- The above LQR design method hasa good manner. But, if the referenc
degraded. So, for best quality of pe
Designing an asymptotic error tracki
- So far, both controller design meth
that the dynamics of the system to h
- The question remains as to how
command?
This deals with performance issue of the sy
= +
=
=
For good tracking performance, it should
() ()
To make this, one solution is to scale the re
= , where is a feedforwar
fig.5.Adding a pre-compensation scale factor to the r
en is because it just satisfies the transient design re
the tracking error smaller, but would require g
to greater cost (more energy, larger actuator, etc.).
eightstethaand xin the animation GUIthat will be att
brought a good stability to the system and fulfillede input is different from zero, ()= , the sy
formance, an asymptotic tracking of a step referen
g controller for good regulation
ods, the pole placement and LQR design, helped to
ave nice properties more importantly to stabilize
well the designed controller in both methods all
tem rather than just stability.
be
erence input ()so that,
d gainused to scale the closed loop transfer functio
eference input command
quirements. Increasing the
eater control force. More
More improved responses
ched with this document.
the design requirements instems performance will be
e inputmust be designed.
pick the gain vector K so
.
ows to track a reference
.
8/12/2019 Control of an Inverted Pendulum System-Libre
14/34
Then, the above equations becomes,
= ( ) + = Then, the new transfer function will be
()() =( ( )) = () So, clearly the scaling factor can be computed as follow. = () =(( )))In this specific problem, before calculating , the = should be modified to a new one =
[ ]because it is redefined that
= ,which means the reference input is only applied to the position of the
cart.
Therefore, having K from the previous LQR design and scaling the reference input with following result, the system
responses are plotted below. =(( ))In MATLAB,
>> Cn = [0 0 1 0]; %modification of C matrix>> Nbar=-inv(Cn*((A-B*K)\B));
>> system_cl = ss(Ac,B*Nbar,C,D);
As can be seen from the above plot, all design
requirements are satisfied. i.e., the overshoot is below the
limits, the settling time is also
8/12/2019 Control of an Inverted Pendulum System-Libre
15/34
Designing a reduced order state observer
If and are measurable, then a reduced order state estimator can be designed to estimate and .To do this taking the original state space representation
= +
= Generally if of he states are measurable, = , where are the measured and unmeasured states respectively.And the system dynamics can be described as:
= + + = + + = For ()to be an estimate of (), that is, for = + + to be an estimate of , the following conditionsmust be satisfied.
- = - = - All eigenvalues of F have to have negative real parts and are different from the eigenvalues of A.
So, to estimate the states
= and
= , the reduced dimension observer can be designed as the following:
Design Procedures:
1. Choosing an arbitrary 22matrixso that all eigenvalues have negative real parts and are different from those of.F =
1.0e+003 *
-0.0632 0.0011-1.0497 0.0016
And checking itseig(F)= -30.8206 +10.2537i and-30.8206 -10.2537i, both eigenvalues of F have negative real
values and all are different from those ofeig(A)= 0,7.1430, -7.2220, -0.1000.
2. Choosing a 2x2 vector so that (,)is controllable, =1.0+004
6.4715 0.0600
8/12/2019 Control of an Inverted Pendulum System-Libre
16/34
0.2790 0.0034And checking (, ) = 2 so, it is satisfied.
3. Solving 2x4 matrix using Lyapunov equation = in MATLAB software=(,,)T =
1.0e+004 *
-0.0035 0.0002 0.0094 -0.0068
-0.1986 0.0084 6.4223 -0.3785
4. Checking if the square matrix of order 4 is singular or non-singular = =
p =
1.0e+004 *
0 0 0.0001 0
0.0001 0 0 0
-0.0035 0.0002 0.0094 -0.0068
-0.1986 0.0084 6.4223 -0.3785
Finding its inverse () = =
1.0e+003 *
-0.0000 0.0010 0 0
2.0405 -0.0009 0.0019 -0.0000
0.0010 0 0 0
0.0624 -0.0005 0.0000 -0.0000
So this means it is non-singular because its inverse is found.
5. If is non-singular, computing = = =1.0e+003 *
-0.1315
-7.2163
Then, the reduced observer can be put as follow.
= + + =All the coefficient matrices are calculated above.
Finally, the estimate is
8/12/2019 Control of an Inverted Pendulum System-Libre
17/34
= And the error is
= = + + ( + ) = =1.+003 0.0000 0.0010 0 02.0405 0.0009 0.0019 0.00000.0010 0 0 00.0624 0.0005 0.0000 0.0000 =1 . 0 +0 0 3 0.0632 0.00111.0497 0.0016
8/12/2019 Control of an Inverted Pendulum System-Libre
18/34
Designing full order state observer (closed loop state estimator)
So far, it was assumed that there is full or partial access to the state () when the controllers in each method aredesigned. But in reality, most often, all of this information is not available.
To address this issue, a replica of the dynamic system that provides an estimate of the system states based on the
measured output of the system should be developed the so called a state observer or state estimator.
Here, a full order state observer will be developed assuming as if all the states cant be measured taking in to
consideration that there might be lack of sensors.
Design procedures:
1- Checking whether the pair (,)is observable2- Developing estimate of ()that will be called ()3- Selecting a suitable real constant vector
so that all eigenvalues of
()have negative real parts
4- Making sure that the estimator eigenvalues are faster than the desired eigenvalues of the state feedbackbecause it will be used together to form a compensator.
So, it has been checked initially that this system is observable. i.e the pair (,)is observable. = + +
= + + ( )
= ( ) + +
= ( ) = Then the closed loop estimator error dynamics is
=
= [ + ] [ + + ( )]
= ( )( ) = ( ) = ( ) Where = = This equation governs the estimator error. If all eigenvalues of ()can be can be assigned in the negative half planethen all entries of the error will approach to zero at faster rates. Thus, there is no need to compute the initial state of theoriginal state equation.
8/12/2019 Control of an Inverted Pendulum System-Libre
19/34
To select a common guideline is to make the estimator poles 4-10 times faster than the slowest controller pole. Makingthe estimator poles too fast can be problematic if the measurement is corrupted by noise or there are errors in the sensor
measurement in general.
Controller poles from the above system with error tracking controller are:
>>contr_poles=eig(Ac)
>>contr_poles =
-7.6280 + 3.4727i
-7.6280 - 3.4727i
-3.1744 + 2.1467i
-3.1744 - 2.1467i
The slowest poles have real parts at -3.1744, so the estimator poles can be placed at -30. Since the closed-loop estimator
dynamics are determined by a matrix ( )that has a similar form to the matrix that determines the dynamics of thestate-feedback system ( ).Therefore, the same commands, that were used to find the state feedback gain,can be used to find the estimator gain .Since ( ) =( ) and have an exact form as then the MATLAB commandplacecan be used to compute L.
%Placing obsserver poles
>> obsr_poles = [-30 -31 -32 -33];>> L = place(A',C',obsr_poles)'>> L =
1.0e+003 *
-0.0006 0.0632-0.0018 1.04970.0626 -0.00080.9732 -0.0341
Final goal is to build the compensator (state feedback controller + full state observer).
Dynamic output feedback compensator is the combination of the regulator (state feedback controller developed using the
LQR method) and full state estimator using
= = ( ) + +
8/12/2019 Control of an Inverted Pendulum System-Libre
20/34
= ( ) + Rewriting with new state = +
=
Where
= = But since there is a reference input, the compensator can
be implemented using the following feedback.
() = () ()Then the state space model of the compensator will be
= + (this can be seen in the block diagram)So, equations that can describe the overall closed loop dynamics will be
Having = and = while = = + = + = + = + ( ) = + ( )= + + In short, the overall closed loop final state space model is:
= + = [ ]
Where
= = By making an equivalent transformationto the above state space model, it can be represented as the following equivalent
state equation. And this equivalent state equation governs the whole controller-estimator configuration.
8/12/2019 Control of an Inverted Pendulum System-Libre
21/34
= + Where = = = [ ] The response of the system with controller + observer is:
Note:
The overall design requirements are satisfied and the system has a relatively good performance. But it can be seen that the
response graph for the inverted pendulum lucks a slight smoothness. That means there is something which creates that
vibration. i.e. there is a tracking problem of the output to the reference input. The next task will solve this problem.
0 1 2 3 4 5 6 7 8 9 10-0.02
0
0.02
cartp
osition(m)
Step Response with controller + observer
0 1 2 3 4 5 6 7 8 9 10-5
0
5x 10-3
pendulum
angle(radians)
8/12/2019 Control of an Inverted Pendulum System-Libre
22/34
#Final-Task
- Redefining = ( )- Designing no steady state error tracking controller for the new SISO system to eliminate the steady state
error
Having the final controller-observerstate space model, if the reference input is a constant different from zero, () = , and if it is applied only on the carts position, it can be taken that = so that the system will become a SISO system.So, the matrix = should be modified to a new one = [ ]when calculating a steady stateerror tracking controller that can be implemented by adding a feedforward gain to scale the reference input.This has the same approach as I used in the LQR controller, but slightly different in calculating the scaling factor sinceit uses the overall controller-estimator configuration closed loop matrices.
So, taking the equivalent state equation of the new system (controller + observer),
= + Where = = = [ ] For good tracking performance, it should be
() () That means DC gain of the response from to should be unity.To make this, one solution is to scale the reference input ()so that, = , where is a feedforward gainused to scale the closed loop transfer function.Then using the closed matrix,
=(()))Where = , = and = [ ] in which = [ ]because thestep reference command is to be applied only to the carts position(
= ).
Then the final inverted pendulum system controlled by designing a controller, a full state observer andadding a no steady state error tracker can be described by the following state space model.
= + = [ ]
8/12/2019 Control of an Inverted Pendulum System-Libre
23/34
And the system response graph can be seen below.
As can be seen clearly from the graph the whole design
has solved clearly the problems of stability and
performance. And the no steady state error tracker has
eliminated the oscillation of the pendulum and made it
smooth.
And it can be seen from the animation GUI graph below
that, no matter the applied input force is varied, the
pendulum quickly balances itself with almost zero
vibration in its steady state. These graphs are draw by
varying the step inputs by sliding the bar from 0.2 to 0.5
randomly and at each time pressing the Run commandwithout clearing the previous output.
Fig.7.Animation GUI for a controlled Inverted pendulum
Explanation:
- Run button performs the simulation and plots theresponse and the animation.
- Clear button clears the both plots. If the plots arenot cleared, then during the next run the step
response will be graphed on the same plot. This is
useful if you want to graphically see the effect of
varying a parameter.
- Exit button closes the GUI- Weighting factor
x - This editable text field weights the cart's position in the LQR controller. Increasing the weightingfactor improves the cart's response, making it reach it's commanded position faster.
theta - This editable text field weights the pendulum's angle. Similar to the x weighting factor, makingthis larger will quicken the pendulum's response. Feel free to change the weighting factors to see wha
happens!
- Step Slider- The slider allows you to change the magnitude of the step disturbance on the cart.- Manual Advance- If this control is checked, the user is able to advance the animation and plot one frame at a
time. The frames are advanced by pressing any key on the keyboard. This function is useful if the animation
moves too fast for the user and will allow the user to better visualize the entirety of the system's motion.
0 1 2 3 4 5 6 7 8 9 10-0.1
0
0.1
0.2
0.3
0.4
cartposition(m)
Step Response with compensator and no steady state error traker
0 1 2 3 4 5 6 7 8 9 10-0.1
-0.05
0
0.05
0.1
0.15
pendulu
ma
ngle(radians)
8/12/2019 Control of an Inverted Pendulum System-Libre
24/34
- Reference Input- This box is automatically checked when the GUI is run. By un-checking it the user removesthe reference input term, Nbar, from the simulation. The reference input is used to correct steady-state errors
common to full-state feedback systems.
Conclusion
Modeling of an inverted pendulum shows that the system is unstable without a controller. Results of applying state
feedback controllers show that the system can be stabilized. While both pole placement and LQR controller methods are
cumbersome because of selection of constants of controller though they can brought a good result if done systematically
with some guide lines.
When a DC reference input is applied to the cart, the system has failed slightly to track the input and has given a stable
output with some oscillations unsatisfactory steady state performances. To eliminate this, a no steady state error tracking
controller is designed and has brought good results as can be seen on the graphs in each steps.
And since in reality all the states cant be measured, a full-order state estimator (observer) is designed. Finally, the state
feedback controller is summed with a full-order state observer to give a pre-compensator and then a steady state error
tracking mechanism is also added to the whole new system. All those have brought a really nice controlled inverted
pendulum system with good performance.
References
[1] Chi-Tsong Chen, Linear system theory and design, 3
rd
edition , 1999 Oxford University press
[2] Chi-Tsong Chen, Analog and digital control system design transfer function, state space and algebraic methods
[3] B.Wayne Bequette, Process control Modeling, Design and simulation, Rensselaer Polytechnic Institute
[4] Friedland, Bernard, Control System Design, McGraw-Hill, Boston, 1986.
[5] P. Kumar, O.N. Mehrotra, J. Mahto, Controller design of inverted pendulum using pole placement and LQR, ISSN2319 - 1163
[6] A. Bazoune, Transient Response Specifications of a Second Order System
[7] Andrew K. Stimac, Standup and Stabilization of the Inverted Pendulum, MIT, June 1999
[8] Stormy Attaway, MATLAB, Practical Introduction to Programming and Problem Solving Second Edition,department of Mechanical engineering Boston university, 2012
[9] http://ocw.mit.edu/courses/aeronautics-and-astronautics/16-30-feedback-control-systems-fall-2010/lecture-notes/
[10] http://ctms.engin.umich.edu/CTMS/index.php?example=InvertedPendulum§ion=ControlStateSpace
8/12/2019 Control of an Inverted Pendulum System-Libre
25/34
Appendix A: Matlab Scripts and Function Files
File-1: InvertedPendulum.m
- This file has all the steps of developing the state feedback controller, full order state observer and the steady stateerror controller. Its outputs are shown in graphs launched automatically when you run it.
%{==================================================================== File:InvertedPendulum.mProject: Controlling an inverted pendulumMethod: state space model and state feedback
By: TSEGAZEAB SHISHAYE (ID:2012420012), July-2013,NWPU, Xi'an-China=================================================================== %}
%--------System Modeling-------%
% Defining variables
clc;clear;
M=0.5;m=0.5;b=0.1;l=.3;I=.006;g=9.8;d=I*(M+m)+M*m*l^2;
% State-space model
disp('====================================' )disp('state space model of the system is:')disp('====================================' )
A=[0 1 0 0;m*g*l(M+m)/d 0 0 m*l*b/d;0 0 0 1;-g*m^2*l^2/d 0 0 -b*(I+m*l^2)/d];B=[0;-m*l/d;0;(I+m*l^2)/d];
C=[0 0 1 0;1 0 0 0];D=[0;0];
system=ss(A,B,C,D)
% Transfer function of the system
disp('=======================================' )disp('The transfer function of the system is:')disp('=======================================' )
inputs = {'u'};outputs = {'x'; 'tetha'};G=tf(system)set(G,'InputName',inputs)set(G,'OutputName',outputs)
%--------System Analysis-------%
%------Checking stability of the system--------%
%open-Loop impulse response
inputs = {'u'};outputs = {'x'; 'theta'};set(G,'InputName',inputs)set(G,'OutputName',outputs)
figure(1);clf;subplot(221)t=0:0.01:1;
8/12/2019 Control of an Inverted Pendulum System-Libre
26/34
impulse(G,t);grid;title('Open-Loop Impulse Response of the system')
%Open-Loop step response
subplot(222)t = 0:0.05:10;u = ones(size(t));[y,t] = lsim(G,u,t);plot(t,y)title('Open-Loop Step Response of the system')axis([0 2 -50 50])legend('x','theta')grid;
% Poles of the system and pole-zero mapping
disp('===========================')disp('Poles of the system are:')disp('===========================')
Poles=eig(A)subplot(2,2,3:4)pzmap(system);title('Pole-zero mapping of inverted pendulum')
% Checking controllability
disp('=========================')disp('checking controllability:')disp('=========================')CO=ctrb(system);Rank_CO=rank(CO)unco=length(A)-rank(CO)
if(unco==0)disp('system is controllable!')
elsedisp(['number of uncontrollable states are:',unco])
end;
% Checking Obserevability
disp('=======================')disp('Checking observability:')disp('=======================')OB=obsv(system);Rank_OB=rank(OB)unobsv=length(A)-rank(OB)if(unobsv==0)
disp('system is observable!')else
disp(['number of unobservable states are:',unobsv])end;
%--------Designing a closed loop controller using...%...State feed-back control method-------%
%-----(1)-pole placement methode-------%
disp('================================================' )disp('desirable poles and the respective gain vectors:')disp('================================================' )
% First test....taking to 2 and 3 times of the real part of the dominant% poles to the left
desirable_1=[-2.433 -1.622 -0.811+1.584j -0.811-1.584j]K1=place(A,B,desirable_1)Ac1 = A-B*K1;system_c1 = ss(Ac1,B,C,D);
8/12/2019 Control of an Inverted Pendulum System-Libre
27/34
t = 0:0.01:10;r =0.2*ones(size(t));figure(2);clf;subplot(311)[y,t,x]=lsim(system_c1,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pend-angle(rad)')title('Step Response using pole placement-1st test')grid
% Second test.....taking to 5 and 10 times of the real part of dominant% poles to the left
desirable_2=[-8.11 -4.055 -0.811+1.584j -0.811-1.584j]K2=place(A,B,desirable_2)Ac2 = A-B*K2;system_c2 = ss(Ac2,B,C,D);
t = 0:0.01:10;r =0.2*ones(size(t));subplot(312)[y,t,x]=lsim(system_c2,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pend-angle(rad)')title('Step Response using pole placement-2nd test')grid
% Third test....taking to 12 and 14 times of the real part of the dominant% poles to the left
desirable_3=[-11.354 -9.732 -0.811+1.584j -0.811-1.584j]K3=place(A,B,desirable_3)Ac3 = A-B*K3;system_c3 = ss(Ac3,B,C,D);t = 0:0.01:10;r =0.2*ones(size(t));subplot(313)[y,t,x]=lsim(system_c3,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pend-angle(rad)')title('Step Response using pole placement-3rd test')grid
% controlling using LQR Method%-----------------------------
disp('===============================================' )disp('The weight matrices Q and R and the vector K:')disp('===============================================' )
Q = C'*C;Q(1,1) = 80; %increasing the weight on the pendulum's angle(tetha)Q(3,3) = 400 % increasing the weight on the cart's position(x)R = 1K = lqr(A,B,Q,R) %state-feedback control gain matrixAc = A-B*K; %constrol matrix
system_c = ss(Ac,B,C,D); %the controlled system state space modelt = 0:0.01:10;r =0.2*ones(size(t));figure(3);clf[y,t,x]=lsim(system_c,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)')title('Step Response using LQR')grid
% [LQR + steady state error controller]%--------------------------------------
8/12/2019 Control of an Inverted Pendulum System-Libre
28/34
Cn = [0 0 1 0]; % Modifiying C matrix for y=xNbar=-inv(Cn*((A-B*K)\B)); %calculating gain scaling factorsys_lqr_et = ss(Ac,B*Nbar,C,D);
t = 0:0.01:10;r =0.2*ones(size(t));figure(4);clf[y,t,x]=lsim(sys_lqr_et,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)')title('Step Response using LQR and steady state error controller')grid
%Designing state observer (state estimator)%------------------------------------------
ob = obsv(system_c); % first checking observability of the controllerobservability = rank(ob)contr_poles = eig(Ac) %finding Controller poles
obsr_poles = [-30 -31 -32 -33] % 10 times faster than the controllerL = place(A',C',obsr_poles)' % placing observer poles
%response of the system with controller + observer = compensator%---------------------------------------------------------------
Aco = [(A-B*K) (B*K);zeros(size(A)) (A-L*C)];Bco = [B;zeros(size(B))];Cco = [C zeros(size(C))];Dco = [0;0];
sys_co_ob = ss(Aco,Bco,Cco,Dco);
t = 0:0.01:10;r = 0.2*ones(size(t));
figure(5);clf[y,t,x]=lsim(sys_co_ob,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');
set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)')title('Step Response using compensator (controller + observer)')grid
% the new system with Compensator + no steady state error tracker%----------------------------------------------------------------
Acl = [(A-B*K) (B*K);zeros(size(A)) (A-L*C)];Bcl = [B;zeros(size(B))];Ccl = [C zeros(size(C))];Dcl = [0;0];Ccln=[Cn zeros(size(Cn))];%reference step command applied to x onlyNbar=-inv(Ccln*(Acl\Bcl))
Bclt = [B*Nbar;zeros(size(B))]; %modifing Bcl for the error traking purpose
sys_cm_et = ss(Acl,Bclt,Ccl,Dcl);
t = 0:0.01:10;r = 0.2*ones(size(t));
figure(6);clf[y,t,x]=lsim(sys_cm_et,r,t);[AX,H1,H2] = plotyy(t,y(:,1),t,y(:,2),'plot');set(get(AX(1),'Ylabel'),'String','cart position (m)')set(get(AX(2),'Ylabel'),'String','pendulum angle (radians)')title('Step Response using compensator with steady state error controller')grid
8/12/2019 Control of an Inverted Pendulum System-Libre
29/34
8/12/2019 Control of an Inverted Pendulum System-Libre
30/34
globalpendanglglobalTglobalNbarglobalstepval
% Nbar data stored in the Run.UserData
A = [0 1 0 0;51.58 0 0 0.5263;0 0 0 1;-7.737 0 0 -0.1789];
B = [0; -5.263; 0; 1.789];
C = [0 0 1 0;1 0 0 0];D = [0;0];Cn = [0 0 1 0]; %C matrix when Y=x, or step command only applied to x
% Get the weighing factors from the editable text fields of the GUI% x=for pendulum angle, y=for cart position
x=str2num(get(handles.xtext,'string'));y=str2num(get(handles.ytext,'String'));
Q=[x 0 0 0;0 0 0 0;0 0 y 0;0 0 0 0];
R = 1;
%Finding the state feedback gain K matrix with the lqr command
disp('state feedback gain vector is:')K = lqr(A,B,Q,R)
%The resulting LQR controller matricesAc = [(A-B*K)];Bc = [B];Cc = [C];Dc = [D];
%finding Controller poles
disp('controller poles are:')cp= eig(Ac);
% making the observer poles 10 times faster than the...%real part of the slowest pole of the controller
disp('observer poles are:')obsr_poles = [real(min(cp))*10 real(min(cp))*10-1
real(min(cp))*10-2 real(min(cp))*10-3];
% placing observer poles
disp('gain vector for the output feedback to the observer is:')L = place(A',C',obsr_poles)'
%Compensator(controller + observer) matrices
Acl = [(A-B*K) (B*K);zeros(size(A)) (A-L*C)];Bcl = [B;zeros(size(B))];Ccl = [C zeros(size(C))];Dcl = [0];
%Modifing Ccl because reference step command applied to x only ==> y=x
Ccln=[Cn zeros(size(Cn))];
%there should be some modification in the input signal for good tracking performance. that means...%there should be extra gain used to scale the closed loop transfer function to make the steady state steperror=0.
Nbarval = get(handles.reference,'Value');ifNbarval == 0Nbar = 1;set(handles.Run,'UserData',Nbar);stepaxis=stepval/1000;
elseifNbarval == 1
8/12/2019 Control of an Inverted Pendulum System-Libre
31/34
%calculating the scale factor for the overall closed-loop system
Nbar=-inv(Ccln*(Acl\Bcl))set(handles.Run,'UserData',Nbar);
end
%Get the value of the step input from the step slider
stepval=get(handles.stepslider, 'Value');
T=0:0.1:6;U=stepval*ones(size(T));
%modifing Bcl for the steady state error traking purpose
Bclt = [B*Nbar;zeros(size(B))];[Y,X]=lsim(Acl,Bclt,Ccl,Dcl,U,T);cartpos=X(:,3);pendangl=X(:,1);
%Pendulum and cart data
cart_length=0.3;cl2=cart_length/2;
ltime=length(cartpos);
cartl=cartpos-cl2;cartr=cartpos+cl2;
pendang=-pendangl;pendl=0.6;
pendx=pendl*sin(pendang)+cartpos;pendy=pendl*cos(pendang)+0.03;
axes(handles.axes1)plot(T(1),cartpos(1), 'r', 'EraseMode', 'none')plot(T(1),pendangl(1), 'b', 'EraseMode', 'none')
%Set the axis for the step response plot
axes(handles.axes1)ifstepval > 0axis([0 6 -stepval/2 stepval*2])
elseifstepval < 0
axis([0 6 stepval*2 -stepval/2])elseaxis([0 6 -0.5 0.5])
end
title(sprintf('Step Response to %0.4f cm input',stepval))xlabel('Time (sec)')
hold on
%Plot the first frame of the animation
axes(handles.axes2)claL = plot([cartpos(1) pendx(1)], [0.03 pendy(1)], 'b', 'EraseMode', ...'xor','LineWidth',[7]);hold on
J = plot([cartl(1) cartr(1)], [0 0], 'r', 'EraseMode', ...'xor','LineWidth',[20]);
axis([-.7 0.7 -0.1 0.7])title('Animation of Inverted pendulum')xlabel('X Position (m)')ylabel('Y Position (m)')
%Check if the animation is to be advanced manually
manual=get(handles.manualbox,'Value');
%Run the animation
fori = 2:ltime-1,ifmanual == 1
8/12/2019 Control of an Inverted Pendulum System-Libre
32/34
pauseend
set(J,'XData', [cartl(i) cartr(i)]);set(L,'XData', [cartpos(i) pendx(i)]);set(L,'YData', [0.03 pendy(i)]);drawnow;
axes(handles.axes1)plot([T(i),T(i+1)],[cartpos(i),cartpos(i+1)], 'r', 'EraseMode', 'none')plot([T(i),T(i+1)],[pendangl(i),pendangl(i+1)], 'b', 'EraseMode', 'none')
end
%Add legend to step plot
axes(handles.axes1)hold onlegend('Pendulum Angle (rad.)','Cart Position (cm.)')
guidata(hObject, handles);
functionxtext_Callback(hObject, eventdata, handles)% hObject handle to xtext (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of xtext as text% str2double(get(hObject,'String')) returns contents of xtext as a double
% --- Executes during object creation, after setting all properties.functionxtext_CreateFcn(hObject, eventdata, handles)% hObject handle to xtext (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor' ))
set(hObject,'BackgroundColor','white');end
functionytext_Callback(hObject, eventdata, handles)% hObject handle to ytext (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of ytext as text% str2double(get(hObject,'String')) returns contents of ytext as a double
% --- Executes during object creation, after setting all properties.functionytext_CreateFcn(hObject, eventdata, handles)% hObject handle to ytext (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.ifispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor' ))
set(hObject,'BackgroundColor','white');end
% --- Executes on button press in reference.functionreference_Callback(hObject, eventdata, handles)% hObject handle to reference (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hint: get(hObject,'Value') returns toggle state of reference
% --- Executes during object creation, after setting all properties.functionreference_CreateFcn(hObject, eventdata, handles)% hObject handle to reference (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% --- Executes on slider movement.
8/12/2019 Control of an Inverted Pendulum System-Libre
33/34
functionstepslider_Callback(hObject, eventdata, handles)% hObject handle to stepslider (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'Value') returns position of slider% get(hObject,'Min') and get(hObject,'Max') to determine range of slider%Get the value of the step input from the step slider%
stepval=get(hObject,'Value');set(handles.text5,'string',sprintf('%6.4f',stepval));
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.functionstepslider_CreateFcn(hObject, eventdata, handles)% hObject handle to stepslider (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: slider controls usually have a light gray background.ifisequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor' ))
set(hObject,'BackgroundColor',[.9 .9 .9]);end
% --- Executes during object creation, after setting all properties.functiontext5_CreateFcn(hObject, eventdata, handles)% hObject handle to text5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in manualbox.functionmanualbox_Callback(hObject, eventdata, handles)% hObject handle to manualbox (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of manualbox
% --- Executes during object creation, after setting all properties.functionmanualbox_CreateFcn(hObject, eventdata, handles)% hObject handle to manualbox (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in clear.functionclear_Callback(hObject, eventdata, handles)% hObject handle to clear (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
%Callback for the RESET button%axes(handles.axes1)claaxis([0 6 -0.5 0.5])title('Step Response')xlabel('Time (sec)')
axes(handles.axes2)cartpos=0;
cart_length=0.3;cl2=cart_length/2;
cartl=cartpos-cl2;cartr=cartpos+cl2;
pendang=0;pendl=0.6;
pendx=pendl*sin(pendang)+cartpos;pendy=pendl*cos(pendang)+0.03;claK = plot([cartpos(1) pendx(1)], [0.03 pendy(1)], 'b', 'EraseMode', ...'xor','LineWidth',[7]);hold onJ = plot([cartl(1) cartr(1)], [0 0], 'r', 'EraseMode', ...
8/12/2019 Control of an Inverted Pendulum System-Libre
34/34
'xor','LineWidth',[20]);guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.functiontext1_CreateFcn(hObject, eventdata, handles)% hObject handle to text1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
functionaxes1_CreateFcn(hObject, eventdata, handles)% hObject handle to axes1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
% --- Executes during object creation, after setting all properties.functionaxes2_CreateFcn(hObject, eventdata, handles)% hObject handle to axes2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes2
% --- Executes on button press in exit.functionexit_Callback(hObject, eventdata, handles)% hObject handle to exit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)close(inv_pend_ani)