Skip to content

Commit

Permalink
Add buttonEnable carState field (#1698)
Browse files Browse the repository at this point in the history
* first draft, works!

* common func with override

* this is common

* Revert "this is common"

This reverts commit 93ec5f9.

* forgot vw

* no

* Update opendbc/car/gm/carstate.py
  • Loading branch information
sshane authored Feb 7, 2025
1 parent 1e11a0f commit bdc2fe3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions opendbc/car/car.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions opendbc/car/gm/carstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 11 additions & 0 deletions opendbc/car/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions opendbc/car/volkswagen/carstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = []

Expand Down

0 comments on commit bdc2fe3

Please sign in to comment.