Skip to content

Commit 1ef1347

Browse files
committed
Only calculate all distances between car and path points once
1 parent af47468 commit 1ef1347

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

PathTracking/pure_pursuit/pure_pursuit.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import math
1010
import matplotlib.pyplot as plt
1111

12+
old_nearest_point_index = None
13+
1214
k = 0.1 # look forward gain
1315
Lfc = 1.0 # look-ahead distance
1416
Kp = 1.0 # speed proportional gain
@@ -71,14 +73,31 @@ def pure_pursuit_control(state, cx, cy, pind):
7173

7274
return delta, ind
7375

76+
def calc_distance(state, point_x, point_y):
77+
dx = state.rear_x - point_x
78+
dy = state.rear_y - point_y
79+
return math.sqrt(distance_x ** 2 + distance_y ** 2)
80+
7481

7582
def calc_target_index(state, cx, cy):
7683

77-
# search nearest point index
78-
dx = [state.rear_x - icx for icx in cx]
79-
dy = [state.rear_y - icy for icy in cy]
80-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
81-
ind = d.index(min(d))
84+
if old_nearest_point_index is None:
85+
# search nearest point index
86+
dx = [state.rear_x - icx for icx in cx]
87+
dy = [state.rear_y - icy for icy in cy]
88+
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
89+
ind = d.index(min(d))
90+
else:
91+
index = old_nearest_point_index
92+
distance_this_index = calc_distance(state, cx[index], cy[index])
93+
while True:
94+
index = index + 1 if (index + 1) < len(cx) else index
95+
distance_next_index = calc_distance(state, cx[index], cy[index])
96+
if distance_this_index < distance_next_index
97+
break
98+
distance_this_index = distance_next_index
99+
old_nearest_point_index = index
100+
82101
L = 0.0
83102

84103
Lf = k * state.v + Lfc

0 commit comments

Comments
 (0)