-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.py
436 lines (375 loc) · 11.8 KB
/
helpers.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
import os
import json
import matplotlib as mpl
import matplotlib.animation as animation
import numpy as np
import pylab as plt
PATH_OBSTACLES = {
"barrel-racing": ["B1", "B2", "B8", "D1", "D2", "D5", "D10"],
"slalom": ["B1", "B2", "D1", "D2", "D4", "D5", "D6", "D7", "D8", "D10"],
"slalom-simplified": ["B2", "D2", "D4", "D8", "D10"],
}
DEFAULT_OBSTACLE_DIA_IN = 3 # inches
def in2m(inches):
# Inches to meters
return inches * 2.54 / 100
def rotate_around_origin(point, theta):
x, y = point
return (
x * np.cos(theta) - y * np.sin(theta),
y * np.cos(theta) + x * np.sin(theta),
)
def transform_geometry(geometry, pose):
x, y, theta = pose
transformed_geometry = []
for point1, point2 in geometry:
new_point1 = rotate_around_origin(point1, theta) + np.array([x, y])
new_point2 = rotate_around_origin(point2, theta) + np.array([x, y])
transformed_geometry.append((new_point1, new_point2))
return transformed_geometry
def plot_robot(ax, pose, robot_geometry, robot_axis_plot_size):
# Plot robot geometry
ax.add_collection(
mpl.collections.LineCollection(
transform_geometry(robot_geometry, pose), color="k"
)
)
# Plot robot axes
ax.add_collection(
mpl.collections.LineCollection(
transform_geometry([[(0, 0), (robot_axis_plot_size, 0)]], pose),
color="r",
)
)
ax.add_collection(
mpl.collections.LineCollection(
transform_geometry([[(0, 0), (0, robot_axis_plot_size)]], pose),
color="g",
)
)
def plot_traj(
title,
xs,
ys,
thetas,
obstacles,
robot_geometry,
robot_axis_plot_size,
targets=[],
goal=None,
limits=(-1, in2m(360) + 1, -1, in2m(180) + 1),
draw_bounds=True,
invert=False,
robot_plot_mod=5,
save_png=False,
):
plt.style.use("ggplot")
fig, ax = plt.subplots()
if draw_bounds:
ax.add_patch(
mpl.patches.Rectangle(
(0, 0),
in2m(360),
in2m(180),
linewidth=1,
edgecolor="k",
facecolor="none",
)
)
for obx, oby, obr in obstacles:
ax.add_artist(plt.Circle((obx, oby), obr, color="r"))
for tx, ty, tr in targets:
ax.add_artist(plt.Circle((tx, ty), tr, color="g"))
if goal is not None:
ax.add_collection(
mpl.collections.LineCollection(
[goal],
color="r",
linewidths=4,
)
)
plt.scatter(xs, ys, marker=".", color="#FAB604")
for i, pose in enumerate(zip(xs, ys, thetas)):
if robot_plot_mod is None:
if i == 0:
plot_robot(ax, pose, robot_geometry, robot_axis_plot_size)
else:
if i % robot_plot_mod == 0:
plot_robot(ax, pose, robot_geometry, robot_axis_plot_size)
# Always plot last pose
if i == len(xs) - 1:
plot_robot(ax, pose, robot_geometry, robot_axis_plot_size)
plt.xlabel("X position (m)")
plt.ylabel("Y position (m)")
plt.xlim(limits[0], limits[1])
plt.ylim(limits[2], limits[3])
plt.gca().set_aspect("equal", adjustable="box")
plt.title(title)
if invert:
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()
if save_png:
if not os.path.exists("renders"):
os.makedirs("renders")
plt.savefig(
os.path.join("renders", "{}.png".format(title)), transparent=True, dpi=800
)
def anim_traj(
title,
xs,
ys,
thetas,
obstacles,
robot_geometry,
robot_axis_plot_size,
timestep,
invert=False,
targets=[],
goal=None,
limits=(-1, in2m(360) + 1, -1, in2m(180) + 1),
draw_bounds=True,
save_gif=False,
):
fig, ax = plt.subplots()
num_states = len(xs)
if draw_bounds:
if draw_bounds:
ax.add_patch(
mpl.patches.Rectangle(
(0, 0),
in2m(360),
in2m(180),
linewidth=1,
edgecolor="k",
facecolor="none",
)
)
for obx, oby, obr in obstacles:
ax.add_artist(plt.Circle((obx, oby), obr, color="r"))
for tx, ty, tr in targets:
ax.add_artist(plt.Circle((tx, ty), in2m(6), color="g"))
if goal is not None:
ax.add_collection(
mpl.collections.LineCollection(
[goal],
color="r",
linewidths=4,
)
)
plt.scatter(xs, ys, marker=".")
# Plot first pose
plot_robot(ax, (xs[0], ys[0], thetas[0]), robot_geometry, robot_axis_plot_size)
# Plot last pose
plot_robot(
ax,
(xs[num_states - 1], ys[num_states - 1], thetas[num_states - 1]),
robot_geometry,
robot_axis_plot_size,
)
# Animation function
def animate(i):
if save_gif:
print("Rendering Frame: {}".format(i))
pose = list(zip(xs, ys, thetas))[i]
# Hack to remove the old robot poses
ax.collections = ax.collections[:7]
plot_robot(ax, pose, robot_geometry, robot_axis_plot_size)
return ax.collections
plt.xlabel("X position (m)")
plt.ylabel("Y position (m)")
plt.xlim(limits[0], limits[1])
plt.ylim(limits[2], limits[3])
plt.gca().set_aspect("equal", adjustable="box")
plt.title(title)
if invert:
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()
anim = animation.FuncAnimation(
fig, animate, frames=num_states, interval=timestep, blit=True, repeat=True
)
if save_gif:
if not os.path.exists("renders"):
os.makedirs("renders")
anim.save(
os.path.join("renders", "{}.gif".format(title)),
writer="pillow",
dpi=100,
fps=(int)(1 / (timestep / 1000)),
)
save_gif = False
return anim
def load_init_json(init_json, start_pose, num_states):
x1, y1, theta1 = start_pose
with open(init_json) as f:
init_traj = json.load(f)
init_traj_len = len(init_traj)
n_per_waypoint = int((num_states + 1) / init_traj_len)
x_init = []
y_init = []
theta_init = []
for i in range(init_traj_len):
x2, y2 = init_traj[i]
theta2 = np.arctan2(y2 - y1, x2 - x1)
theta2 = np.unwrap([theta1, theta2])[-1]
x_init += list(np.linspace(x1, x2, n_per_waypoint))
y_init += list(np.linspace(y1, y2, n_per_waypoint))
theta_init += list(np.linspace(theta1, theta2, n_per_waypoint))
x1, y1, theta1 = x2, y2, theta2
# Fill in remainder, if any
remainder = num_states - len(x_init) + 1
x_init += [x2] * remainder
y_init += [y2] * remainder
theta_init += [theta2] * remainder
return x_init, y_init, theta_init
def plot_wheel_vel_accel_jerk(times, vl, vr, al, ar, jl, jr):
plt.plot(
times,
vl,
label="Left Wheel Velocity",
linewidth=4,
color="red",
)
plt.plot(
times,
vr,
label="Right Wheel Velocity",
linewidth=4,
color="blue",
)
plt.plot(
times,
al,
label="Left Wheel Acceleration",
color="firebrick",
)
plt.plot(
times,
ar,
label="Right Wheel Acceleration",
color="royalblue",
)
plt.plot(
times,
jl,
label="Left Wheel Jerk",
linestyle="--",
color="lightcoral",
)
plt.plot(
times,
jr,
label="Right Wheel Jerk",
linestyle="--",
color="cornflowerblue",
)
plt.legend(loc="lower left")
plt.xlabel("Time (s)")
def plot_wheel_forces(times, lon_fl, lon_fr, lat_f):
plt.plot(times, lon_fl, label="Longitudinal Force (Left)")
plt.plot(times, lon_fr, label="Longitudinal Force (Right)")
plt.plot(times, lat_f, label="Centripetal Force")
plt.legend(loc="lower left")
plt.xlabel("Time (s)")
plt.ylabel("Force (N)")
def plot_total_force(times, total_force_left, total_force_right):
plt.plot(
times,
total_force_left,
label="Total Force (Left)",
)
plt.plot(
times,
total_force_right,
label="Total Force (Right)",
)
plt.legend(loc="lower left")
plt.xlabel("Time (s)")
plt.ylabel("Force (N)")
def interp_state_vector(times, states, new_dt):
interp_times = np.arange(0, times[-1], new_dt)
return np.interp(interp_times, times, states[:-1])
def ids2obstales(ids, diameter):
obstacles = []
for id in ids:
id = id.upper()
row = ord(id[0]) - 64
col = int(id[1:])
x = in2m(30) * col
y = in2m(180) - row * in2m(30)
obstacles.append((x, y, diameter / 2))
return obstacles
def create_obstacles(pathname, diameter_inches=DEFAULT_OBSTACLE_DIA_IN):
return ids2obstales(PATH_OBSTACLES[pathname], in2m(diameter_inches))
def besph2cart(bearing: float, elevation: float):
"""Converts a bearing and elevation in spherical coordinates to a 3D cartesian point on the unit circle."""
return np.array(
[
np.cos(bearing) * np.cos(elevation),
np.sin(bearing) * np.cos(elevation),
np.sin(elevation),
]
)
def cart2besph(x: float, y: float, z: float):
"""Converts the direction of a 3D cartesian point to a bearing and elevation in spherical coordinates."""
return np.array(
[np.arctan2(y, x), np.arctan2(z, np.sqrt(np.power(x, 2) + np.power(y, 2)))]
)
class LineBuilder:
def __init__(self, line):
self.line = line
self.xs = list(line.get_xdata())
self.ys = list(line.get_ydata())
self.dragging = None
self.connect()
def connect(self):
self.cidpress = self.line.figure.canvas.mpl_connect(
"button_press_event", self.on_press
)
self.cidrelease = self.line.figure.canvas.mpl_connect(
"button_release_event", self.on_release
)
self.cidmotion = self.line.figure.canvas.mpl_connect(
"motion_notify_event", self.on_motion
)
def disconnect(self):
self.line.figure.canvas.mpl_disconnect(self.cidpress)
self.line.figure.canvas.mpl_disconnect(self.cidrelease)
self.line.figure.canvas.mpl_disconnect(self.cidmotion)
def on_press(self, event):
if event.inaxes != self.line.axes:
return
point_threshold = 0.1
# Find nearest point to cursor
min_distance = float("inf")
for i, point in enumerate(zip(self.xs, self.ys)):
x, y = point
distance = np.sqrt((event.xdata - x) ** 2 + (event.ydata - y) ** 2)
if distance < min_distance:
min_distance = distance
closest_index = i
if event.button == 1:
if min_distance < point_threshold:
self.dragging = closest_index
else:
self.xs.append(event.xdata)
self.ys.append(event.ydata)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
elif event.button == 3:
if min_distance < point_threshold:
self.xs.pop(closest_index)
self.ys.pop(closest_index)
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
def on_motion(self, event):
if self.dragging is None:
return
if event.inaxes != self.line.axes:
return
self.xs[self.dragging] = event.xdata
self.ys[self.dragging] = event.ydata
self.line.set_data(self.xs, self.ys)
self.line.figure.canvas.draw()
def on_release(self, event):
self.dragging = None
self.line.figure.canvas.draw()