13 Read the file and extract path, cone, and car data.
16 tuple: (path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y)
18 path_x, path_y = [], []
19 cone_x, cone_y, cone_colors = [], [], []
20 car_x, car_y =
None,
None
22 with open(filename,
'r')
as f:
24 parts = line.strip().split()
29 path_x.append(float(parts[1]))
30 path_y.append(float(parts[2]))
32 cone_x.append(float(parts[1]))
33 cone_y.append(float(parts[2]))
34 cone_colors.append(
'yellow' if parts[3] ==
'yellow_cone' else 'blue')
36 car_x = float(parts[1])
37 car_y = float(parts[2])
38 return path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y
42 Compute axis limits given all data points and default limits.
45 tuple: (x_min, x_max, y_min, y_max)
48 x_min = min(default_x[0], min(all_x))
49 x_max = max(default_x[1], max(all_x))
51 x_min, x_max = default_x
53 y_min = min(default_y[0], min(all_y))
54 y_max = max(default_y[1], max(all_y))
56 y_min, y_max = default_y
57 return x_min, x_max, y_min, y_max
59def plot_parsed_data(ax, path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y, min_x, min_y):
61 Plot the parsed data on the provided axes.
64 ax.scatter(cone_x, cone_y, c=cone_colors, edgecolors=
'black', s=100)
68 ax.plot(path_x, path_y,
'r-', linewidth=1, zorder=1)
70 ax.scatter(path_x[1:], path_y[1:], c=
'red', s=50, label=
'Path', zorder=2)
71 ax.scatter(path_x[0], path_y[0], c=
'orange', s=50, label=
'Start', zorder=3)
74 if car_x
is not None and car_y
is not None:
75 ax.scatter(car_x, car_y, c=
'green', s=150, label=
'Car Position', zorder=4)
78 all_x = path_x + cone_x
79 all_y = path_y + cone_y
80 x_min, x_max, y_min, y_max =
compute_limits(all_x, all_y, min_x, min_y)
81 ax.set_xlim(x_min - 5, x_max + 5)
82 ax.set_ylim(y_min - 5, y_max + 5)
88 ax.set_title(
'Track Visualization')
90def plot_data(filename, min_x=(-10, 10), min_y=(-10, 10)):
92 Parse the file and plot the track, cones, and car position.
95 filename (str): Path to the data file.
96 min_x (tuple): Default x-axis limits.
97 min_y (tuple): Default y-axis limits.
99 path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y =
parse_data_file(filename)
100 _, ax = plt.subplots(figsize=(10, 8))
101 plot_parsed_data(ax, path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y, min_x, min_y)
105 folder_path =
"/home/ws/src/planning/test/integration_tests/results/"
110 choice = int(input(
"\nEnter the number of the file to plot (0 to exit): "))
113 if 1 <= choice <= len(txt_files):
114 selected_file = os.path.join(folder_path, txt_files[choice-1])
115 plot_data(selected_file, min_x=(-5, 20), min_y=(-5, 30))
117 print(
"Invalid selection. Please try again.")
119 print(
"Please enter a valid number.")
plot_parsed_data(ax, path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y, min_x, min_y)