From bdc2fe3a9aa692bcf46c6ab052405564f6d8e379 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 6 Feb 2025 19:56:36 -0600 Subject: [PATCH] Add buttonEnable carState field (#1698) * first draft, works! * common func with override * this is common * Revert "this is common" This reverts commit 93ec5f9290a0dec335ee5890846cbeafe0fe8cc9. * forgot vw * no * Update opendbc/car/gm/carstate.py --- opendbc/car/car.capnp | 1 + opendbc/car/gm/carstate.py | 9 +++++++++ opendbc/car/interfaces.py | 11 +++++++++++ opendbc/car/volkswagen/carstate.py | 10 ++++++++++ 4 files changed, 31 insertions(+) diff --git a/opendbc/car/car.capnp b/opendbc/car/car.capnp index f5f562f436..cf28e1e8b6 100644 --- a/opendbc/car/car.capnp +++ b/opendbc/car/car.capnp @@ -214,6 +214,7 @@ struct CarState { # button presses buttonEvents @11 :List(ButtonEvent); + buttonEnable @57 :Bool; # user is requesting enable, usually one frame. set if pcmCruise=False leftBlinker @20 :Bool; rightBlinker @21 :Bool; genericToggle @23 :Bool; diff --git a/opendbc/car/gm/carstate.py b/opendbc/car/gm/carstate.py index dbecf5266a..1694ae838c 100644 --- a/opendbc/car/gm/carstate.py +++ b/opendbc/car/gm/carstate.py @@ -33,6 +33,15 @@ def __init__(self, CP): self.distance_button = 0 + def update_button_enable(self, buttonEvents: list[structs.CarState.ButtonEvent]): + if not self.CP.pcmCruise: + for b in buttonEvents: + # The ECM allows enabling on falling edge of set, but only rising edge of resume + if (b.type == ButtonType.accelCruise and b.pressed) or \ + (b.type == ButtonType.decelCruise and not b.pressed): + return True + return False + def update(self, can_parsers) -> structs.CarState: pt_cp = can_parsers[Bus.pt] cam_cp = can_parsers[Bus.cam] diff --git a/opendbc/car/interfaces.py b/opendbc/car/interfaces.py index 3384664e4b..edb689dc10 100644 --- a/opendbc/car/interfaces.py +++ b/opendbc/car/interfaces.py @@ -19,6 +19,7 @@ from opendbc.can.parser import CANParser GearShifter = structs.CarState.GearShifter +ButtonType = structs.CarState.ButtonEvent.Type V_CRUISE_MAX = 145 MAX_CTRL_SPEED = (V_CRUISE_MAX + 4) * CV.KPH_TO_MS @@ -243,6 +244,8 @@ def update(self, can_packets: list[tuple[int, list[CanData]]]) -> structs.CarSta if ret.cruiseState.speedCluster == 0: ret.cruiseState.speedCluster = ret.cruiseState.speed + ret.buttonEnable = self.CS.update_button_enable(ret.buttonEvents) + # save for next iteration self.CS.out = ret @@ -345,6 +348,14 @@ def update_blinker_from_stalk(self, blinker_time: int, left_blinker_stalk: bool, return bool(left_blinker_stalk or self.left_blinker_cnt > 0), bool(right_blinker_stalk or self.right_blinker_cnt > 0) + def update_button_enable(self, buttonEvents: list[structs.CarState.ButtonEvent]): + if not self.CP.pcmCruise: + for b in buttonEvents: + # Enable OP long on falling edge of enable buttons + if b.type in (ButtonType.accelCruise, ButtonType.decelCruise) and not b.pressed: + return True + return False + @staticmethod def parse_gear_shifter(gear: str | None) -> structs.CarState.GearShifter: if gear is None: diff --git a/opendbc/car/volkswagen/carstate.py b/opendbc/car/volkswagen/carstate.py index 55e2834506..4515888151 100644 --- a/opendbc/car/volkswagen/carstate.py +++ b/opendbc/car/volkswagen/carstate.py @@ -6,6 +6,8 @@ from opendbc.car.volkswagen.values import DBC, CANBUS, NetworkLocation, TransmissionType, GearShifter, \ CarControllerParams, VolkswagenFlags +ButtonType = structs.CarState.ButtonEvent.Type + class CarState(CarStateBase): def __init__(self, CP): @@ -18,6 +20,14 @@ def __init__(self, CP): self.upscale_lead_car_signal = False self.eps_stock_values = False + def update_button_enable(self, buttonEvents: list[structs.CarState.ButtonEvent]): + if not self.CP.pcmCruise: + for b in buttonEvents: + # Enable OP long on falling edge of enable buttons + if b.type in (ButtonType.setCruise, ButtonType.resumeCruise) and not b.pressed: + return True + return False + def create_button_events(self, pt_cp, buttons): button_events = []