Speed Limit: Keep the Confirmation Steady Through Map Dropouts

This commit is contained in:
Danny
2026-11-20 19:07:49 -06:00
committed by firestar5683
parent 602b54181c
commit 14fe0d040d
3 changed files with 54 additions and 4 deletions
+24 -3
View File
@@ -62,6 +62,7 @@ from openpilot.starpilot.speed_limits import offset_document
SLC_SIGN_HOLD_NS = 150_000_000
SLC_PENDING_HOLD_NS = 3_000_000_000 # map limits drop out for a second or two while a new limit waits for the driver
EVENTS_DISPLAY_MAX_AGE_NS = 1_500_000_000 # selfdrived sends onroadEvents on every change and at least once a second
@@ -348,6 +349,7 @@ class RuntimeSnapshotAdapter:
self._reverse_driver_camera = ReverseDriverCamera()
self._vision_stop = StopConfirmation(display_only=True)
self._slc_sign_cache: tuple[int, tuple[int, int], str, SpeedLimitObservation] | None = None
self._slc_pending_live: tuple[int, tuple, SpeedLimitObservation] | None = None
self._mono_clock, self._boot_clock = mono_clock, boot_clock
self._device_after_mono_ns = mono_clock()
self._device_offset_ns: int | None = None
@@ -619,6 +621,20 @@ class RuntimeSnapshotAdapter:
observation = speed_limit_from_message(slc) if show_slc else SpeedLimitObservation()
if slc is None and started:
observation = SpeedLimitObservation(kind=ObservationKind.STALE)
# A source gap during the same pending decision keeps its live reading, so the prompt does not flash.
pending_key = ((drive_key, str(slc.sessionId), int(slc.decisionId))
if show_slc and drive_key is not None and slc.hasPending and getattr(slc, 'decisionId', 0) > 0 else None)
if pending_key is None:
self._slc_pending_live = None
elif observation.kind == ObservationKind.VALID:
self._slc_pending_live = (int(sm.logMonoTime['slcState']), pending_key, observation)
elif self._slc_pending_live is not None:
stamp, key, live = self._slc_pending_live
if key == pending_key and 0 <= now_ns - stamp <= SLC_PENDING_HOLD_NS:
observation = replace(observation, kind=ObservationKind.VALID, source=live.source,
speed_limit_mps=live.speed_limit_mps, status=live.status)
else:
self._slc_pending_live = None
if mode != ShellMode.ONROAD or drive_key is None or not show_slc:
self._slc_sign_cache = None
elif retaining_limit(observation):
@@ -683,12 +699,17 @@ class RuntimeSnapshotAdapter:
display_car_valid and getattr(display_car, 'gasPressed', False) and long_engaged and
str(getattr(display_selfdrive, 'state', '')) == 'overriding' and
cp is not None and cp.openpilotLongitudinalControl)
cruise_state = getattr(car, 'cruiseState', None)
# Display hint only: 100 Hz messages miss the 20 ms action gate on ~10% of frames, which blinked the cue.
display_system_long = bool(display_long_active and display_controls is not None and
str(display_controls.longControlState) != "off" and
display_selfdrive is not None and display_selfdrive.enabled and display_car_valid and
ui.CP is not None and ui.CP.openpilotLongitudinalControl)
cruise_state = getattr(display_car, 'cruiseState', None)
wheel_confirmation = bool(
system_long and control is not None and getattr(control, 'enabled', False) and
display_system_long and display_control is not None and getattr(display_control, 'enabled', False) and
cruise_state is not None and getattr(cruise_state, 'available', False) and
not (cp is not None and getattr(cp, 'brand', '') == 'toyota' and
(car.standstill or cruise_state.standstill or not cruise_state.enabled)))
(display_car.standstill or cruise_state.standstill or not cruise_state.enabled)))
developer_metrics = ()
if not (onroad_view_only and stopped_timer_profile == "compact"):
delay_key = ((after, str(cp.carFingerprint), str(cp.steerControlType), str(cp.lateralTuning.which()), float(cp.steerActuatorDelay))
@@ -247,3 +247,30 @@ def test_sources_row_keeps_each_source_while_no_limit_is_known():
ui.sm.logMonoTime['slcState'] = NOW + 50_000_000
shown = adapter.build(ShellMode.ONROAD, now_ns=NOW + 50_000_000).onroad.speed_limit
assert [(row.source, row.kind) for row in shown.source_readings] == [('dashboard', 'unknown'), ('map', 'unknown')]
def test_map_gap_during_pending_decision_keeps_the_prompt_steady():
ui = ui_fake()
live = ui.sm.messages['slcState']
live.source, live.speedLimit, live.status = 'map', 29.06, 'pending'
live.hasPending, live.pendingSpeedLimit, live.lastReceivedSpeedLimit, live.lastReceivedSource = True, 29.06, 29.06, 'map'
adapter = RuntimeSnapshotAdapter(ui)
before = adapter.build(ShellMode.ONROAD, now_ns=NOW).onroad
gap = NS(**{**vars(live), 'observationKind': 'unknown', 'source': 'none', 'speedLimit': 0.0, 'status': 'unknown'})
ui.sm.messages['slcState'] = gap
ui.sm.logMonoTime['slcState'] = NOW + 2_000_000_000
held = adapter.build(ShellMode.ONROAD, now_ns=NOW + 2_000_000_000).onroad
assert (held.speed_limit.kind, held.speed_limit.source, held.speed_limit.speed_limit_mps) == (ObservationKind.VALID, 'map', 29.06)
assert held.speed_limit.decision_id == 7 and held.speed_limit.pending_speed_limit_mps == 29.06
assert resolve_unified_speed(held) == resolve_unified_speed(before)
ui.sm.logMonoTime['slcState'] = NOW + 3_000_000_001
expired = adapter.build(ShellMode.ONROAD, now_ns=NOW + 3_000_000_001).onroad
assert expired.speed_limit.kind != ObservationKind.VALID
adapter = RuntimeSnapshotAdapter(ui)
ui.sm.messages['slcState'] = live
ui.sm.logMonoTime['slcState'] = NOW
adapter.build(ShellMode.ONROAD, now_ns=NOW)
ui.sm.messages['slcState'] = NS(**{**vars(gap), 'decisionId': 8})
ui.sm.logMonoTime['slcState'] = NOW + 100_000_000
other = adapter.build(ShellMode.ONROAD, now_ns=NOW + 100_000_000).onroad
assert other.speed_limit.kind != ObservationKind.VALID, "a new decision never borrows the old reading"
@@ -1112,7 +1112,9 @@ def test_wheel_confirmation_hint_preserves_toyota_resume(brand, standstill, crui
ui.sm.put('controlsState', NS(longControlState='pid'))
adapter = RuntimeSnapshotAdapter(ui)
assert adapter.build(ShellMode.ONROAD, now_ns=NOW).onroad.slc_wheel_confirmation_available == expected
ui.sm.logMonoTime['carControl'] = NOW - 200_000_000
ui.sm.logMonoTime['carControl'] = NOW - 50_000_000
assert adapter.build(ShellMode.ONROAD, now_ns=NOW).onroad.slc_wheel_confirmation_available == expected
ui.sm.logMonoTime['carControl'] = NOW - 201_000_000
assert not adapter.build(ShellMode.ONROAD, now_ns=NOW).onroad.slc_wheel_confirmation_available