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 plot_data(filename, min_x=(-10, 10), min_y=(-10, 10)):
12 path_x, path_y = [], []
13 cone_x, cone_y, cone_colors = [], [], []
14 car_x, car_y = None, None
15
16 with open(filename, 'r') as f:
17 for line in f:
18 data = line.strip().split()
19 if len(data) == 0:
20 continue
21 if data[0] == 'P':
22 path_x.append(float(data[1]))
23 path_y.append(float(data[2]))
24 elif data[0] == 'C':
25 cone_x.append(float(data[1]))
26 cone_y.append(float(data[2]))
27 cone_colors.append('yellow' if data[3] == 'yellow_cone' else 'blue')
28 elif data[0] == 'V':
29 car_x = float(data[1])
30 car_y = float(data[2])
31
32 fig, ax = plt.subplots(figsize=(11, 10))
33
34 ax.scatter(cone_x, cone_y, c=cone_colors, edgecolors='black', s=100)
35
36 ax.plot(path_x[0:], path_y[0:], 'r-', linewidth=1, zorder=1)
37 ax.scatter(path_x[1:], path_y[1:], c='red', s=50, label='Path', zorder=2)
38 ax.scatter(path_x[0], path_y[0], c='orange', s=50, label='Start', zorder=3)
39
40 if car_x is not None and car_y is not None:
41 ax.scatter(car_x, car_y, c='green', s=150, label='Car Position', zorder=4)
42
43 all_x = path_x + cone_x
44 all_y = path_y + cone_y
45
46 x_min = min(min_x[0], min(all_x)) if all_x else min_x[0]
47 x_max = max(min_x[1], max(all_x)) if all_x else min_x[1]
48 y_min = min(min_y[0], min(all_y)) if all_y else min_y[0]
49 y_max = max(min_y[1], max(all_y)) if all_y else min_y[1]
50
51 ax.set_xlim(x_min - 5, x_max + 5)
52 ax.set_ylim(y_min - 5, y_max + 5)
53
54 ax.grid(True)
55 ax.set_xlabel('X')
56 ax.set_ylabel('Y')
57 ax.set_title('Track Visualization')
58 plt.show()
59
60def main():
61 folder_path = "/home/ws/src/planning/test/integration_tests/results/"
62 txt_files = list_txt_files(folder_path)
63
64 # open all the files on the first run
65 for i in range(1, len(txt_files)+1):
66 selected_file = os.path.join(folder_path, txt_files[i-1])
67 plot_data(selected_file, min_x=(-2, 20), min_y=(-2, 20))
68
69 while True:
70 try:
71 choice = int(input("\nEnter the number of the file to plot (0 to exit): "))
72 if choice == 0:
73 break
74 if 1 <= choice <= len(txt_files):
75 selected_file = os.path.join(folder_path, txt_files[choice-1])
76 plot_data(selected_file, min_x=(-5, 20), min_y=(-5, 30))
77 else:
78 print("Invalid selection. Please try again.")
79 except ValueError:
80 print("Please enter a valid number.")
81
82if __name__ == "__main__":
83 main()
plot_data(filename, min_x=(-10, 10), min_y=(-10, 10))
list_txt_files(folder_path)
Definition _debug_plot.py:5
Definition main.py:1