Skip to content

Commit 22e92ed

Browse files
authored
Simplified calc_target_index function in stanley_controller.py
Used: - np.hypot to replace np.sqrt(x**2 + y**2) - np.argmin to find location of a minimum in a numpy array
1 parent 153cfd0 commit 22e92ed

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

PathTracking/stanley_controller/stanley_controller.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,19 @@ def calc_target_index(state, cx, cy):
127127
:param cy: [float]
128128
:return: (int, float)
129129
"""
130-
# Calc front axle position
130+
# Calc front axle position
131131
fx = state.x + L * np.cos(state.yaw)
132132
fy = state.y + L * np.sin(state.yaw)
133133

134134
# Search nearest point index
135135
dx = [fx - icx for icx in cx]
136136
dy = [fy - icy for icy in cy]
137-
d = [np.sqrt(idx ** 2 + idy ** 2) for (idx, idy) in zip(dx, dy)]
138-
closest_error = min(d)
139-
target_idx = d.index(closest_error)
137+
d = np.hypot(dx, dy)
138+
target_idx = np.argmin(d)
140139

141140
# Project RMS error onto front axle vector
142141
front_axle_vec = [-np.cos(state.yaw + np.pi / 2),
143-
- np.sin(state.yaw + np.pi / 2)]
142+
-np.sin(state.yaw + np.pi / 2)]
144143
error_front_axle = np.dot([dx[target_idx], dy[target_idx]], front_axle_vec)
145144

146145
return target_idx, error_front_axle

0 commit comments

Comments
 (0)