Vous êtes sur la page 1sur 18

’The Inverted Pendulum’

April 20, 2011

Report Integration Project S&C

Abstract
In the report we show how to control an inverted pendulum in a laboratory
setup. First we derive the model, then the unknown parameters are identified.
The the system is analysed and for stability. After that, we decided to design
an LQR and a classical controller. The design procedure is explained and the
results are shown.

Modelling the Pendulum


The pendulum is modelled with the Lagrange equations. The following assumptions
are made:
1. The pendulum is a rigid body, with an infinitely small radius. The cart is also
a rigid mass.
2. No air resistance and friction in the bearing. The only friction considered is
the cart and the back emf.
The schematic setup is give in figure 1. The physical parameters are given in table
1. First we derived the dynamics of the mechanical system� before� we model the
d
motor. The system has two degrees of freedom i.e. q = and the cart is
α

Figure 1: Schematic drawing of the inverted pendulum

1
Symbol Parameter Value
l half length of the pendulum 0.30m
m mass of the pendulum 0.085kg or 0.210kg
1
J inertia of the pendulum 3 ml
2

M mass of the cart 0.49kg

Table 1: Physical parameters and their values

driven by force F . The cart has friction b� . The coordinates of the center of mass
of the pendulum are given by:

x = d + l sin α
y = l cos α

and their derivatives with respect to time are:

ẋ = d˙ + α̇l cos α
ẏ = −α̇l sin α

Now the kinetic energy of the cart and the pendulum are given by:
1 ˙2 ˙
Tcart = M d − b1 dd
2
1 1
Tpendulum = m(ẋ2 + ẏ 2 + l2 α̇)
2 3

T = Tcart + Tpendulum

And the potential energy:


V = mgy
And the dissipation because of friction:
1 � ˙2
D= bd
2
Then the Lagrange equation is used and rewritten to get the equations of motion
of the pendulum:
� �
d ∂T ∂T ∂V ∂D F
( )− + + =
dt ∂ q̇ ∂q ∂q ∂ q̇ 0
¨ ˙ ˙2
d(M + m) + b1 d + α̈ml cos α − mlα sin α = F
1 ¨ cos α − mgl sin α
α̈( ml2 + ml2 ) + dml = 0
3
� �� �
J

If the controller works, we can assume that α is small, since that’s the goal we want
to achieve. Physically α is always small, since it the pendulum is saturated so that

2
we can never reach large values. Here we think we have found good arguments for
linearization. α is small means sin α ≈ α and cos α ≈ 1 and α˙2 = 0 . Then the
linearised equations of motion are
¨
d(M + m) + b1 d˙ + α̈ml = F
1 ¨ − mglα
α̈( ml2 + ml2 ) + dml = 0
3
Now we have to derive the dynamics of the DC motor. Here we give the equation
for current i to motor angle θ and voltage V:
di
Ri + L = V − b2 d˙
dt
Because the inductance for this type of motor is very small we ignore it by setting
L = 0. Furthermore, b2 consists of the radius of the wheel to go from θ → d
and the electrical motor constant. It is irrelevant for the modelling to distinguish
those because we identify them together later. Secondly, the torque of the motor is
proportional to the current by the motor constant and the force is proportional to
the force. We take these also together as C because it is irrelevant to distinguish
them.
1 ˙
i = (V − b2 d)
R
C ˙
F = Ci = (V − b2 d)
R
This model is linear and plugged into the the linearized pendulum model:

¨ C
d(M + m) + b1 d˙ + α̈ml = ˙
(V − b2 d)
R
¨ C C
d(M + m) + (b1 + b2 )d˙ + α̈ml = V
� ��R � R
����
b ku

Then we have:
¨
d(M + m) + bd˙ + α̈ml = ku
1 ¨ − mglα
α̈( ml2 + ml2 ) + dml = 0
3
Now we have the linearized model from normalized input u to the position d and
angle α. Two parameters are still unknown i.e. k and b. We will identify them in
the next paragraph using optimization. In the EOM we can see that α̈ depends on d¨
and vice verse. This is an algebraic loop. We there for first rewrite the equation so
that we can put them in the form of ẋ = Ax + Bu. We use the matlab symbolic

3
toolbox to prevent mistakes. The state space description of the model is then:
    
0 0 1 0 d 0
 0 0 0 1  α   0 
   
ẋ =  gm2 l2
− q − (J+ml2 )b  ˙   +  (J+ml 2
) u
 0 q 0  d  q 
g(M +m)ml
0 q
bml
q 0 α̇ − mlk
q
� � � �
1 0 0 0 0
y = x+ u
0 1 0 0 0
q = J(M + m) + M ml2

Secondly, for other types of design, it is more use full to have the transfer function
of the model. First we obtain the Laplace transform:

D(s)s2 (M + m) + bD(s)s + A(s)s2 ml = kU (s)


1
A(s)s2 ( ml2 + ml2 ) + D(s)s2 ml − mglA(s) = 0
3
Then we can we can rewrite the system as an MIMO with one input and two outputs
� � � 2

D(s) 1 − (J+ml
p
)k 2
s + kmlg
p
= kml 2
U (s)
A(s) s4 − b(J+ml
2)
s3 + (M +m)gml s2 + bgml p s
p p p s
� �

= U (s)
Gd

with p = (ml)2 − (M + m)(J + ml2 ).

System Identification
Now the parameters of k and b are still unknown. To identify we design the experi-
ment without the pendulum, which means setting m = 0. The equation of motion
of the cart then becomes:
¨ + bd˙ = ku
dM
We remove the pendulum because the system is otherwise highly unstable. This
reduced system is enough to identify the two unknowns. We subject the pendulum
to a chirp signal and use optimization techniques to estimate the parameters. The
signal covers frequencies 21 Hz to 10Hz during the period of 10 seconds. We choose
100Hz as a sampling frequency. We use lsqnonlin in matlab to minimize the
square
� of �the error function ε = ds − d(x) where x is the vector of parameters:
b
x= . Here ds is the measured response to the chirp signal and d(x) is the
k
output of the transfer function without pendulum: D(s) k
U (s) = M s2 +bs .
The matlab code for this optimization algorithm is found in the appendix. From
the project description of SC4070 we find the values for k and b and use them as an

4
Figure 2: Measured(green) and modelled(blue) response to the chirp signal

� �
5.7221
initial condition to prevent finding a local minimum. We find x = . In
3.4869
figure 2 the measured(green) and the modelled(blue) response are depicted. We
see that the signal is followed pretty well, but that there is drift. The reason could
be that the ’road’ is not entirely straight. But since this dynamics are pretty slow
compared to the others, we consider it as�a disturbance.
� A job for the controller.
0
The eigenvalues of the cart dynamics are . Then, according to [2], the
−11.68
sampling frequencyshould be ωs = 10 ∗ smallestEV and the experiment duration
1
should be 10 largestEV . Both are not satisfied. However, the sampling frequency
is practically of the same order, and we are already on the limit of the controller.
Faster wouldn’t be possible. Secondly, the largest eigenvalue is 0 hence marginally
stable. It would ask for an infinitely long experiment duration. Also, subjecting the
system to a impulse response, we find a rise time of around 0.1s. Because the best
sampling frequency is to divide the rise time in 4 − 10 samples we also evidence for
100Hz. Hence we conclude that our experiment was set-up satisfactory.

Analysis
Now we have all parameters determined, we can analyse the linearised system. The
poles of the system are:  
0
 5.1252 
p=  −4.6438 

−16.2945

5
The system has no transmission zeros. It has one RHP pole and therefore the
system is OL unstable. This makes closed loop design by bode plots rather com-
plicated. With the matlab commands ctrb(a,b) and obsv(a,c) we check the
controllability and observability of the system. The both yield matrices with full
row and column rank respectively. This gives us a good basis to design a controller
with full state feedback. This will be the first controller we design. Secondly, we
found evidence on the web that it is possible to control the system with an PID
controller. This will be the second controller we design.

Objectives
Before designing the controllers we will first define our objectives. The basis for this
objectives is found in the literature[4].
• The pendulum must be stable against disturbances

• Settling time for d of less than 2 and α of less than 0.5 seconds.
• Rise time for d of less than 0.5 seconds.
• Overshoot of α less than 0.05 radians(0.2 is the limit)

LQR and Observer Design


The only state measurable are the supplied power, the position and the angle of
the pendulum. The configuration of the controller K and observer L is depicted in
figure 3 . First we design the controller. The regulator is designed by finding a u
such that the cost function
ˆ ∞
J= xT Qx + uT Rudt
0

is minimized. Here Q is the weight on the state and R the weight on the input.
We choose Q initially as C T C and R as 1 because the system just has one input.
Then we check the step performances and if necessary increase the weight on the
state or input until the objectives are reached. . Pretty nice results are obtained for
Q = diag(100, 1, 0, 0) and R = 1.
But before we can apply this controller we have to do something else. For
control, it would be necessary to know what are the speeds and the angular speed
of the pendulum. Just differentiating the angle and the position would just amplify
the noise, hence we use a trick. We design an observer which makes the error
e = x − x̂ go to zero asymptotically. The dynamics of the observer are:

x̂˙ = Ax̂ + Bu + L(y − ŷ)

6
Figure 3: The State Feedback control configuration

Figure 4: Step responses of the angle and position for two different weight matri-
ces(Theoretical)

7
Figure 5: Measured results and reconstructed state of the LQR controlled system

Then we design L in the following way[4]:

e = x − x̂ → 0
ė = ẋ − x̂˙ = Ax + Bu − Ax̂ − Bu − L(y − ŷ)
ė = Ae − LCe
ė = (A − LC)e

The error e will go to zero if the matrix A − LC has only LHP poles. A rule thumb
say that the poles of the observer need to be 4-10 times faster as the fastest pole of
the matrix A − BK. To find the best we play a little on the real system. We want
the observer as fast as possible but not amplify the noise. The best result is found
at 5 times the fastest pole. Now with the matlab command place we place the
poles. Not all poles can be placed in the same place. Hence we add a small amount
to have the poles in slightly different positions. In practice the controller works very
well. Also the observer does a good job. Figure 5 depicts the measured results.

Classical Design
The second controller design we try is by a classical control design procedure. The
system is characterized by two outputs (d and α) and one input. The angle is

8
Figure 6: Block diagram of the cascaded PID controlled system

significantly faster than the distance. This gives us good reasons for cascade control.
In figure 6 , the block diagram of our controller is shown. The black blocks is the
fast circle in the cascade for the angle control. Later we we denote this part by
the transfer function H(s). First, we design the angle controller Cα (s) by adding
poles and zeros and using the nyquist criterion. This is obtained as follows. First
of all, the problem is the RHP pole. Using the Nyquist criterion, we need an
anticlockwise encirclement of the −1 point to have closed loop stability. Note that
the numerator of Gα is negative, so the sign of the controller should be negative to
use the controller with negative feedback.
The nyquist plot gives not so much insight hence we try also rootlocus. First of
all, we try to create two branches of the RHP pole. We archieve this with a pure
integrator. The next step is to draw the branches to the LHP. This is archieved
with a zero between the two most left poles of Gα . With adjusting the gain we
get stable behaviour. But the phase margin is still pretty small, so we add another
zero close to the other one. As a last step, because simulink can only handle proper
transfer functions, we need to add a pole. To give it minor influence, we place it
far left. After a little bit of trial and error we get:
(s + 6.8)(s + 5.8)
Cα = −50
s(s + 50)
and resulting nyquist plot is given in figure 7 . We have a phase margin of 60deg.
which provides us a good damping. The gain margin is infinite. If we look to the
closed loop behaviour, e.g. the sensitivity, we have a peak 1.7dB which satisfies
Skogestad et al. who demands MS < 2 and the bandwidth of ωB = 1rad/s and
this gives us a good disturbance rejection.
Now the angle is stabilized in upright position against disturbances in the input
signal theory. The results for the stable pendulum in the real system are shown in
figure 8 . It is stable, but, because the position is uncontrolled, it walks away. We
need a second controller who can do the job.
Now we can use this input signal to control the position of the cart. The cart

9
Figure 7: Nyquist plot of Gα Cα

10
Figure 8: angle control (no position control)

11
position is calculated by: d = Gd u. The output d is the one we want to control at
the reference position e.g. 0. Our control input is v and the transfer function from
v to d is:
d
H(s) =
v
d = Gd u
Cα (s)
u = Cα (s)(v − Gα (s)u) = v
1 + Cα (s)Gα (s)
Gd (s)Cα (s)
d = v = H(s)v
1 + C α Gα
It is important to cancel the equal poles and zeros which have origin in the same
block. Otherwise, designing a controller is impossible. We can’t cancel RHP poles
in the controller by the same zeros in the controller. This would lead to internal
instability. First we take therefore a closer look to the transfer function H(s). The
see one RHP zeros and one pole at the origin. This transfer function is therefore
marginally stable in open loop. This explains the ’walking away’ behaviour in the
open-loop. Hence we need one anticlockwise encirclement in the nyquist plot. The
placement of the poles and zeros of the controller is a job which is quite precairious.
Hence we use the sisotool of matlab to start with a good guess. Then we start
shaping until we have the desired behaviour. The first guess is a PID controller with
the following values:
0.42s2 + 0.58s + 1
Cd = −8.2
s
This controller is not proper, hence we add a pole. We place the pole at −100 to
make it’s influence small. The closed loop is now stable with open loop specifica-
tions: G.M. = 4.98, P.M.28.6 and the closed loop complementary sensitivity has
values ωT = 10rad/s and MT = 8dB. This value is a little to high, and we try to
move the poles to get a lower peak value and improve gain and phase margin, in
order to improve performance. After moving the poles to the real axis we get closer
to the desired behaviour. Because the order of the system now is reduced, we can
remove the pole at −100. The controller now is
1 + 2.4s
Cd = −1.2
s
and has G.M. = 9.25, P.M. = 41.3 and ωT = 2rad/s and MT = 3dB. This is all
shown in figure 9 . Implementation on the real system shows satisfactory behaviour,
depicted in figure 10 .

Conclusion
As wel with LQR as classical control, it has been possible to design controllers
which met the design requirements fairly well. The only is the position, which keeps
oscillating around the set equilibrium.

12
Figure 9: SISO Tool design results

matlab code
Parameter estimation by optimization
%%%%%%%%%%%%%%

% optimization

clc;clear;

load measurement

lb=[];ub=[];

x0=[8 5];

opt=optimset(’LargeScale’,’off’);

opt=optimset(opt,’algorithm’,’Levenberg-Marquardt’);

opt=optimset(opt,’Jacobian’,’off’,’DerivativeCheck’,’on’);

opt=optimset(opt,’LineSearchTyp’,’cubicpoly’,’Diagnostics’,’on’);

[x,resnorm]=lsqnonlin(@fun,x0,lb,ub,opt)

13
Figure 10: Classical control with position control

14
b=x(1);km=x(2);M=0.49;

G=ss([0 1;0 -b/M],[0;km/M],[1 0],[0]);

Y=lsim(G,u(:,2),u(:,1),[ds(1,2) 0]);

plot(u(:,1),[Y,ds(:,2)]);

function [N]=fun(x)

load measurement.mat

b=x(1);km=x(2);M=0.49;

G=ss([0 1;0 -b/M],[0;km/M],[1 0],[0]);

d=lsim(G,u(:,2),u(:,1),[ds(1,2) 0]);

N=(ds(:,2)-d);

State space design


%%%%%%%%%%%%%%%

% State Space Control

%%%%%%%%%%%%%%%

clc;clear;

% Given variabels

l=0.30;

m=0.210;

J=m*l*l/3;

M=0.49;

g=9.81;

h=0.01;hwinit;

% Estimated variabels

b=5.7221;

km=3.4869;%0.3018;

% State space model, x=[d a dd ad]’

15
q=J*(M+m)+M*m*l*l;
A=[0 0 1 0;
0 0 0 1;
0 -g*m*m*l*l/q -(J+m*l*l)*b/q 0;
0 (M+m)*g*m*l/q b*l*m/q 0 ];
B=[0;
0;
(J+m*l*l)*km/q;
-m*l*km/q];
C=[1 0 0 0;
0 1 0 0];
D=[0;0];
sys=ss(A,B,C,D);
%% LQR design-Optimal Controller
% LQR Controller
Q=[100 0 0 0;
0 1 0 0;
0 0 0 0;
0 0 0 0];
R=1;
K=lqr(A,B,Q,R)
Ac=[(A-B*K)];Bc=B;Cc=C;Dc=D; % Closed loop
% Design of the observer(4-10 times the largest pole)
q=-5*min(real(eig(Ac)));
L=place(A’,C’,[-q -q+1 -q+2 -q+3]);
sysc=ss(Ac,Bc,Cc,Dc);
dt=0.01;T=10;
t=0:dt:T;input=ones(1,T/dt+1);
[Y,T,X]=lsim(sysc,input,t,[0,0,0,0]);
plot(T,[(C*X’)’])
legend(’position’,’angle’)
title(’Q=diag([100 1 0 0]’)

16
PID Controller
clc;clear;

h=0.01;hwinit;

l=0.30;

m=0.210;

J=m*l*l/3;

M=0.49;

g=9.81;

b=5.7221;

km=3.4869;

q=(m*l)^2-(M+m)*(J+m*l*l);

den=[1 -b*(J+m*l*l)/q (M+m)*g*m*l/q b*g*m*l/q 0];

numa=[km*m*l/q 0 0];

numd=[-(J+m*l*l) 0 m*l*g]*km/q;

Ga=tf(numa,den);

Gd=tf(numd,den);

%% Angle controller

z1=5.8;p1=0;z2=6.8;p2=50;

Ca=-50*tf([1 z1],[1 p1])*tf([1 z2],[1 p2]);

sysa=feedback(Ga*Ca,1);%

%nyquist(Ga*Ca)

%% Design the controlled angle matrix

H=Gd*inv(1+Ga*Ca);

H=tf(minreal(ss(H)));

%% Position controller

sisotool(H)

17
References
[1] Michel Verhaegen and Vincent Verdult, Filtering and System Identification,
Cambridge University Press 2007
[2] Sigurd Skogestadt and Ian Postlethwaite, Multivariable Feedback Control, Wiley
2005
[3] http://www.engin.umich.edu/group/ctm/examples/pend/invpen.html

[4] Bernard Friedland, Control System Design, McGraw-Hill 1986

18

Vous aimerez peut-être aussi