Recover Polestar stock relay after a receive backlog

This commit is contained in:
firestar5683
2026-11-20 13:50:54 -06:00
parent 4e0e99039a
commit b00d753a24
3 changed files with 25 additions and 5 deletions
+9 -3
View File
@@ -1,4 +1,4 @@
from collections import deque
from collections import Counter, deque
from opendbc.car import structs, get_safety_config
from opendbc.car.interfaces import CarInterfaceBase
@@ -44,8 +44,14 @@ class CarInterface(CarInterfaceBase):
actuators, messages = super().apply(c, now_nanos)
if self.CP.carFingerprint == CAR.POLESTAR_2:
if not self.CC.lat_active:
# Preserve source counters and cadence instead of resampling the latest frame.
messages = list(self.stock_relay)
# Panda only retains four source packets per address. After a backlog,
# resume each stream with its newest packet instead of bursting stale copies.
counts = Counter(address for address, _, _ in self.stock_relay)
if max(counts.values(), default=0) > 4:
latest = {frame[0]: index for index, frame in enumerate(self.stock_relay)}
messages = [frame for index, frame in enumerate(self.stock_relay) if index == latest[frame[0]]]
else:
messages = list(self.stock_relay)
self.stock_relay.clear()
return actuators, messages
@@ -50,7 +50,7 @@ def test_pending_queue_is_bounded():
ci.update([(1_000_000_000, received)])
assert len(ci.stock_relay) == 128
_, output = ci.apply(structs.CarControl.new_message().as_reader(), 1_010_000_000)
assert output == forwarded(received[-128:])
assert output == forwarded(received[-1:])
@pytest.mark.parametrize('changes', [
@@ -85,3 +85,17 @@ def test_stock_received_while_active_cannot_leak_into_later_withdrawal():
assert not ci.stock_relay
_, output = ci.apply(structs.CarControl.new_message().as_reader(), 1_020_000_000)
assert output == []
def test_recovery_sends_latest_frames_in_source_order():
ci = interface()
angle = packets()
status = (0x16, bytes(8), 2)
other = (0x58, bytes(8), 0)
received = [status, angle[0], other, angle[1], status, angle[2], other, angle[0], angle[1], status, other, other]
for i, frame in enumerate(received):
ci.update([(1_000_000_000 + i * 10_000_000, [frame])])
_, output = ci.apply(structs.CarControl.new_message().as_reader(), 1_120_000_000)
assert output == forwarded([angle[1], status, other])
_, output = ci.apply(structs.CarControl.new_message().as_reader(), 1_130_000_000)
assert output == []
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "58fff7c4909208cd803848183a254fdfa9f23e10eaa8aa07c16077bf410e37c2", "schema_version": 1}
{"protected_inventory_sha256": "c557a9a9d18228404bc39397b036af488ca990b69951e8afa9856b9ceb90b00c", "schema_version": 1}