Autonomy: rangefinders, maps, avoidance and precision landing · 6 MIN READ
Potential fields: attraction, repulsion from a scan, and the local-minimum trap
Build an artificial potential field from a goal and rangefinder readings, fly its negative gradient as a velocity command, watch it stall inside a U-shaped obstacle, detect the stall, and escape by wall following, with an honest account of what fields cannot guarantee.
A potential field treats the goal as a valley and obstacles as hills and lets the vehicle roll downhill: the velocity command is the negative gradient of attraction plus repulsion. It takes a few dozen lines, reacts to every scan and draws smooth curves around scattered obstacles. It also has a famous flaw: wherever the pushes and the pull cancel, the vehicle stops, and a U-shaped building between it and the goal can hold it there indefinitely.
drones · A MAP OF THE IDEA
- Pull towards the goal
- Push from scan points
- Fly the summed gradient
- Escape on no progress
Useful when
- A few lines per scan, no map, smooth paths around scattered obstacles.
- Any number of obstacles combine in one sum.
Watch out
- Concave obstacles create minima that stop the vehicle with no error.
- Gains depend on speed, braking and beam count; narrow gaps oscillate.
Implementation & operation cost: A scan at 10 Hz, a sum over beams per cycle, a progress monitor and an escape behaviour, tuned per airframe and speed.
Attraction, repulsion, gradient
The attractive potential is quadratic near the goal, so the pull grows with the distance, and conic beyond a radius, so the pull stays at the cruise speed: here 1 m/s per metre up to 5 m/s. Khatib's repulsive potential (1986) for an obstacle at distance rho inside an influence distance rho0 is (eta/2)(1/rho - 1/rho0) squared; its negative gradient pushes straight away from the obstacle with strength eta (1/rho - 1/rho0) / rho squared, zero at rho0 and unbounded at the surface. A scan gives no obstacle list, only points, so every beam that reads under rho0 adds a push away from its point. The sum, clamped to the cruise speed, becomes the velocity setpoint, the way a companion computer streams setpoints to the autopilot in the cascaded control lesson. The reactive avoidance lesson limited speed along one direction; a field chooses the direction as well.
Weighting the beams and tuning
Summing over beams makes the push depend on how many beams hit: a wall that 30 beams see pushes 30 times harder than a pole one beam sees, and doubling the resolution doubles every push. Weight each beam by its angular width, 2 pi / n, so the sum approximates an integral over the field of view, or keep only the nearest point of each cluster. Then tune the repulsion against the cruise speed and the braking. The program uses eta = 300 and rho0 = 8 m at 5 m/s. With eta = 40 the push is too weak to keep a margin and the drone passes within 0.9 m of a trunk and 0.7 m of a wall, close to the 0.3 m contact distance; with eta = 1000 it backs off in front of a tree near the line to the goal and makes no progress for seconds. Koren and Borenstein's 1991 analysis lists the classic failures: traps at local minima, no passage between closely spaced obstacles, and oscillation near obstacles and in narrow passages.
A field, a trap and an escape
Three flights at 10 m use 72-beam scans at 10 Hz. The first crosses four trees with tree 3 exactly on the straight line to the goal and prints a trace every 3 s. The second flies into a U of overlapping 1.5 m cylinders that opens towards the start, with the goal 30 m behind its bottom wall. The program watches progress: if the distance to the goal has not dropped by 0.5 m in 4 s, it prints the attraction, the repulsion and their sum at that moment. Without an escape it keeps flying the field for 20 s more. The third flight escapes by wall following: it keeps the nearest scan point on its right at 3 m while moving at 2.5 m/s, and returns to the field once the goal is closer than where it got stuck and the line to it is clear. A truth check records the closest approach to any surface.
package main
import (
"fmt"
"log"
"math"
"gopherforge.app/drone"
)
const (
beams = 72
ka = 1.0 // 1/s: attraction, m/s per metre to the goal
vmax = 5.0 // m/s: the attraction saturates here (conic far away)
d0 = 8.0 // m: obstacles further than this do not push
kr = 300.0 // repulsion gain
dw = 3.0 // m: wall-following standoff
)
var goal = drone.Vec3{X: 70, Z: 10}
func point(r float64, i int) drone.Vec3 {
a := 2 * math.Pi * float64(i) / beams
return drone.Vec3{X: r * math.Cos(a), Y: r * math.Sin(a)}
}
func clampLen(v drone.Vec3, max float64) drone.Vec3 {
if l := v.Len(); l > max {
return v.Scale(max / l)
}
return v
}
// Attract is minus the gradient of a potential that is quadratic near the
// goal and conic (constant slope) beyond vmax/ka metres.
func Attract(toGoal drone.Vec3) drone.Vec3 { return clampLen(toGoal.Horizontal().Scale(ka), vmax) }
// Repel sums Khatib's repulsion over the scan points within d0. Each beam is
// weighted by its angle, so a wall does not push harder because more beams
// hit it than hit a pole.
func Repel(scan []float64) drone.Vec3 {
var f drone.Vec3
for i, r := range scan {
if r < d0 {
p := point(r, i)
f = f.Sub(p.Scale(kr * (1/r - 1/d0) / (r * r * r) * 2 * math.Pi / beams))
}
}
return f
}
// Follow keeps the nearest scan point on the right at dw metres.
func Follow(scan []float64) drone.Vec3 {
near := 0
for i := range scan {
if scan[i] < scan[near] {
near = i
}
}
n := point(1, near) // unit vector to the wall
t := drone.Vec3{X: -n.Y, Y: n.X}
return t.Scale(2.5).Add(n.Scale(math.Max(-2, math.Min(2, 1.5*(scan[near]-dw)))))
}
// Free reports whether the straight line to the goal is clear within range.
func Free(scan []float64, toGoal drone.Vec3) bool {
u := toGoal.Horizontal().Scale(1 / toGoal.Horizontal().Len())
for i, r := range scan {
p := point(r, i)
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 && along < toGoal.Len() && across < 2 {
return false
}
}
return true
}
func clearance(world []drone.Obstacle, 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
}
// Fly runs the field at 10 Hz. Without escape it reports a stall and keeps
// flying the field for 20 s; with escape it follows the wall when progress
// stops, and returns to the field once the goal is closer and in clear view.
func Fly(label string, world []drone.Obstacle, escape, trace bool) *drone.Sim {
sim := drone.New(drone.Config{Wind: drone.Vec3{X: -1, Y: 1}, Gust: 0.5, Obstacles: world, Seed: 8})
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)
fmt.Println(label)
start := sim.Time()
mode, best, bestAt, stuckAt, minC, next := "field", math.Inf(1), start, 0.0, math.Inf(1), start
sim.RunUntil(150, func(t float64) bool {
st := d.State()
toGoal := goal.Sub(st.Pos).Horizontal()
minC = math.Min(minC, clearance(world, st.Pos))
if toGoal.Len() < 1 || st.Mode == drone.Crashed {
return true
}
if math.Mod(math.Round(t/sim.Dt()), 5) != 0 {
return false
}
scan := d.Scan(beams)
if toGoal.Len() < best-0.5 {
best, bestAt = toGoal.Len(), t
if mode == "stuck" {
fmt.Printf(" t=%4.1f s %v moving towards the goal again\n", t-start, st.Pos)
mode = "field"
}
}
att, rep := Attract(toGoal), Repel(scan)
if trace && t >= next {
next += 3
fmt.Printf(" t=%4.1f s %v attraction %.1f, repulsion %.1f, speed %.1f m/s\n",
t-start, st.Pos, att.Len(), rep.Len(), st.Vel.Horizontal().Len())
}
switch {
case mode == "stuck" && t-bestAt > 24:
return true
case mode == "field" && t-bestAt > 4:
fmt.Printf(" t=%4.1f s %v no progress for 4 s, %.1f m from the goal: attraction %.2f, repulsion %.2f, sum %.2f m/s\n",
t-start, st.Pos, toGoal.Len(), att.Len(), rep.Len(), att.Add(rep).Len())
mode, stuckAt = "stuck", toGoal.Len()
if escape {
mode = "follow"
}
case mode == "follow" && toGoal.Len() < stuckAt-1 && Free(scan, toGoal):
fmt.Printf(" t=%4.1f s %v closer than %.1f m and clear: back to the field\n", t-start, st.Pos, stuckAt)
mode, best, bestAt = "field", toGoal.Len(), t
}
if mode == "follow" {
d.SetVelocity(Follow(scan), 0)
} else {
d.SetVelocity(clampLen(att.Add(rep), vmax), 0)
}
return false
})
if goal.Sub(d.State().Pos).Horizontal().Len() < 1 {
d.Land()
sim.RunUntil(30, func(float64) bool { return d.State().Landed })
}
st := d.State()
fmt.Printf(" end at t=%.1f s %v, %.1f m from the goal, closest surface %.1f m\n",
sim.Time()-start, st.Pos, goal.Sub(st.Pos).Horizontal().Len(), minC)
return sim
}
func main() {
scattered := []drone.Obstacle{
{Name: "tree 1", X: 18, Y: 5, Radius: 2, Height: 20},
{Name: "tree 2", X: 30, Y: -4, Radius: 2.5, Height: 20},
{Name: "tree 3", X: 45, Radius: 2, Height: 20},
{Name: "tree 4", X: 58, Y: 4, Radius: 1.5, Height: 20},
}
var cup []drone.Obstacle // a U open to the west: bottom at x=40, arms along y=+-10
for y := -10.0; y <= 10; y += 2.5 {
cup = append(cup, drone.Obstacle{Name: fmt.Sprintf("wall %.0f,%.0f", 40.0, y), X: 40, Y: y, Radius: 1.5, Height: 20})
}
for x := 25.0; x < 40; x += 2.5 {
cup = append(cup, drone.Obstacle{Name: fmt.Sprintf("wall %.0f,10", x), X: x, Y: 10, Radius: 1.5, Height: 20},
drone.Obstacle{Name: fmt.Sprintf("wall %.0f,-10", x), X: x, Y: -10, Radius: 1.5, Height: 20})
}
a := Fly("four trees, tree 3 on the straight line to the goal:", scattered, false, true)
b := Fly("a U open towards the start, field only:", cup, false, false)
c := Fly("the same U, wall following when stuck:", cup, true, false)
if err := a.SaveAs("field"); err != nil {
log.Fatal(err)
}
if err := b.SaveAs("trapped"); err != nil {
log.Fatal(err)
}
if err := c.Save(); err != nil {
log.Fatal(err)
}
}In the trees the drone cruises at 5.0 m/s; beside tree 2 at (26.1, -1.1) the repulsion reaches 11.9, more than twice the attraction, slows it to 2.3 m/s and pushes it north. That detour means tree 3, on the straight line, is passed on its north side at (46.8, 6.1) at full speed. Tree 4, ahead and slightly to the left, slows it to 1.7 m/s at (53.1, 2.6) before it slips past; it lands 0.9 m from the goal, never closer than 1.6 m to a surface. In the U the forces cancel at (35.4, 0.0): attraction 5.00, repulsion 5.10, sum 0.10 m/s, and 20 s later the drone is still at (35.3, 0.0), 34.7 m from the goal, with no error anywhere. With the escape it follows the wall north around the arm, and at t=30.0 s at (39.7, 14.3) the goal is closer than 34.6 m and in clear view; the field takes it to the goal, where it lands 0.9 m off with 2.3 m of clearance throughout.
Local minima, and getting out
A local minimum is any point away from the goal where the gradient vanishes; a concave side facing the goal creates one, and an obstacle exactly between vehicle and goal creates a saddle on its axis that only noise or wind breaks. You cannot tune it away. A random walk, a few seconds in a direction drawn from a seeded generator before trying the field again, is simple and often enough in open ground, but it wanders and gives no bound on time in a deep trap. Wall following, as here, borrows the boundary-following and leave rules of the Bug family of algorithms; their versions with strict leave rules are proven to reach a reachable goal with ideal sensing and localisation, and this program's version, with a noisy scan and a lagging velocity loop, has no such proof. Navigation functions and harmonic potentials have no minima except the goal but need the map in advance. In practice a field is a local layer under a planner such as the A* of the path planning lesson, with a progress monitor and an escape.
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 field.gfflight with the curve through the trees, trapped.gfflight with the drone hovering inside the U until the recording ends, and flight.gfflight with the wall-following detour around the north arm and the landing at the goal marker. Set kr to 40 and the closest approach drops to under a metre; set it to 1000 and tree 4 stops the first flight for several seconds.
TAKE THIS WITH YOU
A potential field adds a pull towards the goal to pushes away from every scan point inside an influence distance and flies the negative gradient as a velocity command. Weight beams by their angle, tune the repulsion against speed and braking, watch progress, and plan an escape for the local minima that concave obstacles always create.
Check your understanding
A drone flying a potential field stops inside a U-shaped building that lies between it and the goal, and reports no fault. Why?
Go to the source
Progress stays in this browser. No account needed.