Inverted Pendulum Dynamics and Control
Inverted Pendulum Dynamics and Control
Fall 2014
x
θ
L/2
L
cos θ
L / 2 mg 2
F M
θ
About bar having mass ' m '
∑ F= x R=
x m
xm (1)
(a)
mg
L
2
cos θ ∑F y = Ry − mg = mym (2)
Rx L L
Ry ∑ M =R x
2
cos θ + Ry sin θ = Iθ
2
(3)
Ry
Rx
∑F x =F − Rx =M
x (4)
Fig 2.
(a) free-body diagram of rod
(b) free-body diagram of cart
Variable Relation
L
xm= x − sin θ
2
y L
θ ym = cos θ
2
L
sin θ
2
xm L
xm= x − θ cos θ
L
cos θ
2
2 L
y m = − θ sin θ
x 2
x
L L
Fig 3. Relation between two center of gravities
x + θ 2 sin θ − θ cos θ
xm =
2 2
L L
− θ 2 cos θ − θsin θ
ym =
2 2
Equations & Desired State
L L
(1) Rx = x + θ 2 sin θ − θ cos θ
m
2 2
Initial condition
L L
(2) Ry = mg + m − θ 2 cos θ − θsin θ θ ( 0 ) = θ0
2 2
2 x ( 0) = 0
L L L
(3) Iθ = m
x cos θ − m θ + mg sin θ (5)
2 2 2
(4) ( M + m ) x + m
2
(
L 2
)
θ sin θ − θ cos θ =
F (6)
Equations & Desired State
1 L
2
L
=
x I + m θ − mg sin θ
L
m cos θ 2 2
2
put this into (6) and arrange
1 2 L
θ −m ( L / 2 ) sin (θ ) cos (θ ) θ 2 + ( M + m)mg sin (θ ) + m ( L / 2 ) cos (θ ) F
2
(
( M + m) I + m ( L / 2 )
2
)
− m 2 ( L / 2 ) cos 2 (θ )
2
2
1 m 2 ( L / 2 )2 sin (θ ) cos (θ ) g − m ( I + m( L / 2) 2 ) ( L / 2) sin(θ )θ 2 + ( I + m( L / 2) 2 ) F
( )
x
( M + m) I + m ( L / 2 ) − m 2 ( L / 2 ) cos 2 (θ )
2 2
1. Nonlinear System Control
x = θ θ x
T
x
0
ydes =
0 I. 2nd Oscillation Controller θ
= =
II. P Controller Nonlinear y Cx
x
III. PD Controller u System x
( ) m L / 2 sin cos
( M + m) I + m ( L / 2 ) − m 2 ( L / 2 ) cos 2 (θ )
2 2
2
F is a control input u ( t ) , F (t ) = u ( t )
II u ( t ) = F = − Kθ ( t )
III u (t ) =− Kθ ( t ) − Cθ
F=
θ Control : by Force control
( ) m L / 2 sin cos
( M + m) I + m ( L / 2 ) − m 2 ( L / 2 ) cos 2 (θ )
2 2
2
( M + m) L
2 ( M + m) L
=
L
I + m − m cos θ and B = sin θ
Let A
L 2
L
cos θ
2
m cos θ
2 m
2 2
θ =
−2ζωnθ − ωn2θ
L
∴ u ( t ) =F =− 2 A ⋅ ζωn ⋅ θ − A ⋅ ωn2 ⋅ θ − B ⋅ mg + m sin θ ⋅ θ 2
2
With appropriate values of ζ and ωn , θ can be zero at t → ∞
θ Control : by Force control
1 L B 1
From the equation θ =− m θ 2 sin θ + ⋅ mg + F
A 2 A A
We want to control by
II u (t ) =− Kθ ( t ) ( K > 0 )
F=
1 L B K
θ =− m θ 2 sin θ + ⋅ mg − θ
A 2 A A
θ =
1 L
− m θ 2 sin θ +
A 2
B
A
⋅ mg − (
1
A
Kθ + Cθ )
In this case, θ goes to zero when t → ∞ ?
Simulation : Force Control
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m ζ = 0.5 K = 30
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 ωn = 0.7 rad / s C = 15
0 1
-20 0
dx/dt [m]
x [m]
-40 -1
-60 -2
-80
0 10 20 30 40 50 -3
0 10 20 30 40 50
time [sec]
time [sec]
0.1 0.4
0 0
-0.05
-0.2
-0.1
0 10 20 30 40 50 -0.4
0 10 20 30 40 50
time [sec]
time [sec]
Fig 5. Simulation results of nonlinear system for three kinds of control
Simulation : Animation
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m ζ = 0.5 K = 30
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 ωn = 0.7 rad / s C = 15
PD control
0 ㅡ : ζ , ωn control
-2
--- : P control
--- : PD control
-4
0 10 20 30 40 50
time [sec]
Y Equation of motions
x 2
L L L
θ Iθ = m
x cos θ − m θ + mg sin θ
2 2 2
( )
L/2 L
( M + m ) x + m θ2 sin θ − θ cos θ =F
L
cos θ
2
L / 2 mg 2
F M Control input : u ( t ) = F
X
θ ( 0 ) θ 0=
Initial Condition := , θ ( 0 ) 0,=
x ( 0 ) 0,=
x ( 0 ) 0
( M + m)
L
2
L
, B=
( M + m)
L
sin θ
I + m ( L / 2)
2
Where =
A I + m − m cos θ C=
L 2 L m ( L / 2 ) cos θ
m cos θ 2 m cos θ 2
2 2
Angle and Angle Rate Controller
Control Strategy
− How to control u (t ) =
F (t ) ?
− Desired state when t → ∞ is θ → 0
u (t ) =F (t ) = ( ) L
A −2ζωnθ − ωn2θ − Bmg + m sin θ ⋅ θ 2
2
ii ) P control
u ( t ) = F ( t ) = − Kθ
iii ) PD control
u (t ) =
F (t ) =
− Kθ − Cθ
2. Linear System Control
x = θ θ x
T
x
0 1 0 0 0
mg ( L / 2 ) m ( L / 2) / ( M + m)
x1 0 0 0
x I + m ( L / 2 ) − m ( L / 2 ) / ( M + m)
2 2 2
I + m ( L / 2 ) − m ( L / 2 ) / ( M + m)
2 2 2
=x = 2
x+ F (t )
x3 0 0 0 1 0
4
x
I + (m ( L / 2 )
2
)
− 1 g 0 0 0
I + m ( L / 2 )
2
(
/ ( M )
+ m )
I + m ( L / 2 ) − m ( L / 2 ) / ( M + m)
( ) ( )
2 2
2
I + m L / 2
2
− m 2
L / 2
2
/ ( M + m )
x1 x2 x3 x4 ]T θ θ x x
T
=
where x [ =
( M + m ) I + m L 2 − m L B=
( M + m) L θ I + m ( L / 2)
2
Where A= , C=
m
L
2 2 m
L 2 m ( L / 2)
2 2
Linear Model : Feedback Control
Let u = − Kx
Then, x = Ax − Bu = ( A − BK ) x ⇒ x = e( A− BK )t
θ θ x x , K = [ k1 k2 k3 k4 ]
=where x [ =x1 x2 x3 x4 ]T
T
The control methods , which were introduced at the 'Nonlinear system' part,
θ ( 0 ) = 0.1rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
0.15 0.05
0.1 0
dθ/dt [rad/s]
θ [rad]
-0.05
0.05
-0.1
0
-0.15
-0.05 -0.2
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
1 0.4
0.5 0.2
0
dx/dt [m/s] 0
x [m]
-0.5 -0.2
-1 -0.4
-1.5 -0.6
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 9. Simulation results of linear system for tracking control
Simulation : Linear Feedback Control - Animation
θ ( 0 ) = 0.1rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
6
Force [N]
-2
0 20 40 60 80 100
time [sec]
Let xss and uss as state x and input u repectively at steady state
Then, u =uss − K ( x − xss )
x Ax + Bu
=
x1 x2 x3 x4 ]T θ θ x x ,
T
if the system is like =where x [ =
=y Cx + Du
=0 Ax ss + Buss
At the steady state, this system becomes (*) ( At steady state, x = 0 )
=
yss Cx ss + Duss
Linear Model : Tracking Problem
= 0 AN x rss + BN u rss
Then
= rss CN x rss + DN u rss
A B Nx 0
It can be also written as a matrix form like C D N = 1
u
A B
Assume that the inverse of is exists, then this equation can be solved for N x and N u
C D
−1
Nx A B 0
N = C D 1
u
From above relation
Nu r − K ( x − N x r ) =
u= − Kx + ( N u + K N x ) r
where =
N Nu + K N x
=
− Kx + Nr
Simulation : Reference Tracking Control
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m =K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 xss= rss= 5
0.15 0.1
0.1 0
dθ/dt [rad/s]
θ [rad]
0.05 -0.1
0 -0.2
-0.05 -0.3
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
8 1.5
6 1
dx/dt [m/s]
4 0.5
x [m]
2 0
0 -0.5
-2 -1
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m =K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 xss= rss= 5
5
Force [N]
-5
-10
0 20 40 60 80 100
time [sec]
Fig 12. Reference Tracking Input and Simulation results Animation
3. Nonlinear System with Linear Controller
x = θ θ x
T
x
Y Equation of motions
x 2
L L L
θ Iθ = m
x cos θ − m θ + mg sin θ
2 2 2
( )
L/2 L
( M + m ) x + m θ2 sin θ − θ cos θ =F
L
cos θ
2
L / 2 mg 2
F M Control input : u ( t ) = F
X
θ ( 0 ) θ 0=
Initial Condition := , θ ( 0 ) 0,=
x ( 0 ) 0,=
x ( 0 ) 0
( M + m)
L
2
L
, B=
( M + m)
L
sin θ
I + m ( L / 2)
2
Where =
A I + m − m cos θ C=
L 2 L m ( L / 2 ) cos θ
m cos θ 2 m cos θ 2
2 2
Linear Feedback Control
Let u = − Kx
Then, x = Ax − Bu = ( A − BK ) x ⇒ x = e( A− BK )t
θ θ x x , K = [ k1 k2 k3 k4 ]
=where x [ =x1 x2 x3 x4 ]T
T
The control methods , which were introduced at the 'Nonlinear system' part,
θ ( 0 ) = 0.1rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
0.15 0.05
Linear System
0
0.1
dθ/dt [rad/s]
Nonlinear System
θ [rad]
-0.05
0.05
-0.1
0
-0.15
-0.05 -0.2
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
1 0.05
0.5 0
-0.5 -0.1
-1 -0.15
-1.5 -0.2
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 9. Simulation results for linear Feedback control
Nonlinear System vs Linear System with Linear Feedback Control
θ ( 0 ) = 0.1rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
8
Linear System
6
Nonlinear System
Force [N]
-2
0 20 40 60 80 100
time [sec]
8
Fig 10. Feedback Control Input (Nonlinear system)
Linear System and Simulation results Animation
6
Nonlinear System
Force [N]
-2
0 0.5 1 1.5 2 2.5 3
time [sec]
Nonlinear System vs Linear System with Linear Feedback Control
θ ( 0 ) = 1.57 rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
10 40
Linear System
5 20
dθ/dt [rad/s]
Nonlinear System
θ [rad]
0 0
-5 -20
-10 -40
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
100 100
50
0 dx/dt [m/s] 0
x [m]
-50
-100
-100
-200 -150
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 11. Simulation results for linear feedback control
Nonlinear System vs Linear System with Linear Feedback Control
θ ( 0 ) = 1.57 rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
2000
Linear System
1000 Nonlinear System
Force [N]
-1000
-2000
0 20 40 60 80 100
time [sec]
2000
Linear System Fig 12. Feedback Control Input (Nonlinear system)
1000 Nonlinear System and Simulation results Animation
Force [N]
-1000
-2000
0 0.5 1 1.5 2 2.5 3
time [sec]
Nonlinear System with Linear Feedback Control
θ ( 0 ) = 2.44 rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
15 40
10 20
dθ/dt [rad/s]
θ [rad]
5 0
0 -20
1500 200
500 0
0 -100
-500 -200
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 13. Simulation results for linear feedback control
Nonlinear System with Linear Feedback Control - Animation
θ ( 0 ) = 2.44 rad x ( 0) = 0 m
Simulation
=K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2
2000
Linear System
1000 Nonlinear System
Force [N]
-1000
-2000
0 20 40 60 80 100
time [sec]
Fig 14. Feedback Control Input (Nonlinear system) and Simulation results Animation
Tracking Problem
Let xss and uss as state x and input u repectively at steady state
Then, u =uss − K ( x − xss )
x Ax + Bu
=
x1 x2 x3 x4 ]T θ θ x x ,
T
if the system is like =where x [ =
=y Cx + Du
=0 Ax ss + Buss
At the steady state, this system becomes (*) ( At steady state, x = 0 )
=
yss Cx ss + Duss
Tracking Problem
= 0 AN x rss + BN u rss
Then
= rss CN x rss + DN u rss
A B Nx 0
It can be also written as a matrix form like C D N = 1
u
A B
Assume that the inverse of is exists, then this equation can be solved for N x and N u
C D
−1
Nx A B 0
N = C D 1
u
From above relation
Nu r − K ( x − N x r ) =
u= − Kx + ( N u + K N x ) r
where =
N Nu + K N x
=
− Kx + Nr
Nonlinear System vs Linear System with Reference Tracking Control
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m =K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 xss= rss= 5
0.15 0.1
Linear System
0.1 0
dθ/dt [rad/s]
Nonlinear System
θ [rad]
0.05 -0.1
0 -0.2
-0.05 -0.3
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
8 1.5
6 1
2 0
0 -0.5
-2 -1
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 15. Simulation results for tracking control
Nonlinear System vs Linear System with Reference Tracking Control
Simulation
θ ( 0 ) = 0.1rad x ( 0) = 0 m =K [70.5, 22.7, − 0.3, − 0.6483]
Condition θ ( 0 ) = 0 rad / s x ( 0 ) = 0 m / s 2 xss= rss= 5
5
Linear System
Nonlinear System
Force [N]
-5
-10
0 20 40 60 80 100
time [sec]
5
-5
Linear System
Nonlinear System
-10
0 0.5 1 1.5 2 2.5 3
time [sec]
Nonlinear System vs Linear System with Reference Tracking Control
10 40
Linear System
5 20
dθ/dt [rad/s]
Nonlinear System
θ [rad]
0 0
-5 -20
-10 -40
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
100 100
50
0 dx/dt [m/s] 0
x [m]
-50
-100
-100
-200 -150
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 17. Simulation results for tracking control
Nonlinear System vs Linear System with Reference Tracking Control
2000
Linear System
1000 Nonlinear System
Force [N]
-1000
-2000
0 20 40 60 80 100
time [sec]
2000
Linear System
Fig 18. Reference Tracking Input (Nonlinear system)
Nonlinear System
1000 and Simulation results Animation
Force [N]
-1000
-2000
0 0.5 1 1.5 2 2.5 3
time [sec]
Nonlinear System vs Linear System with Reference Tracking Control
10 40
5 20
dθ/dt [rad/s]
θ [rad]
0 0
100
0 dx/dt [m/s]
x [m]
0
-200
-100
-400 -200
0 20 40 60 80 100 0 20 40 60 80 100
time [sec] time [sec]
Fig 19. Simulation results for tracking control
Nonlinear System vs Linear System with Reference Tracking Control
2000
Linear System
1000 Nonlinear System
Force [N]
-1000
-2000
0 20 40 60 80 100
time [sec]
Open ‘RUN_file.m’
1. Simulation(시간, 초기조건 등) 환경 설정
2. Parameter(질량, 길이 등) 값 설정
3. Gain 값 설정
4. 실행
5. ‘Simulaton_file.m’이 실행된다.
‘Simulation_file.m’
Global 변수 선언
Simulation time 설정
t0 : 초기 시간(=0)
tf : simulation 종료 시간
del_t : sampling time 설정
Initial Condition 설정
x0 : Cart의 초기 위치
d_x0 : Cart의 초기 속도
theta_0 : Rod의 초기 각도
d_theta_0 : Rod의 초기 각속도
Desired X 좌표 설정
rss : 목표 도달 X 좌표
Dynamic Parameter 설정
m : Rod의 질량
M : Cart의 질량
L : Rod의 길이
I : Rod의 moment of inertia (자동으로 계산)
g : 중력가속도 (고정값)
Control Gain 설정
K : P gain 값 설정 (양수)
C : D gain 값 설정 (양수, 0일 시에는 P Control)
Zeta : 댐핑 값 설정
wn : 공진주파수 설정
How to use – Simulation_file.m
ode45
Ode45라는 함수를 통해 nonlinear equation을 실시
간으로 풀어낼 수 있다.(부록B에 ode45에 대해 설명)
▶ ODE45 ?
▶ Why ode45?
▶ How to use?
함수 이름 작성, 변수 선언
(이 때 함수 이름은 저장 시에도 동일한 이름으로 저장해야 한다.)
x
m
Example
mx + cx += ( 0 ) x0 , x=
kx 0, x= ( 0 ) x0 k c
x = x dx가 (2x1)행렬임을
x x1
1 2
먼저 선언해야 한다.
=
x = x , c k
2 x2 =
x − x2 − x1
m m
부록 : ode45
Example
mx + cx += ( 0 ) x0 , x=
kx 0, x= ( 0 ) x0
▶ 그럼 이 때 다음과 같이, 앞에서 만든 ode45를 이용한 m-file을 작성하여 실행하면 다음과 같다.
10
Displacement
Velocity
5
X
-5
-10
0 2 4 6 8 10
t