Avoid duplicate same-frame Switchback wheel map reads

This commit is contained in:
firestar5683
2026-11-17 21:02:58 -06:00
parent 2b63e11578
commit cb31a6d02c
3 changed files with 71 additions and 2 deletions
+4 -1
View File
@@ -828,9 +828,12 @@ class Car:
gm_claim.claim(self.traffic_receipt[1], self.traffic_receipt[2], self.traffic_receipt[3])
self.switchback_receipt = None
if self.switchback_button_tracker is not None and self.switchback_settings_owner is not None:
media_map = getattr(self, 'ioniq_media_map', None)
map_current = media_map is not None and media_map.checked_ns == now_ns
self.switchback_receipt = conditional_traffic_candidate(
CS, self.switchback_button_tracker, self.params, self.switchback_settings_owner,
self.CP, self.sm, now_ns=now_ns, media=media_observation, action=7)
self.CP, self.sm, now_ns=now_ns, media=media_observation, action=7,
media_buttons=media_map.buttons if map_current else None, media_map_captured=map_current)
if consumed is not None:
self.slc_receipts.append(('confirmationAccept' if consumed.button == 'accel' else 'confirmationReject',
consumed, 0.0, 0.0, now_ns))
@@ -0,0 +1,66 @@
import ast
import importlib.util
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from openpilot.starpilot.conditional_mode.manual import WheelMapCache, read_button_map
NOW = 10_000_000_000
def dispatch(cache, params):
spec = importlib.util.find_spec('openpilot.selfdrive.car.card')
tree = ast.parse(Path(spec.origin).read_text())
cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'Car')
method = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == 'state_update')
block = next(n for n in method.body if isinstance(n, ast.If) and
ast.unparse(n.test) == 'self.switchback_button_tracker is not None and self.switchback_settings_owner is not None')
consumer = Mock()
host = SimpleNamespace(ioniq_media_map=cache, switchback_button_tracker=object(), switchback_settings_owner=object(),
params=params, CP=object(), sm=object())
env = {'self': host, 'CS': object(), 'now_ns': NOW, 'media_observation': object(), 'conditional_traffic_candidate': consumer}
exec(compile(ast.Module(body=[block], type_ignores=[]), spec.origin, 'exec'), env)
consumer.assert_called_once()
return consumer.call_args.kwargs
@pytest.mark.parametrize('action', (0, 7))
def test_switchback_uses_the_exact_same_frame_wheel_map_once(tmp_path, action):
from openpilot.common.params import Params
params = Params(str(tmp_path))
params.put('ModeButtonControl', action, block=True)
cache = WheelMapCache()
cache.buttons = read_button_map(params, include_ioniq_media=True)
cache.checked_ns = NOW
result = dispatch(cache, params)
assert result.get('media_map_captured', False) is True
assert result['media_buttons'] is cache.buttons
assert result['media_buttons'].mode == action
assert result['action'] == 7 and result['now_ns'] == NOW
@pytest.mark.parametrize('stamp', (0, NOW - 1, NOW + 1))
def test_stale_future_and_unchecked_maps_still_require_a_new_read(stamp):
cache = WheelMapCache()
cache.checked_ns = stamp
result = dispatch(cache, object())
assert result.get('media_map_captured', False) is False
assert result.get('media_buttons') is None
def test_missing_cache_retains_the_original_reader():
result = dispatch(None, object())
assert result.get('media_map_captured', False) is False
assert result.get('media_buttons') is None
def test_same_frame_failed_read_is_not_retried_into_a_different_map():
cache = WheelMapCache()
cache.checked_ns = NOW
assert cache.buttons is None
result = dispatch(cache, object())
assert result.get('media_map_captured', False) is True
assert result['media_buttons'] is None
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "7227359fd9155e42f7af851c5e9e0b6422f4a2d6d48df47debdec79cb5900cf0", "schema_version": 1}
{"protected_inventory_sha256": "91daddf550f6f4f4375a736632563e5fba766c5de6098c9974e0789c4158a08b", "schema_version": 1}