-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelemetry_rate.py
More file actions
73 lines (56 loc) · 2.21 KB
/
Copy pathtelemetry_rate.py
File metadata and controls
73 lines (56 loc) · 2.21 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
62
63
64
65
66
67
68
69
70
71
72
73
"""Measure Sim2Bot telemetry rate — shows the high-rate batching (A2).
Packets arrive at ~30 Hz, but each carries a batch of per-physics-substep samples,
so the *effective* sample rate a controller can reconstruct is much higher
(hundreds of Hz to ~1 kHz). This prints both so you can see the difference.
Setup:
pip install sim2bot
Run this script, then open the app (https://app.sim2bot.com) and click
Tools -> Bridge -> Connect:
python examples/telemetry_rate.py # WebSocket (TCP)
python examples/telemetry_rate.py udp # UDP control/telemetry transport
"""
from __future__ import annotations
import sys
import threading
import time
from sim2bot import Robot
DURATION_S = 5.0
def main(transport: str) -> None:
packets = 0
samples = 0
last_latency = None
lock = threading.Lock()
def on_packet(state) -> None:
nonlocal packets, samples, last_latency
if state.robot != 0:
return
with lock:
packets += 1
samples += len(state.samples)
last_latency = state.latency
with Robot(transport=transport, auto_bridge=True, wait_for_sim=True, wait_for_sim_timeout=30) as robot:
info = robot.describe()
print(f"transport: {transport}")
print(f"robot: [{info[0].index}] {info[0].name} id={info[0].id or '(none)'} (dof {info[0].dof})")
robot.on_telemetry(on_packet)
print(f"measuring for {DURATION_S:.0f}s ...")
time.sleep(DURATION_S)
with lock:
p, s, lat = packets, samples, last_latency
pkt_hz = p / DURATION_S
eff_hz = s / DURATION_S
per_pkt = s / p if p else 0
print(f" packets/s: {pkt_hz:.0f}")
print(f" samples/packet: {per_pkt:.1f}")
print(f" effective sample rate: {eff_hz:.0f} Hz <- high-rate batch")
if lat is not None:
print(f" telemetry latency: {lat * 1000:.0f} ms")
if s == 0:
print(" (no batch samples — is the sim paused? unpause it in the toolbar.)")
if __name__ == "__main__":
# Optional transport arg: "ws" (default) or "udp".
transport_arg = sys.argv[1] if len(sys.argv) > 1 else "ws"
try:
main(transport_arg)
except KeyboardInterrupt:
pass