-
Notifications
You must be signed in to change notification settings - Fork 4
/
cbf_qp.py
392 lines (332 loc) · 12.4 KB
/
cbf_qp.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""
See explanation below in the __name__ guard.
For critical part, see _asif method of ASIF.
"""
from cartpole import Controller, CartPole, simulate, G
from nominal_control import ControlLQR
import numpy as np
from qpsolvers import solve_qp
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.animation as animation
class ASIF(Controller):
"""Active Set Invariance Filter
Implementation of the popular CBF-QP
ASIF takes in the nominal control signal(u_nom) and filters it to
generate u_filtered. Under the hood it is an optimization problem
with following objective function:
u_f = argmin || u_f - u_nom ||^2
s.t. h_dot(x, u_f) >= -gamma*h(x)
___________________ ________
x | | u_nom | | u_filtered
-----> | nominal control | ------> | ASIF | ------------->
|___________________| |________|
"""
def __init__(
self,
nominal_control,
cp: CartPole,
barrier_cart_vel,
gamma,
asif_enabled=True,
use_nonlinear_dynamics=True,
):
"""
For our case of cartpole, limitations on cart *velocity* is enforced
by this ASIF.
"""
self.nominal_control = nominal_control
self.cp = cp
self.barrier_cart_vel = barrier_cart_vel
self.gamma = gamma
self.asif_enabled = asif_enabled
self.use_nonlinear_dynamics = use_nonlinear_dynamics
if self.use_nonlinear_dynamics:
self._h_dot = self._h_dot_nonlinear
else:
self._h_dot = self._h_dot_linear
self._log = {
"cbf_nominal": [],
"cbf_filtered": [],
"qp_g_nominal": [],
"qp_g_filtered": [],
"qp_h": [],
"u_nom": [],
"u_filtered": [],
}
def control_law(self, state):
u_nominal = self.nominal_control(state)
u_filtered = self._asif(u_nominal, state)
if self.asif_enabled is False:
u_filtered = u_nominal
# if np.isclose(u_filtered, u_nominal)[0] == False:
# print(f"ASIF active! {u_nominal=}, {u_filtered=}")
return u_filtered
def _asif(self, u_nominal, state):
m_cart = self.cp.m_cart
m_pole = self.cp.m_pole
l = self.cp.l
# objective function, same for all CBF-QP
p = np.array([1.0])
q = np.array([-u_nominal])
# constraints
if self.use_nonlinear_dynamics:
# the terms come from self.h_dot_nonlinear, organized for standart
# qp solver format
delta = m_pole * np.sin(state[2]) ** 2 + m_cart
if state[1] >= 0:
g = np.array([1 / delta])
h = -1 * np.array(
[
m_pole * l * (state[3] ** 2) * np.sin(state[2]) / delta
+ m_pole
* G
* np.sin(state[2])
* np.cos(state[2])
/ delta
]
) + self.gamma * (self._h(state))
else:
g = np.array([-1 / delta])
h = np.array(
[
m_pole * l * (state[3] ** 2) * np.sin(state[2]) / delta
+ m_pole
* G
* np.sin(state[2])
* np.cos(state[2])
/ delta
]
) + self.gamma * (self._h(state))
else:
# the terms come from self.h_dot_linear, organized for standart
# qp solver format
if state[1] >= 0:
g = np.array([1 / m_cart])
h = np.array(
[
m_pole * G / m_cart * state[2]
+ self.gamma * self._h(state)
]
)
else:
g = np.array([-1 / m_cart])
h = np.array(
[
-m_pole * G / m_cart * state[2]
+ self.gamma * self._h(state)
]
)
u_filtered = solve_qp(
p,
q,
g,
h,
# lb=np.array([-80.]),
# ub=np.array([80.]),
solver="cvxopt",
)
self._log["cbf_filtered"].append(self.cbf_cstr(state, u_filtered))
self._log["cbf_nominal"].append(self.cbf_cstr(state, u_nominal))
self._log["qp_g_filtered"].append(g @ u_filtered)
self._log["qp_g_nominal"].append(g @ u_nominal)
self._log["qp_h"].append(h)
self._log["u_nom"].append(u_nominal)
self._log["u_filtered"].append(u_filtered)
return u_filtered
def _h(self, state):
if state[1] >= 0:
return self.barrier_cart_vel - state[1]
else:
return self.barrier_cart_vel + state[1]
def _h_dot_nonlinear(self, state, u):
"""Equations from cartpole._gen_dynamics._dynamics"""
m_cart = self.cp.m_cart
m_pole = self.cp.m_pole
l = self.cp.l
delta = m_pole * np.sin(state[2]) ** 2 + m_cart
if state[1] >= 0:
return (
-1
* (
m_pole * l * (state[3] ** 2) * np.sin(state[2]) / delta
+ m_pole * G * np.sin(state[2]) * np.cos(state[2]) / delta
)
- u / delta
)
else:
return (
m_pole * l * (state[3] ** 2) * np.sin(state[2]) / delta
+ m_pole * G * np.sin(state[2]) * np.cos(state[2]) / delta
) + u / delta
def _h_dot_linear(self, state, u):
"""Equations from cartpole.get_ss_A"""
m_cart = self.cp.m_cart
m_pole = self.cp.m_pole
if state[1] >= 0:
return m_pole * G / m_cart * state[2] - u / m_cart
else:
return -m_pole * G / m_cart * state[2] + u / m_cart
def cbf_cstr(self, state, u):
return self.gamma * self._h(state) + self._h_dot(state, u)
def visualize(l, y, t, dt, asif: ASIF, infodict, save=None):
"""
Args:
l: Pendulum length, CartPole.l
y: simulation outputs
t: simulation timesteps
dt
"""
a_x1 = y[:, 0]
a_y1 = 0.0
a_x2 = l * np.sin(y[:, 2]) + a_x1
a_y2 = -l * np.cos(y[:, 2]) + a_y1
fig = plt.figure(figsize=(12, 10.65))
# ax = fig.add_subplot(241, autoscale_on=True, aspect='equal',\
# xlim=(-3, 3), ylim=(-3, 3))
# ax.grid()
# (line,) = ax.plot([], [], "o-", lw=2)
# time_template = "time = %.1fs"
# time_text = ax.text(0.05, 0.9, "", transform=ax.transAxes)
# def init():
# line.set_data([], [])
# time_text.set_text("")
# return line, time_text
# def animate(i):
# thisx = [a_x1[i], a_x2[i]]
# thisy = [a_y1, a_y2[i]]
# line.set_data(thisx, thisy)
# time_text.set_text(time_template % (i * dt))
# return line, time_text
# ani = animation.FuncAnimation(
# fig,
# animate,
# np.arange(1, len(y)),
# interval=30,
# blit=True,
# init_func=init,
# )
# time domain plot
ax = fig.add_subplot(2, 3, 1)
ax.set_title("States: Time")
ax.set_xlabel("Time [s]")
ax.set_ylabel(r"Position[m, rad]")
ax.grid()
ax.plot(t, y[:, 0], color="C1", label="x")
ax.plot(t, y[:, 1], "--", c="C1", label="x_dot")
ax.plot(t, y[:, 2], c="C2", label="theta")
ax.plot(t, y[:, 3], "--", c="C2", label="theta_dot")
ax.legend(loc="upper right")
ax2 = fig.add_subplot(2, 3, 4)
ax2_plt = ax2.scatter(y[:, 0], y[:, 1], c=t, alpha=0.2)
ax2.set_title("Cart States: Phase Plane")
ax2.set_xlabel("Cart position [m]")
ax2.set_ylabel("Cart velocity [m/s")
ax2.grid(True)
ax2.axhline(color="black")
ax2.axhline(asif.barrier_cart_vel, linestyle="--", color="red")
ax2.axhline(-asif.barrier_cart_vel, linestyle="--", color="red")
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = fig.colorbar(ax2_plt, cax=cax, orientation="vertical")
# cbar.set_label('Time')
ax3 = fig.add_subplot(2, 3, 5)
ax3_plt = ax3.scatter(y[:, 2], y[:, 3], c=t, alpha=0.2)
ax3.set_title("Pole States: Phase Plane")
ax3.set_xlabel("Pole position [rad]")
ax3.set_ylabel("Pole velocity [rad/s]")
ax3.grid(True)
ax3.axhline(color="black")
ax3.axvline(3.14, color="black")
divider = make_axes_locatable(ax3)
cax = divider.append_axes("right", size="5%", pad=0.05)
cbar = fig.colorbar(ax3_plt, cax=cax, orientation="vertical")
# cbar.set_label('Time')
asif_log = asif.match_log_with_time(t, infodict["nfe"])
if len(asif_log["cbf_nominal"]) > 0:
ax4 = fig.add_subplot(2, 3, 2)
ax4.plot(t, asif_log["cbf_nominal"], label="u_nominal")
ax4.plot(t, asif_log["cbf_filtered"], "--", label="u_filtered")
ax4.set_title("CBF")
ax4.set_xlabel("Time [s]")
ax4.set_ylabel("Value")
ax4.legend(loc="upper right")
ax4.grid(True)
ax5 = fig.add_subplot(2, 3, 3)
ax5.plot(t, asif_log["qp_h"], label="h")
ax5.plot(t, asif_log["qp_g_nominal"], "-.", label="u_nominal")
ax5.plot(t, asif_log["qp_g_filtered"], "--", label="u_filtered")
ax5.set_title("QP Constraint")
ax5.set_xlabel("Time [s]")
ax5.set_ylabel("Value")
ax5.legend(loc="upper right")
ax5.grid(True)
ax6 = fig.add_subplot(2, 3, 6)
ax6.plot(t, asif_log["u_nom"], label="nominal")
ax6.plot(t, asif_log["u_filtered"], "--", label="filtered")
ax6.set_title("Control Signal")
ax6.set_xlabel("Time [s]")
ax6.set_ylabel("Force [N]")
ax6.legend(loc="upper right")
ax6.grid(True)
if save:
ani.save(f"{save}.mp4", fps=20)
# plt.tight_layout()
plt.subplots_adjust(
left=0.074,
bottom=0.11,
right=0.955,
top=0.943,
wspace=0.56,
hspace=0.21,
)
plt.show()
if __name__ == "__main__":
cp = CartPole(m_cart=5, m_pole=2, l=1.5)
controller = ControlLQR(cp)
"""
We are limiting nominal control of LQR with respect to cart velocity with
the use of ASIF.
Advice: switch asif_enabled below and see its effects
In this example, I have adjusted the parameters rather carefully; the ASIF
does not cause a major difference in the nominal control. If you make the
constraint more strict, it will cause problems. See note 1 below.
Note 1:
Current ASIF formulation do not work well with the nominal control,
the simple constraint on only cart velocity causes the ASIF
to ignore pendulum state, if we deviate considerably from nominal control
the pendulum is likely to fall. Once it has fallen, LQR cant recover since
we are out of its operation range (for LQR we are using linearized dynamics
around the pendulum up orientation).
To apply it on an actual case, we better make it more robust. My guess is
a more involved CBF, i.e. h, would do the job. An alternative is to use
a controller which can operate independent of the pendulum orientation.
Note 2:
I am surprised by the effect of using linearized dynamics on ASIF, it causes
a much stronger correction which kicks the system out of its pendulum up
orientation. Then, it becomes unstable as explained above.
I have made various attempts; however, I could not get ASIF to
operate with linearized dynamics.
"""
asif = ASIF(
controller,
cp,
barrier_cart_vel=0.45,
gamma=10,
asif_enabled=False,
use_nonlinear_dynamics=True,
)
cp.set_control_law(asif)
## go right
## Our formulation of ASIF is quite sensitive to initial conditions,
## see 'Note 1' above
controller.state_ref = [1.5, 0, np.pi, 0]
x0 = [0, 0.0, 178 * np.pi / 180, 0]
## go left
## Our formulation of ASIF is quite sensitive to initial conditions,
## see 'Note 1' above
# controller.state_ref = [-1.5, 0, np.pi, 0]
# x0 = [0, 0., 182 * np.pi/180, 0]
dt = 0.03
(y, infodict), t = simulate(cp, x0=x0, dt=dt, full_output=True)
visualize(cp.l, y, t, dt, asif, infodict)