Skip to content

sim2bot.Robot.state

state(robot: int = 0) -> Optional[RobotState]

Return the newest complete telemetry packet for one robot.

Parameters:

  • robot (int, default: 0 ) –

    Robot index returned by describe.

Returns:

Notes

State is latest-value rather than queued. A slow consumer does not fall behind by reading old packets.

Source code in sim2bot/client.py
def state(self, robot: int = 0) -> Optional[RobotState]:
    """Return the newest complete telemetry packet for one robot.

    Args:
        robot: Robot index returned by
            [`describe`][sim2bot.client.Robot.describe].

    Returns:
        Latest [`RobotState`][sim2bot.client.RobotState], or ``None`` before
        the first packet arrives.

    Notes:
        State is latest-value rather than queued. A slow consumer does not
        fall behind by reading old packets.
    """
    with self._states_lock:
        return self._states.get(robot)

Practical examples

Examples use objects discovered from the current scene rather than hard-coded robot metadata.

Read joint and TCP state

state = sim.state(robot=arm.index)
if state is not None:
    print("q [rad]", state.q)
    print("TCP [m]", state.tcp)

Wait for the first packet

import time

deadline = time.time() + 2.0
while sim.state(robot=arm.index) is None and time.time() < deadline:
    time.sleep(0.01)
state = sim.state(robot=arm.index)

Guidance

Usage tip

Check for None during startup. Once telemetry begins, each call returns the newest complete packet.

Common error

A tight loop reading state() does not create new samples; packet production is controlled by the browser telemetry rate.


See also