-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvelocity_obj.py
457 lines (364 loc) · 14.3 KB
/
velocity_obj.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import vtkplotter as vtk
import numpy as np
# from: https://github.com/raphaelkba/Roboxi/blob/master/mpc.py
from mpc import NonlinearMPC
from collections import deque
#some inspiration from:
#https://github.com/AtsushiSakai/PythonRobotics/blob/master/PathTracking/model_predictive_speed_and_steer_control/model_predictive_speed_and_steer_control.py
DT = 0.05 # [s] time tick
WB = 2.5 # [m]
CAR_PADDING = 0.5
MAX_STEER = .8 #http://street.umn.edu/VehControl/javahelp/HTML/Definition_of_Vehicle_Heading_and_Steeing_Angle.htm
MAX_VEL = 50
HORIZON_SECS = 2
def rotate(theta):
rot = np.array([
[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]
])
return rot
def rotate2D(theta):
rot = np.array([
[np.cos(theta), -np.sin(theta)],
[np.sin(theta), np.cos(theta)],
])
return rot
class Agent():
def __init__(self, map, name, state=[0,0,0,0,0]):
self.map = map
#state is x,y,theta,velocity,phi
self.state = np.array(state)
self.runge = np.copy(self.state)
self.name=name
self.radius = 2.5
self.past_states = deque(maxlen=5)
#initialize points for visualization
pos = np.concatenate((state[0:2],[0]))
assert(len(pos) == 3)
"""
For creating the car bounding box, we assume the middle of the back
axle is the origin. Then, make some points assuming that the car is at
theta=0. Next, rotate everyting by theta degrees and add the position
of the back axle to all the points
"""
box = np.array([
[0, CAR_PADDING, 0],
[WB, CAR_PADDING, 0],
[WB, -CAR_PADDING, 0],
[0, -CAR_PADDING, 0],
])
#draw the outline of the bounding box
box = box @ rotate(state[2]).T
box += pos
shifted_box = np.concatenate(([box[-1]], box[:-1]))
self.bounding_box = vtk.shapes.Lines(box, shifted_box, lw=3)
map.vp += [self.bounding_box]
#visualize the velocity
vel = self.getVel3D()
self.vel_arrow = vtk.shapes.Line(pos, pos + vel, c="r", lw=3)
map.vp += [self.vel_arrow]
"""
To create the convex set for obstacle collision, we define a set
B = {y:Gy<=g} where G is the normals and g are the offsets. We start
by creating 4 normals to define a rectangle. then we find the offsets
by projecting onto the normals.
"""
self.G = np.array([
[-1,0],
[0, -1],
[1,0],
[0,1],
])
box_pts = np.array(self.bounding_box.points())
self.G = self.G @ rotate2D(state[2]).T
self.g = np.diagonal(self.G @ box_pts[:,:2].T).reshape(self.G.shape[0],1)
def getPos3D(self):
return np.array([self.state[0], self.state[1],0])
def getVel3D(self):
theta = self.state[2]
return np.array(self.state[3] * np.array([np.cos(theta), np.sin(theta), 0]))
def visVelocityObstacle(self):
agents = self.map.get_neighbors(self.name)
vp = self.map.vp
A = []
b = []
planes = [] #for visualizing the constraint
for a in agents[:1]:
a_pos = a.getPos3D()
pos = self.getPos3D()
a_vel = a.getVel3D()
#relative v arrow
relVel = self.getVel3D() - a.getVel3D()
#vp += [vtk.shapes.Arrow(pos, pos+relVel, c="green")]
perp = np.cross([0,0,1], pos-a_pos)
perp = perp / np.linalg.norm(perp)
perp *= a.radius + self.radius
perp2 = -1*perp
# planes += [vtk.shapes.Arrow(a_pos, a_pos+perp, c="green")]
# planes += [vtk.shapes.Arrow(a_pos, a_pos+perp2, c="green")]
leg1 = a_pos - pos + perp
leg2 = a_pos - pos + perp2
leg1Normal = np.cross(leg1,[0,0,1])
leg2Normal = np.cross([0,0,1], leg2)
leg1Normal = leg1Normal / np.linalg.norm(leg1Normal)
leg2Normal = leg2Normal / np.linalg.norm(leg2Normal)
"""
create the truncation hyperplane location
Formula: (((obstacle pos - agent pos) / T ) + agent pos) + a_vel
TODO: must add the radius to both of them.
"""
trunc_pt = (a_pos - pos) / HORIZON_SECS + pos + a_vel
trunc_direction = (a_pos - pos) / np.linalg.norm(a_pos - pos)
# radius_norm = (a.radius + self.radius) / HORIZON_SECS
# trunc_dir_scaled = trunc_direction * radius_norm
# trunc_pt -= trunc_dir_scaled
trunc_direction *= -1 #want points on the other side of it.
planes += [vtk.shapes.Circle(trunc_pt, r=.1, c="green")]
planes += [vtk.shapes.Plane(trunc_pt, normal=trunc_direction, c="blue")]
#note we are appending as row vectors here.
A.append(leg1Normal[:2])
A.append(leg2Normal[:2])
# A.append(trunc_direction[:2])
#project the pos onto the normal for the offset.
#the normals are defined with respect to relatie v,
#so when we add a's v we're back in absolute v frame.
b.append(leg1Normal[:2] @ (pos[:2] + a_vel[:2]))
b.append(leg2Normal[:2] @ (pos[:2] + a_vel[:2]))
# b.append(trunc_direction[:2] @ (trunc_pt[:2]))
planes += [vtk.shapes.Plane(pos=pos+a_vel, normal=leg1Normal, sx=3)]
planes += [vtk.shapes.Plane(pos=pos+a_vel, normal=leg2Normal, sx=3)]
# planes += [vtk.shapes.Plane(pos=trunc_pt+a_vel, normal=trunc_direction, sx=3)]
#now we define the constraints like we would for the opt prob.
A = np.array(A)
b = np.array(b).reshape((len(b), 1))
return A, b, planes
#redefine using pfaffian constraints
#and Euler Discretization of dynamics
def dynamics_step(self, u):
#subtract the translation from visualization points
pts = np.array(self.bounding_box.points())
pos = np.array([self.state[0], self.state[1], 0])
pts = pts - pos
#update the state of the car [x, y, theta, vel, phi]
theta = self.state[2]
phi = self.state[4]
f = lambda x,u: np.array([
x[3] * np.cos(x[2]),
x[3] * np.sin(x[2]),
x[3] * np.tan(x[4])/WB,
u[0],
u[1],
])
state_dot = f(self.state, u)
# k1 = f(self.state , u)
# k2 = f(self.state + DT/2*k1, u)
# k3 = f(self.state + DT/2*k2, u)
# k4 = f(self.state + DT*k3, u)
# self.state = self.state + DT/6*(k1+2*k2+2*k3+k4)
self.state = self.state + state_dot * DT
# #check velocity
# if abs(self.state[3]) >= MAX_VEL:
# self.state[3] = np.sign(self.state[3]) * MAX_VEL
# #check steering angle
# if abs(self.state[4]) >= MAX_STEER:
# self.state[4] = np.sign(self.state[4]) * MAX_STEER
theta_f = self.state[2]
d_theta = theta_f - theta
#rotate visualization points around origin
pts = pts @ rotate(d_theta).T
self.G = self.G @ rotate2D(d_theta).T
#add back the new translation
pos = np.array([self.state[0], self.state[1], 0])
pts = pts + pos
self.g = np.diagonal(self.G @ pts[:,:2].T).reshape(self.G.shape[0],1)
#add a trace of where the car has been
self.map.vp += [vtk.shapes.Sphere(pos=pos, r=.1, alpha=.5, c="red")]
#update the point visualizations
self.bounding_box.points(pts)
self.vel_arrow.points([pos, pos+self.getVel3D()])
return self.state
def visConvexBoundingBox(self):
print("g", self.g)
print("G", self.G)
pos = np.array([self.state[0], self.state[1], 0])
dots = np.random.normal(loc=pos[:2].reshape((2,1)), scale=1, size=(2,300))
show = np.all(np.greater(self.g, self.G@dots), axis=0)
points = [vtk.shapes.Circle(list(v)+[0], c="purple", r=.05)
for v,s in zip(dots.T, show.T) if s]
return points
class Map():
agents = []
def __init__(self, vp):
self.vp = vp
def add_agent(self, agent):
self.agents += [agent]
def get_neighbors(self, name):
return [a for a in self.agents if a.name != name]
def create_agent(self, name=None, state=[0,0,0,0,0]):
if name == None:
name = "agent_" + str(len(self.agents))
a = Agent(self, name, state)
self.add_agent(a)
return a
def make_circle_path(num_points):
path = np.array([[50*np.cos(np.pi * x - (np.pi / 2)),
50*np.sin(np.pi * x - (np.pi / 2))]
for x in np.linspace(0,1,num_points)]).T
return path
def make_sinusoid_path(num_points):
path = np.array([[ x,
-8 * (np.cos(x/8) - 1),]
for x in np.linspace(0,50,num_points)]).T
return path
def plot_warm_start(warm_start, vp):
warm_start_3d = np.block([
[warm_start[:2,:]],
[np.zeros(len(warm_start.T))]
])
vp += [vtk.shapes.Tube(warm_start_3d.T, c="yellow", alpha=.3, r=.2)]
def plot_path(path, vp):
z = np.zeros((1,len(path[0])))
path = np.vstack((path, z))
vp += [vtk.shapes.Circle(path[:,0]+[0], c="green", r=.1)]
vp += [vtk.shapes.Circle(path[:,-1]+[0], c="red", r=.5)]
vp += [vtk.shapes.Tube(path.T, c="blue", alpha=1, r=.08)]
def make_line_path(num_points):
#state is x,y,theta,velocity,phi
path = np.linspace([-10,0], [10,0], num_points).T
return path
def closest_path_point(path, state, vp):
s = state[:2]
diff = path.T - s
diff_norm = np.linalg.norm(diff, axis=1)
i = np.argmin(diff_norm)
return path[:,i:]
import time
def follow_path(vp, map):
#generate a wavy path and visualize
num_points = 200
#path = make_circle_path(num_points)
#path = make_line_path(num_points)
path = make_sinusoid_path(num_points)
plot_path(path, vp)
#follow path with car
a = map.create_agent("main", state=np.append(path[:,0],[0,0,0]))
vp.show(interactive=0)
"""Adding MPC from toolbox"""
mpc = NonlinearMPC(HORIZON_SECS, 0.1, WB, vp)
input("start")
#while we're not at our destination yet
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
while norm > 1:
path = closest_path_point(path, a.state, vp)
start_time = time.time()
controls, viz = mpc.MPC(a.state, path, a)
time_f = time.time()
print("optimization time: ", time_f - start_time)
# pts = a.visConvexBoundingBox()
# vp += pts
vp.show(interactive=0)
# if len(pts) > 0:
# vp.clear(pts)
a.dynamics_step(controls[:,0])
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
print("GOAL REACHED")
vp.show(interactive=1)
def go_around_moving_box(vp, map):
"""Generate the Path to follow"""
num_points = 200
path = make_line_path(num_points)
#path = make_sinusoid_path(num_points)
plot_path(path, vp)
# """Define a box and visualize it"""
# C = np.array([
# [0,1],
# [0,-1],
# [1,0],
# [-1,0]
# ])
# d = np.matrix([.5,0.5,.5,.5]).T
# vels = np.random.normal(scale=1, size=(2,500))
# show = np.all(np.greater(d,C@vels), axis=0)
# vp += [vtk.shapes.Sphere(list(v)+[0], c="purple", r=.05)
# for v,s in zip(vels.T, show.T) if s]
"""Add the agents to the map and initialize controller"""
a = map.create_agent("main", state=np.append(path[:,0],[0,0,0]))
o_pos = a.state + [20, 0, -np.pi,2,0]
o = map.create_agent("obstacle", state=o_pos)
A, b, _ = a.visVelocityObstacle()
mpc = NonlinearMPC(HORIZON_SECS, 0.1, WB, vp, A=A)
vp.show()
input("being visualization: [hit enter]")
"""Loop until arrive at destination"""
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
times = []
while norm > 1:
A, b, planes = a.visVelocityObstacle()
#visualize the plane and it's feasible region
# vp += planes
#truncate the path to the points in the future
path = closest_path_point(path, a.state, vp)
#time the optimization step
start_time = time.time()
controls, viz = mpc.MPC(a.state, path, a, A=A, b=b)
time_f = time.time()
vp += viz
vp.show(interactive=0)
# vp.clear(planes)
if len(viz) > 0:
vp.clear(viz)
#update simulation
a.dynamics_step(controls[:,0])
o.dynamics_step([1,0])
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
time_diff = time_f - start_time
times.append(time_diff)
print("GOAL REACHED")
print("mean time: ", np.mean(times), "max: ", np.max(times), "median", np.median(times))
input("Finished? [hit enter]")
vp.show(interactive=1)
def go_around_box(vp, map):
num_points = 200
path = make_line_path(num_points)
#path = make_sinusoid_path(num_points)
plot_path(path, vp)
"""Define a box and visualize it"""
#row vector normals
C = np.array([
[0,1],
[0,-1],
[1,0],
[-1,0]
])
d = np.matrix([.5,0.5,.5,.5]).T
vels = np.random.normal(scale=1, size=(2,500))
show = np.all(np.greater(d,C@vels), axis=0)
vp += [vtk.shapes.Sphere(list(v)+[0], c="purple", r=.05)
for v,s in zip(vels.T, show.T) if s]
"""Adding MPC from toolbox"""
a = map.create_agent("main", state=np.append(path[:,0],[0,0,0]))
mpc = NonlinearMPC(HORIZON_SECS, 0.1, WB, vp, C=C)
vp.show()
input("being visualization: [hit enter]")
#while we're not at our destination yet
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
while norm > 2:
vp.show(interactive=0)
path = closest_path_point(path, a.state, vp)
start_time = time.time()
controls, viz = mpc.MPC(a.state, path, a, C=C, d=d)
time_f = time.time()
print("optimization time: ", time_f - start_time)
a.dynamics_step(controls[:,0])
norm = np.linalg.norm(a.state[:2] - path[:2, -1])
print("GOAL REACHED")
vp.show(interactive=1)
def main():
vp = vtk.Plotter(size=(1080, 720), axes=0, interactive=0)
map = Map(vp)
#follow_path(vp, map)
#go_around_box(vp, map)
go_around_moving_box(vp, map)
vp.show(interactive=1)
main()