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)
35 "trajectory-frame": "lightblue",
37 "log": ("gold", "orange", "blueviolet", "blue", "navy", "black"),
41 """Return `xcoords`, `ycoords` arrays of car frame.
44 pose -- The pose of a car.
46 dr = (LENGTH - WHEEL_BASE) / 2
50 lfx += (WIDTH / 2.0) * cos(pose[2] + pi / 2.0)
51 lfx += df * cos(pose[2])
52 lfx += SAFETY_DIST * cos(pose[2])
55 lrx += (WIDTH / 2.0) * cos(pose[2] + pi / 2.0)
56 lrx += -dr * cos(pose[2])
57 lrx += -SAFETY_DIST * cos(pose[2])
60 rrx += (WIDTH / 2.0) * cos(pose[2] - pi / 2.0)
61 rrx += -dr * cos(pose[2])
62 rrx += -SAFETY_DIST * cos(pose[2])
65 rfx += (WIDTH / 2.0) * cos(pose[2] - pi / 2.0)
66 rfx += df * cos(pose[2])
67 rfx += SAFETY_DIST * cos(pose[2])
70 lfy += (WIDTH / 2.0) * sin(pose[2] + pi / 2.0)
71 lfy += df * sin(pose[2])
72 lfy += SAFETY_DIST * sin(pose[2])
75 lry += (WIDTH / 2.0) * sin(pose[2] + pi / 2.0)
76 lry += -dr * sin(pose[2])
77 lry += -SAFETY_DIST * sin(pose[2])
80 rry += (WIDTH / 2.0) * sin(pose[2] - pi / 2.0)
81 rry += -dr * sin(pose[2])
82 rry += -SAFETY_DIST * sin(pose[2])
85 rfy += (WIDTH / 2.0) * sin(pose[2] - pi / 2.0)
86 rfy += df * sin(pose[2])
87 rfy += SAFETY_DIST * sin(pose[2])
89 xcoords = (lfx, lrx, rrx, rfx)
90 ycoords = (lfy, lry, rry, rfy)
91 return (xcoords, ycoords)
93 def load_scenario(fname):
94 """Load scenario from file."""
96 raise ValueError("File name as argument needed")
97 with open(fname, "r") as f:
98 scenario = loads(f.read())
101 def load_trajectory(fname):
102 """Load trajectory from file."""
104 raise ValueError("File name as argument needed")
105 with open(fname, "r") as f:
106 trajectory = loads(f.read())
109 def plot_nodes(nodes=[]):
110 """Return xcoords, ycoords of nodes to plot.
113 nodes -- The list of nodes to plot.
120 return (xcoords, ycoords)
122 def plot_segments(segments=[]):
123 """Return xcoords, ycoords of segments to plot.
126 segments -- The list of segments to plot.
130 if __name__ == "__main__":
131 s = load_scenario(SCEN_FILE)
133 t = load_trajectory(TRAJ_FILE) # fixed to trajectories
137 plt.rcParams["font.size"] = 24
140 ax = fig.add_subplot(121)
141 ax.set_aspect("equal")
142 ax.set_title("Final path")
143 ax.set_xlabel("x [m]")
144 ax.set_ylabel("y [m]")
149 plt.plot(*plot_nodes(o["segment"]), color="black")
153 ax.add_artist(plt.Circle((o["circle"][0], o["circle"][1]),
155 color="black", fill=False, hatch="/"))
160 plt.plot(*plot_nodes(t["node"]), color=COLOR["node"],
161 marker=".", linestyle = "None")
166 for edges in t["edge"]:
168 plt.plot([e[0][0], e[1][0]], [e[0][1], e[1][1]],
176 plt.plot(*car_frame(i), color=COLOR["sample"])
178 plt.plot(*plot_nodes(t["samp"]), color=COLOR["sample"],
179 marker=".", linestyle = "None")
181 print("No RRTSample")
184 for path in range(len(t["path"])):
185 plt.plot(*plot_nodes(t["path"][path]), color=COLOR["path"])
190 for traj in range(len(t["traj"])):
192 for i in t["traj"][traj]:
193 plt.plot(*car_frame(i), color=COLOR["log"][traj],
194 label=t["cost"][traj])
198 *plot_nodes(t["traj"][traj]),
199 color=COLOR["log"][traj],
200 label=t["cost"][traj])
203 *plot_nodes(t["traj"][traj]),
205 label=t["cost"][traj])
207 print("No trajectory")
208 plt.plot(*car_frame(s["init"]), color="red", lw=2)
209 plt.plot(*car_frame(s["goal"]), color="red", lw=2)
210 plt.plot(*plot_nodes([s["init"]]), color="red", marker="+", ms=12)
211 plt.plot(*plot_nodes([s["goal"]]), color="red", marker="+", ms=12)
214 handles, labels = ax.get_legend_handles_labels()
215 #lgd = ax.legend(handles, labels, loc="upper center",
216 # bbox_to_anchor=(0.5, -0.11), title="Cost")
218 ax = fig.add_subplot(122)
219 ax.set_aspect("equal")
220 ax.set_title("All edges")
221 ax.set_xlabel("x [m]")
222 ax.set_ylabel("y [m]")
227 plt.plot(*plot_nodes(o["segment"]), color="black")
231 ax.add_artist(plt.Circle((o["circle"][0], o["circle"][1]),
233 color="black", fill=False))
238 plt.plot(*plot_nodes(t["node"]), color=COLOR["node"],
239 marker=".", linestyle = "None")
244 for edges in t["edge"]:
246 plt.plot([e[0][0], e[1][0]], [e[0][1], e[1][1]],
254 plt.plot(*car_frame(i), color=COLOR["sample"])
256 plt.plot(*plot_nodes(t["samp"]), color=COLOR["sample"],
257 marker=".", linestyle = "None")
259 print("No RRTSample")
262 for path in range(len(t["path"])):
263 plt.plot(*plot_nodes(t["path"][path]), color=COLOR["path"])
268 for traj in range(len(t["traj"])):
270 for i in t["traj"][traj]:
271 plt.plot(*car_frame(i), color=COLOR["log"][traj],
272 label=t["cost"][traj])
276 *plot_nodes(t["traj"][traj]),
277 color=COLOR["log"][traj],
278 label=t["cost"][traj])
281 *plot_nodes(t["traj"][traj]),
283 label=t["cost"][traj])
285 print("No trajectory")
286 plt.plot(*car_frame(s["init"]), color="red", lw=2)
287 plt.plot(*car_frame(s["goal"]), color="red", lw=2)
288 plt.plot(*plot_nodes([s["init"]]), color="red", marker="+", ms=12)
289 plt.plot(*plot_nodes([s["goal"]]), color="red", marker="+", ms=12)
292 handles, labels = ax.get_legend_handles_labels()
293 #lgd = ax.legend(handles, labels, loc="upper center",
294 # bbox_to_anchor=(0.5, -0.11), title="Cost")
298 #plt.savefig("{}.png".format(argv[2]), bbox_extra_artists=(lgd,),
299 # bbox_inches='tight')