Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
skidpad_mapper.py
Go to the documentation of this file.
1# pylint: skip-file
2# mypy: ignore-errors
3
4#!
5# @brief This script generates the coordinates for the skidpad event of the FSG
6# competition.
7
8import math
9import numpy as np
10import matplotlib.pyplot as plt
11
12# Define track parameters
13INNER_RADIUS, OUTER_RADIUS = 7.625, 10.625
14RADIUS = 9.125
15CENTER_X, CENTER_Y = 9.125, 0
16START_LINE, END_LINE = -15.0, 15.0
17STRAIGHT_POINTS = 5
18ROUND_CASES = 3
19
20# Initialize lists to store coordinates and colors
21xPoints = []
22yPoints = []
23
24xCones = []
25yCones = []
26
27colorList = []
28
29# Parameters for dividing circles and cones
30circleDivisions = 32
31coneDivisions = 16
32
33
34#!
35# @brief Function to shift a value by a fixed amount
36def shift(x):
37 return x + 14.4
38
39
40# =========== Generate Cone Coordinates ===========
41
42# Generate inner cones
43for angle in np.arange(math.pi, -math.pi, -2 * math.pi / coneDivisions):
44 # Calculate coordinates using polar to Cartesian conversion
45 x = CENTER_X + INNER_RADIUS * math.cos(angle)
46 y = CENTER_Y + INNER_RADIUS * math.sin(angle)
47 x = round(x, ROUND_CASES)
48 y = round(y, ROUND_CASES)
49 xCones.append(x)
50 yCones.append(y)
51 colorList.append("#ff0000")
52
53# Generate outer cones
54for angle in np.arange(0, 2 * math.pi, 2 * math.pi / coneDivisions):
55 # Calculate coordinates using polar to Cartesian conversion
56 x = -CENTER_X + INNER_RADIUS * math.cos(angle)
57 y = CENTER_Y + INNER_RADIUS * math.sin(angle)
58 x = round(x, ROUND_CASES)
59 y = round(y, ROUND_CASES)
60 xCones.append(x)
61 yCones.append(y)
62 colorList.append("#0000ff")
63
64# Generate outer cones (continued)
65for angle in np.arange(
66 3 * math.pi / 4, -6.9 * math.pi / 8, -2 * math.pi / coneDivisions
67):
68 # Calculate coordinates using polar to Cartesian conversion
69 x = CENTER_X + OUTER_RADIUS * math.cos(angle)
70 y = CENTER_Y + OUTER_RADIUS * math.sin(angle)
71 x = round(x, ROUND_CASES)
72 y = round(y, ROUND_CASES)
73 xCones.append(x)
74 yCones.append(y)
75 colorList.append("#0000ff")
76
77# Generate outer cones (continued)
78for angle in np.arange(math.pi / 4, 15 * math.pi / 8, 2 * math.pi / coneDivisions):
79 # Calculate coordinates using polar to Cartesian conversion
80 x = -CENTER_X + OUTER_RADIUS * math.cos(angle)
81 y = CENTER_Y + OUTER_RADIUS * math.sin(angle)
82 x = round(x, ROUND_CASES)
83 y = round(y, ROUND_CASES)
84 xCones.append(x)
85 yCones.append(y)
86 colorList.append("#ff0000")
87
88# =========== Generate Straight Section Points ===========
89
90for i in np.arange(START_LINE, 0, abs(START_LINE) // STRAIGHT_POINTS):
91 x = 0.0
92 y = round(i, ROUND_CASES)
93 xPoints.append(x)
94 yPoints.append(y)
95 colorList.append("#00aa00")
96
97# =========== Generate Circular Section Points ===========
98
99for i in range(0, 2):
100 for angle in np.arange(math.pi, -math.pi, -2 * math.pi / circleDivisions):
101 # Calculate coordinates using polar to Cartesian conversion
102 x = CENTER_X + RADIUS * math.cos(angle)
103 y = CENTER_Y + RADIUS * math.sin(angle)
104 x = round(x, ROUND_CASES)
105 y = round(y, ROUND_CASES)
106 xPoints.append(x)
107 yPoints.append(y)
108 colorList.append("#00aa00")
109
110for i in range(0, 2):
111 for angle in np.arange(0, 2 * math.pi, 2 * math.pi / circleDivisions):
112 # Calculate coordinates using polar to Cartesian conversion
113 x = -CENTER_X + RADIUS * math.cos(angle)
114 y = CENTER_Y + RADIUS * math.sin(angle)
115 x = round(x, ROUND_CASES)
116 y = round(y, ROUND_CASES)
117 xPoints.append(x)
118 yPoints.append(y)
119 colorList.append("#00aa00")
120
121# =========== Generate Remaining Straight Section Points ===========
122
123for i in np.arange(0, END_LINE + 0.01, END_LINE // STRAIGHT_POINTS):
124 x = 0.0
125 y = round(i, ROUND_CASES)
126 xPoints.append(x)
127 yPoints.append(y)
128 colorList.append("#00aa00")
129
130# =========== Reflect Cone and Point Coordinates ===========
131
132for i in range(len(yCones)):
133 yCones[i], xCones[i] = -xCones[i], shift(yCones[i])
134
135for i in range(len(yPoints)):
136 yPoints[i], xPoints[i] = -xPoints[i], shift(yPoints[i])
137
138# Print lengths of lists for debugging
139print(len(xPoints))
140print(len(yPoints))
141print(len(xCones))
142print(len(yCones))
143print(len(yPoints + yCones))
144print(len(colorList))
145
146# =========== Write Data to Files ===========
147
148# Write point coordinates to a file ("skidpad.txt")
149f1 = open("skidpad.txt", "w")
150f1.write(f"{1.0} {0.0}\n")
151for i in range(1, len(xPoints)):
152 f1.write(f"{xPoints[i]} {yPoints[i]}\n")
153f1.close()
154
155# Write cone coordinates to a file ("skidpad_map.txt")
156f2 = open("skidpad_map.txt", "w")
157for i in range(1, len(xCones)):
158 f2.write(f"{xCones[i]} {yCones[i]}\n")
159f2.close()
160
161# =========== Visualize Cones and Points ===========
162
163# Scatter plot visualization
164plt.scatter(xCones + xPoints, yCones + yPoints, s=2, c=colorList)
165plt.axis("equal")
166plt.show()
double round(double number, int n)
round to n decimal places
Definition tests.cpp:17