fix(beat): follow a controller that restarted its beat counter
A run of three consecutive indices that all look old means the controller rebooted and restarted its counter, so resync to it; one or two stray old frames are still dropped as late copies. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
495da4e366
commit
08d7277204
2 files changed
+38
-1
No files matched your search
+17
-1
@@ -5,6 +5,7 @@ MSG_BEAT = 0x25
|
||||
FRAME_LEN = 8 # [4C][25][flags][idx u16][period u16][fire_in]
|
||||
MIN_GUARD_MS = 30
|
||||
FREERUN_MISS_MAX = 4
|
||||
RESTART_BEATS = 3 # consecutive beats that all look old mean the controller restarted its counter
|
||||
|
||||
|
||||
def _diff(a, b):
|
||||
@@ -27,6 +28,8 @@ class BeatSync:
|
||||
self.downbeat = False
|
||||
self.bar = 0
|
||||
self.missed = 0
|
||||
self.stale_idx = None # newest index seen while every frame looked old
|
||||
self.stale_n = 0
|
||||
self.freerun_fire = False # True when the last fire was invented, not triggered by a frame
|
||||
# fires: beats acted on; dups: second copies dropped; late: too-soon or old frames;
|
||||
# freerun: beats the driver made up because no frame arrived
|
||||
@@ -42,11 +45,24 @@ class BeatSync:
|
||||
self.stats["dups"] += 1
|
||||
return False # second copy of a beat we already took
|
||||
guard = max(MIN_GUARD_MS, self.period // 2)
|
||||
if _diff(idx, self.last_idx) >= 0x8000 or (
|
||||
if _diff(idx, self.last_idx) >= 0x8000:
|
||||
# Behind us. A late copy is one or two frames; a controller that rebooted and
|
||||
# restarted its counter sends a run of consecutive indices, so follow it.
|
||||
if idx == self.stale_idx:
|
||||
self.stats["late"] += 1
|
||||
return False
|
||||
self.stale_n = self.stale_n + 1 if self.stale_idx is not None and _diff(idx, self.stale_idx) == 1 else 1
|
||||
self.stale_idx = idx
|
||||
if self.stale_n < RESTART_BEATS:
|
||||
self.stats["late"] += 1
|
||||
return False
|
||||
elif (
|
||||
self.fire_at is None and now - self.last_fire < guard and self.period
|
||||
):
|
||||
self.stats["late"] += 1
|
||||
return False # late copy of an older beat, or too soon after the last one
|
||||
self.stale_idx = None
|
||||
self.stale_n = 0
|
||||
self.last_idx = idx
|
||||
self.period = period
|
||||
self.downbeat = bool(flags & 1)
|
||||
|
||||
@@ -84,6 +84,27 @@ def test_stats_count_dups_and_freerun():
|
||||
assert b.stats["freerun"] == 1 and b.stats["fires"] == 2
|
||||
|
||||
|
||||
def test_controller_restart_is_followed():
|
||||
b = BeatSync()
|
||||
assert b.on_frame(frame(29000), 0)
|
||||
assert not b.on_frame(frame(1), 500) # looks old
|
||||
assert not b.on_frame(frame(1), 502) # second copy of the same beat
|
||||
assert not b.on_frame(frame(2), 1000)
|
||||
assert b.on_frame(frame(3), 1500) # third consecutive: the counter really restarted
|
||||
assert not b.on_frame(frame(3), 1502)
|
||||
assert b.on_frame(frame(4), 2000)
|
||||
|
||||
|
||||
def test_stray_old_frames_do_not_reset():
|
||||
b = BeatSync()
|
||||
assert b.on_frame(frame(100), 0)
|
||||
assert not b.on_frame(frame(90), 100)
|
||||
assert b.on_frame(frame(101), 500)
|
||||
assert not b.on_frame(frame(99), 600)
|
||||
assert not b.on_frame(frame(98), 700) # not consecutive upward
|
||||
assert b.on_frame(frame(102), 1000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for name, fn in list(globals().items()):
|
||||
if name.startswith("test_"):
|
||||
|
||||
Reference in new issue
Block a user