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("RRT* final path")
143 ax.set_xlabel("Longitudinal direction [m]")
144 ax.set_ylabel("Lateral direction [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))
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(4):#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(*plot_nodes([s["init"]]), color=COLOR["start"], marker=".")
209 plt.plot(*plot_nodes([s["goal"]]), color=COLOR["goal"], marker=".")
212 handles, labels = ax.get_legend_handles_labels()
213 #lgd = ax.legend(handles, labels, loc="upper center",
214 # bbox_to_anchor=(0.5, -0.11), title="Cost")
216 ax = fig.add_subplot(122)
217 ax.set_aspect("equal")
218 ax.set_title("RRT* all edges")
219 ax.set_xlabel("Longitudinal direction [m]")
220 ax.set_ylabel("Lateral direction [m]")
225 plt.plot(*plot_nodes(o["segment"]), color="black")
229 ax.add_artist(plt.Circle((o["circle"][0], o["circle"][1]),
231 color="black", fill=False))
236 plt.plot(*plot_nodes(t["node"]), color=COLOR["node"],
237 marker=".", linestyle = "None")
242 for edges in t["edge"]:
244 plt.plot([e[0][0], e[1][0]], [e[0][1], e[1][1]],
252 plt.plot(*car_frame(i), color=COLOR["sample"])
254 plt.plot(*plot_nodes(t["samp"]), color=COLOR["sample"],
255 marker=".", linestyle = "None")
257 print("No RRTSample")
260 for path in range(len(t["path"])):
261 plt.plot(*plot_nodes(t["path"][path]), color=COLOR["path"])
266 for traj in range(len(t["traj"])):
268 for i in t["traj"][traj]:
269 plt.plot(*car_frame(i), color=COLOR["log"][traj],
270 label=t["cost"][traj])
274 *plot_nodes(t["traj"][traj]),
275 color=COLOR["log"][traj],
276 label=t["cost"][traj])
279 *plot_nodes(t["traj"][traj]),
281 label=t["cost"][traj])
283 print("No trajectory")
284 plt.plot(*plot_nodes([s["init"]]), color=COLOR["start"], marker=".")
285 plt.plot(*plot_nodes([s["goal"]]), color=COLOR["goal"], marker=".")
288 handles, labels = ax.get_legend_handles_labels()
289 #lgd = ax.legend(handles, labels, loc="upper center",
290 # bbox_to_anchor=(0.5, -0.11), title="Cost")
294 #plt.savefig("{}.png".format(argv[2]), bbox_extra_artists=(lgd,),
295 # bbox_inches='tight')