Metadata-Version: 2.5
Name: lerobot_teleoperator_rfabricd
Version: 0.1.2
Summary: LeRobot plugin: the rFabric remote operator drives a LeRobot robot over the rFabric realtime control UDS (rfabricd teleoperator and its follower robot types), and the leader relay serves local leader arms to the operator console.
Project-URL: Homepage, https://rfabric.io
Project-URL: Repository, https://github.com/rfabric/lerobot-teleoperator-rfabricd
Author-email: rFabric <engineering@rfabric.io>
License: Apache-2.0
Keywords: lerobot,rfabric,robotics,teleoperation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: cryptography>=44.0
Requires-Dist: lerobot<0.8,>=0.6.1
Requires-Dist: numpy>=2.0
Requires-Dist: rfabric-control-wire>=0.1.1
Requires-Dist: websockets>=15.0
Provides-Extra: dev
Requires-Dist: lerobot[dataset]<0.8,>=0.6.1; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: feetech
Requires-Dist: lerobot[feetech]<0.8,>=0.6.1; extra == 'feetech'
Provides-Extra: kinematics
Requires-Dist: lerobot[kinematics]<0.8,>=0.6.1; extra == 'kinematics'
Description-Content-Type: text/markdown

# lerobot_teleoperator_rfabricd

LeRobot plugin that lets an rFabric remote operator drive a LeRobot robot, with
stock `lerobot-teleoperate` and `lerobot-record`. It is the follower end of the
rFabric realtime control UDS: `rfabricd` connects to the socket
the teleoperator binds and relays the operator console's control frames
(`rfabric-control-wire`).

Works with stock LeRobot 0.6 and 0.7. LeRobot discovers the plugin by its
distribution name (`lerobot_teleoperator_*`); installing it in the LeRobot
environment adds:

| Flag | What it is |
|---|---|
| `--teleop.type=rfabricd` | The remote operator |
| `--robot.type=rfabric_bi_so_follower` | `bi_so_follower` that shares its joint readings with the teleoperator |
| `--robot.type=rfabric_so101_follower` | `so101_follower` that shares its joint readings with the teleoperator |

The operator sends absolute joint positions (`joint_targets`), one velocity
(`-1..1`) per joint, or - with `--teleop.urdf_path` set - the gripper's velocity
(see [End-effector control](#end-effector-control)); turning velocities into
joint positions needs the arm's present position, which LeRobot gives a
teleoperator only through the robot. The follower robot types are the stock
robots - same flags, calibration files, features and `robot_type` in datasets -
that also publish every observation and completed write to the teleoperator in
the same process.

On the operator's machine the package also installs `rfabric-leader-relay`
(see [Leader relay](#leader-relay)): local SO-101 leader arms drive the remote
follower through the operator console.

## Install

Into the Python environment LeRobot runs from:

```bash
uv pip install lerobot_teleoperator_rfabricd
```

## Run

Teleoperate a bimanual SO-101 (what `rfabric-arms.service` runs):

```bash
lerobot-teleoperate \
  --robot.type=rfabric_bi_so_follower --robot.id=bimanual_follower \
  --robot.left_arm_config.port=/dev/ttyACM0 --robot.right_arm_config.port=/dev/ttyACM1 \
  --robot.left_arm_config.max_relative_target=24 --robot.right_arm_config.max_relative_target=24 \
  --teleop.type=rfabricd --teleop.socket_path=/run/rfabric/control.sock \
  --teleop.arms='[left,right]' --fps=60
```

Record while the remote operator drives (the dataset action is the joint
position target):

```bash
lerobot-record <the same --robot.* and --teleop.* flags> \
  --dataset.repo_id=local/remote_demo --dataset.single_task="Pick the cube"
```

A single-arm robot uses `--robot.type=rfabric_so101_follower` and leaves
`--teleop.arms` empty.

## Embodiment

The teleoperator declares the robot's state and actions in the layout below,
from the follower's first observation (`<arm>_<joint>.pos` keys). The robot's
`Embodiment` on the platform must declare the same state components and action
groups, with the same names, joints and order: rfabricd compares the two
fingerprints (`embodiment`) and refuses the session when they differ.

| Part | Name | Declared as | Elements |
|---|---|---|---|
| Action group, one per arm | the arm tag (`left`, `right`); `arm` for a single arm | `target: joints`, `mode: position` | the arm's joints in observation order |
| State component, one per arm | `<group>_position` (`left_position`) | `quantity: joint_position` | the same joints |

Groups and components follow `--teleop.arms` order. For a bimanual SO-101
(`--teleop.arms='[left,right]'`) the action names run from `left.shoulder_pan`
to `left.gripper`, then `right.shoulder_pan` to `right.gripper`; the state names
from `left_position.shoulder_pan` to `right_position.gripper`; the fingerprint is
`4199509803`. A single SO-101 is group `arm` and component `arm_position`,
fingerprint `3280823783`. Values are in the follower's joint units (degrees for
SO-101 with `use_degrees`, the gripper `0..100`).

The matching `Embodiment` of a bimanual SO-101 (add `low`, `high` and `maxRate`
per group; a remote inference session needs `maxRate`):

```json
{
  "state": {"components": [
    {"name": "left_position", "quantity": "joint_position", "joints": ["shoulder_pan", "shoulder_lift", "elbow_flex", "wrist_flex", "wrist_roll", "gripper"]},
    {"name": "right_position", "quantity": "joint_position", "joints": ["shoulder_pan", "shoulder_lift", "elbow_flex", "wrist_flex", "wrist_roll", "gripper"]}
  ]},
  "actions": {"groups": [
    {"name": "left", "target": "joints", "mode": "position", "joints": ["shoulder_pan", "shoulder_lift", "elbow_flex", "wrist_flex", "wrist_roll", "gripper"]},
    {"name": "right", "target": "joints", "mode": "position", "joints": ["shoulder_pan", "shoulder_lift", "elbow_flex", "wrist_flex", "wrist_roll", "gripper"]}
  ]}
}
```

## What the operator gets

- **Capabilities**: on each rfabricd connection, a `mode` frame (control wire
  version 2) with the fingerprint, `stateNames`, `actionNames`, the `groups`,
  `accepts: [joint_targets, joint_velocity, stop, home]` -
  `[joint_targets, joint_velocity, end_effector_velocity, stop, home]` with
  `--teleop.urdf_path` set - `positionHold: true`, each group's `home` row
  (`--teleop.home_pose`) and its `velocityScale`: per joint the speed of
  normalised velocity 1 (`joint_speed`, `gripper_speed` for the gripper),
  which rfabricd uses to bound operator velocities by the group's `maxRate`.
- **State**: one `snapshot` at `state_publish_hz` (at least 5 Hz): the
  fingerprint and every joint position, packed as float32 in `stateNames`
  order, from an observation the robot read within 200 ms
  (`STALL_HOLD_MAX_MS`). While LeRobot's loop stalls (for example saving an
  episode) nothing is sent, and rfabricd publishes the state as NaN: no
  measurement.
- **Joint targets**: a `joint_targets` frame (`arm` names the group) is that
  arm's position target as given, joint by joint; joints it leaves out hold.
  rfabricd limits how fast consecutive targets move (the group's `maxRate`) and
  turns an operator `home` into targets toward the home row; a `home` that
  reaches the teleoperator is the home row as one target.
- **Safety**: an arm holds its present position when it receives `stop`, when its
  last command (`joint_targets`, `joint_velocity` or `end_effector_velocity`) is
  older than `watchdog_ms`, when rfabricd disconnects, on a malformed control
  payload and on a frame of another control-wire version; a `stop` without an
  arm tag stops every arm. A stale command never keeps moving anything. After
  end-effector control the jaws are the exception: they keep their last commanded
  target through every hold, so a stop never loosens a grip (see
  [End-effector control](#end-effector-control)).
- **Stop ack**: once the follower's write that carries a `stop`'s hold returned,
  the teleoperator answers with `ack{kind:"stop"}` - the preflight stop check of
  rfabricd, which the operator console requires before motion by default.
- **Episodes**: operator `terminate` / `success` / `rerecord` mode frames are
  reported by `get_teleop_events()`.

## Servo settings (`--teleop.*`)

| Flag | Default | Meaning |
|---|---|---|
| `joint_speed` | `60.0` | Joint speed at full velocity, per second (degrees for SO-101) |
| `gripper_speed` | `180.0` | Gripper speed at full velocity, per second (`0..100`) |
| `lead_cap` | `12.0` | How far a target may run ahead of the present position |
| `velocity_tau_s` | `0.10` | Low-pass time constant on operator velocities; `0` disables |
| `velocity_idle_epsilon` | `0.04` | Smoothed velocity below which a released joint holds |
| `joint_signs` | `{}` | `-1` reverses a joint, e.g. `'{right_shoulder_pan: -1}'` |
| `home_pose` | `{}` | Home position per joint, keyed like `joint_signs`; a joint left out homes to `0` |
| `watchdog_ms` | `200` | Command age after which an arm holds |

Keep `--robot.*.max_relative_target` at about twice `lead_cap`: the robot then
caps any single command as a second line of defence.

## End-effector control

With `--teleop.urdf_path` set, the teleoperator also accepts
`end_effector_velocity` frames - what the operator console's gamepad sends, one
every 33 ms while any input is non-zero, then 0.5 s of all-zero frames:

```json
{"kind": "end_effector_velocity", "arm": "left",
 "linear": {"x": 0.0, "y": 0.0, "z": 1.0}, "pitch": 0.0, "roll": 0.0, "gripper": -1.0}
```

Every value is a fraction (`-1..1`) of a top speed: `linear` moves the
gripper's wrist point, `pitch` tilts the gripper about its wrist, `roll` turns it
about its own outward axis, `gripper`
opens (`+`) or closes (`-`) the jaws. Like `joint_velocity` it is the arm's
latest command: the follower smooths it and integrates it every control tick
with the tick's real duration, so the distance travelled depends on how long a
stick is held, not on how many frames arrived. placo's kinematics, loaded from
the URDF, turn it into joint targets.

| Flag | Default | Meaning |
|---|---|---|
| `urdf_path` | unset | The follower arm's URDF; unset, the teleoperator accepts joint velocities only |
| `tip_speed` | `0.10` | Wrist point speed at full `linear` input, in metres per second |
| `pitch_speed` | `90.0` | Tilt speed at full `pitch` input, in degrees per second |
| `roll_speed` | `90.0` | `wrist_roll` speed at full `roll` input, in degrees per second |

`gripper_speed`, `velocity_tau_s`, `velocity_idle_epsilon`, `lead_cap` and
`watchdog_ms` ([Servo settings](#servo-settings---teleop)) apply to it too.

It needs placo, from the plugin's `kinematics` extra (LeRobot's `kinematics`),
and the arm's URDF with its meshes (`assets/`, which the URDF references by
relative path):

```bash
uv pip install 'lerobot_teleoperator_rfabricd[kinematics]'
git clone --depth 1 --filter=blob:none --sparse https://github.com/TheRobotStudio/SO-ARM100.git
git -C SO-ARM100 sparse-checkout set Simulation/SO101
# then: --teleop.urdf_path=$PWD/SO-ARM100/Simulation/SO101/so101_new_calib.urdf
```

The teleoperator loads one kinematics per arm at connect and fails there if
placo is missing or the URDF does not load; the first follower observation must
carry every URDF joint (`so101_new_calib.urdf` names its joints after the
SO-101 motors).

- **Frames and units**: the wrist point is the origin of the URDF frame
  `wrist_link`, on the `wrist_flex` axis, which `wrist_flex` and `wrist_roll` do
  not move. It moves in the arm's base frame (the URDF's root, `base_link`: for
  SO-101 `+x` points forward from the base, `+y` to its left seen from behind,
  `+z` up). The tilt is the elevation of the gripper's outward axis
  (`gripper_frame_link`'s `+z`) above the base's horizontal plane, in degrees:
  positive `pitch` raises the fingertips. A roll turns the gripper about that
  outward axis, right-hand rule: positive `roll` is clockwise seen from behind
  the gripper. Joints are in the follower's units: degrees for SO-101
  (`use_degrees`), the gripper `0..100`.
- **Smoothing**: each input is low-passed with `velocity_tau_s`, the joint
  servo's filter, so a full stick ramps the arm up over about `3 *
  velocity_tau_s` instead of jumping, and a released stick (or the all-zero
  frames) eases it to rest; an input whose smoothed value falls below
  `velocity_idle_epsilon` stops. At rest the arm holds its commanded joints,
  without drift.
- **Motion**: per tick the wrist point's target moves `tip_speed * dt` at full
  input, the tilt's target `pitch_speed * dt` and `wrist_roll`'s target
  `roll_speed * dt`. `shoulder_pan`, `shoulder_lift` and `elbow_flex` come from
  placo's wrist-position solve, seeded with the present joints and inside the
  URDF's joint ranges; `wrist_flex` then sets the tilt, within its URDF range,
  so a move keeps the gripper's tilt.
- **Roll**: `wrist_roll` alone turns the gripper - its axis is the gripper's
  outward axis, so neither the wrist point nor the tilt moves. Its direction
  comes from the kinematics at load (which way `wrist_roll` turns
  `gripper_frame_link` about its `+z`): on SO-101 a positive `roll` turns
  `wrist_roll` negative. It stays within the URDF's `wrist_roll` range; a
  `wrist_roll` already past it (a calibration that disagrees with the URDF) is
  not pulled back in. With no `roll` input `wrist_roll` holds its target.
- **Limits**: the body's step from the present joints - shoulder, elbow,
  `wrist_flex` and `wrist_roll` together - is scaled down as a whole until no
  joint moves more than `lead_cap`, which keeps its direction. A step that
  would leave the wrist further from its target than it is now - a soft solve
  at a singularity, such as the arm stretched out - or a failed solve is
  refused: the shoulder and elbow hold. After each tick the targets become what
  the commanded joints reach, so pushing on at the edge of the workspace or at
  a joint limit, `wrist_roll`'s included, winds nothing up and the reverse
  direction moves the arm at once.
- **Gripper**: its target moves `gripper_speed * dt` at full input, within
  `0..100`, and stays within `lead_cap` of the jaws' present position: a grip
  closed on an object squeezes with a bounded force, and the jaws never slam.
  At zero input the target stays, so a grip keeps squeezing.
- **Holds**: a `stop` or a command older than `watchdog_ms` holds the body at
  its present position at once, and the jaws at their last commanded target:
  a grip closed on an object keeps squeezing through the console's trailing
  all-zero frames, the watchdog, a `stop` or a disconnect. Only a new command
  moves the jaws: an `end_effector_velocity` with a non-zero `gripper`, or a
  `joint_velocity` on the `gripper` joint, which moves it from its present
  opening.
- **Entering Cartesian control**: the body's targets start from the present
  joints on an arm's first `end_effector_velocity` command and after a `stop`,
  the watchdog or a `joint_velocity` command; the gripper's target starts from
  the target it was held at, else from its present opening.
- **Bimanual**: one kinematics per arm tag, each in its own arm's base frame -
  a frame tagged `left` moves the left gripper in the left arm's frame.

## Leader relay

`rfabric-leader-relay` runs on the operator's machine, next to one SO-101 leader
arm (or a bimanual pair), and serves the leader's joint positions to the
operator console in the browser. The console checks the leader against the
robot, waits until each leader arm is posed like its follower, then sends the
positions to the robot as `joint_targets`: the leader drives the remote
follower. Only the console joins the robot's session.

### Install and run

Install the package with the motor bus extra where the leaders are plugged in:

```bash
uv pip install 'lerobot_teleoperator_rfabricd[feetech]'
```

Calibrate each leader once with stock `lerobot-calibrate` (the relay never
calibrates; it refuses an uncalibrated leader), then start the relay with the
same `--teleop.*` flags and the console's origin.

One leader, for a robot with `rfabric_so101_follower` (action group `arm`):

```bash
rfabric-leader-relay \
  --teleop.type=so101_leader --teleop.port=/dev/tty.usbmodem58760431551 --teleop.id=blue \
  --origins='[https://app.rfabric.io]'
```

Two leaders, for a robot with `rfabric_bi_so_follower` (groups `left`, `right`):

```bash
rfabric-leader-relay \
  --teleop.type=bi_so_leader --teleop.id=bimanual_leader \
  --teleop.left_arm_config.port=/dev/tty.usbmodem5A460852721 \
  --teleop.right_arm_config.port=/dev/tty.usbmodem5A460819811 \
  --arms='[left,right]' --origins='[https://app.rfabric.io]'
```

It prints the pairing address and a token:

```text
rFabric leader relay: 2 arm(s) (left, right) at 60 Hz
Pair the operator console: address wss://127.0.0.1:8791  token jMCayTisVhKvWZOjRrYn6A
First time in this browser: open https://127.0.0.1:8791/ and accept its certificate
  (SHA-256 13:B8:...)
```

| Flag | Default | Meaning |
|---|---|---|
| `--teleop.*` | required | The leader, as stock LeRobot takes it; its calibration files apply |
| `--origins` | required | Origins of the operator console allowed to pair, exact (`https://app.rfabric.io`) |
| `--arms` | `[]` | Arm tags prefixing the leader's action keys (`[left,right]` for `bi_so_leader`); empty for one leader |
| `--port` | `8791` | Loopback port |
| `--fps` | `60` | Samples per second, `30` to `60` |
| `--certificate_dir` | `~/.config/rfabric/leader-relay` | Where the relay keeps its TLS certificate (`$XDG_CONFIG_HOME` respected) |

### Pair the console

1. First time in a browser: open `https://127.0.0.1:8791/` and accept the
   relay's certificate (compare the SHA-256 the relay printed). The browser
   remembers it; the relay keeps the same certificate for about a year.
2. In the operator console's controls, under **Leader arms**, enter the address
   and the token and pair.
3. Pose each leader arm like its follower: an arm follows once every joint is
   within 5 degrees of the follower (the gripper within 5 of its `0..100`).
   Until then the console shows how far off each joint is.

The deadman, the stop, the latency lockout and the verified stop path apply to
leader input as to every other input; after any of them stops an arm, it follows
again only after a new pose match. With `teleop.deadmanRequired` on, both hands
are on the leaders: use a foot pedal that sends Space, or turn the setting off.
Leader input never takes an arm from a model or another source: take the arm
over first (Take over on the robot's session page, or one key, stick or jog
command), then match the pose; an arm whose group the robot hands to another
source stops following without sending anything. The console's speed setting
does not scale leader input: the robot `Embodiment`'s `maxRate` per group is the
speed bound (`rfabricd` matches a group's joints to the follower's
`<group>_position` state component), and without one the follower's own
`max_relative_target` is the only limit.

### What the relay guarantees

- **Read-only**: it connects the leader as stock LeRobot does (torque off,
  without calibrating it) and then only reads joint positions (`get_action()`);
  it never enables torque or writes a goal position.
- **Loopback only**: it listens on `127.0.0.1`, never another interface, over
  TLS (Safari refuses plain `ws://` to loopback from an `https` page).
- **Paired console only**: a WebSocket handshake without the token or from an
  origin not in `--origins` is refused (`403`), and so is a second console while
  one is paired (`409`). The token changes on every start. One read is on the
  bus at a time, whichever console asks.
- **Samples**: each read is one control-wire `control` frame per arm, payload
  `joint_targets{joints, arm}`, the frames of one read sharing `tsNs`, the
  relay's wall clock when the read started (the console ages the sample from
  it); `arm` is the arm tag (`left`, `right`) or `arm` for one leader, joints
  keyed without tag and `.pos`, in the leader's units (degrees, the gripper
  `0..100`). A failed read closes the console with code `1011` and stops the
  relay.

## Development

```bash
uv sync --extra dev --extra feetech
.venv/bin/ruff check src tests && .venv/bin/ruff format --check src tests
.venv/bin/mypy src
.venv/bin/pytest
```

`rfabric-control-wire` resolves from the sibling checkout
(`[tool.uv.sources]`).
