Telemetry and data recording¶
Telemetry callbacks run on the SDK reader thread. Hand each packet to a queue immediately, then process, plot, or save it in your own loop.
Consume telemetry without blocking the reader¶
import csv
import time
from queue import Empty, SimpleQueue
from sim2bot import Robot, RobotState
def main() -> None:
packets: SimpleQueue[RobotState] = SimpleQueue()
with Robot(auto_bridge=True, wait_for_sim=True) as sim:
sim.on_telemetry(packets.put)
deadline = time.monotonic() + 5.0
try:
with open("telemetry.csv", "w", newline="") as output:
writer = csv.writer(output)
writer.writerow(["robot", "sim_time", "tcp_x", "tcp_y", "tcp_z"])
while time.monotonic() < deadline:
try:
state = packets.get(timeout=0.5)
except Empty:
continue
if len(state.tcp) >= 3:
writer.writerow([state.robot, state.t, *state.tcp[:3]])
finally:
sim.on_telemetry(None)
if __name__ == "__main__":
main()
state() exposes the newest complete packet. states() exposes the newest
packet's physics-substep samples. Neither API builds an unbounded history.
Keep callbacks lightweight
Plotting, file I/O, networking, or heavy numerical work inside the callback blocks the SDK reader thread and can make telemetry appear frozen.