-
Notifications
You must be signed in to change notification settings - Fork 12
/
control.py
309 lines (262 loc) · 11.6 KB
/
control.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
from video import make_video
import os
import pygame
import numpy as np
import time
import osqp
import cvxpy
import scipy.sparse as sparse
import scipy as sp
kRandom = True
kDebug = False
kDraw = False
kVideo = False
def filter_output(agent_idx, agents, x_nom, T=1, G_all=None, g_all=None, m=None, z=None):
N_a = len(agents)
x0 = np.array([agents[agent_idx].position[0], agents[agent_idx].position[1],
agents[agent_idx].velocity[0], agents[agent_idx].velocity[1]])
amax = 0.8*agents[agent_idx].max_acceleration
# Get dynamics (with error)
Ad, Bd = agents[agent_idx].update_linearization_err()
Av = Ad[2:4,:]
Bv = Bd[2:4,:]
eps_m = 0.01
zp_max = 0.001
zv_max = 0.001
zph_max = 0.001
zvh_max = 0.001
for t in range(T):
# Set up QP to minimize actuation deviation
dual_f = 16
duals = dual_f*(N_a-1)
P = np.zeros((7 + duals, 7 + duals))
P[2, 2] = 1
P[3, 3] = 1
P[4, 4] = 1
P[5, 5] = 1
P[6, 6] = agents[agent_idx].eps
P = sparse.csc_matrix(P)
q = np.hstack([0.0, 0.0, -x_nom[:, t+1], 0.0, np.zeros(duals)])
# Velocity/Acceleration Constraints
up = agents[agent_idx].max_velocity - np.matmul(Av, x0)
up = np.hstack((up, agents[agent_idx].max_acceleration,
agents[agent_idx].max_acceleration))
lp = -agents[agent_idx].max_velocity - np.matmul(Av, x0)
lp = np.hstack((lp, -agents[agent_idx].max_acceleration, -
agents[agent_idx].max_acceleration))
A_np = np.vstack([np.hstack([Bv, np.zeros((2, 4)), np.zeros((2, 1)), np.zeros((2, duals))]),
np.hstack([np.eye(2), np.zeros((2, 4)), np.zeros((2, 1)), np.zeros((2, duals))])])
# Dynamics Constraint
A_np = np.vstack([A_np, np.hstack([Bd, -np.eye(4), np.zeros((4, 1)), np.zeros((4, duals))])])
lp = np.hstack((lp, -np.matmul(Ad, x0)))
up = np.hstack((up, -np.matmul(Ad, x0)))
# Robust CBC Constraint
pr, vr = x0[0:2], x0[2:4]
fp, _, fv, gv = agents[agent_idx].get_dynamics(x0) # Get robot dynamics
if (m is not None):
fp = fp + m[0,0:2]
fv = fv + m[0,2:4]
idx = 0
for j in range(len(agents)):
if (j == agent_idx):
continue
xh = np.concatenate([agents[j].position, agents[j].velocity], axis=0)
ph, _, vh, _ = agents[j].get_dynamics_human(xh, t)
xh = np.concatenate([ph, vh], axis=0)
fp_h, _, fv_h, _ = agents[j].get_dynamics_human(xh, t+1) # Project "human" dynamics (t+1 steps)
if (m is not None):
fp_h = fp_h + m[j,0:2]
fv_h = fv_h + m[j,2:4]
if (z is not None):
zp_max = z[0,0]
zv_max = z[0,1]
zph_max = z[j,0]
zvh_max = z[j,1]
# Define matrices in QP (see eq. 15 in paper)
den_p = max(np.linalg.norm(fp - fp_h) + zp_max + zph_max, eps_m)
den_m = max(np.linalg.norm(fp - fp_h) - zp_max - zph_max, eps_m)
H1 = np.hstack([-(fv - fv_h) / den_m, -(fp - fp_h) / den_m, (fv - fv_h) / den_m, (fp - fp_h) / den_m ])
H2 = np.hstack([ -gv / den_m , np.zeros((2,2)), gv / den_m, np.zeros((2,2)) ])
H3 = -np.matmul((fp - fp_h), gv) / den_p
if (G_all is None):
G = np.kron(np.eye(2), np.array([[1, 0, 0, 0], [-1, 0, 0, 0], [0, 1, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, -1, 0], [0, 0, 0, 1], [0, 0, 0, -1]]))
else:
G = G_all[j-1,:,:]
if (g_all is None):
g = np.kron(np.ones(2), np.array([zp_max, zp_max, zv_max, zv_max, zph_max, zph_max, zvh_max, zvh_max]))
else:
g = g_all[j-1,:]
kc = min(np.dot(fp - fp_h, fv - fv_h) / den_p, np.dot(fp - fp_h, fv - fv_h) / den_m) + np.sqrt(amax*(max(den_m - agents[agent_idx].Ds, 0.))) + (agents[agent_idx].gamma - 1)*np.sqrt(amax*(max(np.linalg.norm(pr - ph) - agents[agent_idx].Ds, 0.))) + (agents[agent_idx].gamma-1)*np.dot(pr - ph, vr - vh)/(max(np.linalg.norm(pr - ph), eps_m))
h_l1 = np.expand_dims(np.hstack([H3, np.zeros(4), -1.0, np.zeros(dual_f*idx), g, np.zeros(dual_f*(len(agents)-2-idx))]), axis=0)
h_l2 = np.hstack([np.transpose(H2), np.zeros((8, 4)), np.zeros((8,1)), np.zeros((8,dual_f*idx)), -np.transpose(G), np.zeros((8, dual_f*(len(agents)-2-idx)))])
h_r2 = -H1
A_np = np.vstack([A_np, h_l1, h_l2])
lp = np.hstack((lp, -np.inf, h_r2 - 0.001*np.ones(8)))
up = np.hstack((up, kc, h_r2 + 0.001*np.ones(8)))
idx += 1
h_l3 = np.hstack([np.zeros((duals, 7)), -np.eye(duals)])
A_np = np.vstack([A_np, h_l3])
lp = np.hstack((lp, -np.inf*np.ones(duals)))
up = np.hstack((up, np.zeros(duals)))
Ap = sparse.csc_matrix(A_np)
# Setup workspace and solve QP
prob = osqp.OSQP()
prob.setup(P, q, Ap, lp, up, verbose=False)
res = prob.solve()
if (t == 0):
ctrl = res.x[0:2]
return ctrl
def filter_output_primal(agent_idx, agents, x_nom, T=1):
x0 = np.array([agents[agent_idx].position[0], agents[agent_idx].position[1],
agents[agent_idx].velocity[0], agents[agent_idx].velocity[1]])
amax = 0.8*agents[agent_idx].max_acceleration
# Get dynamics (with error)
Ad, Bd = agents[agent_idx].update_linearization_err()
Av = Ad[2:4,:]
Bv = Bd[2:4,:]
for t in range(T):
# MPC Problem (u(0), x(1), eps)
# Minimize position deviation
P = np.eye(7)
P[0, 0] = 0
P[1, 1] = 0
P[6, 6] = agents[agent_idx].eps
P = sparse.csc_matrix(P)
q = np.hstack([0.0, 0.0, -x_nom[:, t+1], 0.0])
# Velocity/Acceleration Constraints
mult = 1
up = mult*agents[agent_idx].max_velocity - np.matmul(Av, x0)
up = np.hstack((up, mult*agents[agent_idx].max_acceleration,
mult*agents[agent_idx].max_acceleration))
lp = -mult*agents[agent_idx].max_velocity - np.matmul(Av, x0)
lp = np.hstack((lp, -mult*agents[agent_idx].max_acceleration, -
mult*agents[agent_idx].max_acceleration))
A_np = np.vstack([np.hstack([Bv, np.zeros((2, 4))]),
np.hstack([np.eye(2), np.zeros((2, 4))])])
# Dynamics Constraint
A_np = np.vstack([A_np, np.hstack([Bd, -np.eye(4)])])
lp = np.hstack((lp, -np.matmul(Ad, x0)))
up = np.hstack((up, -np.matmul(Ad, x0)))
# Barrier Constraint
for j in range(len(agents)):
if (j == agent_idx):
continue
pd = np.array([x0[0] - (agents[j].position[0] + agents[j].velocity[0]*agents[j].dt*t),
x0[1] - (agents[j].position[1] + agents[j].velocity[1]*agents[j].dt*t)])
vd = np.array([x0[2] - agents[j].velocity[0],
x0[3] - agents[j].velocity[1]])
c = pd + vd*agents[j].dt
h_const = np.dot(c, vd) / np.linalg.norm(c) + np.sqrt(abs(amax)*(max(np.linalg.norm(c) - agents[agent_idx].Ds, 0))) - (1 - agents[agent_idx].gamma)*np.dot(
pd, vd)/np.linalg.norm(pd) - (1 - agents[agent_idx].gamma)*np.sqrt(abs(amax)*(max(np.linalg.norm(pd) - agents[agent_idx].Ds, 0))) # Ignore u_{user}
h_u = c*agents[j].dt/np.linalg.norm(c)
ub = h_const
Ab = -h_u
A_np = np.vstack(
[A_np, np.hstack([np.expand_dims(Ab, axis=0), np.zeros((1, 4))])])
lp = np.hstack((lp, -np.inf))
up = np.hstack((up, ub))
# Slack variable
A_np = np.vstack([A_np, np.zeros((1, A_np.shape[1]))])
ab = np.zeros((A_np.shape[0], 1))
ab[-1, 0] = 1
for j in range(len(agents)-1):
ab[-2-j, 0] = -1
A_np = np.hstack([A_np, ab])
Ap = sparse.csc_matrix(A_np)
lp = np.hstack((lp, 0.0))
up = np.hstack((up, np.inf))
# Setup workspace and solve QP
prob = osqp.OSQP()
prob.setup(P, q, Ap, lp, up, verbose=False)
res = prob.solve()
ctrl = res.x[0:2]
return ctrl
# Get nominal trajectory to give us desired input
def get_trajectory(agent, goal=None, N=12, agents=None, agent_idx=None):
Ad, Bd = agent.update_linearization()
# Sizes
nx = 4
nu = 2
# Constraints
umin = -agent.max_acceleration*np.ones(nu)
umax = agent.max_acceleration*np.ones(nu)
xmin = np.array([-20, -20, -agent.max_velocity, -agent.max_velocity])
xmax = np.array([100, 100, agent.max_velocity, agent.max_velocity])
# Objective function
Q = sparse.diags([10., 10., 1., 1., ])
QN = Q
R = 0.02*sparse.eye(nu)
# Initial and reference states
x_init = np.array([agent.position[0], agent.position[1], agent.velocity[0], agent.velocity[1]])
x0 = np.array([agent.position[0], agent.position[1], agent.velocity[0], agent.velocity[1]])
if (goal is None):
xg = np.array([agent.goal[0], agent.goal[1], 0.0, 0.0])
else:
xg = np.array([goal[0], goal[1], 0.0, 0.0])
# Cast MPC problem to a QP: x = (x(0),x(1),...,x(N),u(0),...,u(N-1), eps)
# - quadratic objective
P = sparse.block_diag([sparse.kron(sparse.eye(N), Q), QN, sparse.kron(sparse.eye(N), R)]).tocsc()
# - linear objective
q = np.hstack([np.kron(np.ones(N), -Q.dot(xg)), -QN.dot(xg), np.zeros((N)*nu)])
# - linear dynamics
Ax = np.kron(np.eye(N+1), -np.eye(nx)) + np.kron(np.eye(N+1, k=-1), Ad)
Bu = np.kron(np.vstack(
[np.zeros((1, N)), np.eye(N)]), Bd)
# Dynamics constraints
Aeq = np.hstack([Ax, Bu])
leq = np.hstack([-x0[0], -x0[1], -x0[2], -x0[3], np.zeros((N)*nx)])
# leq = np.zeros((N+1)*nx)
ueq = leq
# - input and state constraints
Aineq = np.eye((N+1)*nx + N*nu)
lineq = np.hstack([np.kron(np.ones(N+1), xmin),
np.kron(np.ones(N), umin)])
uineq = np.hstack([np.kron(np.ones(N+1), xmax),
np.kron(np.ones(N), umax)])
A_vel = np.hstack([np.kron(np.eye(N+1), np.array([[0, 0, 1, 0], [
0, 0, 0, 1]])), np.zeros((nu*(N+1), nu*N))])
Aineq = np.vstack([Aineq, A_vel])
lineq = np.hstack([lineq, np.kron(np.ones(N+1), np.array(
[-agent.max_velocity, -agent.max_velocity]))])
uineq = np.hstack([uineq, np.kron(np.ones(N+1), np.array(
[agent.max_velocity, agent.max_velocity]))])
A = np.vstack([Aeq, Aineq])
l = np.hstack([leq, lineq])
u = np.hstack([ueq, uineq])
A = sparse.csc_matrix(A)
# Create an OSQP object
prob = osqp.OSQP()
# Setup workspace
prob.setup(P, q, A, l, u, warm_start=True, verbose=False)
# Simulate in closed loop
nsim = 1
# Store data Init
xst = np.zeros((nx, nsim))
ust = np.zeros((nu, nsim))
# Solve
res = prob.solve()
# Check solver status
if res.info.status != 'solved':
raise ValueError('OSQP did not solve the problem!')
# Apply first control input to the plant
if (agents is not None):
ctrl = res.x[-N*nu-1:-(N-1)*nu-1]
else:
ctrl = res.x[-N*nu:-(N-1)*nu]
x0 = Ad.dot(x0) + Bd.dot(ctrl)
i = 0
# Store Data
xst[:, i] = x0
ust[:, i] = ctrl
x_path = np.transpose(np.reshape(res.x[0:N*nx], (N, nx)))
if (agents is not None):
u_path = np.reshape(res.x[-N*nu-1:-1], (N, nu))
else:
u_path = np.reshape(res.x[-N*nu:], (N, nu))
# Update initial state
l[:nx] = -x0
u[:nx] = -x0
prob.update(l=l, u=u)
return ctrl, x_path, x_init #, u_path
if __name__ == '__main__':
print("Control Program Run")