From 811bdc1d49ac5d04ebd05c18de906c0b3c7243dc Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Fri, 3 Jan 2020 23:13:36 +0100 Subject: [PATCH 1/5] :hammer: Refactored pure_pursuit.py - Added new method to state class - Removed global variable old_nearest_point_index - Created a class States that store the results --- PathTracking/pure_pursuit/pure_pursuit.py | 163 ++++++++++++---------- 1 file changed, 86 insertions(+), 77 deletions(-) diff --git a/PathTracking/pure_pursuit/pure_pursuit.py b/PathTracking/pure_pursuit/pure_pursuit.py index dfe8a8e1..49e32677 100644 --- a/PathTracking/pure_pursuit/pure_pursuit.py +++ b/PathTracking/pure_pursuit/pure_pursuit.py @@ -17,7 +17,6 @@ dt = 0.1 # [s] L = 2.9 # [m] wheel base of vehicle -old_nearest_point_index = None show_animation = True @@ -31,17 +30,37 @@ class State: self.rear_x = self.x - ((L / 2) * math.cos(self.yaw)) self.rear_y = self.y - ((L / 2) * math.sin(self.yaw)) + def update(self, a, delta): -def update(state, a, delta): + self.x += self.v * math.cos(self.yaw) * dt + self.y += self.v * math.sin(self.yaw) * dt + self.yaw += self.v / L * math.tan(delta) * dt + self.v += a * dt + self.rear_x = self.x - ((L / 2) * math.cos(self.yaw)) + self.rear_y = self.y - ((L / 2) * math.sin(self.yaw)) - state.x = state.x + state.v * math.cos(state.yaw) * dt - state.y = state.y + state.v * math.sin(state.yaw) * dt - state.yaw = state.yaw + state.v / L * math.tan(delta) * dt - state.v = state.v + a * dt - state.rear_x = state.x - ((L / 2) * math.cos(state.yaw)) - state.rear_y = state.y - ((L / 2) * math.sin(state.yaw)) + def calc_distance(self, point_x, point_y): - return state + dx = self.rear_x - point_x + dy = self.rear_y - point_y + return math.hypot(dx, dy) + + +class States: + + def __init__(self): + self.x = [] + self.y = [] + self.yaw = [] + self.v = [] + self.t = [] + + def append(self, t , state): + self.x.append(state.x) + self.y.append(state.y) + self.yaw.append(state.yaw) + self.v.append(state.v) + self.t.append(t) def PIDControl(target, current): @@ -50,20 +69,59 @@ def PIDControl(target, current): return a -def pure_pursuit_control(state, cx, cy, pind): +class Trajectory: + def __init__(self, cx, cy): + self.cx = cx + self.cy = cy + self.old_nearest_point_index = None - ind = calc_target_index(state, cx, cy) + def __call__(self, state): + if self.old_nearest_point_index is None: + # search nearest point index + dx = [state.rear_x - icx for icx in self.cx] + dy = [state.rear_y - icy for icy in self.cy] + d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)] + ind = d.index(min(d)) + self.old_nearest_point_index = ind + else: + ind = self.old_nearest_point_index + distance_this_index = state.calc_distance(self.cx[ind], self.cy[ind]) + while True: + ind = ind + 1 if (ind + 1) < len(self.cx) else ind + distance_next_index = state.calc_distance(self.cx[ind], self.cy[ind]) + if distance_this_index < distance_next_index: + break + distance_this_index = distance_next_index + self.old_nearest_point_index = ind + + L = 0.0 + + Lf = k * state.v + Lfc + + # search look ahead target point index + while Lf > L and (ind + 1) < len(self.cx): + dx = self.cx[ind] - state.rear_x + dy = self.cy[ind] - state.rear_y + L = math.hypot(dx, dy) + ind += 1 + + return ind + + +def pure_pursuit_control(state, trajectory, pind): + + ind = trajectory(state) if pind >= ind: ind = pind - if ind < len(cx): - tx = cx[ind] - ty = cy[ind] + if ind < len(trajectory.cx): + tx = trajectory.cx[ind] + ty = trajectory.cy[ind] else: - tx = cx[-1] - ty = cy[-1] - ind = len(cx) - 1 + tx = trajectory.cx[-1] + ty = trajectory.cy[-1] + ind = len(trajectory.cx) - 1 alpha = math.atan2(ty - state.rear_y, tx - state.rear_x) - state.yaw @@ -73,48 +131,6 @@ def pure_pursuit_control(state, cx, cy, pind): return delta, ind -def calc_distance(state, point_x, point_y): - - dx = state.rear_x - point_x - dy = state.rear_y - point_y - return math.hypot(dx, dy) - - -def calc_target_index(state, cx, cy): - - global old_nearest_point_index - - if old_nearest_point_index is None: - # search nearest point index - dx = [state.rear_x - icx for icx in cx] - dy = [state.rear_y - icy for icy in cy] - d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)] - ind = d.index(min(d)) - old_nearest_point_index = ind - else: - ind = old_nearest_point_index - distance_this_index = calc_distance(state, cx[ind], cy[ind]) - while True: - ind = ind + 1 if (ind + 1) < len(cx) else ind - distance_next_index = calc_distance(state, cx[ind], cy[ind]) - if distance_this_index < distance_next_index: - break - distance_this_index = distance_next_index - old_nearest_point_index = ind - - L = 0.0 - - Lf = k * state.v + Lfc - - # search look ahead target point index - while Lf > L and (ind + 1) < len(cx): - dx = cx[ind] - state.rear_x - dy = cy[ind] - state.rear_y - L = math.hypot(dx, dy) - ind += 1 - - return ind - def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): """ @@ -144,25 +160,18 @@ def main(): lastIndex = len(cx) - 1 time = 0.0 - x = [state.x] - y = [state.y] - yaw = [state.yaw] - v = [state.v] - t = [0.0] - target_ind = calc_target_index(state, cx, cy) + states = States() + states.append(time, state) + trajectory = Trajectory(cx, cy) + target_ind = trajectory(state) while T >= time and lastIndex > target_ind: ai = PIDControl(target_speed, state.v) - di, target_ind = pure_pursuit_control(state, cx, cy, target_ind) - state = update(state, ai, di) + di, target_ind = pure_pursuit_control(state, trajectory, target_ind) + state.update(ai, di) - time = time + dt - - x.append(state.x) - y.append(state.y) - yaw.append(state.yaw) - v.append(state.v) - t.append(time) + time += dt + states.append(time, state) if show_animation: # pragma: no cover plt.cla() @@ -171,7 +180,7 @@ def main(): lambda event: [exit(0) if event.key == 'escape' else None]) plot_arrow(state.x, state.y, state.yaw) plt.plot(cx, cy, "-r", label="course") - plt.plot(x, y, "-b", label="trajectory") + plt.plot(states.x, states.y, "-b", label="trajectory") plt.plot(cx[target_ind], cy[target_ind], "xg", label="target") plt.axis("equal") plt.grid(True) @@ -184,7 +193,7 @@ def main(): if show_animation: # pragma: no cover plt.cla() plt.plot(cx, cy, ".r", label="course") - plt.plot(x, y, "-b", label="trajectory") + plt.plot(states.x, states.y, "-b", label="trajectory") plt.legend() plt.xlabel("x[m]") plt.ylabel("y[m]") @@ -192,7 +201,7 @@ def main(): plt.grid(True) plt.subplots(1) - plt.plot(t, [iv * 3.6 for iv in v], "-r") + plt.plot(states.t, [iv * 3.6 for iv in states.v], "-r") plt.xlabel("Time[s]") plt.ylabel("Speed[km/h]") plt.grid(True) From f81140e65bcf4648c8aae4bdf14e48080bfbd706 Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 4 Jan 2020 09:45:05 +0100 Subject: [PATCH 2/5] :pencil: Renamed method name __call__ with search_target_index in Trajectory class Thanks to @AtsushiSakai remarks https://github.com/AtsushiSakai/PythonRobotics/pull/269#discussion_r363005681 --- PathTracking/pure_pursuit/pure_pursuit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PathTracking/pure_pursuit/pure_pursuit.py b/PathTracking/pure_pursuit/pure_pursuit.py index 49e32677..d7cf8fe9 100644 --- a/PathTracking/pure_pursuit/pure_pursuit.py +++ b/PathTracking/pure_pursuit/pure_pursuit.py @@ -75,7 +75,7 @@ class Trajectory: self.cy = cy self.old_nearest_point_index = None - def __call__(self, state): + def search_target_index(self, state): if self.old_nearest_point_index is None: # search nearest point index dx = [state.rear_x - icx for icx in self.cx] @@ -110,7 +110,7 @@ class Trajectory: def pure_pursuit_control(state, trajectory, pind): - ind = trajectory(state) + ind = trajectory.search_target_index(state) if pind >= ind: ind = pind @@ -163,7 +163,7 @@ def main(): states = States() states.append(time, state) trajectory = Trajectory(cx, cy) - target_ind = trajectory(state) + target_ind = trajectory.search_target_index(state) while T >= time and lastIndex > target_ind: ai = PIDControl(target_speed, state.v) From bfc874492390a803e803992324777376a6b12e8c Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 4 Jan 2020 09:50:41 +0100 Subject: [PATCH 3/5] :hammer: Refcatored modification in pure_pursuit.py Thanks to @AtsushiSakai remark https://github.com/AtsushiSakai/PythonRobotics/pull/269#discussion_r363005775 --- PathTracking/pure_pursuit/pure_pursuit.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/PathTracking/pure_pursuit/pure_pursuit.py b/PathTracking/pure_pursuit/pure_pursuit.py index d7cf8fe9..4e8b1bed 100644 --- a/PathTracking/pure_pursuit/pure_pursuit.py +++ b/PathTracking/pure_pursuit/pure_pursuit.py @@ -80,8 +80,8 @@ class Trajectory: # search nearest point index dx = [state.rear_x - icx for icx in self.cx] dy = [state.rear_y - icy for icy in self.cy] - d = [idx ** 2 + idy ** 2 for (idx, idy) in zip(dx, dy)] - ind = d.index(min(d)) + d = np.hypot(dx, dy) + ind = np.argmin(d) self.old_nearest_point_index = ind else: ind = self.old_nearest_point_index @@ -100,9 +100,7 @@ class Trajectory: # search look ahead target point index while Lf > L and (ind + 1) < len(self.cx): - dx = self.cx[ind] - state.rear_x - dy = self.cy[ind] - state.rear_y - L = math.hypot(dx, dy) + L = state.calc_distance(self.cx[ind], self.cy[ind]) ind += 1 return ind From 3bdf2555345b93c89068549fc1fd87ce7a7e92dc Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 4 Jan 2020 09:57:30 +0100 Subject: [PATCH 4/5] :hammer: Removed unnecessary parenthesis in pure_pursuit.py --- PathTracking/pure_pursuit/pure_pursuit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PathTracking/pure_pursuit/pure_pursuit.py b/PathTracking/pure_pursuit/pure_pursuit.py index 4e8b1bed..bfc3c7cc 100644 --- a/PathTracking/pure_pursuit/pure_pursuit.py +++ b/PathTracking/pure_pursuit/pure_pursuit.py @@ -136,7 +136,7 @@ def plot_arrow(x, y, yaw, length=1.0, width=0.5, fc="r", ec="k"): """ if not isinstance(x, float): - for (ix, iy, iyaw) in zip(x, y, yaw): + for ix, iy, iyaw in zip(x, y, yaw): plot_arrow(ix, iy, iyaw) else: plt.arrow(x, y, length * math.cos(yaw), length * math.sin(yaw), From 08428bed153dfe49085e63becd6f399a713dd57c Mon Sep 17 00:00:00 2001 From: Guillaume Jacquenot Date: Sat, 4 Jan 2020 09:57:47 +0100 Subject: [PATCH 5/5] :pencil: Added Gjacquenot as author as suggested by @AtsushiSakai --- PathTracking/pure_pursuit/pure_pursuit.py | 1 + 1 file changed, 1 insertion(+) diff --git a/PathTracking/pure_pursuit/pure_pursuit.py b/PathTracking/pure_pursuit/pure_pursuit.py index bfc3c7cc..bcaf8697 100644 --- a/PathTracking/pure_pursuit/pure_pursuit.py +++ b/PathTracking/pure_pursuit/pure_pursuit.py @@ -3,6 +3,7 @@ Path tracking simulation with pure pursuit steering control and PID speed control. author: Atsushi Sakai (@Atsushi_twi) + Guillaume Jacquenot (@Gjacquenot) """ import numpy as np