Srijan Guchhait

A rule-based safety layer around a learned drone policy

Shadow mode, a small mode machine with hysteresis, and collision-driven probing: wrapping a PPO obstacle-avoidance policy without retraining it, in simulation.

  • robotics
  • reinforcement-learning
  • safety

I trained a PPO policy to steer a small multirotor around obstacles using depth data. Then I moved it from a synthetic environment into a physics simulator. The policy works in many runs. A learned policy gives no guarantee in the run where it matters. I did not want to retrain it each time it failed in a new way, so I put a small rule-based controller around it. Everything in this post happens in simulation.

The policy contract

The policy is a plain actor-critic network with two hidden layers of 128 tanh units. Its interface is small and fixed.

  • The observation has 39 numbers. Thirty-two are normalized depth rays, ordered from right to left. The other seven are the goal direction, the goal distance, the forward speed, the yaw rate and the previous two actions.
  • The action has two numbers between -1 and 1: forward speed and yaw.

The simulator's depth camera has 256 by 144 pixels and a 66 degree field of view. The adapter clips depth at 8 meters, the range the policy saw in training. I kept both dimensions identical to the original environment. That let me load the original checkpoint unchanged, and I never overwrite it.

Validate the transfer first

Before I wrote any safety logic, I wrote offline validators for the parts that fail silently. They check depth conversion, observation bounds, feature order, action bounds, the yaw sign and the reward components. Each one prints PASS or FAIL.

Two checks saved me time. The simulator reports depth along the optical axis. The policy expects horizontal range, so the adapter divides each value by the cosine of the ray angle. Without that step, the policy sees walls that are too far away. The ray order also matters: ray 0 is the right side. If you assume the opposite, the drone turns into the obstacle.

The controller never owns the vehicle

The hybrid controller takes the policy action, the 32 depth rays and a dictionary of vehicle state. It returns a proposed action and some diagnostics. It never holds a simulator client, so it cannot send a command. I can test it without the simulator.

Two rules limit it. It may lower the forward speed but never raise it above the policy command, and it never commands reverse. It changes yaw only in the two hazard modes, and only when a safe side exists.

Five modes

  • Normal: the policy action passes through unchanged.
  • Caution: forward speed falls linearly as the closest forward depth nears the emergency distance.
  • Avoidance: speed drops and a committed yaw turns the drone toward the safer side.
  • Emergency: forward speed goes to zero. The controller keeps a committed turn if one is safe.
  • Recovery: after clearance returns, the policy takes over again.

The defaults are modest. The safety distance is 2.0 meters and the emergency distance is 0.8 meters. Caution caps the speed at 1.5 m/s, and avoidance uses 0.8 m/s.

Hysteresis stops the mode from flickering. Once the controller enters Emergency, it stays there until the minimum depth passes 1.2 meters. The hazard modes last until the depth passes 2.5 meters. After the controller picks a direction, it holds that direction for one second, so the drone does not swing between left and right.

Shadow mode first

The evaluator has two flags. In shadow mode, the controller computes and records its proposal, and the policy action still flies the vehicle. This showed me when the controller would have stepped in, and why, without changing the behavior I measured. Control mode sends the proposal to the simulator. It is a separate flag, and the two flags exclude each other. The documentation says not to point control mode at hardware.

Every run writes a CSV. Each row holds the policy action, the proposed action and the command that was sent. It also holds the mode, the reason for any change, the minimum depth and the time spent in each mode. I can answer "why did it turn there" from the log alone.

Probing

The second version adds collision-driven probing. When the vehicle is blocked, it stops or nearly stops and makes one small yaw to one side. Then it compares the forward clearance with the clearance before the turn. It commits to the turn only if the clearance grew. It tries the opposite side once. If both sides fail, it stays stopped in a bounded recovery.

A collision resets the probe state and blocks the direction that just failed. The controller then waits for a fresh depth frame before it moves again. This stops it from acting on a stale image after a bump.

Near the goal, a terminal controller slows the drone, limits the speed by heading error and stops inside the goal tolerance.

Why a wrapper

Retraining changes the whole policy at once and says little about one failure. A wrapper changes only what I can read. I turn it off with a flag, and every decision leaves a row in a log. The cost is that the wrapper encodes my assumptions about geometry, such as which side is safe. The shadow logs show me when those assumptions are wrong.

← All posts