Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
skidpad_conesgenerator.py
Go to the documentation of this file.
1import math
2import os
3import matplotlib.pyplot as plt
4import numpy as np
5
6# Circle parameters
7centerX = 15.0
8centerY = 9.125
9radius = 7.625
10outerRadius = 10.625 # Outer radius for the second circle
11numPoints = 100 # Number of points to describe the circle
12
13file_name = "./src/planning/src/utils/skidpadcones1.txt"
14
15with open(file_name, "w") as file:
16 # Generate the straight lines
17 for i in range(0,int(centerX*2+5)*2):
18 x = i/2
19 if math.sqrt((x-centerX)**2 + (centerY-1.5)**2) > outerRadius:
20 file.write(f"{x:.3f} {1.5:.3f} {0}\n")
21 file.write(f"{x:.3f} {-1.5:.3f} {0}\n")
22
23 # Generate and print circular path points
24 for i in range(numPoints):
25 theta = (2 * math.pi / numPoints) * i - math.pi / 2 # Angle in radians, start at x=15, y=0
26 x = centerX + radius * math.cos(theta)
27 y = centerY + radius * math.sin(theta)
28 file.write(f"{x:.3f} {-y:.3f} {0}\n") # Fixed: use {-y:.3f} instead of -{y:.3f}
29
30 for i in range(numPoints):
31 theta = (2 * math.pi / numPoints) * i - math.pi / 2 # Angle in radians, start at x=15, y=0
32 x = centerX + outerRadius * math.cos(theta)
33 y = centerY + outerRadius * math.sin(theta)
34 if abs(y)>1.5:
35 file.write(f"{x:.3f} {-y:.3f} {0}\n") # Fixed: use {-y:.3f} instead of -{y:.3f}
36
37 for i in range(numPoints):
38 theta = (2 * math.pi / numPoints) * i - math.pi / 2 # Angle in radians, start at x=15, y=0
39 x = centerX + radius * math.cos(theta)
40 y = centerY + radius * math.sin(theta)
41 file.write(f"{x:.3f} {y:.3f} {0}\n")
42
43 for i in range(numPoints):
44 theta = (2 * math.pi / numPoints) * i - math.pi / 2 # Angle in radians, start at x=15, y=0
45 x = centerX + outerRadius * math.cos(theta)
46 y = centerY + outerRadius * math.sin(theta)
47 if abs(y)>1.5:
48 file.write(f"{x:.3f} {y:.3f} {0}\n")
49
50 file_path = os.path.abspath(file_name)
51 print(f"File written to: {file_path}")
52
53# Draw all the points in matplotlib
54print(f"Generated {numPoints * 4} points in total.")
55# create a plot to visualize the points
56points = np.loadtxt(file_name)
57plt.figure(figsize=(8, 8))
58plt.scatter(points[:, 0], points[:, 1], c=points[:, 2], cmap='viridis')
59plt.colorbar(label='Speed')
60plt.xlabel('X')
61plt.ylabel('Y')
62plt.title('Skidpad Cones')
63plt.axis('equal')
64plt.grid()
65
66# Save plot instead of showing it (for headless environments)
67plt.savefig('./src/planning/src/utils/skidpad_plot.png', dpi=300, bbox_inches='tight')
68print("Plot saved as skidpad_plot.png")
69
70# Only try to show if we have a display
71try:
72 plt.show()
73except:
74 print("Display not available - plot saved to file instead")