|
| 1 | +""" |
| 2 | +adaptive_pure_pursuit_controller.py |
| 3 | +
|
| 4 | +Adaptive Pure Pursuit path tracking controller. Extends pure pursuit by adapting |
| 5 | +the look-ahead distance using speed and path curvature: shorter look-ahead on |
| 6 | +sharp curves for better tracking, longer look-ahead on straights for stability. |
| 7 | +""" |
| 8 | + |
| 9 | +from math import sin, tan, atan2 |
| 10 | + |
| 11 | + |
| 12 | +class AdaptivePurePursuitController: |
| 13 | + """ |
| 14 | + Controller class by Adaptive Pure Pursuit algorithm. |
| 15 | + Look-ahead distance is adapted by speed (longer at higher speed) and by |
| 16 | + path curvature at the current nearest point (shorter on sharp curves). |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, spec, course=None, color='g', |
| 20 | + min_look_ahead_m=2.0, |
| 21 | + look_forward_gain=0.3, |
| 22 | + curvature_adapt_gain=1.0, |
| 23 | + max_look_ahead_m=15.0, |
| 24 | + speed_proportional_gain=1.0): |
| 25 | + """ |
| 26 | + Constructor |
| 27 | + spec: Vehicle specification object |
| 28 | + course: Course data and logic object (with point_curvature optional) |
| 29 | + color: Color of drawing target point |
| 30 | + min_look_ahead_m: Minimum look-ahead distance [m] |
| 31 | + look_forward_gain: Gain for speed-proportional look-ahead |
| 32 | + curvature_adapt_gain: Gain for curvature-based reduction (0 = disable) |
| 33 | + max_look_ahead_m: Maximum look-ahead distance [m] |
| 34 | + speed_proportional_gain: Gain for speed tracking |
| 35 | + """ |
| 36 | + self.MIN_LOOK_AHEAD_DISTANCE_M = min_look_ahead_m |
| 37 | + self.LOOK_FORWARD_GAIN = look_forward_gain |
| 38 | + self.CURVATURE_ADAPT_GAIN = curvature_adapt_gain |
| 39 | + self.MAX_LOOK_AHEAD_DISTANCE_M = max_look_ahead_m |
| 40 | + self.SPEED_PROPORTIONAL_GAIN = speed_proportional_gain |
| 41 | + self.WHEEL_BASE_M = spec.wheel_base_m |
| 42 | + self.DRAW_COLOR = color |
| 43 | + |
| 44 | + self.course = course |
| 45 | + self.look_ahead_distance_m = self.MIN_LOOK_AHEAD_DISTANCE_M |
| 46 | + self.target_course_index = 0 |
| 47 | + self.target_accel_mps2 = 0.0 |
| 48 | + self.target_speed_mps = 0.0 |
| 49 | + self.target_steer_rad = 0.0 |
| 50 | + self.target_yaw_rate_rps = 0.0 |
| 51 | + |
| 52 | + def _calculate_look_ahead_distance(self, state): |
| 53 | + """ |
| 54 | + Adaptive look-ahead: base term from speed, reduced by path curvature. |
| 55 | + L = clamp((L0 + k * v) / (1 + K_curv * |curvature|), L_min, L_max) |
| 56 | + """ |
| 57 | + base_look_ahead = self.LOOK_FORWARD_GAIN * state.get_speed_mps() + self.MIN_LOOK_AHEAD_DISTANCE_M |
| 58 | + |
| 59 | + curvature_factor = 1.0 |
| 60 | + if self.course and hasattr(self.course, 'point_curvature'): |
| 61 | + nearest_index = self.course.search_nearest_point_index(state) |
| 62 | + curvature = self.course.point_curvature(nearest_index) |
| 63 | + curvature_factor = 1.0 + self.CURVATURE_ADAPT_GAIN * abs(curvature) |
| 64 | + |
| 65 | + self.look_ahead_distance_m = base_look_ahead / curvature_factor |
| 66 | + self.look_ahead_distance_m = max( |
| 67 | + self.MIN_LOOK_AHEAD_DISTANCE_M, |
| 68 | + min(self.MAX_LOOK_AHEAD_DISTANCE_M, self.look_ahead_distance_m), |
| 69 | + ) |
| 70 | + |
| 71 | + def _calculate_target_course_index(self, state): |
| 72 | + """ |
| 73 | + Private function to calculate target point's index on course |
| 74 | + state: Vehicle's state object |
| 75 | + """ |
| 76 | + nearest_index = self.course.search_nearest_point_index(state) |
| 77 | + while self.look_ahead_distance_m > self.course.calculate_distance_from_point(state, nearest_index): |
| 78 | + if nearest_index + 1 >= self.course.length(): |
| 79 | + break |
| 80 | + nearest_index += 1 |
| 81 | + self.target_course_index = nearest_index |
| 82 | + |
| 83 | + def _decide_target_speed_mps(self): |
| 84 | + """Private function to decide target speed [m/s].""" |
| 85 | + self.target_speed_mps = self.course.point_speed_mps(self.target_course_index) |
| 86 | + |
| 87 | + def _calculate_target_acceleration_mps2(self, state): |
| 88 | + """Private function to calculate acceleration input.""" |
| 89 | + diff_speed_mps = self.target_speed_mps - state.get_speed_mps() |
| 90 | + self.target_accel_mps2 = self.SPEED_PROPORTIONAL_GAIN * diff_speed_mps |
| 91 | + |
| 92 | + def _calculate_target_steer_angle_rad(self, state): |
| 93 | + """Private function to calculate steering angle input.""" |
| 94 | + diff_angle_rad = self.course.calculate_angle_difference_rad(state, self.target_course_index) |
| 95 | + self.target_steer_rad = atan2( |
| 96 | + (2 * self.WHEEL_BASE_M * sin(diff_angle_rad)), |
| 97 | + self.look_ahead_distance_m, |
| 98 | + ) |
| 99 | + |
| 100 | + def _calculate_target_yaw_rate_rps(self, state): |
| 101 | + """Private function to calculate yaw rate input.""" |
| 102 | + self.target_yaw_rate_rps = state.get_speed_mps() * tan(self.target_steer_rad) / self.WHEEL_BASE_M |
| 103 | + |
| 104 | + def update(self, state, time_s): |
| 105 | + """ |
| 106 | + Function to update data for path tracking |
| 107 | + state: Vehicle's state object |
| 108 | + time_s: Simulation interval time [sec] |
| 109 | + """ |
| 110 | + if not self.course: |
| 111 | + return |
| 112 | + |
| 113 | + self._calculate_look_ahead_distance(state) |
| 114 | + self._calculate_target_course_index(state) |
| 115 | + self._decide_target_speed_mps() |
| 116 | + self._calculate_target_acceleration_mps2(state) |
| 117 | + self._calculate_target_steer_angle_rad(state) |
| 118 | + self._calculate_target_yaw_rate_rps(state) |
| 119 | + |
| 120 | + def get_target_accel_mps2(self): |
| 121 | + """Function to get acceleration input [m/s^2].""" |
| 122 | + return self.target_accel_mps2 |
| 123 | + |
| 124 | + def get_target_steer_rad(self): |
| 125 | + """Function to get steering angle input [rad].""" |
| 126 | + return self.target_steer_rad |
| 127 | + |
| 128 | + def get_target_yaw_rate_rps(self): |
| 129 | + """Function to get yaw rate input [rad/s].""" |
| 130 | + return self.target_yaw_rate_rps |
| 131 | + |
| 132 | + def draw(self, axes, elems): |
| 133 | + """Function to draw target point on course.""" |
| 134 | + target_point_plot, = axes.plot( |
| 135 | + self.course.point_x_m(self.target_course_index), |
| 136 | + self.course.point_y_m(self.target_course_index), |
| 137 | + marker='o', |
| 138 | + color=self.DRAW_COLOR, |
| 139 | + linewidth=0, |
| 140 | + label="Target Point", |
| 141 | + ) |
| 142 | + elems.append(target_point_plot) |
0 commit comments