mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-04 00:03:41 +08:00
Retry speed-limit wheel actions within their original lifetime
This commit is contained in:
@@ -297,7 +297,7 @@ class WheelConsumer:
|
||||
self.sequence = 0
|
||||
self.last_heartbeat = 0
|
||||
|
||||
def accept(self, event, params, cp, sm, *, now_ns):
|
||||
def accept(self, event, params, cp, sm, *, now_ns, consume=True):
|
||||
try:
|
||||
if not event.valid or str(event.slcCruiseEvent.kind) != "wheelAction" or not eligible(cp):
|
||||
return None
|
||||
@@ -336,16 +336,17 @@ class WheelConsumer:
|
||||
mapping = capture(params)
|
||||
if mapping is None or mapping[key] != action:
|
||||
return None
|
||||
if session != self.session:
|
||||
if self.session is not None:
|
||||
if len(self.retired) >= 8:
|
||||
return None
|
||||
self.retired.add(self.session)
|
||||
self.session, self.sequence = session, 0
|
||||
if wire.sequence <= self.sequence:
|
||||
if session != self.session and self.session is not None and len(self.retired) >= 8:
|
||||
return None
|
||||
self.sequence = int(wire.sequence)
|
||||
self.last_heartbeat = observed
|
||||
return WheelCommand(session, self.sequence, observed, drive, source, key, action)
|
||||
if wire.sequence <= (self.sequence if session == self.session else 0):
|
||||
return None
|
||||
if consume:
|
||||
if session != self.session:
|
||||
if self.session is not None:
|
||||
self.retired.add(self.session)
|
||||
self.session = session
|
||||
self.sequence = int(wire.sequence)
|
||||
self.last_heartbeat = observed
|
||||
return WheelCommand(session, int(wire.sequence), observed, drive, source, key, action)
|
||||
except (AttributeError, KeyError, TypeError, ValueError, OverflowError):
|
||||
return None
|
||||
|
||||
@@ -570,32 +570,44 @@ class StarShellSession:
|
||||
wire = event.slcCruiseEvent.wheelAction
|
||||
if (int(wire.sourceCarStateMonoTime) > int(ui_state.sm.logMonoTime["carState"]) or
|
||||
int(wire.sourceCarControlMonoTime) > int(ui_state.sm.logMonoTime["carControl"])) and now_ns <= int(wire.validUntilMonoTime):
|
||||
if int(wire.actionCode) == 10:
|
||||
self._wheel_pending.appendleft(event)
|
||||
break
|
||||
self._wheel_pending.append(event)
|
||||
continue
|
||||
command = self._wheel_consumer.accept(event, ui_state.params, ui_state.CP, ui_state.sm, now_ns=now_ns)
|
||||
if int(wire.actionCode) == 10 and any(
|
||||
str(p.slcCruiseEvent.wheelAction.sessionId) != str(wire.sessionId) for p in self._wheel_pending):
|
||||
continue
|
||||
command = self._wheel_consumer.accept(event, ui_state.params, ui_state.CP, ui_state.sm, now_ns=now_ns,
|
||||
consume=int(wire.actionCode) != 10)
|
||||
if command is None or command.action == 0:
|
||||
continue
|
||||
authorized = (self._bookmark_authority() if command.action == 8 else
|
||||
self._favorite_authority(allow_personality_notice=command.action == 1))
|
||||
if not authorized:
|
||||
continue
|
||||
actions = self._native_favorite_actions()
|
||||
try:
|
||||
if command.action == 1:
|
||||
if self._wheel_personality is not None:
|
||||
self._wheel_personality()
|
||||
elif command.action in (5, 7, 8):
|
||||
key = {5: EXPERIMENTAL, 6: TRAFFIC, 7: SWITCHBACK, 8: BOOKMARK}[command.action]
|
||||
action = actions.get(key)
|
||||
action = self._native_favorite_actions().get(key)
|
||||
if action is not None and action.available and action.invoke is not None:
|
||||
action.invoke()
|
||||
elif command.action == 10 and self.slc_actions is not None:
|
||||
from openpilot.starpilot.ui.onroad_state import SlcActionKind
|
||||
state = self._live_slc_message(now_ns)
|
||||
if state is not None and not state.hasPending:
|
||||
self.slc_actions.dispatch(SlcUiRequest(SlcActionKind.ADOPT, str(state.sessionId), int(state.decisionId),
|
||||
int(state.presentationId), float(state.speedLimit)),
|
||||
wheel_observed_ns=command.observed_ns)
|
||||
if state is not None and state.hasPending:
|
||||
continue
|
||||
applied = state is not None and self.slc_actions.dispatch(
|
||||
SlcUiRequest(SlcActionKind.ADOPT, str(state.sessionId), int(state.decisionId),
|
||||
int(state.presentationId), float(state.speedLimit)), wheel_observed_ns=command.observed_ns)
|
||||
if applied:
|
||||
self._wheel_consumer.accept(event, ui_state.params, ui_state.CP, ui_state.sm, now_ns=now_ns)
|
||||
elif now_ns < int(wire.validUntilMonoTime):
|
||||
self._wheel_pending.appendleft(event)
|
||||
break
|
||||
elif command.action in (11, 12, 13):
|
||||
slot = self.favorites_owner.snapshot().slots[command.action - 11]
|
||||
if slot.available and slot.request is not None:
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
"""Wheel intent survives UI freshness misses without extending its lifetime."""
|
||||
from collections import deque
|
||||
from types import SimpleNamespace as NS
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from opendbc.car.structs import car
|
||||
from openpilot.cereal import messaging
|
||||
from openpilot.common.params import Params
|
||||
from openpilot.starpilot.controllers.wheel_actions import LIFETIME_NS, WheelConsumer, WheelPublisher
|
||||
from openpilot.starpilot.ui.slc_action_dispatch import SlcActionDispatcher
|
||||
from openpilot.starpilot.ui.tests.test_runtime_panel_actions import runtime_app
|
||||
from openpilot.starpilot.ui.tests.test_runtime_snapshot import BOOT_OFFSET_NS, NOW, ui_fake
|
||||
|
||||
|
||||
DRIVE = NOW - 2_000_000_000
|
||||
|
||||
|
||||
class Publisher:
|
||||
def __init__(self):
|
||||
self.events = []
|
||||
|
||||
def send(self, service, event):
|
||||
self.events.append((service, messaging.log_from_bytes(event.to_bytes())))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def context(tmp_path):
|
||||
ui = ui_fake()
|
||||
ui.params = Params(str(tmp_path))
|
||||
ui.params.put("MainCruiseButtonControl", 10, block=True)
|
||||
ui.CP = car.CarParams(brand="hyundai", carFingerprint="GENESIS_GV60_EV_1ST_GEN",
|
||||
openpilotLongitudinalControl=True)
|
||||
ui.sm["deviceState"].startedMonoTime = DRIVE
|
||||
ui.sm["carState"].canValid, ui.sm["carState"].canTimeout = True, False
|
||||
ui.sm.put("controlsState", NS(longControlState="pid"))
|
||||
ui.sm["slcState"].frameMonoTime = NOW
|
||||
ui.sm["slcState"].actionSequenceId = 0
|
||||
session = runtime_app.StarShellSession.__new__(runtime_app.StarShellSession)
|
||||
session.adapter = runtime_app.RuntimeSnapshotAdapter(ui)
|
||||
session._wheel_sock = object()
|
||||
session._wheel_pending = deque()
|
||||
session._wheel_consumer = WheelConsumer()
|
||||
output = Publisher()
|
||||
clock = [NOW]
|
||||
session.slc_actions = SlcActionDispatcher(output, session._live_slc_message, clock=lambda: clock[0])
|
||||
ingress = deque()
|
||||
favorite_actions = Mock(return_value={})
|
||||
session._native_favorite_actions = favorite_actions
|
||||
owner = WheelPublisher()
|
||||
|
||||
def publish(action=10, publisher=owner, at=NOW):
|
||||
pub = Publisher()
|
||||
publisher.publish((("MainCruiseButtonControl" if action else "", action),), ui.CP, pub,
|
||||
now_ns=at, drive_id=DRIVE, source_car_ns=at, source_control_ns=at)
|
||||
event = pub.events[0][1]
|
||||
ingress.append(event)
|
||||
return event
|
||||
|
||||
def refresh(now):
|
||||
clock[0] = now
|
||||
for name in ui.sm.messages:
|
||||
ui.sm.logMonoTime[name] = now + (BOOT_OFFSET_NS if name == "pandaStates" else 0)
|
||||
ui.sm.recv_time[name] = now / 1e9
|
||||
ui.sm["slcState"].frameMonoTime = now
|
||||
|
||||
def poll(now):
|
||||
with patch.object(runtime_app, "ui_state", ui), \
|
||||
patch.object(runtime_app.messaging, "recv_one_or_none", side_effect=lambda _: ingress.popleft() if ingress else None), \
|
||||
patch.object(session, "_favorite_authority", return_value=True), \
|
||||
patch.object(session, "_native_favorite_actions", side_effect=favorite_actions), \
|
||||
patch("openpilot.starpilot.ui.runtime_snapshot._clock_pair",
|
||||
return_value=(now, now + BOOT_OFFSET_NS, BOOT_OFFSET_NS)):
|
||||
session._poll_wheel(now)
|
||||
|
||||
return NS(ui=ui, session=session, output=output, clock=clock, ingress=ingress,
|
||||
publish=publish, refresh=refresh, poll=poll, favorite_actions=favorite_actions)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("service", ("carState", "carControl", "controlsState", "selfdriveState", "slcState"))
|
||||
@pytest.mark.parametrize("phase_ms", (1, 17, 33))
|
||||
def test_one_press_retries_when_next_ui_frame_has_fresh_sources(context, service, phase_ms):
|
||||
c = context
|
||||
c.publish()
|
||||
c.ui.sm.logMonoTime[service] = NOW - 201_000_000
|
||||
c.poll(NOW)
|
||||
assert not c.output.events
|
||||
assert c.session._wheel_pending
|
||||
c.refresh(NOW + phase_ms * 1_000_000)
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
assert str(c.output.events[0][1].slcAction.kind) == "adopt"
|
||||
assert c.output.events[0][1].slcAction.wheelAdoptMonoTime == NOW
|
||||
assert not c.session._wheel_pending
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
|
||||
|
||||
def test_second_dispatch_read_can_retry_without_consuming_press(context):
|
||||
c = context
|
||||
c.publish()
|
||||
c.clock[0] = NOW + 201_000_000
|
||||
c.poll(NOW)
|
||||
assert not c.output.events
|
||||
assert c.session._wheel_pending
|
||||
c.refresh(NOW + 220_000_000)
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
|
||||
|
||||
def test_heartbeat_does_not_retire_a_waiting_action(context):
|
||||
c = context
|
||||
c.publish()
|
||||
c.publish(0, at=NOW + 10_000_000)
|
||||
c.ui.sm.logMonoTime["controlsState"] = NOW - 201_000_000
|
||||
c.poll(NOW + 10_000_000)
|
||||
assert not c.output.events
|
||||
c.refresh(NOW + 33_000_000)
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
assert c.session._wheel_consumer.sequence == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("delay", (LIFETIME_NS, LIFETIME_NS + 1, LIFETIME_NS * 2))
|
||||
def test_wait_does_not_extend_original_wheel_expiry(context, delay):
|
||||
c = context
|
||||
c.publish()
|
||||
c.ui.sm.valid["controlsState"] = False
|
||||
c.poll(NOW)
|
||||
c.poll(NOW + LIFETIME_NS)
|
||||
assert not c.session._wheel_pending
|
||||
c.ui.sm.valid["controlsState"] = True
|
||||
c.refresh(NOW + delay)
|
||||
c.poll(c.clock[0])
|
||||
assert not c.output.events
|
||||
|
||||
|
||||
@pytest.mark.parametrize("denial", ("mapping", "drive", "cp", "can", "ignition", "pending", "slc_session", "source_future"))
|
||||
def test_waiting_action_revalidates_current_context(context, denial):
|
||||
c = context
|
||||
c.publish()
|
||||
c.ui.sm.valid["controlsState"] = False
|
||||
c.poll(NOW)
|
||||
c.ui.sm.valid["controlsState"] = True
|
||||
c.refresh(NOW + 33_000_000)
|
||||
if denial == "mapping":
|
||||
c.ui.params.put("MainCruiseButtonControl", 0, block=True)
|
||||
elif denial == "drive":
|
||||
c.ui.sm["deviceState"].startedMonoTime += 1
|
||||
elif denial == "cp":
|
||||
c.ui.CP.pcmCruise = True
|
||||
elif denial == "can":
|
||||
c.ui.sm["carState"].canValid = False
|
||||
elif denial == "ignition":
|
||||
c.ui.sm["pandaStates"][0].ignitionLine = False
|
||||
elif denial == "pending":
|
||||
c.ui.sm["slcState"].hasPending = True
|
||||
elif denial == "slc_session":
|
||||
c.ui.sm["slcState"].sessionId = ""
|
||||
else:
|
||||
c.ui.sm.logMonoTime["carState"] = c.clock[0] + 1
|
||||
c.poll(c.clock[0])
|
||||
assert not c.output.events
|
||||
if denial == "pending":
|
||||
assert not c.session._wheel_pending
|
||||
c.ui.sm["slcState"].hasPending = False
|
||||
c.poll(c.clock[0])
|
||||
assert not c.output.events
|
||||
|
||||
|
||||
def test_new_producer_session_retires_old_waiting_action(context):
|
||||
c = context
|
||||
c.publish()
|
||||
c.ui.sm.valid["controlsState"] = False
|
||||
c.poll(NOW)
|
||||
c.publish(0, publisher=WheelPublisher(), at=NOW + 10_000_000)
|
||||
c.refresh(NOW + 33_000_000)
|
||||
c.poll(c.clock[0])
|
||||
assert not c.output.events
|
||||
assert not c.session._wheel_pending
|
||||
|
||||
|
||||
def test_duplicate_action_is_published_once(context):
|
||||
c = context
|
||||
event = c.publish()
|
||||
c.ingress.append(event)
|
||||
c.poll(NOW)
|
||||
assert len(c.output.events) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("brand,fingerprint,key", (("hyundai", "GENESIS_GV60_EV_1ST_GEN", "MainCruiseButtonControl"),
|
||||
("hyundai", "KIA_STINGER", "MainCruiseButtonControl"),
|
||||
("gm", "CHEVROLET_BOLT_EUV", "MainCruiseButtonControl"),
|
||||
("honda", "HONDA_CIVIC", "MainCruiseButtonControl")))
|
||||
def test_unconsumed_preview_keeps_sequence_until_delivery(context, brand, fingerprint, key):
|
||||
c = context
|
||||
c.ui.CP.brand, c.ui.CP.carFingerprint = brand, fingerprint
|
||||
event = c.publish()
|
||||
consumer = c.session._wheel_consumer
|
||||
for _ in range(3):
|
||||
assert consumer.accept(event, c.ui.params, c.ui.CP, c.ui.sm, now_ns=NOW, consume=False).key == key
|
||||
assert consumer.sequence == 0
|
||||
assert consumer.accept(event, c.ui.params, c.ui.CP, c.ui.sm, now_ns=NOW).action == 10
|
||||
assert consumer.accept(event, c.ui.params, c.ui.CP, c.ui.sm, now_ns=NOW) is None
|
||||
|
||||
|
||||
def test_future_source_wait_does_not_let_heartbeat_overtake(context):
|
||||
c = context
|
||||
c.publish(at=NOW + 10_000_000)
|
||||
c.publish(0, at=NOW + 20_000_000)
|
||||
c.poll(NOW + 20_000_000)
|
||||
assert not c.output.events
|
||||
c.refresh(NOW + 33_000_000)
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
assert c.session._wheel_consumer.sequence == 2
|
||||
|
||||
|
||||
def test_existing_slc_duplicate_guard_still_rejects_a_second_press(context):
|
||||
c = context
|
||||
c.publish()
|
||||
c.poll(NOW)
|
||||
assert len(c.output.events) == 1
|
||||
c.refresh(NOW + 100_000_000)
|
||||
c.publish(at=c.clock[0])
|
||||
c.poll(c.clock[0])
|
||||
c.refresh(NOW + 350_000_001)
|
||||
c.poll(c.clock[0])
|
||||
assert len(c.output.events) == 1
|
||||
assert not c.session._wheel_pending
|
||||
|
||||
|
||||
@pytest.mark.parametrize("delay,delivered", ((249_000_000, True), (250_000_000, True), (250_000_001, False)))
|
||||
def test_fresh_retry_respects_original_inclusive_deadline(context, delay, delivered):
|
||||
c = context
|
||||
c.publish()
|
||||
c.ui.sm.valid["controlsState"] = False
|
||||
c.poll(NOW)
|
||||
c.ui.sm.valid["controlsState"] = True
|
||||
c.refresh(NOW + delay)
|
||||
c.poll(c.clock[0])
|
||||
assert bool(c.output.events) == delivered
|
||||
assert not c.session._wheel_pending
|
||||
|
||||
|
||||
def test_adopt_does_not_resolve_unrelated_favorites_before_freshness_read(context):
|
||||
context.publish()
|
||||
context.poll(NOW)
|
||||
assert len(context.output.events) == 1
|
||||
context.favorite_actions.assert_not_called()
|
||||
+1
-1
@@ -1 +1 @@
|
||||
{"protected_inventory_sha256": "28a6328e255b908c189c30dbdce6e3b61366d977faef49e81f03152a456a4889", "schema_version": 1}
|
||||
{"protected_inventory_sha256": "25f85dc7128b4b2d83f38f28431100a9e15396c2ed6902ded1f81a0be872bc94", "schema_version": 1}
|
||||
|
||||
Reference in New Issue
Block a user