Skip to content

Commit 8f6a30b

Browse files
committed
Optimize calc_nearest_index
1 parent 8652a4b commit 8f6a30b

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

PathTracking/lqr_speed_steer_control/lqr_speed_steer_control.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ def calc_nearest_index(state, cx, cy, cyaw):
142142
dx = [state.x - icx for icx in cx]
143143
dy = [state.y - icy for icy in cy]
144144

145-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
145+
d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)]
146146

147147
mind = min(d)
148148

149149
ind = d.index(mind)
150150

151+
mind = math.sqrt(mind)
152+
151153
dxl = cx[ind] - state.x
152154
dyl = cy[ind] - state.y
153155

PathTracking/lqr_steer_control/lqr_steer_control.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ def calc_nearest_index(state, cx, cy, cyaw):
140140
dx = [state.x - icx for icx in cx]
141141
dy = [state.y - icy for icy in cy]
142142

143-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
143+
d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)]
144144

145145
mind = min(d)
146146

147147
ind = d.index(mind)
148148

149+
mind = math.sqrt(mind)
150+
149151
dxl = cx[ind] - state.x
150152
dyl = cy[ind] - state.y
151153

PathTracking/model_predictive_speed_and_steer_control/model_predictive_speed_and_steer_control.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,22 @@ def calc_nearest_index(state, cx, cy, cyaw, pind):
188188
dx = [state.x - icx for icx in cx[pind:(pind + N_IND_SEARCH)]]
189189
dy = [state.y - icy for icy in cy[pind:(pind + N_IND_SEARCH)]]
190190

191-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
191+
d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)]
192192

193-
min_d = min(d)
193+
mind = min(d)
194194

195-
ind = d.index(min_d) + pind
195+
ind = d.index(mind) + pind
196+
197+
mind = math.sqrt(mind)
196198

197199
dxl = cx[ind] - state.x
198200
dyl = cy[ind] - state.y
199201

200202
angle = pi_2_pi(cyaw[ind] - math.atan2(dyl, dxl))
201203
if angle < 0:
202-
min_d *= -1
204+
mind *= -1
203205

204-
return ind, min_d
206+
return ind, mind
205207

206208

207209
def predict_motion(x0, oa, od, xref):

PathTracking/rear_wheel_feedback/rear_wheel_feedback.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,14 @@ def calc_nearest_index(state, cx, cy, cyaw):
8383
dx = [state.x - icx for icx in cx]
8484
dy = [state.y - icy for icy in cy]
8585

86-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
86+
d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)]
8787

8888
mind = min(d)
8989

9090
ind = d.index(mind)
9191

92+
mind = math.sqrt(mind)
93+
9294
dxl = cx[ind] - state.x
9395
dyl = cy[ind] - state.y
9496

0 commit comments

Comments
 (0)