Skip to content

Commit 5eeaf90

Browse files
committed
Add support for Track from raceline file
1 parent b94014c commit 5eeaf90

File tree

2 files changed

+70
-27
lines changed

2 files changed

+70
-27
lines changed

f1tenth_gym/envs/track/raceline.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ def from_centerline_file(
133133
)
134134

135135
@staticmethod
136-
def from_raceline_file(filepath: pathlib.Path, delimiter: str = ";", track_scale: Optional[float] = 1.0) -> Raceline:
136+
def from_raceline_file(filepath: pathlib.Path, delimiter: str = ";", skip_rows: int = 3, track_scale: Optional[float] = 1.0) -> Raceline:
137137
"""
138-
Load raceline from a raceline file.
138+
Load raceline from a raceline file of the format [s, x, y, psi, k, vx, ax].
139139
140140
Parameters
141141
----------
@@ -151,22 +151,19 @@ def from_raceline_file(filepath: pathlib.Path, delimiter: str = ";", track_scale
151151
Raceline
152152
raceline object
153153
"""
154+
if type(filepath) is str:
155+
filepath = pathlib.Path(filepath)
156+
154157
assert filepath.exists(), f"input filepath does not exist ({filepath})"
155-
waypoints = np.loadtxt(filepath, delimiter=delimiter).astype(np.float32)
158+
waypoints = np.loadtxt(filepath, delimiter=delimiter, skiprows=
159+
skip_rows).astype(np.float32)
156160

157161
if track_scale != 1.0:
158162
# scale x-y waypoints and recalculate s, psi, and k
159163
waypoints[:, 1] *= track_scale
160164
waypoints[:, 2] *= track_scale
161165
spline = CubicSpline2D(x=waypoints[:, 1], y=waypoints[:, 2])
162-
ss, yaws, ks = [], [], []
163-
for (x, y) in zip(waypoints[:, 1], waypoints[:, 2]):
164-
i_s, _ = spline.calc_arclength(x, y)
165-
yaw = spline.calc_yaw(i_s)
166-
k = spline.calc_curvature(i_s)
167-
yaws.append(yaw)
168-
ks.append(k)
169-
ss.append(i_s)
166+
ss, yaws, ks = spline.ss, spline.psis, spline.ks
170167
waypoints[:, 0] = ss
171168
waypoints[:, 3] = yaws
172169
waypoints[:, 4] = ks

f1tenth_gym/envs/track/track.py

+62-16
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,7 @@ def from_refline(x: np.ndarray, y: np.ndarray, velx: np.ndarray):
256256
margin_perc = 0.1
257257

258258
spline = CubicSpline2D(x=x, y=y)
259-
ss, xs, ys, yaws, ks, vxs = [], [], [], [], [], []
260-
for i_s in np.arange(0, spline.s[-1], ds):
261-
xi, yi = spline.calc_position(i_s)
262-
yaw = spline.calc_yaw(i_s)
263-
k = spline.calc_curvature(i_s)
264-
265-
# find closest waypoint
266-
closest = np.argmin(np.hypot(x - xi, y - yi))
267-
v = velx[closest]
268-
269-
xs.append(xi)
270-
ys.append(yi)
271-
yaws.append(yaw)
272-
ks.append(k)
273-
ss.append(i_s)
274-
vxs.append(v)
259+
ss, xs, ys, yaws, ks, vxs = spline.ss, spline.xs, spline.ys, spline.psis, spline.ks, velx
275260

276261
refline = Raceline(
277262
ss=np.array(ss).astype(np.float32),
@@ -316,6 +301,67 @@ def from_refline(x: np.ndarray, y: np.ndarray, velx: np.ndarray):
316301
raceline=refline,
317302
centerline=refline,
318303
)
304+
305+
def from_raceline_file(filepath: pathlib.Path, delimiter: str = ";", skip_rows: int = 3, track_scale: float = 1.0) -> Track:
306+
"""
307+
Creates a Track object from a raceline file of the format [s, x, y, psi, k, vx, ax].
308+
309+
Args:
310+
filepath (pathlib.Path): path to the raceline file
311+
delimiter (str, optional): delimiter used in the file. Defaults to ";".
312+
skip_rows (int, optional): number of rows to skip. Defaults to 3.
313+
track_scale (float, optional): scale of the track. Defaults to 1.0.
314+
315+
Returns:
316+
Track: track object
317+
"""
318+
raceline = Raceline.from_raceline_file(filepath, delimiter, skip_rows, track_scale)
319+
xs = raceline.xs
320+
ys = raceline.ys
321+
resolution = 0.05
322+
margin_perc = 0.1
323+
324+
min_x, max_x = np.min(xs), np.max(xs)
325+
min_y, max_y = np.min(ys), np.max(ys)
326+
x_range = max_x - min_x
327+
y_range = max_y - min_y
328+
occupancy_map = 255.0 * np.ones(
329+
(
330+
int((1 + 2 * margin_perc) * x_range / resolution),
331+
int((1 + 2 * margin_perc) * y_range / resolution),
332+
),
333+
dtype=np.float32,
334+
)
335+
# origin is the bottom left corner
336+
origin = (min_x - margin_perc * x_range, min_y - margin_perc * y_range, 0.0)
337+
338+
track_spec = TrackSpec(
339+
name=None,
340+
image=None,
341+
resolution=resolution,
342+
origin=origin,
343+
negate=False,
344+
occupied_thresh=0.65,
345+
free_thresh=0.196,
346+
)
347+
348+
track_spec = TrackSpec(
349+
name=None,
350+
image=None,
351+
resolution=0.05,
352+
origin=(0.0, 0.0, 0.0),
353+
negate=False,
354+
occupied_thresh=0.65,
355+
free_thresh=0.196,
356+
)
357+
return Track(
358+
spec=track_spec,
359+
filepath=None,
360+
ext=None,
361+
occupancy_map=occupancy_map,
362+
raceline=raceline,
363+
centerline=raceline,
364+
)
319365

320366
def save_raceline(self, outdir: pathlib.Path):
321367
"""

0 commit comments

Comments
 (0)