# Arm-rig camera calibration (silhouette IoU, fov-fixed CMA-ES)

Calibrate the shared sim base camera to the real camera using the SO100 arm as a
known-geometry target. Code + data: `outputs/cam_align_v2/` (repo). Verified to
make a sim-trained policy localize the real cube.

## Why not free-fov silhouette IoU
Silhouette IoU has a **fov degeneracy**: a farther camera with a wider fov can
match the bulky base silhouette while misprojecting the gripper TCP. With fov
free, CMA-ES drifts fov 0.84→1.07 and IoU *increases* but the camera is wrong (TCP
lands off the real gripper). **Pin fov** to a independent calibration (0.837596
here, = fovy giving fx=fy≈539.17 at 640x480, cx=320, cy=240). Optimize only
pos / target / roll.

## Why not hand-marked TCP points
TCP = the **midpoint of the jaw tips** (the gap between open jaws, ~53 px apart).
Marking a single jaw tip biases points ~25 px; a point-lstsq solve then fits the
bias and degrades the silhouette. The silhouette fit uses the whole arm mask and
avoids this. (The original camera's TCP projection was already correct — verified
by zooming sim-vs-real gripper: the projected TCP dot lands in the real jaw gap.)

## Procedure
1. **Pose grid.** Command ~25 diverse real poses, gripper open, TCP spread
   u∈[91,362], v∈[58,268], z∈[0.048,0.168]. Use the control server `qpos` action
   (load-tracked). Record the **actual** `qpos` from motor state (raised poses sag
   0.15–0.26 rad in lift — never use the commanded target). Save frame + qpos per
   pose → `grid_data.json`, `frames/poseNN.png`.
2. **Real masks.** SAM2 segment the arm → `masks/`. Gripper seg ids `[5,6,7,8,9]`
   (`grip_ids.json`).
3. **Sim render.** `cam_sim.py:SimCam` — one CPU env
   (`obs_mode="rgb+segmentation"`, cpu sim+render, 640x480). `set_camera(pos,
   target, fov, roll)` sets the mount pose via `look_at` + roll about the optical
   axis (local **+X**), then reads back the ACTUAL extrinsic/intrinsic from
   `sensor.get_params()` so `project()` always matches the render. `masks(qpos,
   ...)` returns (robot_mask, grip_mask).
4. **Optimize.** `optimize_cam3.py`: CMA-ES over x=[px,py,pz,az,el,roll], fov
   fixed, loss = 1 − mean IoU (gripper-weighted variant wIoU). Multi-stage
   320x240 → 640x480. Output `solved_camera3.json`.

## Roll + the env patch
The fit finds roll ≈ −1.6° (−0.0285 rad); including it adds +0.016 IoU (without it
IoU drops below the original). The stock env ignores roll, so patch
`.venv/.../mani_skill/envs/tasks/digital_twins/so100_arm/grasp_cube.py`,
`sample_camera_poses`, DR=False branch:
```python
pose = sapien_utils.look_at(eye=bcs["pos"], target=bcs["target"])
roll = bcs.get("roll", 0.0)
if roll:
    pose = pose * sapien.Pose([0,0,0], [np.cos(roll/2), np.sin(roll/2), 0, 0])
return pose
```
Patch file: `outputs/cam_align_v2/env_roll.patch` (site-packages edit, not in git).
`roll=0` → unchanged. SAPIEN camera optical axis = local +X (NOT Z; rolling about
Z tilts the view).

## Verify (do not skip)
Build the env with the final `base_camera_settings` (DR=False), reset, and compare
the env's AUTO camera placement to `cam_sim`'s manual `look_at_roll` at a grid
qpos: mount pose diff, extrinsic diff, robot-mask IoU, RGB mean-abs-diff. PASS =
diff 0.0, IoU 1.0, RGB diff 0.0. Also confirm roll is applied (rolled-vs-unrolled
extrinsic differs). Save a side-by-side render.

## Result (this rig)
pos=[-0.039150,-0.188601,0.418751], target=[0.251192,-0.076094,0.027543],
fov=0.837596, roll=-0.0285. meanIoU 0.8274→0.8356 (wIoU 0.8444). TCP shifts ~5.9 px
mean (max 9.8) from the verified-correct original → stays at jaw-gap center.
Written to `env_config.json`. Real eval: gripper localizes the cube (localization
transfers); grasp actuation is the residual gap.

## Reproduce
```bash
uv --project . run python -u outputs/cam_align_v2/optimize_cam3.py   # fit
uv --project . run python -u outputs/cam_align_v2/validate_cam.py    # IoU + TCP
```
`grid_data.json` + the real robot regenerate `frames/`/`masks/` (gitignored bulk).
