encapsulate in method

This commit is contained in:
Jason Wen
2024-07-20 18:23:38 -04:00
parent fb06ddf85b
commit 5facba229a
13 changed files with 77 additions and 137 deletions

View File

@@ -43,10 +43,16 @@ def create_button_events(cur_btn: int, prev_btn: int, buttons_dict: dict[int, ca
return events
def create_mads_event(mads_event_lock: bool) -> capnp.lib.capnp._DynamicStructBuilder:
be = car.CarState.ButtonEvent(pressed=mads_event_lock)
be.type = ButtonType.altButton1
return be
def create_mads_event(mads_enabled: bool, prev_mads_enabled: bool,
mads_event_lock: bool) -> tuple[list[capnp.lib.capnp._DynamicStructBuilder], bool]:
events: list[capnp.lib.capnp._DynamicStructBuilder] = []
mads_changed = prev_mads_enabled != mads_enabled
if (mads_changed and mads_event_lock) or (not mads_changed and not mads_event_lock):
events.append(car.CarState.ButtonEvent(pressed=mads_event_lock, type=ButtonType.altButton1))
mads_event_lock = not mads_event_lock
return events, mads_event_lock
def gen_empty_fingerprint():

View File

@@ -93,15 +93,14 @@ class CarInterface(CarInterfaceBase):
ret = self.CS.update(self.cp, self.cp_cam)
self.sp_update_params()
self.CS.button_events = [
ret.buttonEvents = [
*self.CS.button_events,
*create_button_events(self.CS.distance_button, self.CS.prev_distance_button, {1: ButtonType.gapAdjustCruise})
]
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise,
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise,
enable_buttons=(ButtonType.accelCruise, ButtonType.decelCruise, ButtonType.resumeCruise) if not self.CP.pcmCruiseSpeed else
(ButtonType.accelCruise, ButtonType.decelCruise),
resume_button=(ButtonType.resumeCruise,) if not self.CP.pcmCruiseSpeed else
@@ -120,7 +119,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = self.get_sp_started_mads(ret, self.CS)
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0) or not self.CP.pcmCruiseSpeed:
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -138,17 +137,10 @@ class CarInterface(CarInterfaceBase):
ret, self.CS = self.get_sp_common_state(ret, self.CS, gap_button=bool(self.CS.distance_button))
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# events
events = self.create_common_events(ret, c, extra_gears=[car.CarState.GearShifter.low], pcm_enable=False)

View File

@@ -74,15 +74,14 @@ class CarInterface(CarInterfaceBase):
ret = self.CS.update(self.cp, self.cp_cam)
self.sp_update_params()
self.CS.button_events = [
ret.buttonEvents = [
*self.CS.button_events,
*create_button_events(self.CS.distance_button, self.CS.prev_distance_button, {1: ButtonType.gapAdjustCruise})
]
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise)
if ret.cruiseState.available:
if self.enable_mads:
@@ -95,7 +94,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = False
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0):
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -108,16 +107,10 @@ class CarInterface(CarInterfaceBase):
ret, self.CS = self.get_sp_common_state(ret, self.CS, gap_button=bool(self.CS.distance_button))
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
events = self.create_common_events(ret, c, extra_gears=[GearShifter.manumatic], pcm_enable=False)

View File

@@ -210,7 +210,7 @@ class CarInterface(CarInterfaceBase):
# Don't add event if transitioning from INIT, unless it's to an actual button
if self.CS.cruise_buttons != CruiseButtons.UNPRESS or self.CS.prev_cruise_buttons != CruiseButtons.INIT:
self.CS.button_events = [
ret.buttonEvents = [
*create_button_events(self.CS.cruise_buttons, self.CS.prev_cruise_buttons, BUTTONS_DICT,
unpressed_btn=CruiseButtons.UNPRESS),
*create_button_events(self.CS.distance_button, self.CS.prev_distance_button,
@@ -221,11 +221,10 @@ class CarInterface(CarInterfaceBase):
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
if not self.CP.pcmCruise:
if any(b.type == ButtonType.accelCruise and b.pressed for b in self.CS.button_events):
if any(b.type == ButtonType.accelCruise and b.pressed for b in ret.buttonEvents):
self.CS.accEnabled = True
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise)
if ret.cruiseState.available:
if self.enable_mads:
@@ -238,7 +237,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = False
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0):
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -251,17 +250,10 @@ class CarInterface(CarInterfaceBase):
ret, self.CS = self.get_sp_common_state(ret, self.CS, gap_button=bool(distance_button))
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# The ECM allows enabling on falling edge of set, but only rising edge of resume
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low,

View File

@@ -6,7 +6,7 @@ from openpilot.common.numpy_fast import interp
from openpilot.selfdrive.car.honda.hondacan import CanBus
from openpilot.selfdrive.car.honda.values import CarControllerParams, CruiseButtons, CruiseSettings, HondaFlags, CAR, HONDA_BOSCH, \
HONDA_NIDEC_ALT_SCM_MESSAGES, HONDA_BOSCH_RADARLESS
from openpilot.selfdrive.car import create_button_events, get_safety_config
from openpilot.selfdrive.car import create_button_events, get_safety_config, create_mads_event
from openpilot.selfdrive.car.interfaces import CarInterfaceBase
from openpilot.selfdrive.car.disable_ecu import disable_ecu
@@ -268,15 +268,14 @@ class CarInterface(CarInterfaceBase):
ret = self.CS.update(self.cp, self.cp_cam, self.cp_body)
self.sp_update_params()
self.CS.button_events = [
ret.buttonEvents = [
*create_button_events(self.CS.cruise_buttons, self.CS.prev_cruise_buttons, BUTTONS_DICT),
*create_button_events(self.CS.cruise_setting, self.CS.prev_cruise_setting, SETTINGS_BUTTONS_DICT),
]
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise)
if ret.cruiseState.available:
if self.enable_mads:
@@ -289,7 +288,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = False
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0) or not self.CP.pcmCruiseSpeed:
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -304,7 +303,10 @@ class CarInterface(CarInterfaceBase):
min_enable_speed_pcm=(self.CP.pcmCruise and self.CP.minEnableSpeed > 0 and self.CP.pcmCruiseSpeed),
gap_button=(self.CS.cruise_setting == 3))
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# events
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low], pcm_enable=False)

View File

@@ -205,10 +205,9 @@ class CarInterface(CarInterfaceBase):
ret = self.CS.update(self.cp, self.cp_cam)
self.sp_update_params()
self.CS.button_events = create_button_events(self.CS.cruise_buttons[-1], self.CS.prev_cruise_buttons, BUTTONS_DICT)
ret.buttonEvents = create_button_events(self.CS.cruise_buttons[-1], self.CS.prev_cruise_buttons, BUTTONS_DICT)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise)
self.CS.mads_enabled = False if not self.mads_main_toggle else self.CS.mads_enabled
@@ -231,7 +230,7 @@ class CarInterface(CarInterfaceBase):
if not self.CP.pcmCruise or not self.CP.pcmCruiseSpeed:
if not self.CP.pcmCruise:
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if not self.CP.pcmCruiseSpeed:
if not ret.cruiseState.enabled:
@@ -242,17 +241,10 @@ class CarInterface(CarInterfaceBase):
ret, self.CS = self.get_sp_common_state(ret, self.CS, gap_button=(self.CS.cruise_buttons[-1] == 3))
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# On some newer model years, the CANCEL button acts as a pause/resume button based on the PCM state
# To avoid re-engaging when openpilot cancels, check user engagement intention via buttons

View File

@@ -560,12 +560,12 @@ class CarInterfaceBase(ABC):
return mads_enabled
def get_sp_v_cruise_non_pcm_state(self, cs_out, acc_enabled, button_events, vCruise,
def get_sp_v_cruise_non_pcm_state(self, cs_out, acc_enabled, vCruise,
enable_buttons=(ButtonType.accelCruise, ButtonType.decelCruise),
resume_button=(ButtonType.accelCruise, ButtonType.resumeCruise)):
if cs_out.cruiseState.available:
for b in button_events:
for b in cs_out.buttonEvents:
if not self.CP.pcmCruise or not self.CP.pcmCruiseSpeed:
if b.type in enable_buttons and not b.pressed:
acc_enabled = True

View File

@@ -40,15 +40,14 @@ class CarInterface(CarInterfaceBase):
self.sp_update_params()
# TODO: add button types for inc and dec
self.CS.button_events = [
ret.buttonEvents = [
*self.CS.button_events,
*create_button_events(self.CS.distance_button, self.CS.prev_distance_button, {1: ButtonType.gapAdjustCruise})
]
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise)
if ret.cruiseState.available:
if self.enable_mads:
@@ -61,7 +60,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = False
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0) or not self.CP.pcmCruiseSpeed:
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -74,17 +73,10 @@ class CarInterface(CarInterfaceBase):
ret, self.CS = self.get_sp_common_state(ret, self.CS, gap_button=bool(self.CS.distance_button))
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# events
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low, GearShifter.brake],

View File

@@ -60,17 +60,10 @@ class CarInterface(CarInterfaceBase):
be.type = ButtonType.cancel
self.CS.button_events.append(be)
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*self.CS.button_events,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low, GearShifter.brake],
pcm_enable=False)

View File

@@ -150,17 +150,10 @@ class CarInterface(CarInterfaceBase):
be.type = ButtonType.cancel
self.CS.button_events.append(be)
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*self.CS.button_events,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low], pcm_enable=False)

View File

@@ -251,17 +251,10 @@ class CarInterface(CarInterfaceBase):
be.type = ButtonType.cancel
self.CS.button_events.append(be)
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
# events
events = self.create_common_events(ret, c, extra_gears=[GearShifter.sport, GearShifter.low, GearShifter.brake],

View File

@@ -150,7 +150,7 @@ class CarState(CarStateBase):
# Update button states for turn signals and ACC controls, capture all ACC button state/config for passthrough
ret.leftBlinker = ret.leftBlinkerOn = bool(pt_cp.vl["Blinkmodi_02"]["Comfort_Signal_Left"])
ret.rightBlinker = ret.rightBlinkerOn = bool(pt_cp.vl["Blinkmodi_02"]["Comfort_Signal_Right"])
self.button_events = self.create_button_events(pt_cp, self.CCP.BUTTONS)
ret.buttonEvents = self.create_button_events(pt_cp, self.CCP.BUTTONS)
self.gra_stock_values = pt_cp.vl["GRA_ACC_01"]
# Additional safety checks performed in CarInterface.
@@ -256,7 +256,7 @@ class CarState(CarStateBase):
# Update button states for turn signals and ACC controls, capture all ACC button state/config for passthrough
ret.leftBlinker, ret.rightBlinker = ret.leftBlinkerOn, ret.rightBlinkerOn = self.update_blinker_from_stalk(300, pt_cp.vl["Gate_Komf_1"]["GK1_Blinker_li"],
pt_cp.vl["Gate_Komf_1"]["GK1_Blinker_re"])
self.button_events = self.create_button_events(pt_cp, self.CCP.BUTTONS)
ret.buttonEvents = self.create_button_events(pt_cp, self.CCP.BUTTONS)
self.gra_stock_values = pt_cp.vl["GRA_Neu"]
# Additional safety checks performed in CarInterface.

View File

@@ -117,8 +117,7 @@ class CarInterface(CarInterfaceBase):
self.CS.mads_enabled = self.get_sp_cruise_main_state(ret, self.CS)
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled,
self.CS.button_events, c.vCruise,
self.CS.accEnabled = self.get_sp_v_cruise_non_pcm_state(ret, self.CS.accEnabled, c.vCruise,
enable_buttons=(ButtonType.setCruise, ButtonType.resumeCruise))
if ret.cruiseState.available:
@@ -131,7 +130,7 @@ class CarInterface(CarInterfaceBase):
self.CS.madsEnabled = self.get_sp_started_mads(ret, self.CS)
if not self.CP.pcmCruise or (self.CP.pcmCruise and self.CP.minEnableSpeed > 0) or not self.CP.pcmCruiseSpeed:
if any(b.type == ButtonType.cancel for b in self.CS.button_events):
if any(b.type == ButtonType.cancel for b in ret.buttonEvents):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
if self.get_sp_pedal_disengage(ret):
self.CS.madsEnabled, self.CS.accEnabled = self.get_sp_cancel_cruise_state(self.CS.madsEnabled)
@@ -143,19 +142,12 @@ class CarInterface(CarInterfaceBase):
self.CS.accEnabled = ret.cruiseState.enabled or self.CS.accEnabled
ret, self.CS = self.get_sp_common_state(ret, self.CS,
gap_button=any(b.type == ButtonType.gapAdjustCruise and b.pressed for b in self.CS.button_events))
gap_button=any(b.type == ButtonType.gapAdjustCruise and b.pressed for b in ret.buttonEvents))
# MADS BUTTON
if self.CS.out.madsEnabled != self.CS.madsEnabled:
if self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = False
else:
if not self.mads_event_lock:
self.CS.button_events.append(create_mads_event(self.mads_event_lock))
self.mads_event_lock = True
ret.buttonEvents = self.CS.button_events
ret.buttonEvents = [
*ret.buttonEvents,
*create_mads_event(self.CS.madsEnabled, self.CS.out.madsEnabled, self.mads_event_lock) # MADS BUTTON
]
events = self.create_common_events(ret, c, extra_gears=[GearShifter.eco, GearShifter.sport, GearShifter.manumatic],
pcm_enable=False,