|
|
|
@ -4,27 +4,19 @@ from common.numpy_fast import interp |
|
|
|
|
from selfdrive.config import Conversions as CV |
|
|
|
|
from selfdrive.car import apply_std_steer_torque_limits |
|
|
|
|
from selfdrive.car.gm import gmcan |
|
|
|
|
from selfdrive.car.gm.values import DBC, SUPERCRUISE_CARS, CanBus |
|
|
|
|
from selfdrive.car.gm.values import DBC, CanBus |
|
|
|
|
from opendbc.can.packer import CANPacker |
|
|
|
|
|
|
|
|
|
VisualAlert = car.CarControl.HUDControl.VisualAlert |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CarControllerParams(): |
|
|
|
|
def __init__(self, car_fingerprint): |
|
|
|
|
if car_fingerprint in SUPERCRUISE_CARS: |
|
|
|
|
self.STEER_MAX = 150 |
|
|
|
|
self.STEER_STEP = 1 # how often we update the steer cmd |
|
|
|
|
self.STEER_DELTA_UP = 2 # 0.75s time to peak torque |
|
|
|
|
self.STEER_DELTA_DOWN = 5 # 0.3s from peak torque to zero |
|
|
|
|
self.MIN_STEER_SPEED = -1. # can steer down to zero |
|
|
|
|
else: |
|
|
|
|
self.STEER_MAX = 300 |
|
|
|
|
self.STEER_STEP = 2 # how often we update the steer cmd |
|
|
|
|
self.STEER_DELTA_UP = 7 # ~0.75s time to peak torque (255/50hz/0.75s) |
|
|
|
|
self.STEER_DELTA_DOWN = 17 # ~0.3s from peak torque to zero |
|
|
|
|
self.MIN_STEER_SPEED = 3. |
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
self.STEER_MAX = 300 |
|
|
|
|
self.STEER_STEP = 2 # how often we update the steer cmd |
|
|
|
|
self.STEER_DELTA_UP = 7 # ~0.75s time to peak torque (255/50hz/0.75s) |
|
|
|
|
self.STEER_DELTA_DOWN = 17 # ~0.3s from peak torque to zero |
|
|
|
|
self.MIN_STEER_SPEED = 3. |
|
|
|
|
self.STEER_DRIVER_ALLOWANCE = 50 # allowed driver torque before start limiting |
|
|
|
|
self.STEER_DRIVER_MULTIPLIER = 4 # weight driver torque heavily |
|
|
|
|
self.STEER_DRIVER_FACTOR = 100 # from dbc |
|
|
|
@ -61,23 +53,15 @@ def actuator_hystereses(final_pedal, pedal_steady): |
|
|
|
|
|
|
|
|
|
return final_pedal, pedal_steady |
|
|
|
|
|
|
|
|
|
def process_hud_alert(hud_alert): |
|
|
|
|
# initialize to no alert |
|
|
|
|
steer = 0 |
|
|
|
|
if hud_alert == VisualAlert.steerRequired: |
|
|
|
|
steer = 1 |
|
|
|
|
return steer |
|
|
|
|
|
|
|
|
|
class CarController(): |
|
|
|
|
def __init__(self, dbc_name, CP, VM): |
|
|
|
|
self.pedal_steady = 0. |
|
|
|
|
self.start_time = 0. |
|
|
|
|
self.apply_steer_last = 0 |
|
|
|
|
self.car_fingerprint = CP.carFingerprint |
|
|
|
|
self.lka_icon_status_last = (False, False) |
|
|
|
|
self.steer_rate_limited = False |
|
|
|
|
|
|
|
|
|
self.params = CarControllerParams(CP.carFingerprint) |
|
|
|
|
self.params = CarControllerParams() |
|
|
|
|
|
|
|
|
|
self.packer_pt = CANPacker(DBC[CP.carFingerprint]['pt']) |
|
|
|
|
self.packer_ch = CANPacker(DBC[CP.carFingerprint]['chassis']) |
|
|
|
@ -90,9 +74,6 @@ class CarController(): |
|
|
|
|
# Send CAN commands. |
|
|
|
|
can_sends = [] |
|
|
|
|
|
|
|
|
|
alert_out = process_hud_alert(hud_alert) |
|
|
|
|
steer = alert_out |
|
|
|
|
|
|
|
|
|
### STEER ### |
|
|
|
|
|
|
|
|
|
if (frame % P.STEER_STEP) == 0: |
|
|
|
@ -107,76 +88,72 @@ class CarController(): |
|
|
|
|
self.apply_steer_last = apply_steer |
|
|
|
|
idx = (frame // P.STEER_STEP) % 4 |
|
|
|
|
|
|
|
|
|
if self.car_fingerprint in SUPERCRUISE_CARS: |
|
|
|
|
can_sends += gmcan.create_steering_control_ct6(self.packer_pt, |
|
|
|
|
CanBus, apply_steer, CS.out.vEgo, idx, lkas_enabled) |
|
|
|
|
else: |
|
|
|
|
can_sends.append(gmcan.create_steering_control(self.packer_pt, |
|
|
|
|
CanBus.POWERTRAIN, apply_steer, idx, lkas_enabled)) |
|
|
|
|
can_sends.append(gmcan.create_steering_control(self.packer_pt, |
|
|
|
|
CanBus.POWERTRAIN, apply_steer, idx, lkas_enabled)) |
|
|
|
|
|
|
|
|
|
### GAS/BRAKE ### |
|
|
|
|
|
|
|
|
|
if self.car_fingerprint not in SUPERCRUISE_CARS: |
|
|
|
|
# no output if not enabled, but keep sending keepalive messages |
|
|
|
|
# treat pedals as one |
|
|
|
|
final_pedal = actuators.gas - actuators.brake |
|
|
|
|
# no output if not enabled, but keep sending keepalive messages |
|
|
|
|
# treat pedals as one |
|
|
|
|
final_pedal = actuators.gas - actuators.brake |
|
|
|
|
|
|
|
|
|
# *** apply pedal hysteresis *** |
|
|
|
|
final_brake, self.brake_steady = actuator_hystereses( |
|
|
|
|
final_pedal, self.pedal_steady) |
|
|
|
|
# *** apply pedal hysteresis *** |
|
|
|
|
final_brake, self.brake_steady = actuator_hystereses( |
|
|
|
|
final_pedal, self.pedal_steady) |
|
|
|
|
|
|
|
|
|
if not enabled: |
|
|
|
|
# Stock ECU sends max regen when not enabled. |
|
|
|
|
apply_gas = P.MAX_ACC_REGEN |
|
|
|
|
apply_brake = 0 |
|
|
|
|
else: |
|
|
|
|
apply_gas = int(round(interp(final_pedal, P.GAS_LOOKUP_BP, P.GAS_LOOKUP_V))) |
|
|
|
|
apply_brake = int(round(interp(final_pedal, P.BRAKE_LOOKUP_BP, P.BRAKE_LOOKUP_V))) |
|
|
|
|
|
|
|
|
|
# Gas/regen and brakes - all at 25Hz |
|
|
|
|
if (frame % 4) == 0: |
|
|
|
|
idx = (frame // 4) % 4 |
|
|
|
|
|
|
|
|
|
at_full_stop = enabled and CS.out.standstill |
|
|
|
|
near_stop = enabled and (CS.out.vEgo < P.NEAR_STOP_BRAKE_PHASE) |
|
|
|
|
can_sends.append(gmcan.create_friction_brake_command(self.packer_ch, CanBus.CHASSIS, apply_brake, idx, near_stop, at_full_stop)) |
|
|
|
|
|
|
|
|
|
at_full_stop = enabled and CS.out.standstill |
|
|
|
|
can_sends.append(gmcan.create_gas_regen_command(self.packer_pt, CanBus.POWERTRAIN, apply_gas, idx, enabled, at_full_stop)) |
|
|
|
|
|
|
|
|
|
# Send dashboard UI commands (ACC status), 25hz |
|
|
|
|
if (frame % 4) == 0: |
|
|
|
|
can_sends.append(gmcan.create_acc_dashboard_command(self.packer_pt, CanBus.POWERTRAIN, enabled, hud_v_cruise * CV.MS_TO_KPH, hud_show_car)) |
|
|
|
|
|
|
|
|
|
# Radar needs to know current speed and yaw rate (50hz), |
|
|
|
|
# and that ADAS is alive (10hz) |
|
|
|
|
time_and_headlights_step = 10 |
|
|
|
|
tt = frame * DT_CTRL |
|
|
|
|
|
|
|
|
|
if frame % time_and_headlights_step == 0: |
|
|
|
|
idx = (frame // time_and_headlights_step) % 4 |
|
|
|
|
can_sends.append(gmcan.create_adas_time_status(CanBus.OBSTACLE, int((tt - self.start_time) * 60), idx)) |
|
|
|
|
can_sends.append(gmcan.create_adas_headlights_status(CanBus.OBSTACLE)) |
|
|
|
|
|
|
|
|
|
speed_and_accelerometer_step = 2 |
|
|
|
|
if frame % speed_and_accelerometer_step == 0: |
|
|
|
|
idx = (frame // speed_and_accelerometer_step) % 4 |
|
|
|
|
can_sends.append(gmcan.create_adas_steering_status(CanBus.OBSTACLE, idx)) |
|
|
|
|
can_sends.append(gmcan.create_adas_accelerometer_speed_status(CanBus.OBSTACLE, CS.out.vEgo, idx)) |
|
|
|
|
|
|
|
|
|
if frame % P.ADAS_KEEPALIVE_STEP == 0: |
|
|
|
|
can_sends += gmcan.create_adas_keepalive(CanBus.POWERTRAIN) |
|
|
|
|
|
|
|
|
|
# Show green icon when LKA torque is applied, and |
|
|
|
|
# alarming orange icon when approaching torque limit. |
|
|
|
|
# If not sent again, LKA icon disappears in about 5 seconds. |
|
|
|
|
# Conveniently, sending camera message periodically also works as a keepalive. |
|
|
|
|
lka_active = CS.lkas_status == 1 |
|
|
|
|
lka_critical = lka_active and abs(actuators.steer) > 0.9 |
|
|
|
|
lka_icon_status = (lka_active, lka_critical) |
|
|
|
|
if frame % P.CAMERA_KEEPALIVE_STEP == 0 \ |
|
|
|
|
or lka_icon_status != self.lka_icon_status_last: |
|
|
|
|
can_sends.append(gmcan.create_lka_icon_command(CanBus.SW_GMLAN, lka_active, lka_critical, steer)) |
|
|
|
|
self.lka_icon_status_last = lka_icon_status |
|
|
|
|
if not enabled: |
|
|
|
|
# Stock ECU sends max regen when not enabled. |
|
|
|
|
apply_gas = P.MAX_ACC_REGEN |
|
|
|
|
apply_brake = 0 |
|
|
|
|
else: |
|
|
|
|
apply_gas = int(round(interp(final_pedal, P.GAS_LOOKUP_BP, P.GAS_LOOKUP_V))) |
|
|
|
|
apply_brake = int(round(interp(final_pedal, P.BRAKE_LOOKUP_BP, P.BRAKE_LOOKUP_V))) |
|
|
|
|
|
|
|
|
|
# Gas/regen and brakes - all at 25Hz |
|
|
|
|
if (frame % 4) == 0: |
|
|
|
|
idx = (frame // 4) % 4 |
|
|
|
|
|
|
|
|
|
at_full_stop = enabled and CS.out.standstill |
|
|
|
|
near_stop = enabled and (CS.out.vEgo < P.NEAR_STOP_BRAKE_PHASE) |
|
|
|
|
can_sends.append(gmcan.create_friction_brake_command(self.packer_ch, CanBus.CHASSIS, apply_brake, idx, near_stop, at_full_stop)) |
|
|
|
|
|
|
|
|
|
at_full_stop = enabled and CS.out.standstill |
|
|
|
|
can_sends.append(gmcan.create_gas_regen_command(self.packer_pt, CanBus.POWERTRAIN, apply_gas, idx, enabled, at_full_stop)) |
|
|
|
|
|
|
|
|
|
# Send dashboard UI commands (ACC status), 25hz |
|
|
|
|
if (frame % 4) == 0: |
|
|
|
|
can_sends.append(gmcan.create_acc_dashboard_command(self.packer_pt, CanBus.POWERTRAIN, enabled, hud_v_cruise * CV.MS_TO_KPH, hud_show_car)) |
|
|
|
|
|
|
|
|
|
# Radar needs to know current speed and yaw rate (50hz), |
|
|
|
|
# and that ADAS is alive (10hz) |
|
|
|
|
time_and_headlights_step = 10 |
|
|
|
|
tt = frame * DT_CTRL |
|
|
|
|
|
|
|
|
|
if frame % time_and_headlights_step == 0: |
|
|
|
|
idx = (frame // time_and_headlights_step) % 4 |
|
|
|
|
can_sends.append(gmcan.create_adas_time_status(CanBus.OBSTACLE, int((tt - self.start_time) * 60), idx)) |
|
|
|
|
can_sends.append(gmcan.create_adas_headlights_status(CanBus.OBSTACLE)) |
|
|
|
|
|
|
|
|
|
speed_and_accelerometer_step = 2 |
|
|
|
|
if frame % speed_and_accelerometer_step == 0: |
|
|
|
|
idx = (frame // speed_and_accelerometer_step) % 4 |
|
|
|
|
can_sends.append(gmcan.create_adas_steering_status(CanBus.OBSTACLE, idx)) |
|
|
|
|
can_sends.append(gmcan.create_adas_accelerometer_speed_status(CanBus.OBSTACLE, CS.out.vEgo, idx)) |
|
|
|
|
|
|
|
|
|
if frame % P.ADAS_KEEPALIVE_STEP == 0: |
|
|
|
|
can_sends += gmcan.create_adas_keepalive(CanBus.POWERTRAIN) |
|
|
|
|
|
|
|
|
|
# Show green icon when LKA torque is applied, and |
|
|
|
|
# alarming orange icon when approaching torque limit. |
|
|
|
|
# If not sent again, LKA icon disappears in about 5 seconds. |
|
|
|
|
# Conveniently, sending camera message periodically also works as a keepalive. |
|
|
|
|
lka_active = CS.lkas_status == 1 |
|
|
|
|
lka_critical = lka_active and abs(actuators.steer) > 0.9 |
|
|
|
|
lka_icon_status = (lka_active, lka_critical) |
|
|
|
|
if frame % P.CAMERA_KEEPALIVE_STEP == 0 \ |
|
|
|
|
or lka_icon_status != self.lka_icon_status_last: |
|
|
|
|
steer_alert = hud_alert == VisualAlert.steerRequired |
|
|
|
|
can_sends.append(gmcan.create_lka_icon_command(CanBus.SW_GMLAN, lka_active, lka_critical, steer_alert)) |
|
|
|
|
self.lka_icon_status_last = lka_icon_status |
|
|
|
|
|
|
|
|
|
return can_sends |
|
|
|
|