Просмотр исходного кода

fix(host): hold RTL startup_complete until the first MOTOR_STATUS

Production UART through Icarus needs ~13s for the first MOTOR round-trip,
so a 10s tap leaked startup_complete and immediately starved GET_TEMPERATURE.
root 3 месяцев назад
Родитель
Сommit
0897774f6f
1 измененных файлов с 30 добавлено и 9 удалено
  1. 30 9
      tools/rtl_uart_cosim/run_hthd_pty_rtl.py

+ 30 - 9
tools/rtl_uart_cosim/run_hthd_pty_rtl.py

@@ -157,7 +157,7 @@ serial:
   io_timeout_ms: 28000
   inter_byte_timeout_ms: 2000
   response_timeout_ms: 28000
-  get_temp_timeout_ms: 20000
+  get_temp_timeout_ms: 28000
   get_motor_timeout_ms: 28000
   probe_timeout_ms: 28000
   set_all_timeout_ms: 2000
@@ -243,12 +243,21 @@ def _clear_startup_complete(frame: bytes) -> bytes:
 
 
 class UartTap:
-    """Hold MOTOR_STATUS startup_complete=0 for wall-clock delayed-startup tests."""
+    """Hold MOTOR_STATUS startup_complete=0 for wall-clock delayed-startup tests.
 
-    def __init__(self, hold_s: float = 10.0) -> None:
+    Production UART (217 clocks/bit through Icarus) needs ~13s for the first
+    MOTOR_STATUS round-trip. A 10s hold expires first, leaks startup_complete,
+    and hthd immediately sends GET_TEMPERATURE which then times out.
+    Keep holding until hold_s elapses AND at least one MOTOR_STATUS has been
+    rewritten, capped at max_hold_s.
+    """
+
+    def __init__(self, hold_s: float = 10.0, max_hold_s: float | None = None) -> None:
         self.hold_s = hold_s
+        self.max_hold_s = hold_s if max_hold_s is None else max_hold_s
         self._start = 0.0
         self._started = threading.Event()
+        self._cleared_motors = 0
         self.up_buf = bytearray()
         self.dn_buf = bytearray()
         self.get_temp_during_hold = 0
@@ -264,7 +273,12 @@ class UartTap:
     def holding(self) -> bool:
         if not self._started.is_set():
             return False
-        return time.monotonic() < self._start + self.hold_s
+        elapsed = time.monotonic() - self._start
+        if elapsed >= self.max_hold_s:
+            return False
+        if elapsed < self.hold_s:
+            return True
+        return self._cleared_motors == 0
 
     def on_pty_to_rtl(self, data: bytes) -> None:
         self.up_buf.extend(data)
@@ -295,6 +309,7 @@ class UartTap:
         for frame in frames:
             if hold and len(frame) >= 3 and frame[2] == 0x83:
                 frame = _clear_startup_complete(frame)
+                self._cleared_motors += 1
             out.extend(frame)
         return bytes(out)
 
@@ -384,7 +399,7 @@ async def main():
         else:
             raise SystemExit("hthd never reached READY against RTL")
         caps = await client.get_capabilities("prod2u-0")
-        async with await client.acquire_lease("prod2u-0", "rtl-l3", timedelta(seconds=30), auto_renew=False) as lease:
+        async with await client.acquire_lease("prod2u-0", "rtl-l3", timedelta(seconds=30), auto_renew=True) as lease:
             snap = await client.get_snapshot("prod2u-0")
             b = ActionBatch.builder("prod2u-0", capabilities=caps).based_on(snap.snapshot_seq, snap.control_revision)
             b.valid_for(timedelta(seconds=180))
@@ -431,7 +446,7 @@ async def main():
             raise SystemExit(f"expected 27 temperature channels after GET_TEMP, got {len(temps)}")
         print("TEMP_CHANNELS", len(temps), "EVENT_ID", snap.event_id, "QUALITY", sorted({m.quality for m in temps}))
         caps = await client.get_capabilities("prod2u-0")
-        async with await client.acquire_lease("prod2u-0", "rtl-safe", timedelta(seconds=30), auto_renew=False) as lease:
+        async with await client.acquire_lease("prod2u-0", "rtl-safe", timedelta(seconds=30), auto_renew=True) as lease:
             safe = await lease.enter_safe_mode("rtl-safe")
             print("SAFE_ACCEPTED", safe.accepted, safe.dispatch_state, safe.confirmation)
             snap = await client.get_snapshot("prod2u-0")
@@ -626,7 +641,12 @@ def main() -> int:
         return 1
 
     stop = threading.Event()
-    tap = UartTap(hold_s=10.0)
+    if clks == "217":
+        tap = UartTap(hold_s=45.0, max_hold_s=90.0)
+    else:
+        tap = UartTap(hold_s=10.0, max_hold_s=20.0)
+    print(f"DELAYED_STARTUP_HOLD_S={tap.hold_s} MAX={tap.max_hold_s}", flush=True)
+
     threading.Thread(target=_shuttle, args=(master, conn, stop, tap), daemon=True).start()
 
     for _ in range(900):
@@ -654,8 +674,9 @@ def main() -> int:
         # Start hthd only after RTL calibration. Overlapping host UART with
         # Icarus bit-banging 5000-step seek makes MOTOR_STATUS miss the 28s
         # timeout and look like a reconnect loop. After hthd starts, the UART
-        # tap holds MOTOR_STATUS.startup_complete=0 for 10s wall-clock so the
-        # production UART path proves no GET_TEMPERATURE during delayed startup.
+        # tap holds MOTOR_STATUS.startup_complete=0 until hold_s (45s on 217
+        # baud) and at least one MOTOR_STATUS rewrite, so production UART
+        # proves no GET_TEMPERATURE during delayed startup.
         tap.mark_hthd_start()
         hthd = subprocess.Popen([str(hthd_bin), "--config", str(cfg)], cwd=str(HOST), env={**os.environ})
         for _ in range(200):