4 from gazebo_msgs.msg
import ModelStates
5 from std_msgs.msg
import Empty
8 from matplotlib
import pyplot
as plt
11 from track_geometry
import OUTER_WALLS, INNER_WALLS
13 DISTANCE_THRESHOLD = 1
16 current_position =
None 24 global current_path, current_position
25 current_position = np.array([
26 message.pose[1].position.x,
27 message.pose[1].position.y
30 if len(current_path) > 0:
31 distance = np.linalg.norm(current_position - current_path[-1]).item()
32 if distance > DISTANCE_THRESHOLD:
33 path = np.stack(current_path)
37 if len(current_path) == 0:
38 spawns.append(current_position)
40 current_path.append(current_position)
44 if current_position
is not None:
45 crashes.append(current_position)
48 rospy.init_node(
'plot_path', anonymous=
True)
49 rospy.Subscriber(
"/gazebo/model_states", ModelStates, model_state_callback)
50 rospy.Subscriber(
"/crash", Empty, crash_callback)
52 print(
"Recording path... Press CTRL+C to stop recording and create a plot.")
54 while not rospy.is_shutdown():
57 if len(current_path) > 0:
58 paths.append(np.stack(current_path))
60 x_limits = (np.min(OUTER_WALLS[:, 0]) - MARGIN,
61 np.max(OUTER_WALLS[:, 0]) + MARGIN)
62 y_limits = (np.min(OUTER_WALLS[:, 1]) - MARGIN,
63 np.max(OUTER_WALLS[:, 1]) + MARGIN)
64 size = (x_limits[1] - x_limits[0], y_limits[1] - y_limits[0])
66 fig = plt.figure(figsize=(size[0] * 0.22, size[1] * 0.22))
72 plt.plot(path[:, 0], path[:, 1], color=
'blue', linewidth=0.6)
75 crashes = np.stack(crashes)
76 plt.plot(crashes[:, 0], crashes[:, 1],
'rx', markersize=6)
79 spawns = np.stack(spawns)
80 plt.plot(spawns[:, 0], spawns[:, 1],
'go', markersize=2)
82 plt.plot(INNER_WALLS[:, 0], INNER_WALLS[:, 1], color=
'gray', linewidth=0.6)
83 plt.plot(OUTER_WALLS[:, 0], OUTER_WALLS[:, 1], color=
'gray', linewidth=0.6)
87 while filename
is None or os.path.isfile(filename):
88 filename =
'path-{:03d}.pdf'.format(file_index)
91 extent = plt.gca().get_window_extent().transformed(fig.dpi_scale_trans.inverted())
92 plt.savefig(filename, bbox_inches=extent)
93 print(
'\nPlot saved to {:s}'.format(os.path.abspath(filename)))