Remember Subaru MAIN through the temporary steering guard

This commit is contained in:
firestar5683
2026-11-14 09:12:03 -06:00
parent 5bc34966f6
commit 5a62d76315
5 changed files with 125 additions and 9 deletions
+14 -4
View File
@@ -17,6 +17,7 @@ class SubaruStockCardIntent(AolCardIntent):
self._main_event_ns = 0
self._pending_main_ns = 0
self._pending_main_after_ns = 0
self._pending_main_restriction_ns = 0
self.main_arm_ns = 0
def observe_startup_readiness(self, sm, CS, *, now_ns, **kwargs):
@@ -32,25 +33,34 @@ class SubaruStockCardIntent(AolCardIntent):
fresh_main = self._previous_main is False and main
physical_safe = (self.settings.enabled and CS.canValid and not CS.canTimeout and
not self._calibration_inhibit and not self._calibration_rearm_required and
not CS.steerFaultPermanent and not CS.steerFaultTemporary and not CS.accFaulted and
not CS.steerFaultPermanent and not CS.accFaulted and
not any(e.type == structs.CarState.ButtonEvent.Type.cancel and e.pressed for e in CS.buttonEvents) and
CS.gearShifter == structs.CarState.GearShifter.drive)
if self.main_arm and fresh_main and physical_safe:
self._pending_main_ns = now_ns
self._pending_main_after_ns = self._main_observed_ns
self._pending_main_restriction_ns = 0
self._previous_main = main if CS.canValid and not CS.canTimeout else None
self._main_observed_ns = now_ns
if self._pending_main_ns:
if not main or not physical_safe or not 0 <= now_ns - self._pending_main_ns <= 200_000_000:
if CS.steerFaultTemporary and physical_safe:
self._pending_main_restriction_ns = now_ns
pending_since = max(self._pending_main_ns, self._pending_main_restriction_ns)
new_fault = kwargs.get('fault_active') is True and now_ns > self._pending_main_ns
native_rejection = self._pending_main_ns < kwargs.get('native_rejection_ns', 0) <= now_ns
if not main or not physical_safe or new_fault or native_rejection or not 0 <= now_ns - pending_since <= 200_000_000:
self._pending_main_ns = 0
elif self._main_event_ns > self._pending_main_after_ns and kwargs.get('fault_active') is not None:
# Wait for the event observation after MAIN; an earlier MAIN-off fault
# cannot decide this edge. A new fault still consumes it without arming.
if not kwargs['fault_active'] and not self._fault_inhibit:
if kwargs['fault_active'] or self._fault_inhibit:
self._pending_main_ns = 0
elif not CS.steerFaultTemporary and self._main_event_ns > self._pending_main_restriction_ns:
self.allowed_latch = True
self.pause_lateral = False
self._last_latch_edge_ns = self._pending_main_ns
self.main_arm_ns = self._pending_main_ns
self._pending_main_ns = 0
self._pending_main_ns = 0
if not main:
self.main_arm_ns = 0
self._record_latch_clear('subaru_main_off')
@@ -1,5 +1,8 @@
import unittest
from dataclasses import replace
from types import SimpleNamespace
import pytest
from opendbc.car import gen_empty_fingerprint, structs
from opendbc.car.subaru.interface import CarInterface
@@ -92,3 +95,81 @@ class TestSubaruStockAol(unittest.TestCase):
state.out.gearShifter = structs.CarState.GearShifter.reverse
_, decoded = step(controller, command, state)
self.assertEqual(decoded['LKAS_Request'], 0)
def pending_restricted_main():
settings = AolSettings(True, 0., 0, 0, (0, 0, 0), (0, 0, 0))
intent = SubaruStockCardIntent(settings, main_arm=True)
state = structs.CarState(canValid=True, vEgo=2., gearShifter='drive')
now = 1_000_000_000
intent.update(state, now_ns=now, fault_active=False)
state.cruiseState.available = state.steerFaultTemporary = True
now += 10_000_000
observe_main_event(intent, state, now)
intent.update(state, now_ns=now, fault_active=False)
assert not intent.allowed_latch and intent._pending_main_ns == now
return intent, state, now
def observe_main_event(intent, state, now):
sm = SimpleNamespace(logMonoTime={'onroadEvents': now}, valid={'onroadEvents': True})
intent.observe_startup_readiness(sm, state, now_ns=now)
def test_temporary_main_wait_preserves_original_gesture_and_requires_new_clear_event():
intent, state, arm = pending_restricted_main()
for tick in range(1, 351):
now = arm + tick * 10_000_000
observe_main_event(intent, state, now)
intent.update(state, now_ns=now, fault_active=False)
assert not intent.allowed_latch and intent._pending_main_ns == arm and intent.main_arm_ns == 0
state.steerFaultTemporary = False
intent.update(state, now_ns=now + 10_000_000, fault_active=False)
assert not intent.allowed_latch and intent._pending_main_ns == arm
observe_main_event(intent, state, now + 20_000_000)
intent.update(state, now_ns=now + 20_000_000, fault_active=False)
assert intent.allowed_latch and intent.main_arm_ns == intent._last_latch_edge_ns == arm
assert intent._pending_main_ns == 0
@pytest.mark.parametrize('veto', ('main_off', 'invalid_can', 'timeout', 'permanent_eps', 'acc_fault',
'reverse', 'cancel', 'settings_off', 'calibration', 'calibration_rearm', 'event_fault',
'transport_fault_without_event', 'native_rejection'))
def test_pending_temporary_main_is_consumed_by_independent_veto(veto):
intent, state, arm = pending_restricted_main()
if veto == 'main_off':
state.cruiseState.available = False
elif veto in ('invalid_can', 'timeout', 'permanent_eps', 'acc_fault'):
setattr(state, {'invalid_can': 'canValid', 'timeout': 'canTimeout',
'permanent_eps': 'steerFaultPermanent', 'acc_fault': 'accFaulted'}[veto], veto != 'invalid_can')
elif veto == 'reverse':
state.gearShifter = 'reverse'
elif veto == 'cancel':
state.buttonEvents = [structs.CarState.ButtonEvent(type='cancel', pressed=True)]
elif veto == 'settings_off':
intent.settings = replace(intent.settings, enabled=False)
elif veto in ('calibration', 'calibration_rearm'):
setattr(intent, '_calibration_inhibit' if veto == 'calibration' else '_calibration_rearm_required', True)
if veto != 'transport_fault_without_event':
observe_main_event(intent, state, arm + 10_000_000)
else:
intent._main_event_ns = 0
intent.update(state, now_ns=arm + 10_000_000,
fault_active=veto in ('event_fault', 'transport_fault_without_event'),
native_rejection_ns=arm + 10_000_000 if veto == 'native_rejection' else 0)
assert not intent.allowed_latch and intent._pending_main_ns == 0
@pytest.mark.parametrize('observation', ('expired', 'invalid_event', 'missing_fault_result'))
def test_pending_temporary_main_does_not_arm_from_unqualified_clear(observation):
intent, state, arm = pending_restricted_main()
state.steerFaultTemporary = False
now = arm + (200_000_001 if observation == 'expired' else 10_000_000)
if observation != 'invalid_event':
observe_main_event(intent, state, now)
else:
sm = SimpleNamespace(logMonoTime={'onroadEvents': now}, valid={'onroadEvents': False})
intent.observe_startup_readiness(sm, state, now_ns=now)
intent.update(state, now_ns=now, fault_active=None if observation == 'missing_fault_result' else False)
assert not intent.allowed_latch
@@ -27,7 +27,7 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
deadline_gap=False, fixed_angle_handoff=False, host_first_stop=False,
source_outage=False, vm_angle_handoff=0, crawl=False, eps_brake_join=False,
high_angle_guard=False, main_on_brake=False, guard_brake_release=False,
stopped_feedback_gap=False, rearm_on_brake=0):
stopped_feedback_gap=False, rearm_on_brake=0, main_during_eps=False):
angle_handoff = fixed_angle_handoff or vm_angle_handoff
fw = firmware_records(candidate, FW_VERSIONS, 'subaru') if firmware is None else firmware
rows = []
@@ -69,7 +69,8 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
base = time.monotonic_ns() + 100_000_000
try:
published = {}
ticks = (list(range(125)) + list(range(125, 135)) * 30 + list(range(135, 460))
ticks = (list(range(30)) + list(range(30, 50)) * 16 + list(range(50, 460))
if main_during_eps else list(range(125)) + list(range(125, 135)) * 30 + list(range(135, 460))
if hard_stop_native_first else range(460))
gap_tick = 75 if main_only else 210
last_card_published = {}
@@ -108,6 +109,12 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
if rearm_on_brake in (1, 2):
values['Dashlights'] = {'LEFT_BLINKER': int(rearm_on_brake == 1 and 340 <= tick < 355),
'RIGHT_BLINKER': int(rearm_on_brake == 2 and 340 <= tick < 355)}
if main_during_eps:
values['Brake_Pedal'] = {'Speed': 8}
values['Brake_Pressure_L_R'] = {'Steering_Angle': 101 if 20 <= tick < 50 else 0}
if 20 <= tick < 50:
values['Steering_Torque'].update(Steering_Angle=100, Steer_Torque_Sensor=59)
values['Wheel_Speeds'] = dict.fromkeys(('FL', 'FR', 'RL', 'RR'), 8)
if high_angle_guard:
values['Brake_Pedal'] = {'Speed': 8}
values['Brake_Pressure_L_R'] = {'Steering_Angle': 101 if 110 <= tick < 120 else 83 if 120 <= tick < 125 else 0}
@@ -264,7 +271,7 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
if crawl and 95 <= tick < 105:
assert aol and main_only and cs.brakePressed and not command.enabled
assert request and accepted, ('CRAWL_AOL_BRAKE_WITHDREW', tick, decoded_steering)
if not stopped and (65 <= tick < 90 and not (source_outage and 75 <= tick < 84) or
if not stopped and (65 <= tick < 90 and not (main_during_eps and tick < 75) and not (source_outage and 75 <= tick < 84) or
aol and (first_resume or 205 <= tick < 220 or 280 <= tick < 300)):
if angle_handoff and 72 <= tick < 78:
assert not request and accepted, (tick, decoded_steering)
@@ -283,6 +290,10 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
'tick': tick,
'elapsed': elapsed,
'clearReason': card.aol_card_intent.latch_clear_reason if card.aol_card_intent is not None else None,
'mainPending': getattr(card.aol_card_intent, '_pending_main_ns', 0),
'mainRestriction': getattr(card.aol_card_intent, '_pending_main_restriction_ns', 0),
'mainEvent': getattr(card.aol_card_intent, '_main_event_ns', 0),
'mainFaultInhibit': getattr(card.aol_card_intent, '_fault_inhibit', False),
'hardStopOrder': hard_order if hard_stop_receipt else None,
'intent': bool(intent and intent.allowedLatch),
'armed': bool(intent and intent.lateralArmed),
@@ -303,6 +314,14 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
'events': [str(e.name) for e in driven['onroadEvents'].onroadEvents],
}
rows.append(row)
if main_during_eps and 35 <= tick < 50:
assert cs.steerFaultTemporary and not row['lat'] and not row['long'] and row['permission'] == 0, row
assert all(not frame['request'] and frame['output'] == 0 and frame['nativeAccepted'] for frame in row['steering']), row
if main_during_eps and 50 <= tick < 70:
assert not row['lat'] and row['permission'] == 0, row
assert all(not frame['request'] and frame['output'] == 0 and frame['nativeAccepted'] for frame in row['steering']), row
if main_during_eps and 75 <= tick < 90:
assert row['cardLatch'] and row['armed'] and row['lat'] and row['permission'] == 1, ('SUBARU_MAIN_DURING_EPS_NOT_RECOVERED', row)
if high_angle_guard and 115 <= tick < 120:
assert cs.steerFaultTemporary and ci.CS.high_angle_cut, ('HIGH_ANGLE_GUARD_NOT_WITHDRAWN', row)
assert not row['lat'] and row['cardLatch'] and row['armed'] and row['permission'] == 0, ('HIGH_ANGLE_GUARD_NOT_WITHDRAWN', row)
@@ -326,7 +345,7 @@ def test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(candidate,
assert not command.longActive, row
if 10 <= tick < 30:
assert not command.latActive and not bool(intent and intent.allowedLatch) and row['permission'] == 0, row
if 65 <= tick < 90 and not (source_outage and 75 <= tick < 84):
if 65 <= tick < 90 and not (main_during_eps and tick < 75) and not (source_outage and 75 <= tick < 84):
assert command.latActive and bool(intent and intent.allowedLatch) == aol and row['permission'] == int(aol), row
if not stopped and (first_resume or 205 <= tick < 220 or 280 <= tick < 300):
assert command.latActive == aol and bool(intent and intent.allowedLatch) == aol and row['permission'] == int(aol) and not row['controls'], row
@@ -532,3 +551,8 @@ def test_actual_stopped_feedback_gap_retains_only_armed_intent(subaru_native_pai
def test_actual_repeat_main_with_brake_and_signal_pause(subaru_native_pair, tmp_path, signal):
test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(
CAR.SUBARU_IMPREZA_2020, subaru_native_pair, tmp_path, main_only=True, rearm_on_brake=signal)
def test_actual_main_during_temporary_eps_waits_for_clear(subaru_native_pair, tmp_path):
test_actual_full_engage_brake_reverse_temporary_recovery_mainoff(
CAR.SUBARU_IMPREZA_2020, subaru_native_pair, tmp_path, main_only=True, main_during_eps=True)
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "63340af6375344c1c1fdd93193417ee1afad44fd63e50927f671f8c47e3efb2b", "schema_version": 1}
{"protected_inventory_sha256": "641d96b3d3c570d2188521eff62ce0b43ab7d3c5ee88096f61defd42fa70e3e5", "schema_version": 1}
+1
View File
@@ -272,6 +272,7 @@ DEBUG_SAFETY_CLASS_TARGETS = {
RELEASE_TARGETS = (
"openpilot/starpilot/car/subaru/tests/test_stock_aol_callers.py::test_actual_main_during_temporary_eps_waits_for_clear",
"openpilot/starpilot/car/subaru/tests/test_stock_aol_callers.py::test_actual_stopped_feedback_gap_retains_only_armed_intent",
"openpilot/starpilot/car/subaru/tests/test_stock_aol_callers.py::test_actual_repeat_main_with_brake_and_signal_pause",
"openpilot/starpilot/aol/tests/test_subaru_temporary_eps.py",