Linear Control of Inverted Pendulum
Linear Control of Inverted Pendulum
Linear Control of Inverted Pendulum
1 Inverted pendulum
334 M. LANDRY, S. CAMPBELL, K. MORRIS, AND C. AGUILAR
l j
m i
θ l
P g = -g j
F(t) i M
x
O x(t)
y = (y1 , y2 , y3 , y4 )T
(1.4)
= (x, θ, ẋ, θ̇)T .
This is a coupled system
mlθ̇2 sin θ − k ẋ + F
M + m ml cos θ ẍ
=
ml cos θ I + ml2 θ̈ mgl sin θ − cθ̇
Define determinant
D = (M + m)(I + ml2 ) − m2 l2 cos2 θ
Solving for ẍ, θ̈
Define
z = (z1 , z2 , z3 , z4 )> = (x, ẋ, θ, θ̇)>
Then
ż1 = z2 , ż3 = z4
we can write the first order system
ż1 z2
ż2 1 [ml cos z3 (cz4 − mgl sin z3 ) + (I + ml2 )(F − kz2 + mlz42 sin z3 )]
= D
ż3 z4
1 2
ż4 D
[(M + m)(−cz4 + mgl sin z3 ) − ml cos z3 (F − kz2 + mlz4 sin z3 )]
In the experimental setup of Landry et al. [1], the force F on the cart is
where u is the voltage in the motor driving the cart, and the second term represents the
electrical resistance in the motor. Let us write the non-linear pendulum model as
dz
= N (z, u)
dt
1.1 Excercises
The matlab programs are in the directory pendulum; go into this directory and start
matlab. E.g. if you are working on the Unix/Linux command line
$ cd control2013 / pendulum
$ matlab
If you have started matlab through some other way, then change the directory inside
matlab
>> cd <Full path to>/control2013 / pendulum
2
Or use the matlab gui to change the working directory.
The solution of the non-linear pendulum is implemented in nlp.m file and ths right
hand side function without any control (F = 0) is implemented in rhs nlp.m file. Study
these two programs.
The pendulum has two equilibrium positions, one upright (θ = 0) and another in the
down position (θ = π).
1. Using nlp.m solve the non-linear pendulum problem with initial condition close to
upright position
5π
z(0) = 0, 0, 180 ,0
and final time T = 100. Is the upright position stable ? What happens to the four
variables ? What is the time asymptotic value of x, ẋ, θ, θ̇ ?
2. Solve the non-linear pendulum problem with initial condition close to downward
position
z(0) = 0, 0, 170π
180
,0
and final time T = 100. Is the downward position stable ?
3. Now set the value of friction coefficients k and c to zero in parameters.m file and
run nlp.m again. How does the solution behave qualitatively ? Is it converging to
a stationary state ?
Note Set the value of k and c back to their initial non-zero values.
where
M +m I + ml2
v1 = , v2 =
I(M + m) + mM l2 I(M + m) + mM l2
then we have the linear model
dz
= Az + Bu
dt
3
1.3 Excercises
1. Generate matrices for linear system
>> parameters
>> [ A , B ] = get_system_matrices ( )
2. Compute eigenvalues
>> e = eig ( A )
>> plot ( real ( e ) , imag ( e ) , 'o' )
Pc = B AB A2 B A3 B
4. Check the controllability using Hautus criterion. Compute all the eigenvectors and
eigenvalues of A> . For each eigenvalue λ and eigenvector V of A>
A> V = λV
check if
B > V 6= 0
If the above condition is true for each unstable eigenvalue, i.e., with real(λ) ≥ 0,
then the system is stabilizable. Is the linear pendulum stabilizable ?
5. Solve linear pendum model (see lp.m and rhs lp.m) with initial condition
5π
z(0) = 0, 0, 180 ,0
upto a final time of T = 5. Compare this solution with solution of non-linear model.
4
>> [ K , X , E ] = lqr ( A , B , Q , R ) ;
Here K is the feedback gain and can be computed as
K = B>X
For inverted pendulum, matrix A has a zero eigenvalue. We replace A with A+ωI, ω > 0,
for determining the control.
3.1 Excercises
1. Compute the minimal norm feedback matrix K using lqr function
>> Q = zeros ( 4 , 4 ) ;
>> R = 1;
>> om = 0 . 0 1 ;
>> As = A + om ∗ eye ( 4 ) ;
>> [ K , X , E ] = lqr ( As , B , Q , R ) ;
Compute feedback gain from solution X of Riccati equation and check that it is same
as K given by lqr function
>> K1 = B ' ∗ X
>> K−K1 % This s h o u l d g i v e you z e r o matrix
5
Observe that unstable eigenvalues of As have been reflected symmetrically about
the imaginary axis.
2. Also check that A − BK is stable by computing its eigenvalues.
>> e1 = eig ( A ) ;
>> e2 = eig ( A−B∗K ) ;
>> plot ( real ( e1 ) , imag ( e1 ) , 'o' , real ( e2 ) , imag ( e2 ) , '*' )
4.1 Excercises
1. We may measure different output quantities, e.g.,
1 0 0 0
1 0 0 0 0 1 0 0
C= 1 0 0 0 , C= 0 0 1 0 , C= , C=
0 0 1 0 0 0 1 0
0 0 0 1
These correspond to controlling (x), (θ), (x, θ) and (x, ẋ, θ, θ̇) respectively. In each
case, check if (A, C) is observable by computing rank of observability matrix
C
CA
Po =
CA2
Is rank(Po ) = 4 or is rank(Po ) < 4 ?
CA3
6
For which C is the rank condition satisfied ? Which of the four state variables is
important ?
2. Compute feedback operator K using lqr function. We will control position and
angle.
>> C = [1 0 0 0; 0 0 1 0 ] ;
>> Q = C '∗C ;
>> R = 1;
>> [ K , X , E ] = lqr ( A , B , Q , R ) ;
>> plot ( real ( E ) , imag ( E ) , 'o' )
Is A − BK stable ?
3. Solve with LQR feedback control. This is implemented in program pend lqr.m
which solves both linear and non-linear model. Compare the linear and non-linear
solutions. Try with R = 0.01 and R = 0.1 and initial condition
z(0) = 0 0 50π
180
0
and for R = 0.01 and R = 0.1. Which value of R gives faster stabilization ? What
is the maximum magnitude of control in each case ?
5. If the initial angle θ0 is too large, then it may not be possible to stabilize the non-
linear pendulum. The linear pendulum is always stabilized. Find angle beyond
which non-linear model cannot be stabilized. Do this for R = 0.01, R = 0.1 and
R = 0.5. Use final time of T = 3 and initial conditions of the form
z(0) = 0 0 θ0 0
Use the evolution of energy as an indicator for finding the threshold angle. If the
energy starts to increase monotonically, then conclude that it is not possible to
stabilize the system.
7
5 Linear system with noise and partial information
Consider the system with noise in the model and initial condition
dz
= Az + Bu + w, z(0) = z0 + η
dt
where w and η are error/noise terms. We may not have access to the full state but only
some partial information which is also corrupted by noise.
1 0 0 0 x
yo = Hz + v, e.g. H= =⇒ yo = +v
0 0 1 0 θ
which corresponds to observing the position of cart x and the position of pendulum θ
only.
Since we have partial information, we have to estimate to estimate the remaining state
variables somehow.
y n = Hz n + v n
y1n
1 (y1n − y1n−1 )
un = −K ∆t
y2n
1 n−1
(y n − y2 )
∆t 2
z n+1 − z n
= Az n+1 + Bun + wn
∆t
This is implemented in lp noest.m.
5.2 Excercise
Run the program lp noest.m and observe that the state and control are very noisy and
hence this approach is not of practical utility.
8
5.3 Estimation problem
Linear estimator
dze
= Aze + Bu + L(yo − Hze )
dt
We determine the filtering gain L by minimizing
1 ∞ 1 ∞ > −1
Z Z
> −1
J= (yo − Hze ) Rv (yo − Hze )dt + w Rw wdt
2 0 2 0
The best L is given by
L = Xe H > Rv−1 (2)
where Xe is solution of algebraic Riccati equation
A with A>
B with H >
Q with Rw
R with Rv
5.4 Excercises
1. Compute the filtering matrix L
>> Rw = ( 5 e−2) ˆ2 ∗ diag ( [ 1 , 1 , 1 , 1 ] ) ;
>> Rv = ( 5 e−2) ˆ2 ∗ diag ( [ 1 , 1 ] ) ;
>> [ L , Xe ] = lqr ( A ' , H ' , Rw , Rv ) ;
>> L = real ( L ' ) ;
3. Compute L from equation (2) and check that it is same as L obtained in step 1
above.
>> L1 = Xe ∗ H ' / Rv ;
>> L − L1
9
5.5 Linear system with linear estimator
The feedback is based on estimated solution u = −Kze
dz
= Az − BKze + w
dt
dze
= LHz + (A − BK − LH)ze + Lv
dt
or in matrix form
d z A −BK z I 0 w
= +
dt ze LH A − BK − LH ze 0 L v
z(0) = z0 + η, ze (0) = z0
5.6 Excercises
1. Run program lp est.m. Observe that initially z and ze differ but after some time,
ze approches z so that the estimation becomes more accurate. You may have to
zoom the plot near the initial time to see the difference.
Note that all the quantities including the control behave in a more regular manner
now as compared to the finite difference estimation used before.
10
5.8 Excercises
1. Run program nlp est.m. Zoom the plot near initial time and compare the non-
linear solution with the linear estimator. Note that ze approaches z for larger times.
2. Increase the initial angle and check if the feedback can be stabilize the non-linear
system. Have the threshold angles evaluated for R = 0.01, R = 0.1 and R = 0.5
changed? If yes, then what are the new threshold angles?
3. How does the control vary as R is changed? Is the behaviour the same as that
observed in the earlier cases?
4. How does the value of R influence the stabilization of the state variables?
5.9 Excercises
Here we will assume that we do not know the value of the friction coefficient k and c. So
the linear estimator will be constructed taking k = 0 and c = 0 whereas the non-linear
model will include the friction. We want to see if the estimator is still able to give a good
estimate of the real state and if the system is stabilized by the feedback.
11
6 Inverted pendulum: Parameters
M Mass of cart 2.4
m Mass of pendulum 0.23
l Length of pendulum 0.36
k Friction in cart 0.05
c Friction in pendulum 0.005
α Coefficient of voltage 1.7189
β Friction in motor 7.682
a Acceleration due to gravity 9.81
7 List of Programs
1. parameters.m: Set parameters for pendulum
4. rhs nlpc.m: Computes right hand side of non-linear model, with feedback
5. rhs lp.m: Computes right hand side of linear model, without feedback
6. rhs nlp.m: Computes right hand side of non-linear model, without feedback
7. rhs lpc.m: Computes right hand side of linear model, with feedback
8. rhs nlpc.m: Computes right hand side of non-linear model, with feedback
9. rhs nlpest.m: Computes right hand side of non-linear model coupled with the
linear estimator and feedback
12. pend lqr.m: Solves the linear and non-linear models with feedback and full state
information
13. lp est.m: Solves the linear model with noise and partial information, coupled esti-
mator and feedback stabilization
14. lp noest.m: Solves the linear model with noise and partial information, feedback
without estimation
12
15. nlp est.m: Solves the non-linear model with noise and partial information, coupled
estimator and feedback stabilization
References
[1] M. Landry, S. A. Campbell, L. Morris and C. O. Aguilar, “Dynamics of an inverted
pendulum with delayed feedback control”, SIAM J. Applied Dynamical Systems,
Vol. 4, No. 2, pp. 333-351, 2005.
13