|
|
|
|
@@ -1,11 +1,14 @@
|
|
|
|
|
from cereal import car
|
|
|
|
|
from openpilot.common.conversions import Conversions as CV
|
|
|
|
|
from openpilot.common.filter_simple import FirstOrderFilter
|
|
|
|
|
from openpilot.common.numpy_fast import interp
|
|
|
|
|
from openpilot.common.realtime import DT_CTRL
|
|
|
|
|
from opendbc.can.packer import CANPacker
|
|
|
|
|
from openpilot.selfdrive.car import apply_driver_steer_torque_limits
|
|
|
|
|
from openpilot.selfdrive.car.gm import gmcan
|
|
|
|
|
from openpilot.selfdrive.car.gm.values import DBC, CanBus, CarControllerParams, CruiseButtons, GMFlags, CC_ONLY_CAR, SDGM_CAR
|
|
|
|
|
from openpilot.selfdrive.controls.lib.drive_helpers import apply_deadzone
|
|
|
|
|
from openpilot.selfdrive.controls.lib.vehicle_model import ACCELERATION_DUE_TO_GRAVITY
|
|
|
|
|
|
|
|
|
|
VisualAlert = car.CarControl.HUDControl.VisualAlert
|
|
|
|
|
NetworkLocation = car.CarParams.NetworkLocation
|
|
|
|
|
@@ -18,6 +21,10 @@ CAMERA_CANCEL_DELAY_FRAMES = 10
|
|
|
|
|
# Enforce a minimum interval between steering messages to avoid a fault
|
|
|
|
|
MIN_STEER_MSG_INTERVAL_MS = 15
|
|
|
|
|
|
|
|
|
|
# Constants for pitch compensation
|
|
|
|
|
PITCH_DEADZONE = 0.01 # [radians] 0.01 ≈ 1% grade
|
|
|
|
|
BRAKE_PITCH_FACTOR_BP = [5., 10.] # [m/s] smoothly revert to planned accel at low speeds
|
|
|
|
|
BRAKE_PITCH_FACTOR_V = [0., 1.] # [unitless in [0,1]]; don't touch
|
|
|
|
|
|
|
|
|
|
class CarController:
|
|
|
|
|
def __init__(self, dbc_name, CP, VM):
|
|
|
|
|
@@ -43,9 +50,12 @@ class CarController:
|
|
|
|
|
self.packer_ch = CANPacker(DBC[self.CP.carFingerprint]['chassis'])
|
|
|
|
|
|
|
|
|
|
# FrogPilot variables
|
|
|
|
|
self.pitch = FirstOrderFilter(0., 0.09 * 4, DT_CTRL * 4) # runs at 25 Hz
|
|
|
|
|
self.accel_g = 0.0
|
|
|
|
|
|
|
|
|
|
def update(self, CC, CS, now_nanos, frogpilot_variables):
|
|
|
|
|
actuators = CC.actuators
|
|
|
|
|
accel = actuators.accel
|
|
|
|
|
hud_control = CC.hudControl
|
|
|
|
|
hud_alert = hud_control.visualAlert
|
|
|
|
|
hud_v_cruise = hud_control.setSpeed
|
|
|
|
|
@@ -92,16 +102,25 @@ class CarController:
|
|
|
|
|
# Gas/regen, brakes, and UI commands - all at 25Hz
|
|
|
|
|
if self.frame % 4 == 0:
|
|
|
|
|
stopping = actuators.longControlState == LongCtrlState.stopping
|
|
|
|
|
|
|
|
|
|
# Pitch compensated acceleration;
|
|
|
|
|
# TODO: include future pitch (sm['modelDataV2'].orientation.y) to account for long actuator delay
|
|
|
|
|
if frogpilot_variables.long_pitch and len(CC.orientationNED) > 1:
|
|
|
|
|
self.pitch.update(CC.orientationNED[1])
|
|
|
|
|
self.accel_g = ACCELERATION_DUE_TO_GRAVITY * apply_deadzone(self.pitch.x, PITCH_DEADZONE) # driving uphill is positive pitch
|
|
|
|
|
accel += self.accel_g
|
|
|
|
|
|
|
|
|
|
if not CC.longActive:
|
|
|
|
|
# ASCM sends max regen when not enabled
|
|
|
|
|
self.apply_gas = self.params.INACTIVE_REGEN
|
|
|
|
|
self.apply_brake = 0
|
|
|
|
|
else:
|
|
|
|
|
brake_accel = actuators.accel + self.accel_g * interp(CS.out.vEgo, BRAKE_PITCH_FACTOR_BP, BRAKE_PITCH_FACTOR_V)
|
|
|
|
|
if frogpilot_variables.sport_plus:
|
|
|
|
|
self.apply_gas = int(round(interp(actuators.accel, self.params.GAS_LOOKUP_BP_PLUS, self.params.GAS_LOOKUP_V_PLUS)))
|
|
|
|
|
self.apply_gas = int(round(interp(accel if frogpilot_variables.long_pitch else actuators.accel, self.params.GAS_LOOKUP_BP_PLUS, self.params.GAS_LOOKUP_V_PLUS)))
|
|
|
|
|
else:
|
|
|
|
|
self.apply_gas = int(round(interp(actuators.accel, self.params.GAS_LOOKUP_BP, self.params.GAS_LOOKUP_V)))
|
|
|
|
|
self.apply_brake = int(round(interp(actuators.accel, self.params.BRAKE_LOOKUP_BP, self.params.BRAKE_LOOKUP_V)))
|
|
|
|
|
self.apply_gas = int(round(interp(accel if frogpilot_variables.long_pitch else actuators.accel, self.params.GAS_LOOKUP_BP, self.params.GAS_LOOKUP_V)))
|
|
|
|
|
self.apply_brake = int(round(interp(brake_accel if frogpilot_variables.long_pitch else actuators.accel, self.params.BRAKE_LOOKUP_BP, self.params.BRAKE_LOOKUP_V)))
|
|
|
|
|
# Don't allow any gas above inactive regen while stopping
|
|
|
|
|
# FIXME: brakes aren't applied immediately when enabling at a stop
|
|
|
|
|
if stopping:
|
|
|
|
|
@@ -132,6 +151,9 @@ class CarController:
|
|
|
|
|
send_fcw = hud_alert == VisualAlert.fcw
|
|
|
|
|
can_sends.append(gmcan.create_acc_dashboard_command(self.packer_pt, CanBus.POWERTRAIN, CC.enabled,
|
|
|
|
|
hud_v_cruise * CV.MS_TO_KPH, hud_control.leadVisible, send_fcw))
|
|
|
|
|
else:
|
|
|
|
|
# to keep accel steady for logs when not sending gas
|
|
|
|
|
accel += self.accel_g
|
|
|
|
|
|
|
|
|
|
# Radar needs to know current speed and yaw rate (50hz),
|
|
|
|
|
# and that ADAS is alive (10hz)
|
|
|
|
|
@@ -172,6 +194,7 @@ class CarController:
|
|
|
|
|
can_sends.append(gmcan.create_pscm_status(self.packer_pt, CanBus.CAMERA, CS.pscm_status))
|
|
|
|
|
|
|
|
|
|
new_actuators = actuators.copy()
|
|
|
|
|
new_actuators.accel = accel
|
|
|
|
|
new_actuators.steer = self.apply_steer_last / self.params.STEER_MAX
|
|
|
|
|
new_actuators.steerOutputCan = self.apply_steer_last
|
|
|
|
|
new_actuators.gas = self.apply_gas
|
|
|
|
|
|