Files
Lumi/docs/sorting/04_RL.md
T

182 lines
8.5 KiB
Markdown
Raw Normal View History

2026-11-24 18:54:47 +09:00
# 04 Reinforcement learning
The learning problem: from the hand above the plate, pick the given ball and drop it into the given box. One ball
per episode; the robot runs the episode once per ball ([02](02_TASK_DESIGN.md) §2).
---
## 1. Frames and units
Everything the policy sees and outputs is in the **arm base frame**, in metres, with the plate centre as the
origin of positions (positions are given relative to it so a moved table does not shift the inputs). Angles in
radians. Inputs are scaled to roughly −1…1 by fixed constants written next to each term in the code.
---
## 2. Observations
| Term | Size | Student (robot) | Teacher (sim only) |
|---|---|---|---|
| Joint angles | 6 | measured | true |
| Joint speeds | 6 | from the controller, filtered | true |
| Hand position (fitted kinematics) | 3 | from joint angles | true |
| Gripper state: open / closing / closed / opening | 4 (one-hot) | from the commands and AI_1 | true |
| Gripper gap from AI_1, scaled 0…1 | 1 | AI_1 | true |
| Ball position (last reading) | 3 | camera, held when not fresh | true, every step |
| Ball reading age (s) and fresh flag | 2 | from timestamps | 0, 1 |
| Ball relative to hand | 3 | last reading − hand | true |
| Target box position | 3 | layout file | true |
| Box relative to hand | 3 | box − hand | true |
| Previous action | 4 | own output | own output |
| Ball speed | 3 | — | true |
| Contact: ball between the fingers | 1 | — | true |
| Physics parameters this episode (ball mass, friction, stroke time) | ~5 | — | true |
The student's input is exactly what the Thor can compute at run time; nothing else. The extra terms are the
teacher's "privileged" information ([§8](#8-teacher-and-student)).
---
## 3. Actions and control modes
| Output | Range | Meaning |
|---|---|---|
| Δx, Δy, Δz of the hand | −1…1, scaled to ±`STEP_MM` | where the hand should be at the next step, relative to now |
| Gripper | −1…1 | > 0.5 close, < −0.5 open, in between keep (hysteresis so it does not chatter) |
The fingers point down with a fixed yaw. The hand target goes through the fitted inverse kinematics (the same
`robot/kinematics.py` the skills use) and the existing move checks (joint limits, head clearance) before it is sent.
2026-11-24 18:57:14 +09:00
Which arm command carries the target depends on whether the controller accepts servo mode ([05](05_SIM_TO_REAL.md) §1):
2026-11-24 18:54:47 +09:00
| | Mode A: servo | Mode B: short moves |
|---|---|---|
| Needs | controller accepts `servo_j` / `servo_p` | nothing new |
| Command | joint target streamed every 8 ms × step_num, with the vendor's NLF filter | one `moveL` of ≤ `STEP_MM` per policy step, wait until it ends |
| Policy rate | 10–20 Hz | 2–4 Hz |
| `STEP_MM` | 10–15 mm per step | 20–30 mm per step |
| Smoothness | continuous motion | stop-and-go, slower |
| Sim | targets every step, arm model with the measured lag | each step = a full move that settles, measured settle time added |
Mode A is the better result (smooth, closed loop at a real control rate) and is what the portfolio should show.
Mode B works with the controller as it is today; the same environment code supports both through one setting
(`control_mode`), and the policy is trained for the mode the robot has.
---
## 4. Reward
Dense terms guide early learning; the success bonus dominates once it is reached. Weights are starting values,
2026-11-24 18:57:14 +09:00
tuned during training.
2026-11-24 18:54:47 +09:00
| Term | Formula (d = distance in m) | Weight |
|---|---|---|
| Reach | `1 − tanh(d_hand_ball / 0.1)` while the ball is not held | 1.0 |
| Grasp | +1 each step the ball is between the closed fingers | 2.0 |
| Lift | +1 when the held ball is ≥ 30 mm above its start height | 2.0 |
| Carry | `1 − tanh(d_ball_box_xy / 0.15)` while held and lifted | 3.0 |
| Above box | +1 when the held ball is inside the box's opening in xy and above the rim | 2.0 |
| Success | +1 once when the ball comes to rest inside the target box | 50 |
| Drop | −1 once if the ball falls anywhere else | −20 |
| Close on nothing | −1 each close that ends without the ball | −2 |
| Action rate | `‖a_t − a_{t−1}‖²` | −0.01 |
| Joint speed | `‖q̇‖²` | −0.001 |
| Other balls moved | −1 per other ball pushed > 20 mm | −5 |
| Near limits | −1 per step within 10° of a joint limit or 30 mm of the head clearance | −1 |
Terms that only the simulator can compute (ball between fingers, ball at rest) are allowed in the reward: the
reward is never needed on the robot.
---
## 5. Episode start and end
| | Value |
|---|---|
| Start | hand at the start pose ± 20 mm, gripper open, ball anywhere on the plate, 0–3 other balls, box target left or right at random |
| Length | 15 s of robot time (mode A at 15 Hz: 225 steps; mode B at 3 Hz: 45 steps) |
| Ends early | success, drop, wrong box, hand outside the workspace box, joint limit hit, any part closer to the head than the clearance |
---
## 6. Curriculum
| Stage | Starts when | Change |
|---|---|---|
| 1 Reach and grasp | — | ball at the plate centre, no other balls, low noise; success = lifted 30 mm |
| 2 Full place | stage 1 success ≥ 80 % | full episode, ball anywhere on the plate |
| 3 Clutter | stage 2 success ≥ 80 % | 0–3 other balls |
| 4 Full randomisation | stage 3 success ≥ 80 % | all ranges in §7 at full width, sensor model on |
Stage changes are automatic (Isaac Lab curriculum terms) and logged.
---
## 7. Domain randomisation
| Parameter | Range | Reason |
|---|---|---|
| Ball diameter / mass / friction / bounce | ±1.5 mm / ±20 % / 0.6–1.2 / 0.1–0.5 | balls differ; rubber ages |
| Table height, plate and box positions | ±10 mm, ±30 mm, ±15 mm | placement by hand |
| Arm lag and gains | measured value ±30 % | controller and load |
| Command delay | 0–60 ms (mode A) / 0–200 ms (mode B) | network and the controller's connection drops |
| Gripper stroke time | measured ±20 % | |
| Ball position bias / noise / delay / dropouts | ≤ 10 mm / σ 3–5 mm / 0.1–0.3 s / 5 % | camera ([03](03_SIMULATION.md) §4) |
| Observation noise on joints | σ 0.002 rad | |
| Action noise | σ 5 % of the step | |
| Gravity direction | ±1° | table and robot not level |
Each range is set from a measurement, not chosen freely; the source of each is written in the config.
---
## 8. Teacher and student
1. **Teacher:** trained with PPO on the full observation including the privileged terms, every source of noise
off at first and on in stage 4. It learns the skill fast because it sees everything.
2. **Student:** same action space, observations limited to what the robot has (§2), trained by imitating the
teacher (DAgger: the student drives, the teacher labels every visited state) and then fine-tuned with PPO.
The student has a small memory (a GRU or the last 4 observations stacked) because the ball position goes stale
while the hand covers it.
3. Only the student goes to the robot.
If the teacher-student step proves unnecessary (a policy trained directly on the student observations reaches the
same success), that result is reported: it is a valid finding, not a failure.
---
## 9. Algorithm and training settings
| Setting | Start value |
|---|---|
| Library | RSL-RL (PPO), as in Isaac Lab's manipulation examples; skrl as an alternative |
| Network | actor and critic MLP 256-128-64, ELU; student adds GRU 128 |
| Parallel scenes | 4096 (fewer if memory needs) |
| Steps per scene per update | 24 |
| PPO | lr 1e-3 adaptive (KL 0.01), γ 0.99, λ 0.95, clip 0.2, entropy 0.005, 5 epochs, 4 mini-batches |
| Seeds | at least 3 per final configuration; the table reports mean and spread |
| Export | student to ONNX (opset fixed in the export script), with the input scaling inside the graph |
### Later extensions (not version 1)
- **Vision policy:** student input is the camera image (or a depth crop around the plate) instead of the ball
position; needs rendered training images and image randomisation.
- **Box finding:** boxes detected by the camera instead of the layout file.
- **Free yaw:** turn the hand for objects that are not round.
- **Other gripper:** a gripper with position control turns the gripper output into a continuous width.
---
## 10. Evaluation in simulation
Every saved policy is evaluated with fixed seeds on:
| Set | Content |
|---|---|
| Nominal | measured values, no randomisation, 1000 episodes |
| Randomised | full §7 ranges, 4096 episodes |
| Stress | ranges at 1.5×, 1000 episodes (how close to the edge it is) |
| MuJoCo | the same student in the MuJoCo twin, 1000 episodes |
Reported: success rate, drop rate, wrong-box rate, mean time to success, mean joint-speed and action-rate cost.