# Connect your robot

> Pair a robot with a code from its page. Write an adapter so your own robot can run approved policies.

Prompt for your AI: Read https://mousemouse.ai/docs/connect-your-robot.md first. Help me write an adapter so my robot can run approved policies from the hub. Ask me before anything that costs money or moves a robot.

Connecting a robot lets a policy from the hub run on your real machine. A small
program on the robot's own computer receives the policy. It runs one only after
a person approves it, for the minutes that person chose.

That program checks the approval itself, without asking the hub. It also
refuses a policy built for a different robot. Then it hands the policy to **an
adapter**: the code that reads your robot's sensors and drives its motors.

With a built-in adapter, pairing is the whole job. Write an adapter of your own
when none of them fits your hardware. This page shows how, and what the driver
trusts an adapter with.

## From the robot's page

**You connect a robot on the hub**, as in [Deploy a policy to your
robot](/docs/deploy-to-a-robot). Open the robot's page → *Deploy* → *Connect
a robot*. The page shows a short code and the lines to paste on the robot's
computer. They install the driver, pair it with the code and start it. A
signed-in person confirms the robot that claimed the code, by comparing the key
fingerprint the robot prints. No API key
ever touches the robot. A policy then reaches it only through **Deploy to
robot** on the policy's page, or a request an agent filed and a person approved
in the Inbox. **Stop now** on the live page ends a run early, and
**Disconnect** on My robots revokes the robot's credential.

With a built-in adapter that is the whole job. A LeRobot arm, such as an
SO-101, connects through the bridge in the next section. Read on past it when
your robot needs an adapter of its own.

## A LeRobot arm, through the bridge

A LeRobot arm connects through **the bridge**: a small server on the arm's
computer. It owns the arm's serial port. Every command to the arm passes
through its safety filter, whether it comes from your code, a LeRobot policy or
the leader arm.

The filter enforces the robot card's limits. Teach mode is the default. Each
joint moves at most 0.5 rad/s, and the gripper at most 250 mm/s. The arm holds
when your code stops talking for 200 ms while it moves. It also holds after 5
out-of-range commands within 2 s, and when a joint's load stays above 80%.
Nothing your code sends turns the filter off.

A real arm moves only while a person has armed it on the hub, for the minutes
they chose. Rehearse every step on the arm's twin first. The twin is the same
server and filter, with MuJoCo in place of the motors.

### Install

```bash
uv tool install "lucen-device[lerobot]"
```

This brings LeRobot and PyTorch, about 1 GB.

### Rehearse on the twin

Add the twin of an SO-101. It speaks LeRobot's units, so a LeRobot policy reads
the twin as it reads the arm. Give its cameras the names your policy was
trained with.

```bash
lucen-device bridge add twin --type so101_follower --camera top --camera front --camera right_wrist@gripper_link
```

Start the bridge, and leave it running:

```bash
lucen-device bridge serve
```

It answers only on this computer, and its token is in a file only you can read.

### Run code

In a second terminal, drive the twin from Python:

```python
from lucen_device import control

arm = control.connect("twin")
print(arm.joints)
arm.move_joints({"shoulder_pan": 0.3})
arm.gripper(1.0)
arm.move_joints({"wrist_roll": 9.0})  # out of range: the filter clamps it
for event in arm.events()["events"][-3:]:
    print(event["kind"], event.get("caller"))
```

Every change the filter makes is logged, with the file and line that sent the
command.

### Run a policy

A LeRobot policy runs through the same filter. This ACT policy was trained on an
SO-101 and is downloaded from Hugging Face:

```python
arm.run_policy(
    "hf:Fiberal/act_restocking_multi_policy", task="restock", duration=20, record=True, wait=True
)
```

### Record

`record=True` saves the session as a LeRobot dataset. It holds the joints, the
commands after the filter, every camera, the task and the filter's decisions.
Every armed session on a real arm is recorded. Upload the last recording as a
private dataset, and check it:

```bash
lucen-device bridge upload latest --to you/so101-twin-session
```

```bash
lucen datasets check you/so101-twin-session
```

### Record on Lucen Arm and Lucen Humanoid Arm

Each hand of the Lucen arms carries a wrist camera. The robot card names them:
`wrist` on Lucen Arm, `left_wrist` and `right_wrist` on Lucen Humanoid Arm. A top
camera on the Humanoid Arm is optional, named `top`. Recordings name each video after
its camera, so a policy trained on them reads the cameras the card lists.

Add each arm's twin. A twin shows each camera on the card, at a stand-in mount
position until the real one is measured. It holds the arm up the way the
robot's own controller does. These lines record 30 frames a second, as a real
arm does. Frames are 160 by 120 pixels, which trains quickly on a laptop:

```bash
lucen-device bridge add arm --type lucen_arm --fps 30 --camera wrist:160x120
```

```bash
lucen-device bridge add humanoid-arm --type lucen_humanoid_arm --fps 30 --camera left_wrist:160x120 --camera right_wrist:160x120
```

If your Humanoid Arm has the top camera, name all three:

```bash
lucen-device bridge add humanoid-arm --type lucen_humanoid_arm --fps 30 --camera left_wrist:160x120 --camera right_wrist:160x120 --camera top:160x120
```

Start the bridge, then record. The record holds whatever moves the arm: your code
through the bridge, or a policy. On a twin, `place_object([x, y, z])` sets a red
ball where its camera can see it. Put the object back between episodes:

```bash
lucen-device bridge record arm --task "Reach the red ball with the hand and hold it there" --episodes 50 --episode-time 7 --reset-time 2.5 --to you/arm-reach
```

```bash
lucen-device bridge record humanoid-arm --task "Reach the red ball with the left hand and hold it there" --episodes 50 --episode-time 7 --reset-time 2.5 --to you/humanoid-arm-reach
```

A real Lucen arm records the same way once a leader arm or a headset drives it
through the bridge. Neither does yet. Then train on the dataset in one sentence:

```bash
lucen train "Reach the red ball with the hand and hold it there" --robot lucen/laika-arm --dataset lucen://you/arm-reach --yes
```

### Stop

```bash
lucen-device bridge estop
```

stops every robot on the bridge, and needs no token. The arm holds where it is,
with its motors on. A second e-stop turns the motors off. So does a hold that
fails, or a motor at its full rated load. **Stop** on the hub's live page stops
it from anywhere. Anyone who can see the robot may press it.

### The arm

Calibrate the arm with LeRobot first. Then register this computer with the hub:

```bash
lucen-device register --robot lucen/so-101 --name bench-arm --adapter lerobot --trust approved
```

The next lines need the arm plugged in, so they are not run in these docs'
checks. Add the arm beside the twin, with your port, id, calibration folder and
cameras, then restart the bridge:

```text
lucen-device bridge add arm --backend lerobot --type so101_follower --port /dev/tty.usbmodem5A7A0185641 --id so101_follower --calibration-dir ~/.cache/huggingface/lerobot/calibration/robots/so_follower --camera front=0 --camera wrist=1
```

The bridge reads the calibration and never writes it. It refuses to start the
arm when the motors disagree with the calibration file. Then ask a person to arm
it:

```python
arm = control.connect("arm")
arm.request_live(minutes=2)
arm.wait_armed()
```

The request appears in the Inbox. A signed-in person reads what can move and the
limits, then arms it. No API key can. The same code then runs on the arm, in
teach mode, until the minutes run out. With a leader arm added
(`--leader-type so101_leader` and its port, id and calibration folder),
`arm.teleop()` hands the arm to the leader, which takes over from any code or
policy.

### Deploy a policy

A LeRobot policy on the hub is a folder: the policy beside its manifest. It is
trained on the hub or imported from Hugging Face. Deploy it like any policy,
with **Deploy to robot** on its page or a request from your AI. After a person
approves it, the driver hands it to the bridge:

```bash
lucen-device serve --once
```

The bridge checks the approval and the folder's digest again. Then it runs that
policy and no other code, in teach mode, recorded, until the approval ends.

## Adapters

Three adapters ship with the driver. `virtual` is a clock at the policy's
control rate, with no physics, inference or motors. So you can rehearse the
whole path without hardware. `command` runs the start / stop commands you
configure. `lerobot` hands a LeRobot policy to the bridge, as above.
**Everything else is a plugin**, and yours is about 140 lines.

Nothing on this page has been run on a physical robot. The example drives a
fake one, and says so in every heartbeat.

The pairing lines take an adapter too: `lucen-device pair` accepts the same
`--adapter` and `--adapter-config` as the `register` command shown below. Use
`register` when you set up the robot's computer by hand with an API key.

## Install the driver with an adapter

An adapter is an ordinary Python distribution that registers a class in the
entry-point group `lucen_device.adapters`. It must be installed **into the
environment `lucen-device` runs from**. With `uv tool`, that is one more
`--with`:

```bash
uv tool install --with ./examples/device-adapter lucen-device
```

`lucen-device` comes from PyPI; `./examples/device-adapter` (in the source
repository) is the example this page is about. Copy the directory, rename it, and replace
its `FakeRobot` with your robot's SDK.

```bash
lucen-device adapters
```

lists every adapter an installed distribution registers, with the distribution
and version it comes from. **It imports nothing.** It reads package metadata
only, because importing an adapter runs its code.

```bash
lucen-device adapters --check example-arm
```

imports that one adapter, reports which protocols its class implements, and
probes its fail-safe (below). Use it while you write yours.

## Loading an adapter is code execution

An adapter runs on the robot's computer, as the user the driver runs as, every
time an approval arrives. The driver keeps that deliberate:

- **You name the adapter at `register`, and the driver loads that one and no
  other.** Listing never imports. `serve` loads exactly what `device.json`
  records.
- **A name must resolve to one place.** If two installed distributions register
  the same adapter name, including `virtual`, the driver refuses and names
  both.
- **What was registered is what runs.** `serve` refuses (`adapter_changed`)
  when the registered name now resolves to a different distribution or class. A
  new *version* of the same distribution is allowed, and reported.
- **The trail says what ran.** The distribution, version and class go into the
  robot's `adapter_info` on the hub and into every signed `started` event.

The hub cannot see your code, so it does not vouch for it. It holds the
person's approval, the digest, the contract fingerprint, and your robot's own
statement of what it loaded.

## The protocols

No import from `lucen_device` is needed; the methods are duck-typed. Implement
what your robot can do.

| Protocol | Methods | What it is for |
|---|---|---|
| `policy` | `start(model_path, manifest, seconds)`, `stop()`, `status()`, `is_running()` | Run an approved ONNX **on the robot**. This is the reflex layer, which runs only on the robot. |
| `chunks` | `begin(contract, seconds)`, `observe()`, `apply(chunk, received_at=…)`, `hold(reason)`, `resume()`, `stop()`, `status()`, `is_running()` | Consume a hub-served vision-policy endpoint: observations out, action chunks in. |
| `install` (optional) | `install(model_path, manifest, slot, approval)` | Put an approved file into one of the robot's skill slots. Return `{"sha256", "slot", "activated"}`. Raise to refuse. |
| `staged_start` (optional) | `stage(name, interface)` | Called with `"safe"` and then `"default_pose"` before `start` / `begin`: reach a safe state, move to the robot card's default pose, only then the policy. |
| `safe_stop` (optional) | `set_safe_stop(mode)` | The robot card's `interface.safety.safe_stop` (`hold`, `zero_torque`, `default_pose`, `stand` or `damp_then_off`) decides what your `hold()` and `stop()` must mean. `stand` brings a legged robot back to standing, balanced. `damp_then_off` damps the joints briefly, then turns the motors off: the robot falls, and a person catches it. Raise if you cannot do it, and nothing starts. |

The constructor receives one dict: your own settings (`lucen-device register
--adapter-config FILE.json`, kept in `device.json`, **never sent to the hub**),
plus `approval_id` and the robot card's `interface`. When an approval hands your
adapter a file, the dict also carries facts from the signed approval, such as
`mode`, `slot` and `sha256`. A key the driver adds replaces your setting of the
same name.

Anything your adapter raises before motion starts is a clean refusal
(`adapter_refused`) on the hub's trail, followed by `stop()`.

## The fail-safe is not optional

A robot may use a hub endpoint only if its adapter implements `chunks` **and**
its `hold()` works. The driver establishes that itself, from your code:

1. the class has every method of the chunk protocol, as a real callable;
2. on a **fresh instance**, `hold("probe")` and then `stop()` return without
   raising, within five seconds.

So **`hold()` and `stop()` must be safe to call in any state, even before
`begin()` or twice in a row, and must not raise.** They are the two calls the
driver makes when everything else has already gone wrong. The probe runs at
`register` and again before every endpoint session. An adapter whose `hold()`
is missing, raises, or hangs registers as **not endpoint-capable**, and the hub
refuses to put an endpoint approval in front of a human for it.

While a session runs, the driver's loop enforces the rest on the robot's own
clock. A chunk that arrives after its deadline is thrown away. Two missed
deadlines or a lost link call `hold()` and post `degraded`. Chunks flowing
again call `resume()` and post `recovered`. If `hold()` raises or does
not return in one second at that moment, the driver calls `stop()` instead,
says `hold_failed` in the trail, and ends the run rather than reconnect onto a
robot it cannot hold. A loop that stops making progress for ten seconds, such
as an `apply()` that never returns, gets `hold()` then `stop()` from a watchdog
thread. That watchdog is a coarse backstop in a Python process, not a real-time
guarantee: **your robot's own controller must also stop when commands stop
arriving.**

## Register

The policy a robot accepts is read from the ONNX's own stamped contract. So
download one the robot runs, such as `lucen/laika-stand-l4-v1b1700`, Lucen Biped's
standing policy, and point `--accept-from` at it:

```bash
lucen pull lucen/laika-stand-l4-v1b1700 ./stand
```

One computer holds one robot's registration. If this one already holds
another, such as `lab-laika` from [the MCP guide](/docs/mcp), give this robot
a folder of its own first. Every `lucen-device` command in this shell then
uses it:

```bash
export LUCEN_DEVICE_CONFIG_DIR=~/.config/lucen-device-lab-arm
```

```bash
lucen-device register --robot lucen/laika --name lab-arm --adapter example-arm --accept-from ./stand/stand_l4_v1b1700.onnx
```

`register` loads the adapter, says on stderr that it is third-party code and
where it came from, probes the fail-safe, pins the hub's signing key and the
robot card's embodiment interface, and sends the hub the adapter's name, its
provenance and the declared capability. An unknown name fails here, before
anything is sent, and lists what *is* installed.

A class nobody packaged can be named directly instead:

```text
--adapter my_robot.adapter:MyArm          an importable module and a class
--adapter ./my_arm.py:MyArm               a file and a class
```

Such a class sets its own `name` attribute, the identifier the hub records
(lower-case letters, digits, `.`, `_`, `-`; up to 64 characters). It may not
use an installed adapter's name. A file adapter's "version" on the
trail is the sha256 of the file.

```bash
lucen-device serve --once
```

handles the approvals a person has signed for this robot, and exits. Nothing runs
until a signed-in person approves it, with **Deploy to robot** on the policy's
page or a request in the Inbox. No API key can.

## The robot card's interface, on the robot

When the robot repo's card declares an embodiment interface, the driver pins it
and its fingerprint at `register` (like the signing key: refreshed only on
purpose, with `lucen-device interface --refresh`) and uses it three ways:

- **`safety.safe_stop`** is passed to `set_safe_stop()`. If the card asks for
  something other than `hold` and the adapter has no `set_safe_stop()`, nothing
  starts. A fail-safe that ignored the robot's declared safe stop would not be
  one.
- **`default_pose`** reaches `stage("default_pose", interface)`.
- **A policy stamped with an `embodiment_fingerprint`** that differs from the
  pinned one is refused (`embodiment_mismatch`). Most policies carry no such
  stamp yet; the `started` event then records `embodiment_check:
  policy_unstamped`. The check is recorded as *not made*, rather than assumed
  to have passed.

## The `command` adapter

For a robot that already has a start script, `--adapter command` runs it. The
commands come from **the robot's own computer**: `--adapter-config`, or the
`LUCEN_DEVICE_START_CMD` / `LUCEN_DEVICE_STOP_CMD` / `LUCEN_DEVICE_STATUS_CMD`
environment variables. They never come from the hub.

```json
{
  "start_cmd": "/opt/robot/run_policy.sh {model_path} --seconds {seconds} --safe-stop {safe_stop}",
  "stop_cmd": "/opt/robot/stop.sh {safe_stop}",
  "status_cmd": ["/opt/robot/status.sh"]
}
```

| Placeholder | Expands to |
|---|---|
| `{model_path}` | the verified ONNX |
| `{manifest_path}` | its `lucen_manifest`, as JSON, written beside it |
| `{interface_path}` | the robot card's interface as JSON; empty when the card has none |
| `{seconds}` | the remaining approved window, whole seconds |
| `{approval_id}` | the approval this run belongs to |
| `{safe_stop}` | `hold`, `zero_torque`, `default_pose`, `stand` or `damp_then_off` from the robot card |

A string runs under `sh -c`; a JSON list runs as is. A literal brace is `{{` or
`}}`, and an unknown placeholder is refused before anything starts.

**Stopping**, when the window ends or the hub refuses `started`: `stop_cmd`
runs first (30 s cap; its exit code is kept in the status), then the start
command's **whole process group** gets `SIGTERM`. It runs in its own session,
so a runner forked by `sh -c` is reached too. After 10 s it gets `SIGKILL`.
`command` has no chunk interface, so a robot driving it cannot use a hub
endpoint. `lucen-legged` is the old name for the same adapter and keeps working.
