Obstacle avoidance
UAT 366 Indoor Autonomous and Multi-Unmanned Aircraft Systems
Lesson
By the end of this module you will be able to
- Explain potential fields and the local-minimum problem
- Choose a flight heading with the vector field histogram (VFH) from LiDAR ranges
- Explain PX4 Collision Prevention and its behaviour when range data stops
- Compare reactive avoidance with planned paths, and the idea of velocity obstacles
- Plan a shortest path on an occupancy grid with A* and explain why the heuristic must not overestimate
Why this matters
Path planning on a known map, taught with A* later in this module, finds a route before take-off. In a real warehouse, though, a box may be left in the aisle or a pallet may stick out, and none of that is on the map. The drone must react to what its sensors see right now. Reactive methods are fast, but they have weaknesses you need to know. Without that knowledge, a drone may freeze in the middle of an aisle or oscillate between racks.
Potential fields
Khatib (1986) proposed the potential field, in which the goal attracts the robot and obstacles repel it, and the robot moves along the resultant force. The idea is simple and runs in real time, but Koren and Borenstein (1991) analysed its inherent limitations: getting stuck in a local minimum where forces cancel, failing to pass between closely spaced obstacles, and oscillating in narrow passages.
Example 1 Simulating a potential field in front of a pillar
A pillar with a 2.5 m radius of influence stands at (5, 0) and the goal is at (10, 0). Compare starting on the line with starting 0.5 m to the side.
import math
def fly(start, goal=(10, 0), obstacle=(5, 0), k_att=1.0, k_rep=5.0, rho0=2.5, step=0.05):
x, y = start
for i in range(2000):
fx, fy = k_att * (goal[0] - x), k_att * (goal[1] - y)
dx, dy = x - obstacle[0], y - obstacle[1]
d = math.hypot(dx, dy)
if d < rho0:
m = k_rep * (1 / d - 1 / rho0) / d ** 2
fx, fy = fx + m * dx / d, fy + m * dy / d
f = math.hypot(fx, fy)
if f < 1e-3:
return f"stuck at ({x:.2f}, {y:.2f}) after {i} steps"
x, y = x + step * fx / max(f, 1), y + step * fy / max(f, 1)
if math.hypot(goal[0] - x, goal[1] - y) < 0.1:
return f"reached goal after {i} steps"
return "timeout"
print("start on the line:", fly((0, 0)))
print("start 0.5 m to the side:", fly((0, 0.5)))
start on the line: stuck at (4.17, 0.00) after 88 steps
start 0.5 m to the side: reached goal after 233 steps
Starting exactly on the line, the repulsive force points straight back against the attraction, so the drone stops in front of the pillar. A small offset gives a sideways force that carries it around. In practice you need a mechanism that detects being stuck and escapes, for example by switching to a path planner.
Vector field histogram
The VFH of Borenstein and Koren (1991) turns the obstacles around the robot into a polar histogram of obstacle density in each direction, then selects the “valley” below a threshold that is closest to the goal direction. It does not get trapped the way a potential field does, because it decides from the whole surroundings.
Example 2 Choosing a heading from LiDAR ranges
A simplified VFH: 36 sectors of 10° each; any sector with an obstacle closer than 3 m is blocked, and the free sector closest to the goal direction is chosen.
ranges = [6.0] * 36 # m per 10° sector, starting at 0°, clockwise
for k in (34, 35, 0, 1, 2): ranges[k] = 1.2 # box blocking the way ahead
for k in (3, 4): ranges[k] = 2.5
for k in (31, 32, 33): ranges[k] = 2.8
THRESH, GOAL_DEG = 3.0, 0
def angle_diff(a, b):
return abs((a - b + 180) % 360 - 180)
blocked = [k * 10 for k, r in enumerate(ranges) if r < THRESH]
free = [k * 10 for k, r in enumerate(ranges) if r >= THRESH]
best = min(free, key=lambda a: angle_diff(a, GOAL_DEG))
print("blocked headings:", blocked)
print(f"chosen heading {best} deg ({angle_diff(best, GOAL_DEG)} deg from goal)")
blocked headings: [0, 10, 20, 30, 40, 310, 320, 330, 340, 350]
chosen heading 50 deg (50 deg from goal)
A real VFH also checks the valley width so that it does not pick a gap too narrow for the drone, and it adjusts speed to the obstacle density ahead.
Flight-controller systems
PX4 Collision Prevention uses range data from sensors on the flight controller, or from the companion computer via MAVLink. It divides the surroundings into 72 sectors, works in Position mode, and slows and stops before a collision, with the distance set by CP_DIST and the delay by CP_DELAY. If range data stops for 0.5 s, movement is restricted; if it stops for 5 s, the vehicle switches to HOLD. ArduPilot has a similar Object Avoidance system. These systems prevent collisions but do not plan a way around.
For moving objects such as forklifts or other drones, the velocity obstacle of Fiorini and Shiller (1998) finds the set of velocities that would cause a future collision if both kept their current velocity, then chooses a velocity outside that set (extended in Module 4).
Planning a path with A*
Reactive methods look only at what the sensors see now. Path planning looks at the whole known map before take-off and finds a route that reaches the goal at the lowest cost, for example the shortest distance. The two work in layers: the planner draws the route through the racks, and the reactive layer above deals with the box that is not on the map.
Step 1: turn the map into a grid. The warehouse plan (for example the map from SLAM in Module 2) is divided into square cells, each marked free or occupied: an occupancy grid. Occupied cells are then inflated by the drone’s radius plus a safety margin, so the planner can treat the drone as a point.
Step 2: search the grid. Dijkstra’s algorithm expands cells in order of their cost-to-come , the cost of the best route found so far from the start to cell , and so explores in all directions. A* (Hart, Nilsson and Raphael, 1968) adds a heuristic , an estimate of the remaining cost to the goal, and always expands the cell with the smallest
LaValle (2006) describes A* as an extension of Dijkstra’s algorithm that reduces the number of states explored by using an estimate of the cost-to-go. If never overestimates the true remaining cost (an admissible heuristic), A* still finds the lowest-cost route. On a grid where the drone may move in 8 directions, the octile distance is admissible, because it is the length of the route with no obstacles at all.
Step 3: make the route flyable. A grid route has sharp corners. It is smoothed or turned into waypoints, and each segment is checked again for clearance before upload.
In three dimensions, or when the vehicle’s motion limits matter, grids become very large. Sampling-based planners such as the rapidly-exploring random tree (RRT) of LaValle (1998) instead grow a tree of random collision-free points from the start until it reaches the goal. They find a route quickly in large spaces, but the route is not the shortest and must be smoothed.
Example 3. A* in a warehouse aisle blocked by a pallet
A 7 × 4 m section of the warehouse in 0.5 m cells (assumed layout). The racks and a pallet at the end of the lower aisle are already inflated. The drone starts at the left of the cross aisle and must reach the right end.
import heapq
import math
GRID = [ # 1 cell = 0.5 m; # = rack or pallet, inflated by the drone radius
"..............",
".##.##.##.##..",
".##.##.##.##..",
".##.##.##.##..",
".##.##.##.##..",
"..........#...",
".##.##.##.#.#.",
".##.##.##...#.",
]
START, GOAL = (5, 0), (5, 13) # (row, column)
MOVES = [(dr, dc) for dr in (-1, 0, 1) for dc in (-1, 0, 1) if (dr, dc) != (0, 0)]
def free(r, c):
return 0 <= r < len(GRID) and 0 <= c < len(GRID[0]) and GRID[r][c] == "."
def can_move(cell, dr, dc): # a diagonal step must not cut the corner of a rack
if not free(cell[0] + dr, cell[1] + dc):
return False
return not (dr and dc) or (free(cell[0] + dr, cell[1]) and free(cell[0], cell[1] + dc))
def octile(a, b): # never overestimates the 8-connected distance
dr, dc = abs(a[0] - b[0]), abs(a[1] - b[1])
return max(dr, dc) + (math.sqrt(2) - 1) * min(dr, dc)
def plan(use_heuristic=True):
h = octile if use_heuristic else (lambda a, b: 0.0)
g, parent, frontier, expanded = {START: 0.0}, {}, [(h(START, GOAL), START)], 0
while frontier:
_, cell = heapq.heappop(frontier)
expanded += 1
if cell == GOAL:
break
for dr, dc in MOVES:
if not can_move(cell, dr, dc):
continue
nxt = (cell[0] + dr, cell[1] + dc)
cost = g[cell] + math.hypot(dr, dc)
if cost < g.get(nxt, math.inf):
g[nxt], parent[nxt] = cost, cell
heapq.heappush(frontier, (cost + h(nxt, GOAL), nxt))
path = [GOAL]
while path[-1] != START:
path.append(parent[path[-1]])
return path[::-1], g[GOAL], expanded
for name, flag in (("A*", True), ("Dijkstra", False)):
path, length, expanded = plan(flag)
print(f"{name:<8} length {length * 0.5:.2f} m, {len(path)} cells, expanded {expanded} cells")
path = set(plan()[0])
for r, row in enumerate(GRID):
print("".join("o" if (r, c) in path else ch for c, ch in enumerate(row)))
A* length 8.50 m, 18 cells, expanded 32 cells
Dijkstra length 8.50 m, 18 cells, expanded 54 cells
..............
.##.##.##.##..
.##.##.##.##..
.##.##.##.##..
.##.##.##.##..
oooooooooo#ooo
.##.##.##o#o#.
.##.##.##ooo#.
The straight line would be 6.5 m, but the pallet blocks the cross aisle, so the route drops into the lower aisle and comes back up: 8.5 m. Both methods find a route of the same length, but A* expands 32 cells instead of 54 because the heuristic keeps the search heading toward the goal. The saving grows with the size of the map. If the pallet was not on the map, the reactive layer would stop the drone in front of it, and the planner would then be run again with the pallet added.
Module lab
Lab: avoidance in SITL and in the lab
- Experiment with the code from Example 1: change the start position, k_rep and the radius of influence, and find the conditions that trap the drone.
- Record LiDAR ranges in a simulated aisle, use the code from Example 2 to choose a heading, then add a valley-width condition.
- Enable Collision Prevention in PX4 SITL, try two values of
CP_DISTand fly toward a wall in Position mode. - Simulate a range-data dropout and check the behaviour at 0.5 s and 5 s.
- Test for real in the lab with soft obstacles at low speed, with a safety net and a pilot ready to take over.
Common mistakes
Watch out
- No mechanism to escape a local minimum.
- Choosing a gap narrower than the drone.
- Assuming the flight-controller system will plan a detour.
- Planning on a grid without inflating obstacles, or with a heuristic that overestimates.
- Not testing the case where the sensor stops sending data.
- First tests at high speed near hard obstacles.
Summary
- Potential fields are simple but get stuck in local minima, cannot pass narrow gaps and oscillate.
- VFH uses a polar histogram to choose the free valley closest to the goal.
- PX4 Collision Prevention slows and stops before a collision and switches to HOLD after 5 s without range data.
- Reactive avoidance must work alongside path planning, and velocity obstacles for moving objects.
- A* expands the cell with the smallest f = g + h; with an admissible heuristic it finds the shortest route while exploring fewer cells than Dijkstra.
Check your understanding
- What causes a local minimum in a potential field?
- How does VFH choose a heading?
- The free sectors nearest the goal are at 40° and 320°, and the goal is at 10°. Which heading should be chosen?
- On an 8-connected grid, what is the octile distance from cell (0, 0) to cell (3, 5)?
- What does PX4 do if range data stops for 5 s?
Answers
- The attractive and repulsive forces are equal and opposite, so the resultant is zero.
- It picks the valley whose obstacle density is below the threshold and which is closest to the goal direction.
- 40°, which is 30° from the goal; 320° is 50° away.
- cells.
- It switches to HOLD.
Key formulas
| Attractive force toward the goal | |
| Repulsive force from an obstacle (when d < ρ₀) | |
| A* evaluation of a cell |
Key references
- Khatib, O. (1986). Real-time obstacle avoidance for manipulators and mobile robots. The International Journal of Robotics Research, 5(1), 90–98. link
- Koren, Y., & Borenstein, J. (1991). Potential field methods and their inherent limitations for mobile robot navigation. In Proceedings of the 1991 IEEE International Conference on Robotics and Automation (pp. 1398–1404). IEEE. link
- Borenstein, J., & Koren, Y. (1991). The vector field histogram—Fast obstacle avoidance for mobile robots. IEEE Transactions on Robotics and Automation, 7(3), 278–288. link
- Fiorini, P., & Shiller, Z. (1998). Motion planning in dynamic environments using velocity obstacles. The International Journal of Robotics Research, 17(7), 760–772. link
- PX4 Autopilot. Collision prevention. PX4 user guide (main). link
- ArduPilot Dev Team. Object avoidance. ArduPilot Copter documentation. link
- Siegwart, R., Nourbakhsh, I. R., & Scaramuzza, D. (2011). Introduction to autonomous mobile robots (2nd ed.). MIT Press. link
- Hart, P. E., Nilsson, N. J., & Raphael, B. (1968). A formal basis for the heuristic determination of minimum cost paths. IEEE Transactions on Systems Science and Cybernetics, 4(2), 100–107. link
- LaValle, S. M. (2006). Planning algorithms. Cambridge University Press. link
- LaValle, S. M. (1998). Rapidly-exploring random trees: A new tool for path planning (TR 98-11). Iowa State University. link
Further reading
Study the assigned knowledge units in advance, review media and take the module quiz
การวางแผนเส้นทางและหลบหลีกสิ่งกีดขวางอัตโนมัติ
LiDAR เรดาร์ และเซนเซอร์หลบหลีกสิ่งกีดขวาง
In class / field
Intensive lab and field practice recorded in a lab notebook
Learning evidence: Lab notebook signed by the instructor