From eb9f52a9fedd7d595322b87ee0b595319d9a8bb1 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 19 Nov 2026 12:21:22 +0100 Subject: [PATCH] leapmotor: announce a cruise cancel the car makes by itself The B10's camera drops ACC on its own, for example when the front passenger unbuckles (route 00000018, 21:25 CET): cruise turns off and unavailable in the same frame, and openpilot only showed pcmDisable and wrongCarMode, a soft chime and no message, at 52 km/h. Such a cancel is now cruiseDisabled: TAKE CONTROL IMMEDIATELY, "Car Cancelled Cruise". The driver's own cancels also make ACC_STATE 1, so braking and the stalk pushed up (now reported as the cancel button) keep the usual disengage. Replayed on the logged CAN: the passenger cancel and two others on route 00000017 (18:47, 19:06) are announced; a brake cancel and a stalk cancel are not. --- .../opendbc/car/leapmotor/carstate.py | 10 +++- .../car/leapmotor/tests/test_leapmotor.py | 18 +++++++ openpilot/selfdrive/car/car_events.py | 9 ++++ openpilot/starpilot/car/events.py | 3 ++ openpilot/starpilot/car/leapmotor/__init__.py | 0 openpilot/starpilot/car/leapmotor/events.py | 43 +++++++++++++++ .../starpilot/car/leapmotor/tests/__init__.py | 0 .../car/leapmotor/tests/test_events.py | 53 +++++++++++++++++++ 8 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 openpilot/starpilot/car/leapmotor/__init__.py create mode 100644 openpilot/starpilot/car/leapmotor/events.py create mode 100644 openpilot/starpilot/car/leapmotor/tests/__init__.py create mode 100644 openpilot/starpilot/car/leapmotor/tests/test_events.py diff --git a/opendbc_repo/opendbc/car/leapmotor/carstate.py b/opendbc_repo/opendbc/car/leapmotor/carstate.py index 5f910f6e68..c6b29a9d2e 100644 --- a/opendbc_repo/opendbc/car/leapmotor/carstate.py +++ b/opendbc_repo/opendbc/car/leapmotor/carstate.py @@ -1,5 +1,5 @@ import numpy as np -from opendbc.car import DT_CTRL, Bus, structs +from opendbc.car import DT_CTRL, Bus, create_button_events, structs from opendbc.can.parser import CANParser from opendbc.car.common.conversions import Conversions as CV from opendbc.car.common.filter_simple import FirstOrderFilter @@ -8,7 +8,10 @@ from opendbc.car.interfaces import CarStateBase from opendbc.car.leapmotor.values import DBC, CarControllerParams GearShifter = structs.CarState.GearShifter +ButtonType = structs.CarState.ButtonEvent.Type GEARS = {0: GearShifter.park, 1: GearShifter.reverse, 2: GearShifter.neutral, 3: GearShifter.drive} +# POWERTRAIN_STATE.STALK_PUSH 3 (stalk pushed up) cancels assist; the engage pushes (1, 2) are not reported +STALK_CANCEL = 3 class CarState(CarStateBase): @@ -27,6 +30,7 @@ class CarState(CarStateBase): self.eps_torque_lagged = FirstOrderFilter(0., CarControllerParams.EPS_TORQUE_LAG, DT_CTRL) self.powertrain_state: dict | None = None self.dashboard_limit = LimitTracker() + self.stalk_cancel = False def update(self, can_parsers) -> structs.CarState: cp = can_parsers[Bus.pt] @@ -101,6 +105,10 @@ class CarState(CarStateBase): # stock ACC / LCC, read from the camera side ret.cruiseState.enabled = bool(cp.vl['POWERTRAIN_STATE']['ASSIST_ENGAGED']) + # the driver's cancel push, so a cancel the car makes by itself can be told apart from the driver's + prev_cancel = self.stalk_cancel + self.stalk_cancel = int(cp.vl['POWERTRAIN_STATE']['STALK_PUSH']) == STALK_CANCEL + ret.buttonEvents = create_button_events(int(self.stalk_cancel), int(prev_cancel), {1: ButtonType.cancel}) # latest VCU frame, copied by the controller to cancel ACC; None until received self.powertrain_state = dict(cp.vl['POWERTRAIN_STATE']) if cp.ts_nanos['POWERTRAIN_STATE']['COUNTER'] > 0 else None # ACC_STATE: 1 not ready (Park, brake, accelerator override), 2 ready, 3 engaged. Engaged also counts as diff --git a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py index 9f101726c6..da0ea6470a 100644 --- a/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py +++ b/opendbc_repo/opendbc/car/leapmotor/tests/test_leapmotor.py @@ -398,6 +398,24 @@ class TestLeapmotorCan(unittest.TestCase): observation, _ = frame(i, limit) self.assertEqual(observation.status, Status.UNKNOWN) + def test_cancel_push_is_a_cancel_button(self): + # only the stalk pushed up (STALK_PUSH 3) is reported, as a cancel press and release; the engage pushes are not + from opendbc.car.leapmotor.carstate import CarState + from opendbc.car.leapmotor.interface import CarInterface + CP = CarInterface.get_non_essential_params("LEAPMOTOR_B10") + CS = CarState(CP) + parsers = CS.get_can_parsers(CP) + packer = CANPacker("leapmotor_b10") + + def frame(i, stalk): + msgs = [packer.make_can_msg("POWERTRAIN_STATE", 0, {"ASSIST_ENGAGED": 1, "STALK_PUSH": stalk, "COUNTER": i % 16})] + parsers[Bus.pt].update([(int((i + 1) * 1e7), msgs)]) + parsers[Bus.cam].update([(int((i + 1) * 1e7), [])]) + return [(b.type, b.pressed) for b in CS.update(parsers).buttonEvents] + + cancel = structs.CarState.ButtonEvent.Type.cancel + pushes = [frame(i, stalk) for i, stalk in enumerate((0, 1, 1, 0, 2, 0, 3, 3, 0))] + self.assertEqual(pushes, [[], [], [], [], [], [], [(cancel, True)], [], [(cancel, False)]]) if __name__ == "__main__": unittest.main() diff --git a/openpilot/selfdrive/car/car_events.py b/openpilot/selfdrive/car/car_events.py index 3543007e3c..e5fc2e7a7d 100644 --- a/openpilot/selfdrive/car/car_events.py +++ b/openpilot/selfdrive/car/car_events.py @@ -26,6 +26,10 @@ class CarEvents: self.low_speed_alert = False self.no_steer_warning = False self.silent_steer_warning = True + self.leapmotor_cancel = None + if CP.brand == 'leapmotor': + from openpilot.starpilot.car.leapmotor.events import CarCancel + self.leapmotor_cancel = CarCancel() def update(self, CS: car.CarState, CS_prev: car.CarState, CC: car.CarControl): if self.CP.brand in ('body', 'mock'): @@ -104,6 +108,11 @@ class CarEvents: # if CC.eps_timer_soft_disable_alert: # events.add(EventName.steerTimeLimit) + elif self.CP.brand == 'leapmotor': + if self.leapmotor_cancel.update(CS, CS_prev): + from openpilot.starpilot.car.leapmotor.events import announce + announce(events) + return events def create_common_events(self, CS: structs.CarState, CS_prev: car.CarState): diff --git a/openpilot/starpilot/car/events.py b/openpilot/starpilot/car/events.py index 8890b31bc0..49aeb029f2 100644 --- a/openpilot/starpilot/car/events.py +++ b/openpilot/starpilot/car/events.py @@ -5,6 +5,9 @@ def event_rules(cp) -> dict: if cp.brand == "honda": from openpilot.starpilot.car.honda.events import event_rules as honda_rules return honda_rules(cp) + if cp.brand == "leapmotor": + from openpilot.starpilot.car.leapmotor.events import event_rules as leapmotor_rules + return leapmotor_rules(cp) return {} diff --git a/openpilot/starpilot/car/leapmotor/__init__.py b/openpilot/starpilot/car/leapmotor/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/openpilot/starpilot/car/leapmotor/events.py b/openpilot/starpilot/car/leapmotor/events.py new file mode 100644 index 0000000000..eb7d7f845e --- /dev/null +++ b/openpilot/starpilot/car/leapmotor/events.py @@ -0,0 +1,43 @@ +"""Leapmotor: a cancel the car makes by itself must not look like the driver's. + +The B10's camera drops its ACC on its own, for example when the front passenger +unbuckles (route 00000018, 2026-11-18): ACC_STATE goes to 1 (not ready), so +cruise is off and unavailable in the same frame. openpilot then only saw +pcmDisable and wrongCarMode, user disables with a soft chime and no message. +The driver's own cancels (brake, the stalk pushed up) also make ACC_STATE 1, +so they are excluded; anything else is announced as a takeover. +""" +from openpilot.cereal import log +from openpilot.selfdrive.selfdrived.events import ET, ImmediateDisableAlert +from opendbc.car import structs + +EventName = log.OnroadEvent.EventName +ButtonType = structs.CarState.ButtonEvent.Type + +CANCEL_PUSH_WINDOW_FRAMES = 50 # 0.5 s at 100 Hz: logged stalk cancels ended assist within about 0.25 s +QUIET_DISABLES = (EventName.pcmDisable, EventName.wrongCarMode) + + +def event_rules(cp) -> dict: + return {EventName.cruiseDisabled: {ET.IMMEDIATE_DISABLE: ImmediateDisableAlert("Car Cancelled Cruise")}} + + +class CarCancel: + def __init__(self): + self.frames_since_cancel_push = CANCEL_PUSH_WINDOW_FRAMES + 1 + + def update(self, cs, cs_prev) -> bool: + """True on the frame the car turns its ACC off without the driver braking or pushing cancel.""" + if any(b.type == ButtonType.cancel for b in cs.buttonEvents): + self.frames_since_cancel_push = 0 + else: + self.frames_since_cancel_push += 1 + return bool(cs_prev.cruiseState.enabled and not cs.cruiseState.enabled and not cs.cruiseState.available and + not cs.brakePressed and not cs_prev.brakePressed and + self.frames_since_cancel_push > CANCEL_PUSH_WINDOW_FRAMES) + + +def announce(events) -> None: + """Replace the quiet user disables with the takeover alert.""" + events.events = [e for e in events.events if e not in QUIET_DISABLES] + events.add(EventName.cruiseDisabled) diff --git a/openpilot/starpilot/car/leapmotor/tests/__init__.py b/openpilot/starpilot/car/leapmotor/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/openpilot/starpilot/car/leapmotor/tests/test_events.py b/openpilot/starpilot/car/leapmotor/tests/test_events.py new file mode 100644 index 0000000000..c219944d1e --- /dev/null +++ b/openpilot/starpilot/car/leapmotor/tests/test_events.py @@ -0,0 +1,53 @@ +from opendbc.car import structs +from opendbc.car.car_helpers import interfaces +from openpilot.cereal import log +from openpilot.selfdrive.car.car_events import CarEvents +from openpilot.selfdrive.selfdrived.events import ET + +EventName = log.OnroadEvent.EventName +ButtonType = structs.CarState.ButtonEvent.Type + + +def state(enabled, available, brake=False, cancel=None): + cs = structs.CarState(vEgo=14., gearShifter=structs.CarState.GearShifter.drive, brakePressed=brake) + cs.cruiseState.enabled, cs.cruiseState.available = enabled, available + if cancel is not None: + cs.buttonEvents = [structs.CarState.ButtonEvent(type=ButtonType.cancel, pressed=cancel)] + return cs + + +def drop(events: CarEvents, cs): + return events.update(cs, state(True, True), None) + + +def car_events(): + return CarEvents(interfaces['LEAPMOTOR_B10'].get_non_essential_params('LEAPMOTOR_B10')) + + +def test_car_cancel_is_a_takeover_alert(): + # the passenger unbuckling on route 00000018: cruise off and unavailable, no brake, no cancel push + events = drop(car_events(), state(False, False)) + assert events.contains(ET.IMMEDIATE_DISABLE) + assert EventName.cruiseDisabled in events.names + assert not events.contains(ET.USER_DISABLE) + alert = events.create_alerts([ET.IMMEDIATE_DISABLE])[0] + assert (alert.alert_text_1, alert.alert_text_2) == ("TAKE CONTROL IMMEDIATELY", "Car Cancelled Cruise") + + +def test_driver_cancels_stay_quiet(): + # braking, or cruise still available (the driver's ordinary cancel), is the usual disengage + for cs in (state(False, False, brake=True), state(False, True)): + events = drop(car_events(), cs) + assert EventName.pcmDisable in events.names + assert not events.contains(ET.IMMEDIATE_DISABLE) + # the stalk pushed up: cruise goes off up to 0.25 s after the press + ev = car_events() + ev.update(state(True, True, cancel=True), state(True, True), None) + for _ in range(25): + ev.update(state(True, True), state(True, True), None) + events = drop(ev, state(False, False)) + assert not events.contains(ET.IMMEDIATE_DISABLE) + # ...but a car cancel long after a push is still announced + for _ in range(60): + ev.update(state(True, True), state(True, True), None) + assert drop(ev, state(False, False)).contains(ET.IMMEDIATE_DISABLE)