PIDLateralController implements lateral control using a PID.
| 148 | return np.clip((self._k_p * error) + (self._k_d * _de) + (self._k_i * _ie), -1.0, 1.0) |
| 149 | |
| 150 | class PIDLateralController(): |
| 151 | """ |
| 152 | PIDLateralController implements lateral control using a PID. |
| 153 | """ |
| 154 | |
| 155 | def __init__(self, vehicle, K_P=1.0, K_D=0.0, K_I=0.0, dt=0.03): |
| 156 | """ |
| 157 | Constructor method. |
| 158 | |
| 159 | :param vehicle: actor to apply to local planner logic onto |
| 160 | :param K_P: Proportional term |
| 161 | :param K_D: Differential term |
| 162 | :param K_I: Integral term |
| 163 | :param dt: time differential in seconds |
| 164 | """ |
| 165 | self._vehicle = vehicle |
| 166 | self._k_p = K_P |
| 167 | self._k_d = K_D |
| 168 | self._k_i = K_I |
| 169 | self._dt = dt |
| 170 | self._e_buffer = deque(maxlen=10) |
| 171 | |
| 172 | def run_step(self, waypoint): |
| 173 | """ |
| 174 | Execute one step of lateral control to steer |
| 175 | the vehicle towards a certain waypoin. |
| 176 | |
| 177 | :param waypoint: target waypoint |
| 178 | :return: steering control in the range [-1, 1] where: |
| 179 | -1 maximum steering to left |
| 180 | +1 maximum steering to right |
| 181 | """ |
| 182 | return self._pid_control(waypoint, self._vehicle.get_transform()) |
| 183 | |
| 184 | def _pid_control(self, waypoint, vehicle_transform): |
| 185 | """ |
| 186 | Estimate the steering angle of the vehicle based on the PID equations |
| 187 | |
| 188 | :param waypoint: target waypoint |
| 189 | :param vehicle_transform: current transform of the vehicle |
| 190 | :return: steering control in the range [-1, 1] |
| 191 | """ |
| 192 | v_begin = vehicle_transform.location |
| 193 | v_end = v_begin + carla.Location(x=math.cos(math.radians(vehicle_transform.rotation.yaw)), |
| 194 | y=math.sin(math.radians(vehicle_transform.rotation.yaw))) |
| 195 | |
| 196 | v_vec = np.array([v_end.x - v_begin.x, v_end.y - v_begin.y, 0.0]) |
| 197 | w_vec = np.array([waypoint.transform.location.x - |
| 198 | v_begin.x, waypoint.transform.location.y - |
| 199 | v_begin.y, 0.0]) |
| 200 | _dot = math.acos(np.clip(np.dot(w_vec, v_vec) / |
| 201 | (np.linalg.norm(w_vec) * np.linalg.norm(v_vec)), -1.0, 1.0)) |
| 202 | |
| 203 | _cross = np.cross(v_vec, w_vec) |
| 204 | |
| 205 | if _cross[2] < 0: |
| 206 | _dot *= -1.0 |
| 207 |