---
name: so100-sim2real
description: End-to-end autonomous sim2real for the SO100 arm (ManiSkill
  SO100GraspCube-v1 + LeRobot) — calibrate the sim camera to the real camera using
  the arm as a calibration rig, train RGB PPO policies yourself in simulation,
  transfer zero-shot to the real arm, then diagnose and repair the residual gap in
  reality. Use when asked to learn/train a policy in sim and deploy it on the real
  SO100, to do sim2real transfer, to align the sim camera to the real camera
  automatically, or to debug why a sim policy fails on the real arm. Trigger
  keywords - sim2real, so100, train policy in simulation, transfer to real,
  zero-shot, camera calibration, arm-rig, silhouette IoU, grasp cube, localization
  vs actuation, repair policy, eval_real_probe, train_ppo_rgb.
---

# SO100 sim2real — learn in sim, transfer, repair in reality

Project: `/home/thomas/src/lerobot-sim2real`. Sim = ManiSkill `SO100GraspCube-v1`;
real = LeRobot `so100_follower` (`/dev/ttyACM1`, OpenCV cam idx 6, 640x480).
Run everything in the project venv: `uv --project . run python -u <script>` (the
agent kernel lacks mani_skill/torch).

**Companion skill:** `so100-robot-control` covers low-level control — homing,
full motor-state reads, the sim<->real joint offset (`QPOS_OFFSET`), load-tracked
motion, wrist-overload diagnosis, camera back-solve. Read it for any real-arm
motion. This skill is the *policy lifecycle* on top of it.

**Repo references:** `docs/sim2real_playbook.md` (the validated method, with
figures) and `outputs/cam_align_v2/README.md` (calibration code + data).

## The one insight that organizes everything

**Camera pose is the dominant RGB sim2real gap.** Get the view right and the
policy's *spatial* behavior transfers zero-shot; get it wrong and it reaches into
empty space. Once the view is right, split any remaining failure into:

- **Localization gap** — gripper misses the cube / hits the tray / self-collides.
  → fix the CAMERA, not the policy.
- **Actuation gap** — gripper navigates TO the cube but doesn't secure it (wrong
  gripper timing/force, jaws close on air). → repair the GRASP, not the camera.

Validated result: with the arm-rig calibration below, a sim-trained policy drives
the real gripper straight to the cube (localization solved); the open problem is
grasp actuation. Do not re-solve the camera for an actuation problem.

## Golden rules

- **Do it yourself; never hand commands back.** Launch training/eval with
  `proc("start", cmd)`, poll the log file (`open(log).read()`), extract frames and
  look. "Not done until it works in reality."
- **Never block the kernel.** `proc("log", pid=)` BLOCKS until exit — read the log
  file directly instead. Long jobs = `proc("start")` + poll.
- **Verify, don't assume.** Read motor state + a camera frame after any motion;
  read back the actual camera extrinsic after setting it; confirm sim render ==
  real overlay. "Torque on" ≠ "moved"; "IoU up" ≠ "camera right" (see fov trap).
- **One bus owner.** The control server (`/tmp/vlm_control.py`) and
  `eval_real_probe.py`/`home_robot.py` all open `/dev/ttyACM1`. Exit the server
  before running a probe; never run two bus clients at once.
- **Match configs.** Eval a policy with the SAME env config (camera+bg+spawn) it
  was trained with. Mismatched bg/camera silently breaks transfer.

## Stage 1 — Calibrate the camera (arm as rig)

Automated, replaces subjective manual overlay. Detail: `references/calibration.md`.
Summary: command ~25 diverse real poses (gripper open), record ACTUAL qpos (raised
poses sag), SAM2 real arm masks, render sim silhouettes (`cam_sim.py:SimCam`),
CMA-ES fit a shared camera (pos/target/roll) to maximize mean IoU. **Pin fov**
(free-fov IoU is degenerate). Apply `outputs/cam_align_v2/env_roll.patch` to the
env so roll is honored (SAPIEN optical axis = local +X). Verify the patched env
reproduces `cam_sim` (mask IoU 1.0, RGB diff 0.0). Write the result to
`env_config.json` `base_camera_settings` (incl. `roll`).

## Stage 2 — Consistent background

The greenscreen bg is a fixed 2D image and MUST match the current camera
viewpoint, or training is unlearnable (success=0 past 11M). Capture robot-free
bg from the real cam (`explore_hide_arm.py`, swing arm out via shoulder_pan=-85),
inpaint the cube, center-crop → `bg_real.png`; set `greenscreen_overlay_path`.
Verify sim-on-bg aligns tray/table/floor/base with the real frame.

## Stage 3 — Train the policy yourself in sim

`train_ppo_rgb.py`. Recipe + dynamics: `references/training.md`. Key facts:
phase transition ~9–10M steps (budget ≥16M); return ≈16–22 = reach-only, 30–45+ =
grasping; spawn shifts cause an exploration bottleneck (prefer warm-start
fine-tune over from-scratch when adapting); 8GB GPU → `--ppo.num-envs 256
--ppo.num-eval-envs 4`, `--ppo.num-eval-steps 64`. Gate: sim `success_once` ≥ 0.6
on a DR-off eval with the training config before deploying.

## Stage 4 — Transfer (state-aware real probe)

Reset arm to rest (`rest_qpos=[0,0,0,pi/2,pi/2,0]` sim convention; use the control
server `qpos` action or `home_robot.py`). Exit the control server, then:
```bash
uv --project . run python -u lerobot_sim2real/scripts/eval_real_probe.py \
  --checkpoint runs/<run>/ckpt_N.pt --env-kwargs-json-path <training-config>.json \
  --num-episodes 3 --max-episode-steps 80 --control-freq 15 \
  --record-dir outputs/real_probe/<run>
```
Logs full motor state every step + TCP + action + real video; load-tracked reset;
safe torque-off exit.

## Stage 5 — Diagnose localization vs actuation

From the probe CSV: per-episode TCP xyz range, gripper-action (a5) range, min
TCP-to-cube distance; extract frames (`video-frames` skill) and LOOK. Gripper ends
at the cube but no lift = actuation gap (camera is right). Gripper misses the cube
= localization gap (re-check camera). A wrist_flex overload mid-episode is a
symptom of the policy misbehaving on the view, not a hardware blocker.

## Stage 6 — Repair, cheapest first

1. Verify the view (Stage 1) before touching the policy.
2. Light fine-tune on the consistent config (warm start transfers grasp).
3. If localization is good but grasp fails: gripper close timing/force, or a small
   real corrective/residual policy. Do NOT re-solve the camera.
4. Re-probe → re-diagnose → repeat until the cube is lifted on video.

## File map

- Calibration: `outputs/cam_align_v2/` (`cam_sim.py`, `optimize_cam3.py`,
  `grid_data.json`, `solved_camera3.json`, `env_roll.patch`).
- Config: `env_config.json` (camera+roll), `bg_real.png`.
- Scripts: `lerobot_sim2real/scripts/{train_ppo_rgb,eval_real_probe,home_robot,explore_hide_arm}.py`.
- Joint offset: `lerobot_sim2real/utils/calibration.py`.
- Artifacts: `outputs/real_probe/<run>/` (video + CSV + snapshots), `runs/<run>/`.
