2022-06-30 17:23:12 -07:00
|
|
|
from opendbc.can.packer import CANPacker
|
2023-02-26 17:32:52 -08:00
|
|
|
from selfdrive.car import apply_driver_steer_torque_limits
|
2020-01-17 10:58:43 -08:00
|
|
|
from selfdrive.car.subaru import subarucan
|
2023-06-23 16:24:27 -04:00
|
|
|
from selfdrive.car.subaru.values import DBC, GLOBAL_GEN2, PREGLOBAL_CARS, CanBus, CarControllerParams, SubaruFlags
|
2020-01-17 10:58:43 -08:00
|
|
|
|
|
|
|
|
|
2022-06-30 17:23:12 -07:00
|
|
|
class CarController:
|
2020-02-19 20:37:07 -08:00
|
|
|
def __init__(self, dbc_name, CP, VM):
|
2022-03-18 15:39:22 -07:00
|
|
|
self.CP = CP
|
2020-01-17 10:58:43 -08:00
|
|
|
self.apply_steer_last = 0
|
2022-07-31 12:43:27 -07:00
|
|
|
self.frame = 0
|
|
|
|
|
|
2021-05-19 05:39:09 +03:00
|
|
|
self.cruise_button_prev = 0
|
2022-07-31 12:43:27 -07:00
|
|
|
self.last_cancel_frame = 0
|
2020-01-17 10:58:43 -08:00
|
|
|
|
2021-12-16 01:13:31 +02:00
|
|
|
self.p = CarControllerParams(CP)
|
2020-02-19 20:37:07 -08:00
|
|
|
self.packer = CANPacker(DBC[CP.carFingerprint]['pt'])
|
2020-01-17 10:58:43 -08:00
|
|
|
|
2023-02-09 15:37:39 -08:00
|
|
|
def update(self, CC, CS, now_nanos):
|
2022-06-30 17:23:12 -07:00
|
|
|
actuators = CC.actuators
|
|
|
|
|
hud_control = CC.hudControl
|
|
|
|
|
pcm_cancel_cmd = CC.cruiseControl.cancel
|
2020-01-17 10:58:43 -08:00
|
|
|
|
|
|
|
|
can_sends = []
|
|
|
|
|
|
2020-08-07 07:55:13 +03:00
|
|
|
# *** steering ***
|
2022-06-30 17:23:12 -07:00
|
|
|
if (self.frame % self.p.STEER_STEP) == 0:
|
2021-12-16 01:13:31 +02:00
|
|
|
apply_steer = int(round(actuators.steer * self.p.STEER_MAX))
|
2020-01-17 10:58:43 -08:00
|
|
|
|
|
|
|
|
# limits due to driver torque
|
|
|
|
|
|
|
|
|
|
new_steer = int(round(apply_steer))
|
2023-02-26 17:32:52 -08:00
|
|
|
apply_steer = apply_driver_steer_torque_limits(new_steer, self.apply_steer_last, CS.out.steeringTorque, self.p)
|
2020-01-17 10:58:43 -08:00
|
|
|
|
2022-06-30 17:23:12 -07:00
|
|
|
if not CC.latActive:
|
2020-01-17 10:58:43 -08:00
|
|
|
apply_steer = 0
|
|
|
|
|
|
2022-03-18 15:39:22 -07:00
|
|
|
if self.CP.carFingerprint in PREGLOBAL_CARS:
|
2023-06-12 23:03:04 -07:00
|
|
|
can_sends.append(subarucan.create_preglobal_steering_control(self.packer, apply_steer, CC.latActive))
|
2020-08-07 07:55:13 +03:00
|
|
|
else:
|
2023-06-12 23:03:04 -07:00
|
|
|
can_sends.append(subarucan.create_steering_control(self.packer, apply_steer, CC.latActive))
|
2020-01-17 10:58:43 -08:00
|
|
|
|
|
|
|
|
self.apply_steer_last = apply_steer
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 07:55:13 +03:00
|
|
|
# *** alerts and pcm cancel ***
|
2022-03-18 15:39:22 -07:00
|
|
|
if self.CP.carFingerprint in PREGLOBAL_CARS:
|
2023-05-26 01:33:04 -04:00
|
|
|
if self.frame % 5 == 0:
|
2020-08-07 07:55:13 +03:00
|
|
|
# 1 = main, 2 = set shallow, 3 = set deep, 4 = resume shallow, 5 = resume deep
|
|
|
|
|
# disengage ACC when OP is disengaged
|
|
|
|
|
if pcm_cancel_cmd:
|
2021-05-19 05:39:09 +03:00
|
|
|
cruise_button = 1
|
2020-08-07 07:55:13 +03:00
|
|
|
# turn main on if off and past start-up state
|
|
|
|
|
elif not CS.out.cruiseState.available and CS.ready:
|
2021-05-19 05:39:09 +03:00
|
|
|
cruise_button = 1
|
2020-08-07 07:55:13 +03:00
|
|
|
else:
|
2021-05-19 05:39:09 +03:00
|
|
|
cruise_button = CS.cruise_button
|
2020-08-07 07:55:13 +03:00
|
|
|
|
|
|
|
|
# unstick previous mocked button press
|
2021-05-19 05:39:09 +03:00
|
|
|
if cruise_button == 1 and self.cruise_button_prev == 1:
|
|
|
|
|
cruise_button = 0
|
|
|
|
|
self.cruise_button_prev = cruise_button
|
2020-08-07 07:55:13 +03:00
|
|
|
|
2021-12-15 07:55:14 +02:00
|
|
|
can_sends.append(subarucan.create_preglobal_es_distance(self.packer, cruise_button, CS.es_distance_msg))
|
2020-08-07 07:55:13 +03:00
|
|
|
|
|
|
|
|
else:
|
2022-07-31 12:43:27 -07:00
|
|
|
if pcm_cancel_cmd and (self.frame - self.last_cancel_frame) > 0.2:
|
2023-06-23 16:24:27 -04:00
|
|
|
bus = CanBus.alt if self.CP.carFingerprint in GLOBAL_GEN2 else CanBus.main
|
2022-07-31 12:43:27 -07:00
|
|
|
can_sends.append(subarucan.create_es_distance(self.packer, CS.es_distance_msg, bus, pcm_cancel_cmd))
|
|
|
|
|
self.last_cancel_frame = self.frame
|
|
|
|
|
|
2023-05-26 01:33:04 -04:00
|
|
|
if self.frame % 10 == 0:
|
2022-07-31 12:43:27 -07:00
|
|
|
can_sends.append(subarucan.create_es_dashstatus(self.packer, CS.es_dashstatus_msg))
|
2020-08-07 07:55:13 +03:00
|
|
|
|
2023-04-18 08:54:27 +03:00
|
|
|
can_sends.append(subarucan.create_es_lkas_state(self.packer, CS.es_lkas_state_msg, CC.enabled, hud_control.visualAlert,
|
|
|
|
|
hud_control.leftLaneVisible, hud_control.rightLaneVisible,
|
|
|
|
|
hud_control.leftLaneDepart, hud_control.rightLaneDepart))
|
2023-06-12 23:03:04 -07:00
|
|
|
|
2023-05-26 01:33:04 -04:00
|
|
|
if self.CP.flags & SubaruFlags.SEND_INFOTAINMENT:
|
2023-06-23 16:24:27 -04:00
|
|
|
can_sends.append(subarucan.create_es_infotainment(self.packer, CS.es_infotainment_msg, hud_control.visualAlert))
|
2023-04-17 22:05:16 -04:00
|
|
|
|
2021-12-16 13:08:20 +01:00
|
|
|
new_actuators = actuators.copy()
|
|
|
|
|
new_actuators.steer = self.apply_steer_last / self.p.STEER_MAX
|
2022-12-10 02:03:40 -08:00
|
|
|
new_actuators.steerOutputCan = self.apply_steer_last
|
2021-12-16 13:08:20 +01:00
|
|
|
|
2022-06-30 17:23:12 -07:00
|
|
|
self.frame += 1
|
2021-12-16 13:08:20 +01:00
|
|
|
return new_actuators, can_sends
|