Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
debug_plot.py
Go to the documentation of this file.
1import matplotlib.pyplot as plt
2import numpy as np
3import os
4
5def list_txt_files(folder_path):
6 txt_files = sorted([f for f in os.listdir(folder_path) if f.endswith('.txt')])
7 for i, file in enumerate(txt_files, 1):
8 print(f"{i}. {file}")
9 return txt_files
10
11def parse_data_file(filename):
12 """
13 Read the file and extract path, cone, and car data.
14
15 Returns:
16 tuple: (path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y)
17 """
18 path_x, path_y = [], []
19 cone_x, cone_y, cone_colors = [], [], []
20 car_x, car_y = None, None
21
22 with open(filename, 'r') as f:
23 for line in f:
24 parts = line.strip().split()
25 if not parts:
26 continue
27 key = parts[0]
28 if key == 'P':
29 path_x.append(float(parts[1]))
30 path_y.append(float(parts[2]))
31 elif key == 'C':
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')
35 elif key == 'V':
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
39
40def compute_limits(all_x, all_y, default_x=(-10, 10), default_y=(-10, 10)):
41 """
42 Compute axis limits given all data points and default limits.
43
44 Returns:
45 tuple: (x_min, x_max, y_min, y_max)
46 """
47 if all_x:
48 x_min = min(default_x[0], min(all_x))
49 x_max = max(default_x[1], max(all_x))
50 else:
51 x_min, x_max = default_x
52 if all_y:
53 y_min = min(default_y[0], min(all_y))
54 y_max = max(default_y[1], max(all_y))
55 else:
56 y_min, y_max = default_y
57 return x_min, x_max, y_min, y_max
58
59def plot_parsed_data(ax, path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y, min_x, min_y):
60 """
61 Plot the parsed data on the provided axes.
62 """
63 # Plot cones.
64 ax.scatter(cone_x, cone_y, c=cone_colors, edgecolors='black', s=100)
65
66 # Plot path data.
67 if path_x and path_y:
68 ax.plot(path_x, path_y, 'r-', linewidth=1, zorder=1)
69 if len(path_x) > 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)
72
73 # Plot car position if available.
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)
76
77 # Compute and set axis limits.
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)
83
84 # Set labels and grid.
85 ax.grid(True)
86 ax.set_xlabel('X')
87 ax.set_ylabel('Y')
88 ax.set_title('Track Visualization')
89
90def plot_data(filename, min_x=(-10, 10), min_y=(-10, 10)):
91 """
92 Parse the file and plot the track, cones, and car position.
93
94 Parameters:
95 filename (str): Path to the data file.
96 min_x (tuple): Default x-axis limits.
97 min_y (tuple): Default y-axis limits.
98 """
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)
102 plt.show()
103
104def main():
105 folder_path = "/home/ws/src/planning/test/integration_tests/results/"
106 txt_files = list_txt_files(folder_path)
107
108 while True:
109 try:
110 choice = int(input("\nEnter the number of the file to plot (0 to exit): "))
111 if choice == 0:
112 break
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))
116 else:
117 print("Invalid selection. Please try again.")
118 except ValueError:
119 print("Please enter a valid number.")
120
121if __name__ == "__main__":
122 main()
parse_data_file(filename)
Definition debug_plot.py:11
plot_parsed_data(ax, path_x, path_y, cone_x, cone_y, cone_colors, car_x, car_y, min_x, min_y)
Definition debug_plot.py:59
list_txt_files(folder_path)
Definition debug_plot.py:5
compute_limits(all_x, all_y, default_x=(-10, 10), default_y=(-10, 10))
Definition debug_plot.py:40
plot_data(filename, min_x=(-10, 10), min_y=(-10, 10))
Definition debug_plot.py:90
Definition main.py:1