Skip to content

Commit 25c6d7a

Browse files
committed
Implement more stable gradient calculation in _calc_kappa_from_xy by using edge order 2, circular padding
1 parent 77c450a commit 25c6d7a

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

f1tenth_gym/envs/track/cubic_spline.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,16 @@ def _calc_yaw_from_xy(self, x, y):
150150
return heading
151151

152152
def _calc_kappa_from_xy(self, x, y):
153-
dx_dt = np.gradient(x, 2)
154-
dy_dt = np.gradient(y, 2)
155-
d2x_dt2 = np.gradient(dx_dt, 2)
156-
d2y_dt2 = np.gradient(dy_dt, 2)
157-
curvature = -(d2x_dt2 * dy_dt - dx_dt * d2y_dt2) / (dx_dt * dx_dt + dy_dt * dy_dt)**1.5
158-
return curvature
153+
# For more stable gradients, extend x and y by two (edge_order 2) elements on each side
154+
# The elements are taken from the other side of the array
155+
x_extended = np.concatenate((x[-2:], x, x[:2]))
156+
y_extended = np.concatenate((y[-2:], y, y[:2]))
157+
dx_dt = np.gradient(x_extended, edge_order=2)
158+
dy_dt = np.gradient(y_extended, edge_order=2)
159+
d2x_dt2 = np.gradient(dx_dt, edge_order=2)
160+
d2y_dt2 = np.gradient(dy_dt, edge_order=2)
161+
curvature = (dx_dt * d2y_dt2 - d2x_dt2 * dy_dt) / (dx_dt * dx_dt + dy_dt * dy_dt)**1.5
162+
return curvature[2:-2]
159163

160164
def calc_position(self, s: float) -> np.ndarray:
161165
"""

0 commit comments

Comments
 (0)