FixTheRobot Explore Robotics / Robot Learning / M9 / M9-L3

This lab works on phones, but writing code is much easier on a laptop.

Robot Learning · Learning for Manipulation and Locomotion · M9-L3 · Intermediate

Wrap a Policy in a Safety Controller

Experiment log · Scout at Meridian General Hospital

Experiment log, Meridian General Hospital, east corridor. The learned navigation policy is good: on a quiet night Scout reaches the goal every time. But about once in twenty steps it emits a full-speed command straight at the pillar, holds it for three steps, and recovers as if nothing happened. In the replay of last week's trial, 11 of 24 runs ended in a crash. Nobody can retrain the policy before the demonstration on Friday.

You are asked for the classical answer: do not touch the learned policy, wrap it. First a speed limit that keeps the direction of a command, then a monitor that predicts where the robot would be one step later and vetoes a command that ends inside the pillar's collision circle, then a fallback that backs away while still heading for the goal. The simulator runs 24 episodes with and without your wrapper and reports crashes, goal rate and the extra steps the safety costs.

Your task

    Simulation
    solution.pyLoading Python…
    Ctrl+Enter runs
    What Python can I use?

    Plain Python with math and heapq. Your code runs in a background worker: an endless loop is stopped after a few seconds and the page stays usable. It runs in your browser and is never sent anywhere.

    Hints

    What this lab teaches

    Stage 1 writes the speed limit: scale a command so its Euclidean length is at most a maximum, keeping its direction (clipping each component separately changes the direction of a diagonal command). Stage 2 writes the monitor: predict the next position from the command and test it against the collision radius plus a safety margin. Stage 3 assembles the pieces into safe_action: limit the command, ask the monitor, and either pass the command through or replace it with a fallback that moves radially away from the pillar while keeping the goal direction.

    Grading is result-based, on glitch rates and start positions you do not see. The wrapped policy must almost never crash, must still reach the goal in nearly every episode, must respect the speed limit at every step, and must not take much longer than the reference wrapper. A wrapper that refuses to move is perfectly safe and perfectly useless, so the goal rate matters as much as the crash count.

    The last question is about what a wrapper like this does and does not promise (lesson M9-T4).

    Theory for this lab

    Learning for Manipulation and Locomotion labs

    1. M9-L1: Predict Grasp Success
    2. M9-L2: Shape a Gait Reward
    3. M9-L3: Wrap a Policy in a Safety Controller: this lab.

    Educational simplification: a point robot on a flat floor with one round pillar, a monitor whose one-step prediction is exactly the simulator's own motion model (a real robot has latency, inertia and sensor error, so its prediction is only approximate), a glitch that is a clean three-step command at the pillar, and a wrapper that knows only about this pillar. It is a toy model of a safety layer, not a certified safety controller.

    Get the next lab

    Get notified when new robotics labs are released. One email per new lab, nothing else.