From e20c8bd884e6df50a61cb37b4bc8f308f642ceb6 Mon Sep 17 00:00:00 2001 From: firestar5683 <168790843+firestar5683@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:07:09 -0600 Subject: [PATCH] Preserve Toyota lateral intent during gas source interruptions --- .../starpilot/aol/source_availability.py | 18 +++- .../aol/tests/test_toyota_brake_source_gap.py | 100 ++++++++++++++++++ openpilot/starpilot/car/toyota/aol.py | 18 ++++ prebuilt.json | 2 +- 4 files changed, 134 insertions(+), 4 deletions(-) diff --git a/openpilot/starpilot/aol/source_availability.py b/openpilot/starpilot/aol/source_availability.py index 022c9fc8c8..9d769ed226 100644 --- a/openpilot/starpilot/aol/source_availability.py +++ b/openpilot/starpilot/aol/source_availability.py @@ -247,13 +247,25 @@ def ordinary_source_availability(self, CS, now_ns, native=None, *, replay: bool, ford_gas = bool(ford_native is not None and (native is None or native == ford_native)) if ford_gas: native = ford_native + toyota_gas = False + if (self.CP.brand == 'toyota' and proof[8] and not proof[9] and accepted_state.gasPressed and CS.gasPressed and + not getattr(self, 'aol_dm_lateral_inhibit', False) and + accepted.allowedLatch and accepted.lateralArmed and accepted.settingsQualified and + not accepted.pauseLateral and not accepted.pauseLongitudinal and + prior_native is not None and prior_native.requestedLateral and prior_native.lateralAllowed and + prior_native.requestedLongitudinal and not prior_native.longitudinalAllowed): + from openpilot.starpilot.car.toyota.aol import healthy_gas_transport_native + gas_native = healthy_gas_transport_native(self.sm, self.CP, CS, self.aol_session_id, now_ns) + toyota_gas = bool(gas_native is not None and (native is None or native == gas_native)) + if toyota_gas: + native = gas_native samples = getattr(self, 'car_state_samples', ()) or ((CS, bool(getattr(self, 'car_state_retained_valid', False) or held_metadata)),) if any(not valid or not sample.canValid or sample.canTimeout or sample.steerFaultPermanent or sample.steerFaultTemporary or sample.accFaulted or sample.doorOpen or sample.seatbeltUnlatched or (sample.brakePressed or sample.regenBraking) and not held_pedal or - sample.gasPressed and not (toyota_pedal or stock_gas or ford_gas) or - (stock_gas or ford_gas) and sample.gasPressed != accepted_state.gasPressed or physical_cancel(sample, self.CP) or + sample.gasPressed and not (toyota_pedal or stock_gas or ford_gas or toyota_gas) or + (stock_gas or ford_gas or toyota_gas) and sample.gasPressed != accepted_state.gasPressed or physical_cancel(sample, self.CP) or sample.gearShifter not in (car.CarState.GearShifter.drive, car.CarState.GearShifter.sport, car.CarState.GearShifter.low, car.CarState.GearShifter.brake) for sample, valid in samples): @@ -301,7 +313,7 @@ def ordinary_source_availability(self, CS, now_ns, native=None, *, replay: bool, lateral_intent_only and not (accepted.allowedLatch and accepted.lateralArmed and not accepted.pauseLateral and not getattr(self, 'aol_dm_lateral_inhibit', False) and (native.requestedLateral and native.lateralAllowed or withdrawn_source)) or - not ford_gas and not healthy_transport(self.sm, self.CP, CS, self.aol_session_id, now_ns, native) or + not (ford_gas or toyota_gas) and not healthy_transport(self.sm, self.CP, CS, self.aol_session_id, now_ns, native) or native.observedMonoTime < prior_native.observedMonoTime or native.pandaSerial != prior_native.pandaSerial or tuple(p.hardwareSerial for p in native.pandaInventory) != tuple(p.hardwareSerial for p in prior_native.pandaInventory)): diff --git a/openpilot/starpilot/aol/tests/test_toyota_brake_source_gap.py b/openpilot/starpilot/aol/tests/test_toyota_brake_source_gap.py index b4a233a735..b043017b4b 100644 --- a/openpilot/starpilot/aol/tests/test_toyota_brake_source_gap.py +++ b/openpilot/starpilot/aol/tests/test_toyota_brake_source_gap.py @@ -114,3 +114,103 @@ def test_rav4_brake_source_gap_does_not_waive_restrictions(fault): assert not axis.desiredLateral and sd.aol_last_intent is None else: assert str(axis.faultReason) != 'availabilityPause' + + +@pytest.mark.parametrize('steering', (False, True)) +def test_corolla_gas_override_source_gap_preserves_lateral_intent(steering): + with ExitStack() as cleanup: + cleanup.enter_context(OpenpilotPrefix()) + case = fixture.IoniqAvailabilityTests(methodName='runTest') + cleanup.callback(case.doCleanups) + cp = CarInterface.get_params(CAR.TOYOTA_COROLLA_TSS2, {0: {}, 1: {}, 2: {}}, [], False, False, False) + sd, _, drive = case._authority_callers(True, cp=cp, genuine_events=True) + refresh_saved_settings(sd) + def state(): + cs = fixture.car_state() + cs.vEgo = cs.vEgoRaw = 30. + cs.cruiseState.enabled = True + return cs + for tick in (0, 1, 2): + axis, _, command = drive(tick, observed_state=state(), gas=True, steering=steering, + offset_ns=5_328_073 if tick == 2 else 0, state_stamp=BASE if tick == 2 else None, + intent_message=intent(BASE, 3) if tick == 2 else None, + long_allowed=False, native_request_long=True) + assert axis.lateralActive and command.latActive and not axis.longitudinalActive and not command.longActive + # Recorded source31.321875ms old, previous output still4.006198ms live. + axis, host, command = drive(5, offset_ns=1_321_875, timeout=True, + state_stamp=BASE, intent_message=intent(BASE, 3), gas=True, steering=steering, + long_allowed=False, native_request_long=True) + assert log.OnroadEvent.EventName.controlsMismatch not in sd.events.names, 'COROLLA_GAS_SOURCE_GAP' + assert str(axis.faultReason) == 'availabilityPause' and axis.sourceCarStateMonoTime == 0 + assert axis.desiredLateral and not axis.desiredLongitudinal and not sd.aol_authority_lost + assert not command.latActive and not command.longActive and host.enabled + assert str(host.state) == 'softDisabling' and log.OnroadEvent.EventName.commIssue in sd.events.names + source = BASE + 27_305_261 + axis, _, command = drive(6, state_stamp=source, intent_message=intent(source, 4), observed_state=state(), + gas=True, steering=steering, long_allowed=False, native_request_long=True) + assert axis.lateralActive and command.latActive and not command.longActive and not sd.aol_authority_lost + + +def test_corolla_gas_source_pause_reaches_actual_card_without_latch_loss(): + from openpilot.starpilot.aol.tests.stock_lateral_test_helpers import callers + with ExitStack() as cleanup: + sd, _, drive, card, receive = callers(cleanup, 'toyota', False) + refresh_saved_settings(sd) + def state(): + cs = fixture.car_state() + cs.vEgo = cs.vEgoRaw = 30. + cs.cruiseState.enabled = True + cs.gasPressed = True + cs.steeringPressed = True + return cs + axis, _, command = drive(2, observed_state=state(), gas=True, steering=True, + offset_ns=5_328_073, state_stamp=BASE, intent_message=intent(BASE, 3), + native_request_long=True, long_allowed=False) + assert axis.lateralActive and command.latActive and not command.longActive + assert receive(BASE + 5_328_073, state=state()) and card.aol_card_intent.allowed_latch + axis, _, command = drive(5, offset_ns=1_321_875, timeout=True, state_stamp=BASE, + intent_message=intent(BASE, 3), gas=True, steering=True, native_request_long=True, long_allowed=False) + assert str(axis.faultReason) == 'availabilityPause' + assert not command.latActive and not command.longActive + assert receive(BASE + 31_321_875, state=state()), 'COROLLA_CARD_GAS_SOURCE_PAUSE' + assert card.aol_card_intent.allowed_latch + source = BASE + 27_305_261 + axis, _, command = drive(6, state_stamp=source, intent_message=intent(source, 4), + observed_state=state(), gas=True, steering=True, native_request_long=True, long_allowed=False) + assert axis.lateralActive and command.latActive and not command.longActive + assert receive(BASE + 40_000_000, state=state()) and card.aol_card_intent.allowed_latch + + +@pytest.mark.parametrize('fault', ('native_denied', 'native_invalid', 'rx_fault', 'relay_fault', + 'latch_withdrawn', 'missing_anchor', 'epoch_changed', 'dm_lockout')) +def test_corolla_gas_source_gap_preserves_every_native_and_intent_veto(fault): + with ExitStack() as cleanup: + cleanup.enter_context(OpenpilotPrefix()) + case = fixture.IoniqAvailabilityTests(methodName='runTest') + cleanup.callback(case.doCleanups) + cp = CarInterface.get_params(CAR.TOYOTA_COROLLA_TSS2, {0: {}, 1: {}, 2: {}}, [], False, False, False) + sd, _, drive = case._authority_callers(True, cp=cp, genuine_events=True) + refresh_saved_settings(sd) + cs = fixture.car_state() + cs.vEgo = cs.vEgoRaw = 30. + cs.cruiseState.enabled = True + for tick in (0, 1, 2): + axis, _, command = drive(tick, observed_state=cs, gas=True, steering=True, + offset_ns=5_328_073 if tick == 2 else 0, state_stamp=BASE if tick == 2 else None, + intent_message=intent(BASE, 3) if tick == 2 else None, + long_allowed=False, native_request_long=True) + assert axis.lateralActive and command.latActive and not command.longActive + if fault == 'missing_anchor': + sd.aol_ordinary_source_anchor = None + elif fault == 'epoch_changed': + sd.aol_intent_companion.integrity_epoch += 1 + elif fault == 'dm_lockout': + sd.aol_dm_lateral_inhibit = True + axis, _, command = drive(5, offset_ns=1_321_875, timeout=True, + state_stamp=BASE, intent_message=intent(BASE, 3, latched=fault != 'latch_withdrawn'), + gas=True, steering=True, long_allowed=False, native_request_long=True, + allowed=fault != 'native_denied', native_valid=fault != 'native_invalid', + panda_fault=fault == 'rx_fault', relay_fault=fault == 'relay_fault') + assert not axis.lateralActive and not axis.longitudinalActive + assert not command.latActive and not command.longActive + assert str(axis.faultReason) != 'availabilityPause' diff --git a/openpilot/starpilot/car/toyota/aol.py b/openpilot/starpilot/car/toyota/aol.py index 04a78b6651..78ece9a8c4 100644 --- a/openpilot/starpilot/car/toyota/aol.py +++ b/openpilot/starpilot/car/toyota/aol.py @@ -272,3 +272,21 @@ def held_lateral_pedal(owner, cs): isfinite(cs.vEgoRaw) and isfinite(cs.vEgo) and (not cs.brakePressed or cs.standstill or cs.vEgo >= settings.pause_brake_mps) and not cs.seatbeltUnlatched and not physical_cancel(cs, owner.CP)) + + +def healthy_gas_transport_native(sm, cp, cs, session, now_ns): + """Retain established LAT intent during gas override; never grant LONG.""" + from math import isfinite + from openpilot.starpilot.nostalgia import physical_cancel + from openpilot.starpilot.aol.transport_pause import _healthy_transport_native + if (not qualified(cp, marked_only=True) or not cp.openpilotLongitudinalControl or + not cs.gasPressed or cs.brakePressed or cs.regenBraking or not cs.cruiseState.available or + not cs.cruiseState.enabled or cs.accFaulted or cs.vehicleSensorsInvalid or cs.steeringDisengage or + cs.doorOpen or cs.seatbeltUnlatched or physical_cancel(cs, cp) or + cs.gearShifter not in (structs.CarState.GearShifter.drive, structs.CarState.GearShifter.sport, + structs.CarState.GearShifter.low, structs.CarState.GearShifter.brake) or + not isfinite(cs.vEgo) or not isfinite(cs.vEgoRaw)): + return None + native = _healthy_transport_native(sm, cp, cs, session, now_ns, require_longitudinal=False) + return native if (native is not None and native.pandaInventory and native.requestedLateral and + native.lateralAllowed and native.requestedLongitudinal and not native.longitudinalAllowed) else None diff --git a/prebuilt.json b/prebuilt.json index bb954bca27..e301264525 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "5b4bf7f926311d3fbc6c7165dcf68a0c3ecbfb447b2c7a742d5237c7fc5a1110", "schema_version": 1} +{"protected_inventory_sha256": "1dccfe210122b5680c8e35e8f7653c10054bf9c34447e83d72da54189fa52888", "schema_version": 1}