-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcartpole_lqr.py
149 lines (117 loc) · 3.42 KB
/
cartpole_lqr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import numpy as np
import scipy as sp
import scipy.linalg
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.animation
"""
LQR (Linear-Quadratic Regulator) optimal control
of the linearized cart-pole balancing problem.
we follow the notation and coordinate systems of
"The pole balancing problem: a benchmark control theory problem"
by Jason Brownlee, technical report, 2005.
"""
# physical constants:
mc = 1.0 # mass cart
mp = 0.1 # mass pole
l = 0.5 # length of pole
g = 9.81 # gravity
# system state global, because python's closures suck
th = 0.50
dth = 0
x = -1
dx = 0
def dynamics(th, dth, x, dx, force, dt):
"""Integrate nonlinear dynamics of cart-pole system."""
sth = np.sin(th)
cth = np.cos(th)
# ODEs
ddth = ((g*sth + cth*(-force - mp*l*dth**2*sth)/(mc + mp))
/ (l*(4/3 - (mp*cth**2)/(mc + mp))))
ddx = (force + mp*l*(dth**2*sth - ddth*cth))/(mc + mp)
# noise
ddth += 0.4 * np.random.normal()
ddx += 0.4 * np.random.normal()
# euler integration
dth = dth + dt * ddth
th = th + dt * dth
dx = dx + dt * ddx
x = x + dt * dx
return th, dth, x, dx
def lqr(A, B, Q, R):
"""Compute LQR optimal feedback control gains.
Args:
A, B: Continuous system dynamics dx/dt = Ax + Bu.
Q, R: Quadratic cost J(t) = x'Q x + u'R u.
Returns:
K: Gain matrix.
"""
B = B.reshape((B.size, 1))
P = sp.linalg.solve_continuous_are(A, B, Q, R)
K = -np.linalg.solve(R, np.dot(B.T, P))
return K
def cartpole_linear():
"""Construct linearized dynamics matrices for the cart-pole system.
Derivation:
ddth = (g*0 + 1*(-force - 0)/(mc + mp) / (l*(4/3 - mp/(mc+mp)))
= C * force, where C = -1/(mc+mp) / (l*4/3 - mp/(mc+mp))
ddx = (force + mp*l*(0-ddth)/(mc + mp)
= force - mp*l*C*force/(mc+mp)
= (1 - mp*l*C/(mc+mp)) * force = C2 * force
s = [th dth x dx]
ds/dt = As + Bforce, where:
A = [0 1 0 0
grav 0 0 0
0 0 0 1
0 0 0 0],
B = [0 C 0 C2].
Returns:
A, B: Dynamics matrices such that $\\dot x \\approx Ax + Bu$.
"""
A = np.array([
[0, 1, 0, 0],
[9.81, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
C1 = (-1/(mc+mp)) / (l*4/3 - mp/(mc+mp))
C2 = 1 - mp*l*C1/(mc+mp)
B = np.array([0, C1, 0, C2])
return A, B
def simulate(K):
# for rendering
fps = 30
dt = 1.0 / fps
cart_w = 0.3
cart_h = 0.15
track_limit = 2.4
xlimits = [-track_limit, track_limit]
secs = 5
frames = fps * secs
fig, ax = plt.subplots()
line, = ax.plot([0, 0], [1, 1])
rect = mpl.patches.Rectangle((0, 0), cart_w, cart_h)
ax.add_patch(rect)
ax.axis("equal")
ax.set_xlim(xlimits)
ax.set_ylim([-.2*l, 2.2*l])
def anim_fn(iframe):
global x, dx, th, dth
line.set_xdata([x, x + 2*l*np.sin(th)])
line.set_ydata([0, 2*l*np.cos(th)])
rect.set_xy((x-cart_w/2, -cart_h/2))
force = np.dot(K, np.array([th, dth, x, dx]))[0]
th, dth, x, dx = dynamics(th, dth, x, dx, force, dt)
return line, rect
ani = mpl.animation.FuncAnimation(fig, anim_fn,
interval=1000*dt, frames=frames, repeat=False,
)
plt.show()
def main():
A, B = cartpole_linear()
Q = np.diag([1, 0.1, 1, 0.1])
R = 0.001 * np.eye(1)
K = lqr(A, B, Q, R)
simulate(K)
if __name__ == "__main__":
main()