Added sorting RL and sim to real
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
# 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.
|
||||
|
||||
Which arm command carries the target depends on what phase 1 finds:
|
||||
|
||||
| | 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,
|
||||
tuned in phase 4 and written in the run record.
|
||||
|
||||
| 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.
|
||||
@@ -0,0 +1,101 @@
|
||||
# 05 Sim to real
|
||||
|
||||
How the policy gets from the simulator onto the robot and how the gap is measured and closed.
|
||||
|
||||
---
|
||||
|
||||
## 1. System identification (before training)
|
||||
|
||||
Each test is done with the owner at the robot, arm in free space above the table, at the trial speed. The data
|
||||
goes into `rl/sorting/sysid/` and the fitted numbers into the simulation config.
|
||||
|
||||
| Test | How | Gives |
|
||||
|---|---|---|
|
||||
| Controller version and servo | read the version in Cobo π; try `servo_move_enable` and a 5 s small sine on J6 (±5°) over the JSON port with STOP ready | mode A or B ([04](04_RL.md) §3) |
|
||||
| Step response, mode A | joint targets in small steps (2°, 5°) on J1, J2, J3 at 10 and 20 Hz; record joint angles at the highest rate the port gives | lag, overshoot, settle time per joint |
|
||||
| Step response, mode B | `moveL` of 10, 20, 30 mm in x, y, z, sent back to back with a 0.2 s gap; record start delay and end time | time per step, which sets the policy rate |
|
||||
| Connection drops | log every retry during the tests | how often a step is delayed; becomes the delay range |
|
||||
| Gripper | 20 opens and 20 closes on nothing and on a ball: AI_1 trace | stroke time, AI_1 on a held ball |
|
||||
| Ball | drop from 10 cm, film at 60 fps; roll down a 5° board | bounce, rolling friction |
|
||||
| Camera on balls | ball at 5 marked plate positions, 20 readings each, ruler truth | bias and noise; detection rate; colour reading |
|
||||
|
||||
---
|
||||
|
||||
## 2. Sim-to-sim check (MuJoCo)
|
||||
|
||||
The trained student runs in the MuJoCo twin with the same observation code. A large drop in success from Isaac Lab
|
||||
to MuJoCo means the policy uses something one engine does that the other does not (usually contact details).
|
||||
That is fixed in simulation (more randomisation, gentler grasp reward) before the robot sees the policy.
|
||||
|
||||
Gate: MuJoCo success ≥ 85 % before the first real trial.
|
||||
|
||||
---
|
||||
|
||||
## 3. Running the policy on the Thor
|
||||
|
||||
| Part | Choice |
|
||||
|---|---|
|
||||
| Runtime | onnxruntime on the CPU (the policy is a small MLP or GRU: well under 1 ms); no GPU needed, so it does not compete with the LLM and whisper |
|
||||
| Loop | one thread in the skill: read state → build observation → policy → checks → command; fixed period with timing logged |
|
||||
| Observation code | one Python module shared word for word with the simulation's student observation (`rl/sorting/obs.py` copied to `real/thor/policies/sort/obs.py` by the export script); a test checks both give the same vector for recorded states |
|
||||
| Arm command | through `robot/motion.py` (mode B) or a new small servo module (mode A) that keeps the existing checks: joint limits, head clearance, workspace box |
|
||||
| Gripper | `robot/gripper.py` open / close, never both outputs on |
|
||||
| Logging | every step: time, observation, action, command sent, controller reply time; one file per episode in `logs/sort/` |
|
||||
|
||||
The policy file, its config (control mode, rates, scaling) and the hash of the training run go together in
|
||||
`policies/sort/`; the skill refuses to run a policy whose config does not match the robot's mode.
|
||||
|
||||
---
|
||||
|
||||
## 4. Safety envelope
|
||||
|
||||
The policy only proposes. Every step passes these before anything moves:
|
||||
|
||||
| Check | Limit |
|
||||
|---|---|
|
||||
| Workspace box | hand within the plate-and-boxes box: x, y from the layout ± 60 mm, z from table + 15 mm to table + 250 mm |
|
||||
| Step size | ≤ `STEP_MM` |
|
||||
| Joint limits | code limits (5° margin) |
|
||||
| Head and column clearance | `HEAD_CLEAR_MM`, `LINK_HEAD_CLEAR_MM` as in the skills |
|
||||
| Speed | trial speed cap, set low for the first trials (≤ 50 mm/s linear, as 11_SAFETY.md says for a start) |
|
||||
| Controller silent | `BLIND_S` 3 s → stop |
|
||||
| Episode time | 15 s → stop, open, back to `look` |
|
||||
| STOP | app STOP, `/stop`, `/arm/stop`, `/tasks/stop` end the loop within one step and stop the arm |
|
||||
|
||||
A refused step ends the episode as "unsafe" and is logged; the run does not continue around it.
|
||||
|
||||
Before the first real trial the collision sensitivity should be raised from the loose defaults (ROADMAP item,
|
||||
10_GRIPPER.md:119): owner's decision.
|
||||
|
||||
---
|
||||
|
||||
## 5. Real trials
|
||||
|
||||
| Step | Setup | Pass to go on |
|
||||
|---|---|---|
|
||||
| 1 Dry run | policy runs with the gripper open and no ball; the hand follows a ball position typed in | hand stays in the workspace box, motion smooth, STOP works |
|
||||
| 2 One ball, centre | one red ball at the plate centre, 10 trials | ≥ 7 of 10 in the box |
|
||||
| 3 One ball, anywhere | 50 trials, ball at marked plate positions, both colours | ≥ 80 % |
|
||||
| 4 Full plate | 4 balls, 2 colours, 20 plates | ≥ 70 % fully sorted, none dropped outside |
|
||||
| 5 Conditions | step 3 again with blinds open / closed and a moved table (± 2 cm) | within 10 points of step 3 |
|
||||
|
||||
Each trial is filmed from a fixed phone position and logged ([08](08_EXPERIMENT_LOG.md)). Every failure gets a
|
||||
cause from a fixed list: missed grasp, slipped, dropped in carry, hit box rim, wrong box, perception (no or wrong
|
||||
reading), unsafe stop, timeout, other.
|
||||
|
||||
---
|
||||
|
||||
## 6. Closing the gap
|
||||
|
||||
When real success is clearly below MuJoCo success, the failure causes decide the fix:
|
||||
|
||||
| Most failures are | Look at | Usual fix |
|
||||
|---|---|---|
|
||||
| Missed grasp | camera bias near the failures; finger gap | widen ball position bias range; re-check the round-object correction |
|
||||
| Slipped / dropped | finger friction and force | add friction lower range; grasp lower on the ball |
|
||||
| Hit box rim | box positions vs layout file | re-measure layout; widen box position randomisation |
|
||||
| Jerky or late motion | step timing logs | refit lag and delay in sim; lower the policy rate |
|
||||
| Unsafe stops | which check | tighten the reward's near-limit term; move the start pose |
|
||||
|
||||
Real-robot fine-tuning (learning on the robot itself) is not in the plan: it is slow and needs resets by hand. If
|
||||
it is tried, it is a separate, documented experiment.
|
||||
Reference in New Issue
Block a user