Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lelab/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
self.broadcast_queue = queue.Queue()
# Latest-wins slot for live joint frames. Joint updates supersede one
# another, so a slow consumer must never make them pile up in the
# queue — otherwise the 3D view drifts further and further behind the
# real robot as the backlog grows.
self._latest_joint_frame: dict[str, Any] | None = None
self.broadcast_thread = None
self.is_running = False
# Guards `active_connections` since the broadcast worker thread also
Expand Down Expand Up @@ -220,8 +225,18 @@ def _broadcast_worker(self):
try:
while self.is_running:
try:
# Get data from queue with timeout
data = self.broadcast_queue.get(timeout=0.1)
# Send the freshest joint frame first (latest-wins slot).
# A frame published between the read and the reset below is
# simply picked up on the next iteration.
Comment on lines +228 to +230

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment says the opposite of what the code does. A frame published between line 231 and line 233 is not picked up on the next iteration, it is destroyed: the read already returned the previous frame and the unconditional = None overwrites the new one.

The consequence is harmless for a latest-wins slot (the next producer frame is 33ms away and supersedes it anyway), so this is only a matter of the comment being wrong, but the window is not narrow: it spans the whole run_until_complete send. A read and clear in one step under a lock removes it, otherwise please just correct the comment to say the frame is dropped.

frame = self._latest_joint_frame
if frame is not None:
self._latest_joint_frame = None
if self.active_connections:
loop.run_until_complete(self._send_to_all_connections(frame))
Comment on lines +231 to +235

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A frame sitting in this slot when the last client disconnects is never drained, and the next client that connects gets it as its first payload.

disconnect drops the count to zero and calls stop_broadcast_thread, which flips is_running and joins. Whatever is in _latest_joint_frame stays there. When a new client connects, start_broadcast_thread restarts the worker and this block ships the old frame straight away, even if teleoperation ended long ago.

Reproduced against your head commit with a mocked WebSocket:

slot after last client left: {'type': 'joint_update', 'joints': {'Rotation': 9.99}, 'timestamp': 2}
new client first payload:    {'type': 'joint_update', 'joints': {'Rotation': 9.99}, 'timestamp': 2}

In practice: stop teleoperation, close the tab, reopen it, and the 3D arm snaps to a pose the real arm is no longer in, with nothing to correct it until teleoperation starts again.

Clearing the slot in start_broadcast_thread fixes it and keeps the invariant local to the thread lifecycle:

self._latest_joint_frame = None
self.is_running = True
self.broadcast_thread = threading.Thread(target=self._broadcast_worker, daemon=True)


# Get data from queue with a short timeout so joint frames
# are polled frequently enough to stay real-time.
data = self.broadcast_queue.get(timeout=0.02)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. Now that joint frames bypass the queue entirely, this timeout is what caps the joint frame rate: the worker can only ship one frame per poll, so 0.02 makes the ceiling 50Hz and the thread wakes 50 times a second even when nothing is happening.

A threading.Event set in broadcast_joint_data_sync and waited on here would remove both the magic constant and the busy poll, and let the worker react to a frame the moment it lands. Fine to keep the timeout if you would rather not restructure the loop, but 0.02 deserves a word about where it comes from.

if data is None: # Poison pill to stop
break

Expand Down Expand Up @@ -258,6 +273,11 @@ async def _send_to_all_connections(self, data: dict[str, Any]):
def broadcast_joint_data_sync(self, data: dict[str, Any]):
"""Thread-safe method to queue data for broadcasting"""
if self.is_running and self.active_connections:
if isinstance(data, dict) and data.get("type") == "joint_update":
# Replace any unsent frame instead of queueing: only the most
# recent robot pose is worth sending.
self._latest_joint_frame = data
return
try:
self.broadcast_queue.put_nowait(data)
except queue.Full:
Expand Down
47 changes: 39 additions & 8 deletions lelab/teleoperate.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@
"elbow_flex": (+1, 1029),
}


def _load_urdf_view_zero() -> dict[str, dict[str, float]] | None:
"""Per-arm 3D-view zero captured by the user at the URDF sleep pose.

The hardcoded tick corrections above assume a canonical calibration; real
calibrations vary enough that the 3D view can be wildly offset. If
``calibration/urdf_view_zero.json`` exists (mapping motor name to
``{"zero_deg": float, "sign": ±1}``), it takes priority:
URDF angle = sign * (raw_normalized_deg - zero_deg).
Comment on lines +52 to +58

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here.

The docstring says "Per-arm", but the path is a single file at the calibration root, shared by every arm and every saved config. Two followers with different calibrations get the same zero pose, and the file silently applies to whichever one is connected. Either key it by config name (next to the calibration JSON it belongs to) or drop "per-arm" from the wording.

Second, the justification. The comment above says the tick constant holds as long as the user pressed ENTER at the middle of range pose during set_half_turn_homings. A 2048 tick homing error is 180.04 degrees, close to the ~200 degrees in the PR body. Worth ruling out a miscalibrated follower before adding a second source of truth for the zero pose. I put the full question in the review summary.

"""
import json
from pathlib import Path

path = Path.home() / ".cache" / "huggingface" / "lerobot" / "calibration" / "urdf_view_zero.json"
Comment on lines +60 to +63

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLAUDE.md asks for shared paths to live in lelab/utils/config.py, not to be rebuilt in feature modules. That file already owns CALIBRATION_BASE_PATH_TELEOP and CALIBRATION_BASE_PATH_ROBOTS and every other ~/.cache/huggingface/lerobot path. Please add the constant there and import it:

# lelab/utils/config.py
URDF_VIEW_ZERO_FILE = os.path.expanduser("~/.cache/huggingface/lerobot/calibration/urdf_view_zero.json")

json is already imported at module level in utils/config.py, and the local import json / from pathlib import Path here can go once the path comes from a constant.

try:
with open(path, encoding="utf-8") as f:
data = json.load(f)
return {k: v for k, v in data.items() if isinstance(v, dict) and "zero_deg" in v}
except (OSError, ValueError):
return None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff format wants two blank lines between this function and the module level block that follows, and quality.yml runs pre-commit with the ruff-format hook, so this is a failing check rather than a style preference.

$ ruff format --diff lelab/teleoperate.py
@@ -68,6 +68,7 @@
     except (OSError, ValueError):
         return None

+
 # Global variables for teleoperation state
1 file would be reformatted

ruff format lelab/teleoperate.py fixes it. ruff check is already clean.


# Global variables for teleoperation state
teleoperation_active = False
teleoperation_thread: threading.Thread | None = None
Expand Down Expand Up @@ -85,6 +106,12 @@ def get_joint_positions_from_robot(robot) -> dict[str, float]:
try:
observation = robot.get_observation()
calibration = robot.calibration or {}
view_zero = getattr(get_joint_positions_from_robot, "_view_zero_cache", "unset")
if view_zero == "unset":
view_zero = _load_urdf_view_zero()
get_joint_positions_from_robot._view_zero_cache = view_zero
if view_zero:
logger.info("Using user-captured urdf_view_zero.json for the 3D view")
Comment on lines +109 to +114

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caches a None result as hard as a real one, so the first call decides for the whole process. There is no endpoint and no script in this PR that writes urdf_view_zero.json, which means the only way to use the feature is to hand write the file, and if the server has already run one teleoperation session the user has to restart lelab entirely for it to be picked up. That is a rough discovery path for a fix aimed at a visibly broken view.

Loading it once per teleoperation session, in handle_start_teleoperation before the worker starts, gives the same one read per session without the process wide stickiness, and it fits how the rest of this module carries per session state in module globals rather than function attributes.


joint_positions: dict[str, float] = {}
debug_rows = []
Expand All @@ -97,13 +124,17 @@ def get_joint_positions_from_robot(robot) -> dict[str, float]:

raw_deg = observation[motor_key]
angle_degrees = raw_deg
correction = _SO101_URDF_CORRECTIONS.get(motor_name)
if correction is not None and motor_name in calibration:
sign, urdf_zero_ticks = correction
cal = calibration[motor_name]
mid = (cal.range_min + cal.range_max) / 2
motor_at_urdf_zero = (urdf_zero_ticks - mid) * 360 / _STS3215_MAX_RES
angle_degrees = sign * (raw_deg - motor_at_urdf_zero)
if view_zero and motor_name in view_zero:
entry = view_zero[motor_name]
angle_degrees = float(entry.get("sign", 1)) * (raw_deg - float(entry["zero_deg"]))
else:
correction = _SO101_URDF_CORRECTIONS.get(motor_name)
if correction is not None and motor_name in calibration:
sign, urdf_zero_ticks = correction
cal = calibration[motor_name]
mid = (cal.range_min + cal.range_max) / 2
motor_at_urdf_zero = (urdf_zero_ticks - mid) * 360 / _STS3215_MAX_RES
angle_degrees = sign * (raw_deg - motor_at_urdf_zero)

joint_positions[urdf_joint_name] = angle_degrees * math.pi / 180.0
debug_rows.append(
Expand Down Expand Up @@ -227,7 +258,7 @@ def teleoperation_worker():
logger.info("Starting teleoperation loop...")
try:
last_broadcast_time = 0
broadcast_interval = 0.05 # 20 FPS
broadcast_interval = 1 / 30 # 30 FPS

while teleoperation_active:
action = teleop_device.get_action()
Expand Down