1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| import numpy as np import copy import math
from typing import List, Type, Optional, Tuple
from nuplan.planning.simulation.planner.project2.frame_transform import cartesian2frenet, local2global_vector from nuplan.planning.simulation.planner.project2.merge_path_speed import transform_path_planning
from nuplan.planning.simulation.planner.project2.reference_line_provider import ReferenceLineProvider from nuplan.common.actor_state.tracked_objects import TrackedObject, TrackedObjects from nuplan.common.actor_state.state_representation import TimePoint from nuplan.common.actor_state.ego_state import EgoState
MAX_SPEED = 17.0 MAX_ACCEL = 5.0 MAX_KAPPA = 1.0 D_ROAD_W = 0.3 DT = 0.25 TARGET_SPEED = 3 D_T_S = 1.0 N_SAMPLE = 2
K_J = 0.1 K_T = 0.1 K_D = 1.0 K_LAT = 1.0 K_LON = 1.0
WEIGHT_PROGRESS = 1.0 WEIGHT_OFFSET = 5.0 WEIGHT_SMOOTH = 10.0
MAXIMUM_JERK = 1.5 MAXIMUM_PROGRESS = 120 MAXIMUM_OFFSET = 1.5
MAXIMUM_DECELERATION = 5.0 MAXIMUM_ACCELERATION = 5.0
class FrenetPath: def __init__(self): self.t = [] self.l = [] self.l_dot = [] self.l_ddot = [] self.l_dddot = [] self.s = [] self.s_dot = [] self.s_ddot = [] self.s_dddot = [] self.d_cost = 0.0 self.s_cost = 0.0 self.cost = 0.0
self.idx2s = [] self.x = [] self.y = [] self.heading = [] self.kappa = []
class QuinticPolynomial: def __init__(self, strat_state, end_state): t_0, x_0, dx_0, ddx_0 = strat_state t_1, x_1, dx_1, ddx_1 = end_state self.k_0, self.k_1, self.k_2 = x_0, dx_0, 0.5 * ddx_0 A = np.array( [ [t_1**3, t_1**4, t_1**5], [3 * t_1**2, 4 * t_1**3, 5 * t_1**4], [6 * t_1, 12 * t_1**2, 20 * t_1**3], ] ) b = np.array([x_1 - self.k_1, dx_0, ddx_0, x_1, dx_1, ddx_1]) b = np.array( [ x_1 - self.k_0 - self.k_1 * t_1 - self.k_2 * t_1**2, dx_1 - self.k_1 - 2 * self.k_2 * t_1, ddx_1 - 2 * self.k_2, ] ) self.time = t_1 self.K = np.linalg.solve(A, b) self.k_3, self.k_4, self.k_5 = self.K
def get_time(self): return self.time
def calc_points(self, t): return self.k_0 + self.k_1 * t + self.k_2 * t**2 + self.k_3 * t**3 + self.k_4 * t**4 + self.k_5 * t**5
def calc_first_derivative(self, t): return self.k_1 + 2 * self.k_2 * t + 3 * self.k_3 * t**2 + 4 * self.k_4 * t**3 + 5 * self.k_5 * t**4
def calc_second_derivative(self, t): return 2 * self.k_2 + 6 * self.k_3 * t + 12 * self.k_4 * t**2 + 20 * self.k_5 * t**3
def calc_third_derivative(self, t): return 6 * self.k_3 + 24 * self.k_4 * t + 60 * self.k_5 * t**2
class QuarticPolynomial: def __init__(self, strat_state, end_state):
t_0, x_0, dx_0, ddx_0 = strat_state t_1, dx_1, ddx_1 = end_state self.k_0, self.k_1, self.k_2 = x_0, dx_0, 0.5 * ddx_0 A = np.array( [ [3 * t_1**2, 4 * t_1**3], [6 * t_1, 12 * t_1**2], ] ) b = np.array([dx_1 - self.k_1 - 2 * self.k_2 * t_1, ddx_1 - 2 * self.k_2]) self.time = t_1 self.K = np.linalg.solve(A, b) self.k_3, self.k_4 = self.K
def get_time(self): return self.time
def calc_points(self, t): return self.k_0 + self.k_1 * t + self.k_2 * t**2 + self.k_3 * t**3 + self.k_4 * t**4
def calc_first_derivative(self, t): return self.k_1 + 2 * self.k_2 * t + 3 * self.k_3 * t**2 + 4 * self.k_4 * t**3
def calc_second_derivative(self, t): return 2 * self.k_2 + 6 * self.k_3 * t + 12 * self.k_4 * t**2
def calc_third_derivative(self, t): return 6 * self.k_3 + 24 * self.k_4 * t
class LatticePlanner: def __init__( self, ego_state: EgoState, reference_path_provider: ReferenceLineProvider, object: List[TrackedObjects], horizon_time: TimePoint, sampling_time: TimePoint, max_velocity: float, ) -> None: self.ego_state = ego_state self.reference_path_provider = reference_path_provider self.objects = object self.horizon_time = horizon_time self.sampling_time = sampling_time self.max_velocity = max_velocity
self.fplist = [] self.best_path = None
def sample_lateral_end_state(self, d_start_state): end_d_candidates = np.array([-0.1, 0.0, 0.1]) end_t_candidates = np.array([8.0, 9.0, 10.0])
sampled_states = [] for t in end_t_candidates: for d in end_d_candidates: state = np.array([t, d, 0.0, 0.0]) sampled_states.append(state) return sampled_states
def sample_lon_end_state(self, s_start_state): end_states = [] time_samples = [] for i in np.arange(8, 10, 0.5): time_samples.append(i)
for time in time_samples: v_upper = min(s_start_state[2] + MAXIMUM_DECELERATION * 1.0, TARGET_SPEED) v_lower = max(s_start_state[2] - MAXIMUM_ACCELERATION * 1.0, 0.0) end_states.append([time, v_upper, 0.0]) end_states.append([time, v_lower, 0.0]) v_range = v_upper - v_lower num_of_mid_points = int(min(4, v_range / 1.0)) if num_of_mid_points > 0: velocity_seg = v_range / (num_of_mid_points + 1) for i in range(num_of_mid_points): end_states.append([time, v_lower + velocity_seg * i, 0.0])
return end_states
def get_trajectory_cost(self, lon_trajectory: QuarticPolynomial, lat_trajectory: QuinticPolynomial) -> float: cost = 0.0 progress = lon_trajectory.calc_points(lon_trajectory.get_time()) if progress < MAXIMUM_PROGRESS: cost += WEIGHT_PROGRESS * (MAXIMUM_PROGRESS - progress) / MAXIMUM_PROGRESS
for t in np.arange(0.0, self.horizon_time.time_s, self.sampling_time.time_s): lateral_jerk = lat_trajectory.calc_third_derivative(t) if lateral_jerk > 1.0: cost += WEIGHT_SMOOTH * (lateral_jerk / MAXIMUM_JERK)
lateral_offset = lat_trajectory.calc_points(t) if lateral_offset > 0.5: cost += WEIGHT_OFFSET * (lateral_offset - 0.5) / MAXIMUM_OFFSET
return cost
def is_valid_lon_trajectory(self, lon_trajectory: QuarticPolynomial) -> bool: t = 0.0 while t < lon_trajectory.get_time(): velocity = lon_trajectory.calc_first_derivative(t) accleration = lon_trajectory.calc_second_derivative(t) if velocity > 10.0 and velocity < 0.0: return False
if accleration > 5.0 and accleration < -5.0: return False
t += 0.1 return True
def calc_frenet_paths(self) -> list: cos_h = math.cos(self.ego_state.car_footprint.oriented_box.center.heading) sin_h = math.sin(self.ego_state.car_footprint.oriented_box.center.heading)
init_cartesian_state = np.array( [ self.ego_state.car_footprint.oriented_box.center.x, self.ego_state.car_footprint.oriented_box.center.y, self.ego_state.dynamic_car_state.rear_axle_velocity_2d.magnitude() * cos_h, self.ego_state.dynamic_car_state.rear_axle_velocity_2d.magnitude() * sin_h, self.ego_state.dynamic_car_state.rear_axle_acceleration_2d.magnitude() * cos_h, self.ego_state.dynamic_car_state.rear_axle_acceleration_2d.magnitude() * sin_h, ] )
reference_line = np.array( [ self.reference_path_provider._x_of_reference_line, self.reference_path_provider._y_of_reference_line, self.reference_path_provider._heading_of_reference_line, self.reference_path_provider._kappa_of_reference_line, self.reference_path_provider._s_of_reference_line, ] )
( s_set, l_set, s_dot_set, l_dot_set, dl_set, l_dot2_set, s_dot2_set, ddl_set, ) = cartesian2frenet( [init_cartesian_state[0]], [init_cartesian_state[1]], [init_cartesian_state[2]], [init_cartesian_state[3]], [init_cartesian_state[4]], [init_cartesian_state[5]], reference_line[0], reference_line[1], reference_line[2], reference_line[3], reference_line[4], )
d_start_state = [ 0, l_set[0], dl_set[0], ddl_set[0], ] s_start_state = [ 0, s_set[0], s_dot_set[0], s_dot2_set[0], ] d_end_states = self.sample_lateral_end_state(d_start_state) s_end_states = self.sample_lon_end_state(s_start_state)
for d_end_state in d_end_states: for s_end_state in s_end_states: fp = FrenetPath() lat = QuinticPolynomial(d_start_state, d_end_state) lon = QuarticPolynomial(s_start_state, s_end_state) fp.t = [ t for t in np.arange( 0.0, self.horizon_time.time_s + 2 * self.sampling_time.time_s, self.sampling_time.time_s ) ] fp.l = [lat.calc_points(t) for t in fp.t] fp.l_dot = [lat.calc_first_derivative(t) for t in fp.t] fp.l_ddot = [lat.calc_second_derivative(t) for t in fp.t] fp.l_dddot = [lat.calc_third_derivative(t) for t in fp.t] fp.s = [lon.calc_points(t) for t in fp.t] fp.s_dot = [lon.calc_first_derivative(t) for t in fp.t] fp.s_ddot = [lon.calc_second_derivative(t) for t in fp.t] fp.s_dddot = [lon.calc_third_derivative(t) for t in fp.t]
fp.cost = self.get_trajectory_cost(lon_trajectory=lon, lat_trajectory=lat)
self.fplist.append(fp)
return self.fplist
def calc_global_paths(self) -> list: for fp in self.fplist: fp.idx2s, fp.x, fp.y, fp.heading, fp.kappa = transform_path_planning( fp.s, fp.l, fp.l_dot, fp.l_ddot, self.reference_path_provider )
return self.fplist
def check_paths(self) -> list: ok_ind = [] for i, _ in enumerate(self.fplist): if any([v > MAX_SPEED for v in self.fplist[i].s_dot]): continue elif any([abs(a) > MAX_ACCEL for a in self.fplist[i].s_ddot]): continue
ok_ind.append(i)
return [self.fplist[i] for i in ok_ind]
def get_optimal_path(self) -> FrenetPath: fplist = self.calc_frenet_paths() fplist = self.calc_global_paths() fplist = self.check_paths() min_cost = float("inf") for fp in fplist: if min_cost >= fp.cost: min_cost = fp.cost self.best_path = fp
return self.best_path
|