Autonomy: rangefinders, maps, avoidance and precision landing · 6 MIN READ

Reactive avoidance: corridors, slow-down zones and stop-and-sidestep

Check the corridor a drone sweeps instead of a single ray, limit speed in proportion to the free distance with a gain that is provably safe for a measured reaction time and braking, and get past a building with stop-and-sidestep, without a map.

Reactive avoidance answers one question many times a second: given what the sensors see now, how fast may I fly in this direction? It needs no map and no plan, which makes it the last line of defence under every planner and the whole of the avoidance in many simple systems. Its failures are all about time and distance: a threshold that ignores speed, a ray that ignores the vehicle's width, a decision that comes one sensor period too late.

drones · A MAP OF THE IDEA

  1. Scan at a fixed rate
  2. Free distance in corridor
  3. Speed from free distance
  4. Sidestep when blocked

Useful when

  • Needs no map and reacts to obstacles nobody charted.
  • The speed limit is provably safe for a measured reaction and braking.

Watch out

  • Sees only the present: dead ends and long detours are not foreseen.
  • Reaction time and braking must be measured, not guessed.

Implementation & operation cost: A 360-degree range sensor at 10 Hz or more, a corridor check and speed limit per scan, and measured braking and latency figures.

01

Stopping distance, now against obstacles

The geofencing lesson predicted a breach with the stopping distance v t_r + v squared / (2a). An obstacle is a fence that the sensor redraws ten times a second, and the same formula decides everything here. The reaction time t_r adds the sensor period, the processing, the velocity loop's response and the command latency; a is the braking you measured, reduced for tailwind and payload. With 0.3 s and 5 m/s2, 8 m/s needs 8.8 m. PX4's collision prevention states the goal in these words: the velocity is restricted so that the vehicle stops in time to keep the distance CP_DIST, and its CP_DELAY parameter is the sensor plus velocity-tracking delay, typically 0.1 to 0.5 s for tracking and up to 0.2 s more for external vision. The sensor's range then caps the speed: if it sees 5 m and you must stop 3 m short, 0.3 v + v squared / 10 must stay under 2 m, so no more than 3.2 m/s whatever the airframe can do.

02

Corridors and a slow-down zone

A single ray along the direction of travel misses the tree that will clip the right-hand propeller. Check the corridor the vehicle sweeps instead: every scan point ahead whose sideways offset from the line of travel is smaller than the half-width (contact radius plus a margin for position and tracking error) limits the free distance to its distance along the line. Then let the free distance set the speed: v = min(vmax, k (free - d_stop)). That zone is safe if at every speed the stopping distance fits into what is left, k t_r + k squared x / (2a) <= 1 for x = free - d_stop, and because k x never exceeds vmax this holds whenever k <= 1 / (t_r + vmax / (2a)): 0.91 per second here. The zone then begins exactly the stopping distance at vmax before d_stop. The square-root profile v = a (sqrt(t_r squared + 2x / a) - t_r) is the fastest safe one; the proportional zone is simpler, slower near obstacles, and approaches the stop exponentially with time constant 1/k.

03

A naive stop, then corridor and sidestep

The program prints the stopping distances for the design values and measures how far the simulator's airframe really brakes from each speed. A naive run then flies at 8 m/s towards a goal 75 m east, behind a warehouse 16 m across, reading one ray straight ahead at 5 Hz and stopping when it reads under 3 m. The second run scans 360 beams at 10 Hz, flies the corridor speed limit towards the goal and, when the limit falls under 0.5 m/s with the goal still beyond the obstacle, compares the free distance to the left and right, sidesteps at up to 2 m/s towards the side with more room, and turns back to the goal only when the corridor to it is clear for the whole sensor range. A pole north of the line makes the choice matter. A truth check records the closest approach to any surface.

go
package main

import (
	"fmt"
	"log"
	"math"
	"strings"

	"gopherforge.app/drone"
)

var (
	world = []drone.Obstacle{
		{Name: "warehouse", X: 40, Radius: 8, Height: 20},
		{Name: "pole", X: 28, Y: 9, Radius: 1.5, Height: 15},
	}
	goal = drone.Vec3{X: 75, Z: 10}
)

const (
	react  = 0.3 // s: sensor period, processing and command latency
	brake  = 5.0 // m/s2: the braking we design for, below what the airframe can do
	vmax   = 8.0 // m/s cruise
	dStop  = 3.0 // m: where we want to come to rest in front of an obstacle
	halfW  = 1.5 // m: half-width of the corridor the drone sweeps (0.3 contact + margin)
	sensor = 0.1 // s: one 360-beam scan every 0.1 s (10 Hz)
)

// Stopping is the distance covered from seeing an obstacle to standing still.
func Stopping(v float64) float64 { return v*react + v*v/(2*brake) }

// Free is how far the drone can fly along direction u (a unit vector) before
// a scan point enters its corridor: points ahead, within halfW of the line.
func Free(scan []float64, u drone.Vec3) float64 {
	free := drone.RangeMax
	for i, r := range scan {
		a := 2 * math.Pi * float64(i) / float64(len(scan))
		p := drone.Vec3{X: r * math.Cos(a), Y: r * math.Sin(a)}
		along, across := p.X*u.X+p.Y*u.Y, math.Abs(p.X*u.Y-p.Y*u.X)
		if r < drone.RangeMax && along > 0 && across < halfW {
			free = math.Min(free, along)
		}
	}
	return free
}

// k keeps the proportional slow-down zone safe: at vmax the drone must still
// stop within the zone, so k <= 1/(react + vmax/(2 brake)).
var k = 1 / (react + vmax/(2*brake))

// Speed is the slow-down zone: proportional to the free distance left.
func Speed(free float64) float64 { return math.Max(0, math.Min(vmax, k*(free-dStop))) }

func unit(v drone.Vec3) drone.Vec3 { return v.Horizontal().Scale(1 / v.Horizontal().Len()) }
func left(u drone.Vec3) drone.Vec3 { return drone.Vec3{X: -u.Y, Y: u.X} }

// clearance is a truth check: distance from the drone to the nearest surface.
func clearance(p drone.Vec3) float64 {
	c := math.Inf(1)
	for _, o := range world {
		c = math.Min(c, math.Hypot(p.X-o.X, p.Y-o.Y)-o.Radius)
	}
	return c
}

func newSim() (*drone.Sim, *drone.Drone) {
	sim := drone.New(drone.Config{Wind: drone.Vec3{X: 1, Y: 2}, Gust: 1, Obstacles: world, Seed: 21})
	d := sim.Drone()
	if err := d.Arm(); err != nil {
		log.Fatal(err)
	}
	d.TakeOff(10)
	sim.RunUntil(30, func(float64) bool { return d.Arrived() })
	sim.Waypoint(goal) // drawn on the replay's map
	return sim, d
}

func hit(sim *drone.Sim) string {
	for _, e := range sim.Events() {
		if strings.HasPrefix(e.Text, "hit ") {
			return fmt.Sprintf("t=%.1f s %s", e.T, e.Text)
		}
	}
	return "no collision"
}

func main() {
	fmt.Printf("stopping distance with %.1f s reaction and %.0f m/s2 braking:\n", react, brake)
	fmt.Println("  v m/s  reaction  braking  total   simulator braking")
	for _, v := range []float64{2, 4, 6, 8, 10} {
		sim := drone.New(drone.Config{Seed: 1})
		d := sim.Drone()
		d.Arm()
		d.TakeOff(10)
		sim.RunUntil(30, func(float64) bool { return d.Arrived() })
		d.SetVelocity(drone.Vec3{X: v}, 0)
		sim.Run(6, nil)
		x := d.State().Pos.X
		d.SetVelocity(drone.Vec3{}, 0)
		sim.RunUntil(10, func(float64) bool { return d.State().Vel.Len() < 0.05 })
		fmt.Printf("  %5.0f  %6.1f m  %5.1f m  %4.1f m  %5.1f m\n", v, v*react, v*v/(2*brake), Stopping(v), d.State().Pos.X-x)
	}

	fmt.Println("naive: 8 m/s at the goal, stop when the ray ahead reads under 3 m (5 Hz)")
	sim, d := newSim()
	d.SetVelocity(drone.Vec3{X: vmax}, 0)
	stopped := false
	sim.RunUntil(20, func(t float64) bool {
		if !stopped && math.Mod(math.Round(t/sim.Dt()), 10) == 0 { // every 0.2 s
			if r := d.Range(0); r < 3 {
				stopped = true
				fmt.Printf("  t=%.1f s x=%.1f m  ahead %.1f m at %.1f m/s: stop\n", t, d.State().Pos.X, r, d.State().Vel.Len())
				d.SetVelocity(drone.Vec3{}, 0)
			}
		}
		return d.State().Mode == drone.Crashed
	})
	sim.Run(2, nil) // the crashed drone falls
	fmt.Printf("  %s\n", hit(sim))
	naive := sim

	fmt.Printf("avoid: corridor %.1f m each side, speed min(%.0f, %.2f (free - %.0f m)), scans at 10 Hz\n", halfW, vmax, k, dStop)
	sim, d = newSim()
	mode, side, minC, path, last := "goal", drone.Vec3{}, math.Inf(1), 0.0, d.State().Pos
	sim.RunUntil(90, func(t float64) bool {
		st := d.State()
		minC, path, last = math.Min(minC, clearance(st.Pos)), path+st.Pos.Sub(last).Horizontal().Len(), st.Pos
		if math.Mod(math.Round(t/sim.Dt()), 5) != 0 {
			return false
		}
		scan, toGoal := d.Scan(360), goal.Sub(st.Pos).Horizontal()
		u := unit(toGoal)
		free := Free(scan, u)
		if math.Mod(math.Round(t/sim.Dt()), 100) == 0 {
			fmt.Printf("  t=%4.1f s %v %-8s free %4.1f m  speed %3.1f m/s\n", t, st.Pos, mode, free, st.Vel.Horizontal().Len())
		}
		switch mode {
		case "goal":
			if Speed(free) < 0.5 && free < toGoal.Len() {
				n, s := Free(scan, left(u)), Free(scan, left(u).Scale(-1))
				mode, side = "sidestep", left(u)
				name := "north"
				if s > n {
					side, name = side.Scale(-1), "south"
				}
				fmt.Printf("  t=%4.1f s %v blocked: free %.1f m; north %.1f m, south %.1f m: sidestep %s\n", t, st.Pos, free, n, s, name)
			}
		case "sidestep":
			if free >= math.Min(toGoal.Len(), drone.RangeMax) {
				mode = "goal"
				fmt.Printf("  t=%4.1f s %v corridor to the goal clear for %.1f m: go\n", t, st.Pos, free)
			}
		}
		if mode == "goal" {
			d.SetVelocity(u.Scale(math.Min(Speed(free), toGoal.Len())), 0)
		} else {
			d.SetVelocity(side.Scale(math.Min(2, Speed(Free(scan, side)))), 0)
		}
		return toGoal.Len() < 3 || st.Mode == drone.Crashed
	})
	d.GoTo(goal) // the last metres: let the position loop stop on the goal
	sim.RunUntil(10, func(float64) bool {
		path, last = path+d.State().Pos.Sub(last).Horizontal().Len(), d.State().Pos
		return d.Arrived()
	})
	fmt.Printf("  %s; t=%.1f s at %v, closest surface %.1f m, path %.1f m (straight %.0f m)\n",
		hit(sim), sim.Time(), d.State().Pos, minC, path, goal.Horizontal().Len())
	d.Land()
	sim.RunUntil(30, func(float64) bool { return d.State().Landed })
	if err := naive.SaveAs("naive"); err != nil {
		log.Fatal(err)
	}
	if err := sim.Save(); err != nil {
		log.Fatal(err)
	}
}
Read the code

The design figures are deliberately pessimistic: at 8 m/s they give 8.8 m (2.4 m reaction, 6.4 m braking) while the simulator brakes in 4.7 m, at 10 m/s 13.0 against 6.9 m. The naive run sees 2.0 m ahead at t=9.0 s while still doing 8.1 m/s; braking needs 4.7 m, and the next line is 't=9.2 s hit warehouse'. At 8 m/s a 5 Hz sensor moves 1.6 m between readings, so the reading that trips a 3 m threshold can be anywhere from 1.4 to 3 m. The avoiding run cruises at 8.1 m/s with 10.6 m free at t=8.0 s, slows inside the zone and is blocked at t=9.9 s at x=28.4 with 3.5 m free: 7.4 m to the north, where the pole stands, 40.0 m to the south, so it sidesteps south at about 2 m/s. At t=16.7 s, 13.5 m south of the line, the corridor is clear for 40.0 m and it flies the diagonal at 8 m/s, stopping on the goal at (74.6, -0.2) at t=24.5 s with 92.1 m of path for 75 m straight, never closer than 2.3 m to a surface.

04

Stop, slide or plan

Stop-and-sidestep is the simplest way past an obstacle, and real autopilots offer similar behaviours in pilot-assisted modes. ArduPilot's simple avoidance in Loiter stops or slides along the obstacle (AVOID_BEHAVE), keeps AVOID_MARGIN and backs away at AVOID_BACKUP_SPD if something comes closer. PX4's collision prevention in Position mode limits the velocity and may turn the commanded direction by up to CP_GUIDE_ANG towards a freer sector; by default it refuses to move towards directions without sensor data. What no reactive method can do is see beyond the present scan: a sidestep can lead into a courtyard with no exit, the side with more room now may be the longer way round, and nothing guarantees arrival. That is why autonomous missions put a planner on top, the A* of the path planning lesson or ArduPilot's BendyRuler and Dijkstra in Auto and Guided, and keep the reactive limit underneath as the layer that never trusts the plan.

05

Fly it in GopherForge

In GopherForge choose New project > Robotics and simulation > Drone flight, replace main.go with this program, keep mission.go and tap Run. Output lists naive.gfflight, where the drone reaches the warehouse wall at full speed and falls, and flight.gfflight, with the slow-down, the stop, the sidestep south past the pole and the diagonal to the goal marker. Set react to 0.6 or brake to 3 and watch the zone start earlier, or move the pole south of the line and see the sidestep switch sides.

TAKE THIS WITH YOU

A fixed range threshold is right at one speed only, because braking grows with the square of speed. Check the corridor the vehicle sweeps, limit speed in proportion to the free distance with k no larger than 1 / (t_r + vmax / 2a), let the sensor range cap vmax, and when blocked, sidestep towards the side with more room until the way to the goal is clear.

Check your understanding

A drone stops whenever its forward ray reads under 3 m. It works at 2 m/s but hits a wall at 8 m/s. What should limit its speed instead?

Go to the source

Progress stays in this browser. No account needed.