2021-01-14 18:43:50 -08:00
|
|
|
import numpy as np
|
2020-01-17 12:48:30 -08:00
|
|
|
from common.realtime import sec_since_boot, DT_MDL
|
2021-06-30 16:19:39 -05:00
|
|
|
from common.numpy_fast import interp
|
2022-06-11 23:19:27 -07:00
|
|
|
from system.swaglog import cloudlog
|
2021-09-13 19:06:54 -07:00
|
|
|
from selfdrive.controls.lib.lateral_mpc_lib.lat_mpc import LateralMpc
|
2022-10-05 21:43:38 -07:00
|
|
|
from selfdrive.controls.lib.lateral_mpc_lib.lat_mpc import N as LAT_MPC_N
|
2023-05-09 14:26:58 -07:00
|
|
|
from selfdrive.controls.lib.drive_helpers import CONTROL_N, MIN_SPEED, get_speed_error
|
2022-01-25 04:40:03 -08:00
|
|
|
from selfdrive.controls.lib.desire_helper import DesireHelper
|
2020-01-17 12:48:30 -08:00
|
|
|
import cereal.messaging as messaging
|
|
|
|
|
from cereal import log
|
|
|
|
|
|
2022-09-04 13:07:51 -07:00
|
|
|
TRAJECTORY_SIZE = 33
|
|
|
|
|
CAMERA_OFFSET = 0.04
|
2020-03-16 16:19:01 -07:00
|
|
|
|
2022-10-07 19:15:04 -07:00
|
|
|
|
|
|
|
|
PATH_COST = 1.0
|
|
|
|
|
LATERAL_MOTION_COST = 0.11
|
|
|
|
|
LATERAL_ACCEL_COST = 0.0
|
2023-02-17 15:28:26 -08:00
|
|
|
LATERAL_JERK_COST = 0.04
|
2022-10-11 14:53:43 -07:00
|
|
|
# Extreme steering rate is unpleasant, even
|
|
|
|
|
# when it does not cause bad jerk.
|
|
|
|
|
# TODO this cost should be lowered when low
|
|
|
|
|
# speed lateral control is stable on all cars
|
2023-02-17 15:28:26 -08:00
|
|
|
STEERING_RATE_COST = 700.0
|
2022-10-07 19:15:04 -07:00
|
|
|
|
|
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
class LateralPlanner:
|
2023-06-17 04:51:37 +02:00
|
|
|
def __init__(self, CP, debug=False):
|
2022-01-25 04:40:03 -08:00
|
|
|
self.DH = DesireHelper()
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2022-08-12 00:47:59 -07:00
|
|
|
# Vehicle model parameters used to calculate lateral movement of car
|
|
|
|
|
self.factor1 = CP.wheelbase - CP.centerToFront
|
|
|
|
|
self.factor2 = (CP.centerToFront * CP.mass) / (CP.wheelbase * CP.tireStiffnessRear)
|
2020-01-17 12:48:30 -08:00
|
|
|
self.last_cloudlog_t = 0
|
|
|
|
|
self.solution_invalid_cnt = 0
|
|
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
self.path_xyz = np.zeros((TRAJECTORY_SIZE, 3))
|
2023-02-17 15:28:26 -08:00
|
|
|
self.velocity_xyz = np.zeros((TRAJECTORY_SIZE, 3))
|
2021-01-14 18:43:50 -08:00
|
|
|
self.plan_yaw = np.zeros((TRAJECTORY_SIZE,))
|
2022-10-05 21:43:38 -07:00
|
|
|
self.plan_yaw_rate = np.zeros((TRAJECTORY_SIZE,))
|
2021-01-14 21:46:01 -08:00
|
|
|
self.t_idxs = np.arange(TRAJECTORY_SIZE)
|
2023-05-17 14:04:17 -07:00
|
|
|
self.y_pts = np.zeros((TRAJECTORY_SIZE,))
|
|
|
|
|
self.v_plan = np.zeros((TRAJECTORY_SIZE,))
|
|
|
|
|
self.v_ego = 0.0
|
|
|
|
|
self.l_lane_change_prob = 0.0
|
|
|
|
|
self.r_lane_change_prob = 0.0
|
2021-01-14 18:43:50 -08:00
|
|
|
|
2023-06-17 04:51:37 +02:00
|
|
|
self.debug_mode = debug
|
|
|
|
|
|
2021-09-13 19:06:54 -07:00
|
|
|
self.lat_mpc = LateralMpc()
|
2022-01-18 18:49:48 +01:00
|
|
|
self.reset_mpc(np.zeros(4))
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2022-01-18 18:49:48 +01:00
|
|
|
def reset_mpc(self, x0=np.zeros(4)):
|
2021-09-13 19:06:54 -07:00
|
|
|
self.x0 = x0
|
|
|
|
|
self.lat_mpc.reset(x0=self.x0)
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
def update(self, sm):
|
2022-10-07 19:15:04 -07:00
|
|
|
# clip speed , lateral planning is not possible at 0 speed
|
2021-03-12 06:08:51 +01:00
|
|
|
measured_curvature = sm['controlsState'].curvature
|
2023-05-09 14:26:58 -07:00
|
|
|
v_ego_car = sm['carState'].vEgo
|
2021-01-19 00:02:53 -08:00
|
|
|
|
2022-01-25 04:40:03 -08:00
|
|
|
# Parse model predictions
|
2021-01-14 18:43:50 -08:00
|
|
|
md = sm['modelV2']
|
|
|
|
|
if len(md.position.x) == TRAJECTORY_SIZE and len(md.orientation.x) == TRAJECTORY_SIZE:
|
|
|
|
|
self.path_xyz = np.column_stack([md.position.x, md.position.y, md.position.z])
|
2021-01-19 00:02:53 -08:00
|
|
|
self.t_idxs = np.array(md.position.t)
|
2022-08-12 00:47:59 -07:00
|
|
|
self.plan_yaw = np.array(md.orientation.z)
|
2022-10-05 21:43:38 -07:00
|
|
|
self.plan_yaw_rate = np.array(md.orientationRate.z)
|
2023-02-17 15:28:26 -08:00
|
|
|
self.velocity_xyz = np.column_stack([md.velocity.x, md.velocity.y, md.velocity.z])
|
2023-05-09 14:26:58 -07:00
|
|
|
car_speed = np.linalg.norm(self.velocity_xyz, axis=1) - get_speed_error(md, v_ego_car)
|
2023-02-17 15:28:26 -08:00
|
|
|
self.v_plan = np.clip(car_speed, MIN_SPEED, np.inf)
|
|
|
|
|
self.v_ego = self.v_plan[0]
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
# Lane change logic
|
2022-09-04 13:07:51 -07:00
|
|
|
desire_state = md.meta.desireState
|
|
|
|
|
if len(desire_state):
|
|
|
|
|
self.l_lane_change_prob = desire_state[log.LateralPlan.Desire.laneChangeLeft]
|
|
|
|
|
self.r_lane_change_prob = desire_state[log.LateralPlan.Desire.laneChangeRight]
|
|
|
|
|
lane_change_prob = self.l_lane_change_prob + self.r_lane_change_prob
|
2022-08-15 14:37:24 -04:00
|
|
|
self.DH.update(sm['carState'], sm['carControl'].latActive, lane_change_prob)
|
2021-08-12 21:06:26 -07:00
|
|
|
|
2022-10-07 19:15:04 -07:00
|
|
|
self.lat_mpc.set_weights(PATH_COST, LATERAL_MOTION_COST,
|
2022-10-11 14:53:43 -07:00
|
|
|
LATERAL_ACCEL_COST, LATERAL_JERK_COST,
|
|
|
|
|
STEERING_RATE_COST)
|
2022-01-25 04:40:03 -08:00
|
|
|
|
2023-02-17 15:28:26 -08:00
|
|
|
y_pts = self.path_xyz[:LAT_MPC_N+1, 1]
|
|
|
|
|
heading_pts = self.plan_yaw[:LAT_MPC_N+1]
|
|
|
|
|
yaw_rate_pts = self.plan_yaw_rate[:LAT_MPC_N+1]
|
2021-01-26 20:27:24 -08:00
|
|
|
self.y_pts = y_pts
|
2021-01-14 18:43:50 -08:00
|
|
|
|
2021-06-30 16:19:39 -05:00
|
|
|
assert len(y_pts) == LAT_MPC_N + 1
|
|
|
|
|
assert len(heading_pts) == LAT_MPC_N + 1
|
2022-10-05 21:43:38 -07:00
|
|
|
assert len(yaw_rate_pts) == LAT_MPC_N + 1
|
2023-02-17 15:28:26 -08:00
|
|
|
lateral_factor = np.clip(self.factor1 - (self.factor2 * self.v_plan**2), 0.0, np.inf)
|
|
|
|
|
p = np.column_stack([self.v_plan, lateral_factor])
|
2021-09-13 19:06:54 -07:00
|
|
|
self.lat_mpc.run(self.x0,
|
2022-01-18 18:49:48 +01:00
|
|
|
p,
|
2021-09-13 19:06:54 -07:00
|
|
|
y_pts,
|
2022-08-12 00:47:59 -07:00
|
|
|
heading_pts,
|
2022-10-05 21:43:38 -07:00
|
|
|
yaw_rate_pts)
|
2022-10-07 00:16:18 -07:00
|
|
|
# init state for next iteration
|
|
|
|
|
# mpc.u_sol is the desired second derivative of psi given x0 curv state.
|
|
|
|
|
# with x0[3] = measured_yaw_rate, this would be the actual desired yaw rate.
|
|
|
|
|
# instead, interpolate x_sol so that x0[3] is the desired yaw rate for lat_control.
|
2021-11-26 07:57:39 -06:00
|
|
|
self.x0[3] = interp(DT_MDL, self.t_idxs[:LAT_MPC_N + 1], self.lat_mpc.x_sol[:, 3])
|
2021-09-13 19:06:54 -07:00
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
# Check for infeasible MPC solution
|
2022-01-12 22:56:41 +08:00
|
|
|
mpc_nans = np.isnan(self.lat_mpc.x_sol[:, 3]).any()
|
2020-01-17 12:48:30 -08:00
|
|
|
t = sec_since_boot()
|
2021-09-13 19:06:54 -07:00
|
|
|
if mpc_nans or self.lat_mpc.solution_status != 0:
|
|
|
|
|
self.reset_mpc()
|
2022-10-07 19:15:04 -07:00
|
|
|
self.x0[3] = measured_curvature * self.v_ego
|
2020-01-17 12:48:30 -08:00
|
|
|
if t > self.last_cloudlog_t + 5.0:
|
|
|
|
|
self.last_cloudlog_t = t
|
|
|
|
|
cloudlog.warning("Lateral mpc - nan: True")
|
|
|
|
|
|
2023-05-31 16:33:07 -07:00
|
|
|
if self.lat_mpc.cost > 1e6 or mpc_nans:
|
2020-01-17 12:48:30 -08:00
|
|
|
self.solution_invalid_cnt += 1
|
|
|
|
|
else:
|
|
|
|
|
self.solution_invalid_cnt = 0
|
2021-01-26 20:27:24 -08:00
|
|
|
|
|
|
|
|
def publish(self, sm, pm):
|
2020-01-17 12:48:30 -08:00
|
|
|
plan_solution_valid = self.solution_invalid_cnt < 2
|
2021-02-03 19:57:30 -08:00
|
|
|
plan_send = messaging.new_message('lateralPlan')
|
2022-04-06 16:43:32 -07:00
|
|
|
plan_send.valid = sm.all_checks(service_list=['carState', 'controlsState', 'modelV2'])
|
2022-01-03 03:55:24 +08:00
|
|
|
|
|
|
|
|
lateralPlan = plan_send.lateralPlan
|
2022-04-05 21:05:45 -07:00
|
|
|
lateralPlan.modelMonoTime = sm.logMonoTime['modelV2']
|
2022-01-11 21:34:47 +08:00
|
|
|
lateralPlan.dPathPoints = self.y_pts.tolist()
|
|
|
|
|
lateralPlan.psis = self.lat_mpc.x_sol[0:CONTROL_N, 2].tolist()
|
2022-10-07 00:16:18 -07:00
|
|
|
|
2022-10-07 19:15:04 -07:00
|
|
|
lateralPlan.curvatures = (self.lat_mpc.x_sol[0:CONTROL_N, 3]/self.v_ego).tolist()
|
|
|
|
|
lateralPlan.curvatureRates = [float(x/self.v_ego) for x in self.lat_mpc.u_sol[0:CONTROL_N - 1]] + [0.0]
|
2022-01-03 03:55:24 +08:00
|
|
|
|
|
|
|
|
lateralPlan.mpcSolutionValid = bool(plan_solution_valid)
|
2022-01-18 14:51:03 +01:00
|
|
|
lateralPlan.solverExecutionTime = self.lat_mpc.solve_time
|
2023-06-17 04:51:37 +02:00
|
|
|
if self.debug_mode:
|
|
|
|
|
lateralPlan.solverCost = self.lat_mpc.cost
|
|
|
|
|
lateralPlan.solverState = log.LateralPlan.SolverState.new_message()
|
|
|
|
|
lateralPlan.solverState.x = self.lat_mpc.x_sol.tolist()
|
|
|
|
|
lateralPlan.solverState.u = self.lat_mpc.u_sol.flatten().tolist()
|
2022-01-03 03:55:24 +08:00
|
|
|
|
2022-01-25 04:40:03 -08:00
|
|
|
lateralPlan.desire = self.DH.desire
|
2022-09-04 13:07:51 -07:00
|
|
|
lateralPlan.useLaneLines = False
|
2022-01-25 04:40:03 -08:00
|
|
|
lateralPlan.laneChangeState = self.DH.lane_change_state
|
|
|
|
|
lateralPlan.laneChangeDirection = self.DH.lane_change_direction
|
2021-02-03 19:57:30 -08:00
|
|
|
|
|
|
|
|
pm.send('lateralPlan', plan_send)
|