-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweak_sync_logic.py
More file actions
61 lines (50 loc) · 1.75 KB
/
weak_sync_logic.py
File metadata and controls
61 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# weak_sync_logic.py
from __future__ import annotations
from dataclasses import dataclass
from statistics import median
from typing import Deque, Literal, Optional, Sequence
Action = Literal["collecting", "skip", "strong_set", "pending", "set"]
@dataclass
class WeakSyncDecision:
action: Action
med: float
sign: int
confirm_count: int
last_sign: int
def decide_weak_sync(
diffs: Sequence[float],
threshold: float,
strong_threshold: float,
confirm_needed: int,
last_sign: int,
confirm_count: int,
) -> WeakSyncDecision:
"""
Pure decision logic for weak sync.
- diffs: collected diff samples (adjusted_time - system_time)
- threshold: deadband threshold
- strong_threshold: force set threshold
- confirm_needed: consecutive same-direction requirement
- last_sign/confirm_count: carry-over state
Returns decision and the next state values.
"""
if len(diffs) == 0:
return WeakSyncDecision("collecting", 0.0, 0, confirm_count=0, last_sign=0)
med = float(median(diffs))
# deadband
if abs(med) < float(threshold):
return WeakSyncDecision("skip", med, 0, confirm_count=0, last_sign=0)
# strong: immediate
if abs(med) >= float(strong_threshold):
return WeakSyncDecision("strong_set", med, 0, confirm_count=0, last_sign=0)
# confirm-stability
sign = 1 if med > 0 else -1
if sign == int(last_sign):
confirm_count = int(confirm_count) + 1
else:
last_sign = sign
confirm_count = 1
if confirm_count < int(confirm_needed):
return WeakSyncDecision("pending", med, sign, confirm_count=confirm_count, last_sign=last_sign)
# confirmed: apply set
return WeakSyncDecision("set", med, sign, confirm_count=0, last_sign=0)