1 # -*- coding: utf-8 -*-
2 """This scipt loads scenario and result trajectory from files and plots it."""
4 from math import copysign, cos, pi, sin
5 from matplotlib import pyplot as plt
8 sign = lambda x: copysign(1, x)
36 "trajectory-frame": "lightblue",
38 "log": ("gold", "orange", "blueviolet", "blue", "navy", "black"),
42 """Return `xcoords`, `ycoords` arrays of car frame.
45 pose -- The pose of a car.
47 dr = (LENGTH - WHEEL_BASE) / 2
51 lfx += (WIDTH / 2.0) * cos(pose[2] + pi / 2.0)
52 lfx += df * cos(pose[2])
53 lfx += SAFETY_DIST * cos(pose[2])
56 lrx += (WIDTH / 2.0) * cos(pose[2] + pi / 2.0)
57 lrx += -dr * cos(pose[2])
58 lrx += -SAFETY_DIST * cos(pose[2])
61 rrx += (WIDTH / 2.0) * cos(pose[2] - pi / 2.0)
62 rrx += -dr * cos(pose[2])
63 rrx += -SAFETY_DIST * cos(pose[2])
66 rfx += (WIDTH / 2.0) * cos(pose[2] - pi / 2.0)
67 rfx += df * cos(pose[2])
68 rfx += SAFETY_DIST * cos(pose[2])
71 lfy += (WIDTH / 2.0) * sin(pose[2] + pi / 2.0)
72 lfy += df * sin(pose[2])
73 lfy += SAFETY_DIST * sin(pose[2])
76 lry += (WIDTH / 2.0) * sin(pose[2] + pi / 2.0)
77 lry += -dr * sin(pose[2])
78 lry += -SAFETY_DIST * sin(pose[2])
81 rry += (WIDTH / 2.0) * sin(pose[2] - pi / 2.0)
82 rry += -dr * sin(pose[2])
83 rry += -SAFETY_DIST * sin(pose[2])
86 rfy += (WIDTH / 2.0) * sin(pose[2] - pi / 2.0)
87 rfy += df * sin(pose[2])
88 rfy += SAFETY_DIST * sin(pose[2])
90 xcoords = (lfx, lrx, rrx, rfx)
91 ycoords = (lfy, lry, rry, rfy)
92 return (xcoords, ycoords)
94 def load_scenario(fname):
95 """Load scenario from file."""
97 raise ValueError("File name as argument needed")
98 with open(fname, "r") as f:
99 scenario = loads(f.read())
102 def load_trajectory(fname):
103 """Load trajectory from file."""
105 raise ValueError("File name as argument needed")
106 with open(fname, "r") as f:
107 trajectory = loads(f.read())
110 def plot_nodes(nodes=[]):
111 """Return xcoords, ycoords of nodes to plot.
114 nodes -- The list of nodes to plot.
121 return (xcoords, ycoords)
123 def plot_segments(segments=[]):
124 """Return xcoords, ycoords of segments to plot.
127 segments -- The list of segments to plot.
131 if __name__ == "__main__":
132 s = load_scenario(SCEN_FILE)
134 t = load_trajectory(TRAJ_FILE) # fixed to trajectories
138 plt.rcParams["font.size"] = 24
139 plt.rcParams['hatch.linewidth'] = 1.0
145 ax = fig.add_subplot(121)
146 ax.set_aspect("equal")
147 ax.set_title("Final path")
148 ax.set_xlabel("x [m]")
149 ax.set_ylabel("y [m]")
153 plt.plot(*plot_nodes(o["segment"]), color="black")
157 ax.add_artist(plt.Circle((o["circle"][0], o["circle"][1]),
159 color="black", fill=False, hatch="//"))
164 plt.plot(*plot_nodes(t["node"]), color=COLOR["node"],
165 marker=".", linestyle = "None")
170 for edges in t["edge"]:
172 plt.plot([e[0][0], e[1][0]], [e[0][1], e[1][1]],
180 plt.plot(*car_frame(i), color=COLOR["sample"])
182 plt.plot(*plot_nodes(t["samp"]), color=COLOR["sample"],
183 marker=".", linestyle = "None")
185 print("No RRTSample")
188 for path in range(len(t["path"])):
189 plt.plot(*plot_nodes(t["path"][path]), color=COLOR["path"])
194 for traj in range(len(t["traj"])):
196 for i in t["traj"][traj]:
197 plt.plot(*car_frame(i), color=COLOR["log"][traj],
198 label=t["cost"][traj], lw=1)
202 *plot_nodes(t["traj"][traj]),
203 color=COLOR["log"][traj],
204 label=t["cost"][traj])
207 *plot_nodes(t["traj"][traj]),
209 label=t["cost"][traj])
211 print("No trajectory")
213 plt.plot(*car_frame(s["init"]), color="red", lw=2)
214 plt.plot(*car_frame(s["goal"]), color="red", lw=2)
215 plt.plot(*plot_nodes([s["init"]]), color="red", marker="+", ms=12)
216 plt.plot(*plot_nodes([s["goal"]]), color="red", marker="+", ms=12)
219 [-2.7, 3.25, 3.25, -2.7],
221 [26, 26, 26.5, 26.5],
222 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
224 [-2.7, -2.7, -0, -0, -2.2, -2.2, 0, 0, -2.7],
225 [1.5, 26.5, 26.5, 19.5, 19.5, 13, 13, 1.5, 1.5],
226 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
228 [3.25, 3.25, 2.75, 2.75, 3.25],
229 [1.5, 26.5, 26.5, 1.5, 1.5],
230 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
232 #plt.text(1, 0.2, s="1", color="red")
233 #plt.text(2, 12.2, s="2", color="red")
234 #plt.text(3.6, 10, s="3", color="black")
235 #plt.text(-0.5, 14.3, s="4", color="black")
236 #plt.text(3, 18, s="5", color="blue")
237 #plt.text(-4.7, 0.8, s="6", color="orange")
238 #plt.text(-2, 11, s="7", color="gray")
240 handles, labels = ax.get_legend_handles_labels()
245 ax = fig.add_subplot(122)
246 ax.set_aspect("equal")
247 ax.set_title("All edges")
248 ax.set_xlabel("x [m]")
249 ax.set_ylabel("y [m]")
253 plt.plot(*plot_nodes(o["segment"]), color="black")
257 ax.add_artist(plt.Circle((o["circle"][0], o["circle"][1]),
259 color="black", fill=False, hatch="//"))
264 plt.plot(*plot_nodes(t["node"]), color=COLOR["node"],
265 marker=".", linestyle = "None")
270 for edges in t["edge"]:
272 plt.plot([e[0][0], e[1][0]], [e[0][1], e[1][1]],
273 color=COLOR["edge"], lw=1)
280 plt.plot(*car_frame(i), color=COLOR["sample"])
282 plt.plot(*plot_nodes(t["samp"]), color=COLOR["sample"],
283 marker=".", linestyle = "None")
285 print("No RRTSample")
288 for path in range(len(t["path"])):
289 plt.plot(*plot_nodes(t["path"][path]), color=COLOR["path"])
294 for traj in range(len(t["traj"])):
296 for i in t["traj"][traj]:
297 plt.plot(*car_frame(i), color=COLOR["log"][traj],
298 label=t["cost"][traj])
302 *plot_nodes(t["traj"][traj]),
303 color=COLOR["log"][traj],
304 label=t["cost"][traj])
307 *plot_nodes(t["traj"][traj]),
309 label=t["cost"][traj])
311 print("No trajectory")
313 plt.plot(*car_frame(s["init"]), color="red", lw=2)
314 plt.plot(*car_frame(s["goal"]), color="red", lw=2)
315 plt.plot(*plot_nodes([s["init"]]), color="red", marker="+", ms=12)
316 plt.plot(*plot_nodes([s["goal"]]), color="red", marker="+", ms=12)
319 [-2.7, 3.25, 3.25, -2.7],
321 [26, 26, 26.5, 26.5],
322 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
324 [-2.7, -2.7, -0, -0, -2.2, -2.2, 0, 0, -2.7],
325 [1.5, 26.5, 26.5, 19.5, 19.5, 13, 13, 1.5, 1.5],
326 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
328 [3.25, 3.25, 2.75, 2.75, 3.25],
329 [1.5, 26.5, 26.5, 1.5, 1.5],
330 facecolor="none", hatch="//", edgecolor="black", linewidth=0)
332 handles, labels = ax.get_legend_handles_labels()