Mazda: Parse cruise control buttons (#307)

* Mazda: parse cruise control buttons

* use them properly

* lint

* Reapply "Mazda: Intelligent Cruise Button Management support (#303)" (#306)

This reverts commit e345c21761.

* should be append instead

* should be extend

* just btns
This commit is contained in:
Jason Wen
2025-10-05 21:35:41 -04:00
committed by GitHub
parent 6999183714
commit 153d90c2e5
4 changed files with 67 additions and 3 deletions

View File

@@ -4,12 +4,15 @@ from opendbc.car.common.conversions import Conversions as CV
from opendbc.car.interfaces import CarStateBase
from opendbc.car.mazda.values import DBC, LKAS_LIMITS
from opendbc.sunnypilot.car.mazda.carstate_ext import CarStateExt
ButtonType = structs.CarState.ButtonEvent.Type
class CarState(CarStateBase):
class CarState(CarStateBase, CarStateExt):
def __init__(self, CP, CP_SP):
super().__init__(CP, CP_SP)
CarStateBase.__init__(self, CP, CP_SP)
CarStateExt.__init__(self, CP, CP_SP)
can_define = CANDefine(DBC[CP.carFingerprint][Bus.pt])
self.shifter_values = can_define.dv["GEAR"]["GEAR"]
@@ -113,8 +116,13 @@ class CarState(CarStateBase):
self.cam_laneinfo = cp_cam.vl["CAM_LANEINFO"]
ret.steerFaultPermanent = cp_cam.vl["CAM_LKAS"]["ERR_BIT_1"] == 1
CarStateExt.update(self, ret, ret_sp, can_parsers)
# TODO: add button types for inc and dec
ret.buttonEvents = create_button_events(self.distance_button, prev_distance_button, {1: ButtonType.gapAdjustCruise})
ret.buttonEvents = [
*create_button_events(self.distance_button, prev_distance_button, {1: ButtonType.gapAdjustCruise}),
*self.button_events,
]
return ret, ret_sp

View File

View File

@@ -0,0 +1,36 @@
"""
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
This file is part of sunnypilot and is licensed under the MIT License.
See the LICENSE.md file in the root directory for more details.
"""
from enum import StrEnum
from opendbc.car import Bus, structs
from opendbc.can.parser import CANParser
from opendbc.sunnypilot.car.mazda.values import BUTTONS
class CarStateExt:
def __init__(self, CP, CP_SP):
self.CP = CP
self.CP_SP = CP_SP
self.button_events = []
self.button_states = {button.event_type: False for button in BUTTONS}
def update(self, ret: structs.CarState, ret_sp: structs.CarStateSP, can_parsers: dict[StrEnum, CANParser]):
cp = can_parsers[Bus.pt]
button_events = []
for button in BUTTONS:
state = (cp.vl[button.can_addr][button.can_msg] in button.values)
if self.button_states[button.event_type] != state:
event = structs.CarState.ButtonEvent.new_message()
event.type = button.event_type
event.pressed = state
button_events.append(event)
self.button_states[button.event_type] = state
self.button_events = button_events

View File

@@ -0,0 +1,20 @@
"""
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
This file is part of sunnypilot and is licensed under the MIT License.
See the LICENSE.md file in the root directory for more details.
"""
from collections import namedtuple
from opendbc.car import structs
ButtonType = structs.CarState.ButtonEvent.Type
Button = namedtuple('Button', ['event_type', 'can_addr', 'can_msg', 'values'])
BUTTONS = [
Button(ButtonType.accelCruise, "CRZ_BTNS", "SET_P", [1]),
Button(ButtonType.decelCruise, "CRZ_BTNS", "SET_M", [1]),
Button(ButtonType.cancel, "CRZ_BTNS", "CAN_OFF", [1]),
Button(ButtonType.resumeCruise, "CRZ_BTNS", "RES", [1]),
]