95 lines
4.0 KiB
Python
95 lines
4.0 KiB
Python
from cereal import car
|
|
from opendbc.can.packer import CANPacker
|
|
from openpilot.selfdrive.car import apply_driver_steer_torque_limits
|
|
from openpilot.selfdrive.car.interfaces import CarControllerBase
|
|
from openpilot.selfdrive.car.mazda import mazdacan
|
|
from openpilot.selfdrive.car.mazda.values import CarControllerParams, Buttons
|
|
from openpilot.common.params import Params
|
|
from openpilot.selfdrive.controls.lib.drive_helpers import V_CRUISE_MAX
|
|
from openpilot.common.conversions import Conversions as CV
|
|
|
|
|
|
params_memory = Params("/dev/shm/params")
|
|
|
|
VisualAlert = car.CarControl.HUDControl.VisualAlert
|
|
|
|
|
|
class CarController(CarControllerBase):
|
|
def __init__(self, dbc_name, CP, VM):
|
|
self.CP = CP
|
|
self.apply_steer_last = 0
|
|
self.packer = CANPacker(dbc_name)
|
|
self.brake_counter = 0
|
|
self.frame = 0
|
|
|
|
def update(self, CC, CS, now_nanos, frogpilot_variables):
|
|
hud_control = CC.hudControl
|
|
hud_v_cruise = hud_control.setSpeed
|
|
if hud_v_cruise > 70:
|
|
hud_v_cruise = 0
|
|
actuators = CC.actuators
|
|
accel = actuators.accel
|
|
|
|
can_sends = []
|
|
|
|
apply_steer = 0
|
|
|
|
if CC.latActive:
|
|
# calculate steer and also set limits due to driver torque
|
|
new_steer = int(round(CC.actuators.steer * CarControllerParams.STEER_MAX))
|
|
apply_steer = apply_driver_steer_torque_limits(new_steer, self.apply_steer_last,
|
|
CS.out.steeringTorque, CarControllerParams)
|
|
|
|
if CC.cruiseControl.cancel:
|
|
# If brake is pressed, let us wait >70ms before trying to disable crz to avoid
|
|
# a race condition with the stock system, where the second cancel from openpilot
|
|
# will disable the crz 'main on'. crz ctrl msg runs at 50hz. 70ms allows us to
|
|
# read 3 messages and most likely sync state before we attempt cancel.
|
|
self.brake_counter = self.brake_counter + 1
|
|
if self.frame % 10 == 0 and not (CS.out.brakePressed and self.brake_counter < 7):
|
|
# Cancel Stock ACC if it's enabled while OP is disengaged
|
|
# Send at a rate of 10hz until we sync with stock ACC state
|
|
can_sends.append(mazdacan.create_button_cmd(self.packer, self.CP, CS.crz_btns_counter, Buttons.CANCEL))
|
|
else:
|
|
self.brake_counter = 0
|
|
if CC.cruiseControl.resume and self.frame % 5 == 0:
|
|
# Mazda Stop and Go requires a RES button (or gas) press if the car stops more than 3 seconds
|
|
# Send Resume button when planner wants car to move
|
|
can_sends.append(mazdacan.create_button_cmd(self.packer, self.CP, CS.crz_btns_counter, Buttons.RESUME))
|
|
# ACC Spam
|
|
elif frogpilot_variables.CSLC:
|
|
if CC.enabled and self.frame % 10 == 0 and CS.cruise_buttons == Buttons.NONE and not CS.out.gasPressed and not CS.distance_button:
|
|
slcSet = get_set_speed(self, hud_v_cruise)
|
|
can_sends.extend(mazdacan.create_mazda_acc_spam_command(self.packer, self, CS, slcSet, CS.out.vEgo, frogpilot_variables, accel))
|
|
|
|
self.apply_steer_last = apply_steer
|
|
|
|
# send HUD alerts
|
|
if self.frame % 50 == 0:
|
|
ldw = CC.hudControl.visualAlert == VisualAlert.ldw
|
|
steer_required = CC.hudControl.visualAlert == VisualAlert.steerRequired
|
|
# TODO: find a way to silence audible warnings so we can add more hud alerts
|
|
steer_required = steer_required and CS.lkas_allowed_speed
|
|
can_sends.append(mazdacan.create_alert_command(self.packer, CS.cam_laneinfo, ldw, steer_required))
|
|
|
|
# send steering command
|
|
can_sends.append(mazdacan.create_steering_control(self.packer, self.CP,
|
|
self.frame, apply_steer, CS.cam_lkas))
|
|
|
|
new_actuators = CC.actuators.copy()
|
|
new_actuators.steer = apply_steer / CarControllerParams.STEER_MAX
|
|
new_actuators.steerOutputCan = apply_steer
|
|
|
|
self.frame += 1
|
|
return new_actuators, can_sends
|
|
|
|
def get_set_speed(self, hud_v_cruise):
|
|
v_cruise = min(hud_v_cruise, V_CRUISE_MAX * CV.KPH_TO_MS)
|
|
|
|
v_cruise_slc: float = 0.
|
|
v_cruise_slc = params_memory.get_float("CSLCSpeed")
|
|
|
|
if v_cruise_slc > 0.:
|
|
v_cruise = v_cruise_slc
|
|
return v_cruise
|