2020-01-17 12:48:30 -08:00
|
|
|
import math
|
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
|
2020-01-17 12:48:30 -08:00
|
|
|
from selfdrive.swaglog import cloudlog
|
2021-09-13 19:06:54 -07:00
|
|
|
from selfdrive.controls.lib.lateral_mpc_lib.lat_mpc import LateralMpc
|
2021-06-30 16:19:39 -05:00
|
|
|
from selfdrive.controls.lib.drive_helpers import CONTROL_N, MPC_COST_LAT, LAT_MPC_N, CAR_ROTATION_RADIUS
|
2021-01-14 18:43:50 -08:00
|
|
|
from selfdrive.controls.lib.lane_planner import LanePlanner, TRAJECTORY_SIZE
|
2020-01-17 12:48:30 -08:00
|
|
|
from selfdrive.config import Conversions as CV
|
|
|
|
|
import cereal.messaging as messaging
|
|
|
|
|
from cereal import log
|
|
|
|
|
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState = log.LateralPlan.LaneChangeState
|
|
|
|
|
LaneChangeDirection = log.LateralPlan.LaneChangeDirection
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-04-05 15:38:53 -07:00
|
|
|
LANE_CHANGE_SPEED_MIN = 30 * CV.MPH_TO_MS
|
2020-01-17 12:48:30 -08:00
|
|
|
LANE_CHANGE_TIME_MAX = 10.
|
|
|
|
|
|
|
|
|
|
DESIRES = {
|
|
|
|
|
LaneChangeDirection.none: {
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState.off: log.LateralPlan.Desire.none,
|
|
|
|
|
LaneChangeState.preLaneChange: log.LateralPlan.Desire.none,
|
|
|
|
|
LaneChangeState.laneChangeStarting: log.LateralPlan.Desire.none,
|
|
|
|
|
LaneChangeState.laneChangeFinishing: log.LateralPlan.Desire.none,
|
2020-01-17 12:48:30 -08:00
|
|
|
},
|
|
|
|
|
LaneChangeDirection.left: {
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState.off: log.LateralPlan.Desire.none,
|
2021-07-12 19:26:50 -07:00
|
|
|
LaneChangeState.preLaneChange: log.LateralPlan.Desire.none,
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState.laneChangeStarting: log.LateralPlan.Desire.laneChangeLeft,
|
|
|
|
|
LaneChangeState.laneChangeFinishing: log.LateralPlan.Desire.laneChangeLeft,
|
2020-01-17 12:48:30 -08:00
|
|
|
},
|
|
|
|
|
LaneChangeDirection.right: {
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState.off: log.LateralPlan.Desire.none,
|
2021-07-12 19:26:50 -07:00
|
|
|
LaneChangeState.preLaneChange: log.LateralPlan.Desire.none,
|
2021-02-03 19:57:30 -08:00
|
|
|
LaneChangeState.laneChangeStarting: log.LateralPlan.Desire.laneChangeRight,
|
|
|
|
|
LaneChangeState.laneChangeFinishing: log.LateralPlan.Desire.laneChangeRight,
|
2020-01-17 12:48:30 -08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 16:19:01 -07:00
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
class LateralPlanner:
|
2021-04-08 12:56:47 -07:00
|
|
|
def __init__(self, CP, use_lanelines=True, wide_camera=False):
|
|
|
|
|
self.use_lanelines = use_lanelines
|
2021-04-07 19:12:35 +02:00
|
|
|
self.LP = LanePlanner(wide_camera)
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
self.last_cloudlog_t = 0
|
|
|
|
|
self.steer_rate_cost = CP.steerRateCost
|
|
|
|
|
|
|
|
|
|
self.solution_invalid_cnt = 0
|
|
|
|
|
self.lane_change_state = LaneChangeState.off
|
2020-01-27 16:19:22 -08:00
|
|
|
self.lane_change_direction = LaneChangeDirection.none
|
2020-01-17 12:48:30 -08:00
|
|
|
self.lane_change_timer = 0.0
|
2020-03-16 16:19:01 -07:00
|
|
|
self.lane_change_ll_prob = 1.0
|
2021-08-12 21:06:26 -07:00
|
|
|
self.keep_pulse_timer = 0.0
|
2020-01-17 12:48:30 -08:00
|
|
|
self.prev_one_blinker = False
|
2021-02-03 19:57:30 -08:00
|
|
|
self.desire = log.LateralPlan.Desire.none
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-11-26 07:57:39 -06:00
|
|
|
self.path_xyz = np.zeros((TRAJECTORY_SIZE, 3))
|
|
|
|
|
self.path_xyz_stds = np.ones((TRAJECTORY_SIZE, 3))
|
2021-01-14 18:43:50 -08:00
|
|
|
self.plan_yaw = np.zeros((TRAJECTORY_SIZE,))
|
2021-01-14 21:46:01 -08:00
|
|
|
self.t_idxs = np.arange(TRAJECTORY_SIZE)
|
2021-01-26 20:27:24 -08:00
|
|
|
self.y_pts = np.zeros(TRAJECTORY_SIZE)
|
2021-01-14 18:43:50 -08:00
|
|
|
|
2021-09-13 19:06:54 -07:00
|
|
|
self.lat_mpc = LateralMpc()
|
2021-09-13 21:23:22 -07:00
|
|
|
self.reset_mpc(np.zeros(6))
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-09-13 19:06:54 -07:00
|
|
|
def reset_mpc(self, x0=np.zeros(6)):
|
|
|
|
|
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):
|
2020-01-17 12:48:30 -08:00
|
|
|
v_ego = sm['carState'].vEgo
|
|
|
|
|
active = sm['controlsState'].active
|
2021-03-12 06:08:51 +01:00
|
|
|
measured_curvature = sm['controlsState'].curvature
|
2021-01-19 00:02:53 -08:00
|
|
|
|
2021-01-14 18:43:50 -08:00
|
|
|
md = sm['modelV2']
|
|
|
|
|
self.LP.parse_model(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)
|
2021-01-14 18:43:50 -08:00
|
|
|
self.plan_yaw = list(md.orientation.z)
|
2021-10-17 19:52:11 -07:00
|
|
|
if len(md.position.xStd) == TRAJECTORY_SIZE:
|
2021-04-06 23:49:29 -07:00
|
|
|
self.path_xyz_stds = np.column_stack([md.position.xStd, md.position.yStd, md.position.zStd])
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
# Lane change logic
|
|
|
|
|
one_blinker = sm['carState'].leftBlinker != sm['carState'].rightBlinker
|
|
|
|
|
below_lane_change_speed = v_ego < LANE_CHANGE_SPEED_MIN
|
|
|
|
|
|
2021-03-22 20:55:57 -07:00
|
|
|
if (not active) or (self.lane_change_timer > LANE_CHANGE_TIME_MAX):
|
2020-01-17 12:48:30 -08:00
|
|
|
self.lane_change_state = LaneChangeState.off
|
2020-01-27 16:19:22 -08:00
|
|
|
self.lane_change_direction = LaneChangeDirection.none
|
2020-01-17 12:48:30 -08:00
|
|
|
else:
|
2021-06-04 10:03:13 +02:00
|
|
|
# LaneChangeState.off
|
2020-01-17 12:48:30 -08:00
|
|
|
if self.lane_change_state == LaneChangeState.off and one_blinker and not self.prev_one_blinker and not below_lane_change_speed:
|
2021-06-04 10:03:13 +02:00
|
|
|
self.lane_change_state = LaneChangeState.preLaneChange
|
|
|
|
|
self.lane_change_ll_prob = 1.0
|
|
|
|
|
|
|
|
|
|
# LaneChangeState.preLaneChange
|
|
|
|
|
elif self.lane_change_state == LaneChangeState.preLaneChange:
|
|
|
|
|
# Set lane change direction
|
2021-04-29 22:03:16 +02:00
|
|
|
if sm['carState'].leftBlinker:
|
|
|
|
|
self.lane_change_direction = LaneChangeDirection.left
|
|
|
|
|
elif sm['carState'].rightBlinker:
|
|
|
|
|
self.lane_change_direction = LaneChangeDirection.right
|
2021-06-04 10:03:13 +02:00
|
|
|
else: # If there are no blinkers we will go back to LaneChangeState.off
|
|
|
|
|
self.lane_change_direction = LaneChangeDirection.none
|
2021-04-29 22:03:16 +02:00
|
|
|
|
2021-06-04 10:03:13 +02:00
|
|
|
torque_applied = sm['carState'].steeringPressed and \
|
2021-11-26 07:57:39 -06:00
|
|
|
((sm['carState'].steeringTorque > 0 and self.lane_change_direction == LaneChangeDirection.left) or
|
2021-06-04 10:03:13 +02:00
|
|
|
(sm['carState'].steeringTorque < 0 and self.lane_change_direction == LaneChangeDirection.right))
|
|
|
|
|
|
|
|
|
|
blindspot_detected = ((sm['carState'].leftBlindspot and self.lane_change_direction == LaneChangeDirection.left) or
|
|
|
|
|
(sm['carState'].rightBlindspot and self.lane_change_direction == LaneChangeDirection.right))
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
if not one_blinker or below_lane_change_speed:
|
|
|
|
|
self.lane_change_state = LaneChangeState.off
|
2020-06-24 17:31:09 -07:00
|
|
|
elif torque_applied and not blindspot_detected:
|
2020-03-16 16:19:01 -07:00
|
|
|
self.lane_change_state = LaneChangeState.laneChangeStarting
|
|
|
|
|
|
2021-06-04 10:03:13 +02:00
|
|
|
# LaneChangeState.laneChangeStarting
|
2020-03-16 16:19:01 -07:00
|
|
|
elif self.lane_change_state == LaneChangeState.laneChangeStarting:
|
2020-05-27 14:35:01 -07:00
|
|
|
# fade out over .5s
|
2021-11-26 07:57:39 -06:00
|
|
|
self.lane_change_ll_prob = max(self.lane_change_ll_prob - 2 * DT_MDL, 0.0)
|
2021-06-04 10:03:13 +02:00
|
|
|
|
2020-03-16 16:19:01 -07:00
|
|
|
# 98% certainty
|
2021-06-04 10:03:13 +02:00
|
|
|
lane_change_prob = self.LP.l_lane_change_prob + self.LP.r_lane_change_prob
|
2020-03-16 16:19:01 -07:00
|
|
|
if lane_change_prob < 0.02 and self.lane_change_ll_prob < 0.01:
|
2020-03-04 18:51:45 -08:00
|
|
|
self.lane_change_state = LaneChangeState.laneChangeFinishing
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-06-04 10:03:13 +02:00
|
|
|
# LaneChangeState.laneChangeFinishing
|
2020-03-16 16:19:01 -07:00
|
|
|
elif self.lane_change_state == LaneChangeState.laneChangeFinishing:
|
|
|
|
|
# fade in laneline over 1s
|
|
|
|
|
self.lane_change_ll_prob = min(self.lane_change_ll_prob + DT_MDL, 1.0)
|
2021-11-10 10:21:36 -06:00
|
|
|
if self.lane_change_ll_prob > 0.99:
|
|
|
|
|
self.lane_change_direction = LaneChangeDirection.none
|
|
|
|
|
if one_blinker:
|
|
|
|
|
self.lane_change_state = LaneChangeState.preLaneChange
|
|
|
|
|
else:
|
|
|
|
|
self.lane_change_state = LaneChangeState.off
|
2020-01-17 12:48:30 -08:00
|
|
|
|
|
|
|
|
if self.lane_change_state in [LaneChangeState.off, LaneChangeState.preLaneChange]:
|
|
|
|
|
self.lane_change_timer = 0.0
|
|
|
|
|
else:
|
|
|
|
|
self.lane_change_timer += DT_MDL
|
|
|
|
|
|
|
|
|
|
self.prev_one_blinker = one_blinker
|
|
|
|
|
|
2021-01-26 20:27:24 -08:00
|
|
|
self.desire = DESIRES[self.lane_change_direction][self.lane_change_state]
|
2020-01-17 12:48:30 -08:00
|
|
|
|
2021-08-12 21:06:26 -07:00
|
|
|
# Send keep pulse once per second during LaneChangeStart.preLaneChange
|
|
|
|
|
if self.lane_change_state in [LaneChangeState.off, LaneChangeState.laneChangeStarting]:
|
|
|
|
|
self.keep_pulse_timer = 0.0
|
|
|
|
|
elif self.lane_change_state == LaneChangeState.preLaneChange:
|
|
|
|
|
self.keep_pulse_timer += DT_MDL
|
|
|
|
|
if self.keep_pulse_timer > 1.0:
|
|
|
|
|
self.keep_pulse_timer = 0.0
|
|
|
|
|
elif self.desire in [log.LateralPlan.Desire.keepLeft, log.LateralPlan.Desire.keepRight]:
|
|
|
|
|
self.desire = log.LateralPlan.Desire.none
|
|
|
|
|
|
2020-01-17 12:48:30 -08:00
|
|
|
# Turn off lanes during lane change
|
2021-02-03 19:57:30 -08:00
|
|
|
if self.desire == log.LateralPlan.Desire.laneChangeRight or self.desire == log.LateralPlan.Desire.laneChangeLeft:
|
2021-01-14 18:43:50 -08:00
|
|
|
self.LP.lll_prob *= self.lane_change_ll_prob
|
|
|
|
|
self.LP.rll_prob *= self.lane_change_ll_prob
|
2021-03-24 01:10:31 -07:00
|
|
|
if self.use_lanelines:
|
|
|
|
|
d_path_xyz = self.LP.get_d_path(v_ego, self.t_idxs, self.path_xyz)
|
2021-11-26 07:57:39 -06:00
|
|
|
self.lat_mpc.set_weights(MPC_COST_LAT.PATH, MPC_COST_LAT.HEADING, self.steer_rate_cost)
|
2021-03-24 01:10:31 -07:00
|
|
|
else:
|
|
|
|
|
d_path_xyz = self.path_xyz
|
2021-11-26 07:57:39 -06:00
|
|
|
path_cost = np.clip(abs(self.path_xyz[0, 1] / self.path_xyz_stds[0, 1]), 0.5, 1.5) * MPC_COST_LAT.PATH
|
2021-04-08 12:56:47 -07:00
|
|
|
# Heading cost is useful at low speed, otherwise end of plan can be off-heading
|
|
|
|
|
heading_cost = interp(v_ego, [5.0, 10.0], [MPC_COST_LAT.HEADING, 0.0])
|
2021-11-26 07:57:39 -06:00
|
|
|
self.lat_mpc.set_weights(path_cost, heading_cost, self.steer_rate_cost)
|
|
|
|
|
y_pts = np.interp(v_ego * self.t_idxs[:LAT_MPC_N + 1], np.linalg.norm(d_path_xyz, axis=1), d_path_xyz[:, 1])
|
2021-06-30 16:19:39 -05:00
|
|
|
heading_pts = np.interp(v_ego * self.t_idxs[:LAT_MPC_N + 1], np.linalg.norm(self.path_xyz, axis=1), self.plan_yaw)
|
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
|
2021-09-13 19:06:54 -07:00
|
|
|
self.x0[4] = v_ego
|
|
|
|
|
self.lat_mpc.run(self.x0,
|
|
|
|
|
v_ego,
|
|
|
|
|
CAR_ROTATION_RADIUS,
|
|
|
|
|
y_pts,
|
|
|
|
|
heading_pts)
|
2021-01-16 13:43:30 -08:00
|
|
|
# init state for next
|
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
|
|
|
|
|
mpc_nans = any(math.isnan(x) for x in self.lat_mpc.x_sol[:, 3])
|
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()
|
|
|
|
|
self.x0[3] = measured_curvature
|
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")
|
|
|
|
|
|
2021-09-13 19:06:54 -07:00
|
|
|
if self.lat_mpc.cost > 20000. 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')
|
2021-03-12 06:08:51 +01:00
|
|
|
plan_send.valid = sm.all_alive_and_valid(service_list=['carState', 'controlsState', 'modelV2'])
|
2021-02-03 19:57:30 -08:00
|
|
|
plan_send.lateralPlan.laneWidth = float(self.LP.lane_width)
|
|
|
|
|
plan_send.lateralPlan.dPathPoints = [float(x) for x in self.y_pts]
|
2021-09-13 19:06:54 -07:00
|
|
|
plan_send.lateralPlan.psis = [float(x) for x in self.lat_mpc.x_sol[0:CONTROL_N, 2]]
|
2021-11-26 07:57:39 -06:00
|
|
|
plan_send.lateralPlan.curvatures = [float(x) for x in self.lat_mpc.x_sol[0:CONTROL_N, 3]]
|
|
|
|
|
plan_send.lateralPlan.curvatureRates = [float(x) for x in self.lat_mpc.u_sol[0:CONTROL_N - 1]] + [0.0]
|
2021-02-03 19:57:30 -08:00
|
|
|
plan_send.lateralPlan.lProb = float(self.LP.lll_prob)
|
|
|
|
|
plan_send.lateralPlan.rProb = float(self.LP.rll_prob)
|
|
|
|
|
plan_send.lateralPlan.dProb = float(self.LP.d_prob)
|
|
|
|
|
|
|
|
|
|
plan_send.lateralPlan.mpcSolutionValid = bool(plan_solution_valid)
|
|
|
|
|
|
|
|
|
|
plan_send.lateralPlan.desire = self.desire
|
2021-10-26 14:32:41 -07:00
|
|
|
plan_send.lateralPlan.useLaneLines = self.use_lanelines
|
2021-02-03 19:57:30 -08:00
|
|
|
plan_send.lateralPlan.laneChangeState = self.lane_change_state
|
|
|
|
|
plan_send.lateralPlan.laneChangeDirection = self.lane_change_direction
|
|
|
|
|
|
|
|
|
|
pm.send('lateralPlan', plan_send)
|