Skip to content

Commit db1258b

Browse files
authored
Merge pull request AtsushiSakai#187 from Kitsunow/master
Optimize PurePursuit, use right axle
2 parents 62ee69d + 58c2a80 commit db1258b

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

PathTracking/pure_pursuit/pure_pursuit.py

Lines changed: 35 additions & 8 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
@@ -26,6 +28,8 @@ def __init__(self, x=0.0, y=0.0, yaw=0.0, v=0.0):
2628
self.y = y
2729
self.yaw = yaw
2830
self.v = v
31+
self.rear_x = self.x - ((L / 2) * math.cos(self.yaw))
32+
self.rear_y = self.y - ((L / 2) * math.sin(self.yaw))
2933

3034

3135
def update(state, a, delta):
@@ -34,6 +38,8 @@ def update(state, a, delta):
3438
state.y = state.y + state.v * math.sin(state.yaw) * dt
3539
state.yaw = state.yaw + state.v / L * math.tan(delta) * dt
3640
state.v = state.v + a * dt
41+
state.rear_x = state.x - ((L / 2) * math.cos(state.yaw))
42+
state.rear_y = state.y - ((L / 2) * math.sin(state.yaw))
3743

3844
return state
3945

@@ -59,30 +65,51 @@ def pure_pursuit_control(state, cx, cy, pind):
5965
ty = cy[-1]
6066
ind = len(cx) - 1
6167

62-
alpha = math.atan2(ty - state.y, tx - state.x) - state.yaw
68+
alpha = math.atan2(ty - state.rear_y, tx - state.rear_x) - state.yaw
6369

6470
Lf = k * state.v + Lfc
6571

6672
delta = math.atan2(2.0 * L * math.sin(alpha) / Lf, 1.0)
6773

6874
return delta, ind
6975

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

7183
def calc_target_index(state, cx, cy):
7284

73-
# search nearest point index
74-
dx = [state.x - icx for icx in cx]
75-
dy = [state.y - icy for icy in cy]
76-
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
77-
ind = d.index(min(d))
85+
global old_nearest_point_index
86+
87+
if old_nearest_point_index is None:
88+
# search nearest point index
89+
dx = [state.rear_x - icx for icx in cx]
90+
dy = [state.rear_y - icy for icy in cy]
91+
d = [abs(math.sqrt(idx ** 2 + idy ** 2)) for (idx, idy) in zip(dx, dy)]
92+
ind = d.index(min(d))
93+
old_nearest_point_index = ind
94+
else:
95+
ind = old_nearest_point_index
96+
distance_this_index = calc_distance(state, cx[ind], cy[ind])
97+
while True:
98+
ind = ind + 1 if (ind + 1) < len(cx) else ind
99+
distance_next_index = calc_distance(state, cx[ind], cy[ind])
100+
if distance_this_index < distance_next_index:
101+
break
102+
distance_this_index = distance_next_index
103+
old_nearest_point_index = ind
104+
78105
L = 0.0
79106

80107
Lf = k * state.v + Lfc
81108

82109
# search look ahead target point index
83110
while Lf > L and (ind + 1) < len(cx):
84-
dx = cx[ind] - state.x
85-
dy = cy[ind] - state.y
111+
dx = cx[ind] - state.rear_x
112+
dy = cy[ind] - state.rear_y
86113
L = math.sqrt(dx ** 2 + dy ** 2)
87114
ind += 1
88115

0 commit comments

Comments
 (0)