Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions PathTracking/pure_pursuit/pure_pursuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def __init__(self, cx, cy):
self.old_nearest_point_index = None

def search_target_index(self, state):
"""Return the target waypoint index and look-ahead distance in meters."""

# To speed up nearest point search, doing it at only first time.
if self.old_nearest_point_index is None:
Expand All @@ -106,12 +107,12 @@ def search_target_index(self, state):
ind = self.old_nearest_point_index
distance_this_index = state.calc_distance(self.cx[ind],
self.cy[ind])
while True:
while ind + 1 < len(self.cx):
distance_next_index = state.calc_distance(self.cx[ind + 1],
self.cy[ind + 1])
if distance_this_index < distance_next_index:
break
ind = ind + 1 if (ind + 1) < len(self.cx) else ind
ind += 1
distance_this_index = distance_next_index
self.old_nearest_point_index = ind

Expand Down Expand Up @@ -231,13 +232,13 @@ def plot_wheel(x, y, yaw, steer=0.0, color=color):
# Plot wheels (darker color for front wheels)
front_color = 'darkblue'
rear_color = color

# Plot four wheels
# Front left
plot_wheel(x + LENGTH/4 * c - WIDTH/2 * s,
y + LENGTH/4 * s + WIDTH/2 * c,
yaw, steer, front_color)
# Front right
# Front right
plot_wheel(x + LENGTH/4 * c + WIDTH/2 * s,
y + LENGTH/4 * s - WIDTH/2 * c,
yaw, steer, front_color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ speed control.
The red line is a target course, the green cross means the target point
for pure pursuit control, the blue line is the tracking.

If the look-ahead search reaches the end of the course,
the final waypoint is used as the target.

Code Link
~~~~~~~~~~~~~~~

Expand Down
40 changes: 39 additions & 1 deletion tests/test_pure_pursuit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import conftest # Add root path to sys.path
from PathTracking.pure_pursuit import pure_pursuit as m

import pytest

def test1():
m.show_animation = False
Expand All @@ -11,5 +11,43 @@ def test_backward():
m.is_reverse_mode = True
m.main()

@pytest.mark.parametrize(
"course_x, rear_x_positions, expected_indices",
[
([0.0], [0.0, 0.0], [0, 0]),
([0.0, 10.0, 20.0], [20.0, 20.0], [2, 2]),
(
[0.0, 10.0, 20.0],
[0.0, 10.0, 20.0, 25.0],
[1, 2, 2, 2],
),
([0.0, 10.0, 20.0], [0.0, 0.0], [1, 1]),
],
ids=[
"single-waypoint",
"repeated-search-at-final-waypoint",
"advance-to-and-beyond-final-waypoint",
"repeated-search-at-interior-waypoint",
],
)
def test_search_target_index_handles_path_end(
course_x, rear_x_positions, expected_indices
):
course = m.TargetCourse(course_x, [0.0] * len(course_x))

for rear_x, expected_index in zip(
rear_x_positions, expected_indices
):
state = m.State(
x=rear_x + m.WB / 2,
y=0.0,
yaw=0.0,
v=0.0,
)

target_index, _ = course.search_target_index(state)

assert target_index == expected_index

if __name__ == '__main__':
conftest.run_this_test(__file__)