Route and waypoint planning
UAT 304 UAS Mission Planning and Autonomous Flight
Lesson
By the end of this module you will be able to
- Order inspection points with the nearest-neighbour method and improve the order with 2-opt
- Split a route into sorties within the battery limit
- Set waypoint altitude and speed to suit the task
- Check the route against restricted areas and obstacles before upload
- Use terrain elevation data (DEM) to check ground clearance and set up terrain following
- Set ArduPilot geofences between the route and the edge of the permitted area
Why this matters
Mapping uses back-and-forth survey lanes. Another common kind of job is point inspection, such as checking poles, water tanks or several flood-risk points across an area. The order in which the points are visited sets the total distance, time and number of sorties. The drone knowledge base’s unit on survey flight planning links GSD, image overlap, flight lines and terrain, and its QGroundControl unit shows how to place waypoints and export a mission. The course case is a municipal operations centre using a multirotor to inspect its area. The numbers are hypothetical.
Ordering the inspection points
The problem “visit every point and return home by the shortest route” is the travelling salesman problem, which is hard to solve exactly when there are many points. In practice, heuristics are used. Nearest neighbour repeatedly chooses the closest unvisited point; it is fast but often leaves crossing lines. 2-opt (Croes, 1958) tries reversing a section of the route and keeps the change if the route gets shorter, repeating until no reversal helps.
Example 1 Ordering 10 inspection points
Coordinates are metres east and north of the take-off point H.
import math
pts = {"H": (0, 0), "P1": (420, 150), "P2": (900, -200), "P3": (1300, 400), "P4": (700, 800),
"P5": (250, 1100), "P6": (-300, 900), "P7": (-600, 300), "P8": (-400, -500),
"P9": (300, -700), "P10": (1100, -900)}
def dist(a, b):
return math.dist(pts[a], pts[b])
def length(route):
return sum(dist(route[i], route[i + 1]) for i in range(len(route) - 1))
left, route = [k for k in pts if k != "H"], ["H"]
while left: # nearest neighbour
nxt = min(left, key=lambda k: dist(route[-1], k))
route.append(nxt)
left.remove(nxt)
route.append("H")
print(f"nearest neighbour: {length(route):.0f} m {'-'.join(route)}")
improved = True
while improved: # 2-opt
improved = False
for i in range(1, len(route) - 2):
for j in range(i + 1, len(route) - 1):
cand = route[:i] + route[i:j + 1][::-1] + route[j + 1:]
if length(cand) < length(route) - 1e-9:
route, improved = cand, True
print(f"after 2-opt: {length(route):.0f} m {'-'.join(route)}")
nearest neighbour: 8078 m H-P1-P2-P3-P4-P5-P6-P7-P8-P9-P10-H
after 2-opt: 7916 m H-P1-P2-P10-P3-P4-P5-P6-P7-P8-P9-H
2-opt moves P10 to follow P2, which is close to it, instead of leaving it as the last point with a long flight home. The total distance falls only slightly in this case, but with more or more scattered points the difference is usually larger. Neither method guarantees the best answer, but both are fast enough for field work.
Splitting into sorties
One battery gives limited flight. When the route is too long, it must be split into sorties, each starting and ending at the take-off point. The maximum distance per sortie must already allow for RTL and wind reserves.
Example 2 Splitting the route by distance per sortie
The drone can fly at most 3,500 m per sortie after reserves. Use the order from Example 1 and add points in order until the limit would be exceeded.
import math
pts = {"H": (0, 0), "P1": (420, 150), "P2": (900, -200), "P3": (1300, 400), "P4": (700, 800),
"P5": (250, 1100), "P6": (-300, 900), "P7": (-600, 300), "P8": (-400, -500),
"P9": (300, -700), "P10": (1100, -900)}
order = ["P1", "P2", "P10", "P3", "P4", "P5", "P6", "P7", "P8", "P9"] # from Example 1
L_MAX = 3500 # m per sortie
def length(route):
return sum(math.dist(pts[route[i]], pts[route[i + 1]]) for i in range(len(route) - 1))
sorties, cur = [], ["H"]
for p in order:
if length(cur + [p, "H"]) <= L_MAX:
cur.append(p)
else:
sorties.append(cur + ["H"])
cur = ["H", p]
sorties.append(cur + ["H"])
for k, s in enumerate(sorties, 1):
print(f"sortie {k}: {'-'.join(s):<20} {length(s):5.0f} m")
print(f"{len(sorties)} sorties, total {sum(length(s) for s in sorties):.0f} m")
sortie 1: H-P1-P2-P10-H 3189 m
sortie 2: H-P3-P4-H 3144 m
sortie 3: H-P5-P6-P7-H 3055 m
sortie 4: H-P8-P9-H 2130 m
4 sorties, total 11518 m
Splitting increases the total distance because the drone must return to the take-off point several times. This in-order split is simple but not optimal. To seriously reduce the number of sorties or the total distance, use vehicle routing methods, which consider the split and the order together.
Setting waypoints
- Altitude must state whether it is relative to the take-off point, to sea level, or to the ground below the drone (terrain following). On sloping ground, altitude relative to take-off can bring the drone too close to the ground or obstacles
- Speed in ArduPilot Copter 4.6 is set with
WPNAV_SPEEDin cm/s, but version 4.7 renames itWP_SPDin m/s, and it can be changed during a mission with the DO_CHANGE_SPEED command. Choose speed to suit the task: stills at an inspection point may need a hover, while flight between points uses an energy-efficient speed - Actions at a point, such as turning the camera, taking a photo or waiting, need time added to the flight-time estimate
- Check before upload with a program as in UAT 301 Module 3: altitude, boundaries and the final command
Using terrain data
A digital elevation model (DEM) is a grid of ground heights above sea level, for example one value every 30 m. Two kinds must be kept apart: a DTM gives the bare ground only, while a DSM also includes buildings, trees and structures. The Copernicus DEM data page states that this dataset is a DSM including buildings, infrastructure and vegetation, available globally at 30 m (GLO-30) and 90 m (GLO-90), with an absolute vertical accuracy better than 4 m at 90%. The grid spacing also tells you that a mound or obstacle narrower than one grid cell may simply vanish from the data.
In planning, the team uses a DEM in two ways. The first is checking ground clearance for a plan whose altitudes are relative to take-off. The second is holding a height above the ground (terrain following). The ArduPilot documentation explains that Copter can do this in autonomous modes using a downward-facing rangefinder or terrain height data from the SRTM database, supplied by the GCS and stored on the SD card. Set TERRAIN_ENABLE = 1 and choose Terrain as the mission’s altitude type. The Copter 4.6.3 description of TERRAIN_SPACING notes that ArduPilot’s SRTM server has 100 m spacing. If the vehicle cannot get terrain data for two seconds in flight, it switches to RTL.
Example 3 Height above ground along the leg H to P5
The H to P5 leg of sortie 3 crosses a narrow earth mound. The team reads ground heights from a DTM every 30 m along the leg. The take-off point is 2 m above sea level, the plan sets 40 m relative to take-off, and the team requires at least 30 m above the ground because roadside trees reach 15 m (ground heights and criteria are assumed).
import math
DEM_SPACING = 30 # m between DEM grid points (assumed)
dem = [2, 2, 3, 3, 4, 6, 9, 13, 16, 18, 18, 17, 15, 12, 10, 9, 8, 8, 9, 11,
14, 16, 17, 18, 19, 28, 27, 21, 18, 17, 16, 15, 15, 14, 14, 13, 13, 12, 12] # m above MSL
HOME_ELEV, ALT_REL_HOME, AGL_TARGET, MIN_CLEAR = 2, 40, 40, 30
leg = math.dist((0, 0), (250, 1100)) # H -> P5
def ground(s, spacing, heights):
i = min(int(s // spacing), len(heights) - 2)
f = s / spacing - i
return heights[i] + f * (heights[i + 1] - heights[i])
samples = [s for s in range(0, int(leg) + 1, 10)]
agl = [(s, HOME_ELEV + ALT_REL_HOME - ground(s, DEM_SPACING, dem)) for s in samples]
s_min, a_min = min(agl, key=lambda x: x[1])
too_low = sum(10 for s, a in agl if a < MIN_CLEAR)
print(f"leg H-P5 {leg:.0f} m, altitude {ALT_REL_HOME} m above home")
print(f"lowest clearance {a_min:.1f} m at {s_min} m from H; below {MIN_CLEAR} m for about {too_low} m")
amsl = [ground(s, DEM_SPACING, dem) + AGL_TARGET for s in samples]
print(f"terrain following {AGL_TARGET} m AGL: {min(amsl) - HOME_ELEV:.0f} to {max(amsl) - HOME_ELEV:.0f} m above home")
coarse = dem[::4] # resample every 120 m, like a coarse grid
print(f"highest ground: 30 m grid {max(ground(s, DEM_SPACING, dem) for s in samples):.1f} m, "
f"120 m grid {max(ground(s, 120, coarse) for s in samples if s <= 120 * (len(coarse) - 1)):.1f} m")
leg H-P5 1128 m, altitude 40 m above home
lowest clearance 14.0 m at 750 m from H; below 30 m for about 700 m
terrain following 40 m AGL: 40 to 66 m above home
highest ground: 30 m grid 28.0 m, 120 m grid 19.0 m
A plan that looks safe on a flat map leaves only 14 m of clearance over the mound, and is below the 30 m criterion for almost 700 m of the leg. The tops of 15 m trees on the mound would be about 1 m above the flight path: a certain collision. The fix is to hold 40 m above the ground, which takes the drone up to 66 m relative to take-off over the mound; that number is needed to set the altitude fence in the next section. The last line is a warning: the coarse 120 m grid misses the top of the mound (19 m instead of 28 m). SRTM data on a 100 m grid carries the same risk, so always survey the real site and keep a margin.
Geofencing
Geofencing is a virtual fence that the autopilot uses to keep the aircraft inside an area and below a set height. If the aircraft breaches the fence, it performs the configured action on its own without waiting for the pilot. In planning, the fence turns the area and height the team is permitted to use (from Module 2) into parameters the aircraft can actually enforce.
ArduPilot’s Geofencing documentation and the Copter 4.6.3 parameter list define the following (the fence names, ranges and defaults below were re-checked against Copter 4.7.1 and are unchanged).
FENCE_ENABLE= 1 turns fences on.FENCE_TYPEis a bitmask (bit 0 maximum altitude, bit 1 circle centred on home, bit 2 inclusion or exclusion polygons and circles, bit 3 minimum altitude); Copter’s default enables the maximum altitude, circle and polygon fencesFENCE_RADIUSis the circle radius (30–10,000 m, default 300 m),FENCE_ALT_MAXthe ceiling (10–1,000 m, default 100 m), measured relative to home in this version, andFENCE_MARGINthe distance the autopilot tries to keep from the fence (default 2 m)FENCE_ACTIONvalues include 1 = RTL or Land, 2 = always Land and 4 = Brake or Land; 0 means report only, which protects nothing- At a breach, ArduPilot sets up a backup fence 20 m further out; breaching it triggers the action again, and if the vehicle ends up more than 100 m beyond the fence it switches to Land
The fence must therefore sit between the planned route and the edge of the permitted area: far enough from the route that normal flight never touches it, and far enough inside the permitted edge that the aircraft can stop in time after a breach. The stopping distance can be estimated as , where is the time to detect and start responding. Inclusion and exclusion polygon fences, and checking points against fences with a program, are covered in detail in UAT 305 Module 5.
Example 4 Choosing the fence radius and ceiling
The landowner and authority permit flight within 1,600 m of H and up to 90 m relative to take-off. Braking uses the default WPNAV_ACCEL of 250 cm/s², climb uses WPNAV_SPEED_UP of 250 cm/s and vertical acceleration WPNAV_ACCEL_Z of 100 cm/s² (Copter 4.7 renames these WP_ACC, WP_SPD_UP and WP_ACC_Z, in m/s² and m/s, with the same default values; the permitted area, reaction time and errors are assumed).
import math
pts = {"P1": (420, 150), "P2": (900, -200), "P3": (1300, 400), "P4": (700, 800), "P5": (250, 1100),
"P6": (-300, 900), "P7": (-600, 300), "P8": (-400, -500), "P9": (300, -700), "P10": (1100, -900)}
APPROVED_R, APPROVED_TOP = 1600, 90 # m from H that is permitted (assumed)
MAX_ALT_PLAN = 66 # m above take-off (from Example 3)
V, T_REACT, A_BRAKE = 8.0, 1.0, 2.5 # m/s, s, m/s² (WPNAV_ACCEL 250 cm/s²)
VZ, A_Z = 2.5, 1.0 # WPNAV_SPEED_UP 250 cm/s, WPNAV_ACCEL_Z 100 cm/s²
GNSS_ERR, BARO_ERR, INNER = 5.0, 3.0, 10.0 # m errors and inner margin (assumed)
far = max(pts, key=lambda k: math.hypot(*pts[k]))
d_far = math.hypot(*pts[far])
stop_h = V * T_REACT + V ** 2 / (2 * A_BRAKE)
stop_v = VZ * T_REACT + VZ ** 2 / (2 * A_Z)
r_lo, r_hi = d_far + INNER, APPROVED_R - stop_h - GNSS_ERR
z_lo, z_hi = MAX_ALT_PLAN + stop_v + BARO_ERR, APPROVED_TOP - stop_v - BARO_ERR
print(f"farthest point {far} {d_far:.0f} m; stopping distance {stop_h:.1f} m horizontal, {stop_v:.1f} m vertical")
print(f"FENCE_RADIUS between {r_lo:.0f} and {r_hi:.0f} m -> {'OK' if r_lo <= r_hi else 'NO ROOM'}")
print(f"FENCE_ALT_MAX between {z_lo:.1f} and {z_hi:.1f} m -> {'OK' if z_lo <= z_hi else 'NO ROOM'}")
farthest point P10 1421 m; stopping distance 20.8 m horizontal, 5.6 m vertical
FENCE_RADIUS between 1431 and 1574 m -> OK
FENCE_ALT_MAX between 74.6 and 81.4 m -> OK
The team picks FENCE_RADIUS = 1500 m and FENCE_ALT_MAX = 78 m. The ceiling window is very narrow because terrain following in Example 3 pushes the altitude relative to take-off up to 66 m. If a window were empty (NO ROOM), the plan must change, for example by flying lower over the mound or routing around it, never by moving the fence beyond the permitted area. A 1.5 km circle is still far larger than the area each sortie actually uses; if the permitted area is a polygon, draw an inclusion fence that follows it, or set a separate fence for each sortie. The 20 m backup fence created after a breach is one more reason not to place the fence right at the permitted edge. Before real flights, test in SITL that the aircraft stops and performs the FENCE_ACTION as expected.
Module lab
Lab: planning a point-inspection route on campus
- Choose 8–12 inspection points on campus, read their coordinates from a map and convert them to metres from the take-off point
- Order them with Example 1 and compare with an order the team chooses by eye
- Split into sorties with Example 2 using the training drone’s real range after reserves
- Extract ground heights along the route from the DEM the GCS uses, check clearance with Example 3 and choose the mission’s altitude type
- Plan the mission in QGroundControl, set altitude, speed and actions at each point, and save a .plan file
- Compute
FENCE_RADIUSandFENCE_ALT_MAXwith Example 4 and set the fence in SITL - Fly it in SITL and compare the real time and distance with the calculation, then fly out of the fence once and record where it stops and what it does
Common mistakes
Watch out
- Ordering points by their numbers instead of by distance
- Not keeping an energy reserve when splitting sorties
- Using altitude relative to take-off on sloping ground
- Forgetting the time spent taking photos at each point
- Believing a heuristic always gives the best answer
- Using a DSM as ground height, or a coarse grid, without a margin for mounds or obstacles the grid cannot see
- Placing the fence right at the permitted edge, or setting
FENCE_ACTION= 0, which only reports
Summary
- Visiting every point by the shortest route is the travelling salesman problem; use nearest neighbour and improve with 2-opt
- Long routes are split into sorties, each within the battery’s range after reserves
- Set waypoint altitude, speed and actions to suit the task and terrain
- Use a DEM to check ground clearance along the route, tell a DTM from a DSM, and beware of coarse grids that miss narrow mounds
- The fence must sit far enough from the route for normal flight and at least the stopping distance plus errors inside the permitted edge
- Check the plan with a program before every upload
Check your understanding
- What is the distance from A (0,0) to B (300,400)?
- Flying at 10 m/s with a 1 s reaction time and 2.5 m/s² braking, what is the stopping distance, and if the permitted edge is 1,000 m away, what is the largest circle fence radius (ignoring other errors)?
- How does 2-opt improve a route?
- A route is 7,900 m and each sortie can fly 3,500 m. What is the minimum number of sorties (ignoring the extra return distance)?
- Why is altitude relative to take-off dangerous on sloping ground?
Answers
- m
- m, so the fence radius is at most m
- It reverses a section of the route and keeps the change if the route gets shorter, repeating until no reversal helps
- sorties, although in practice more are usually needed because every sortie must return to the take-off point
- The ground may rise along the way, so a drone holding altitude relative to take-off gets close to the ground or obstacles
Key formulas
| Route length | |
| Condition for each sortie | |
| Stopping distance (for placing the fence) |
Key references
- Croes, G. A. (1958). A method for solving traveling-salesman problems. Operations Research, 6(6), 791–812. link
- QGroundControl. QGroundControl user guide. link
- QGroundControl Dev Team. Plan file format. QGroundControl developer guide. link
- ArduPilot Dev Team. Mission command list. ArduPilot Copter documentation. link
- ArduPilot Dev Team. Parameter list (Copter stable V4.6.3). ArduPilot Copter documentation. link
- ArduPilot Dev Team. Parameter list (Copter stable V4.7.1). ArduPilot Copter documentation. link
- ArduPilot Dev Team. Terrain following (in Auto, Guided, etc). ArduPilot Copter documentation. link
- Copernicus Data Space Ecosystem. Copernicus DEM: Global and European digital elevation model. European Space Agency. link
- ArduPilot Dev Team. Geofencing (fences). ArduPilot Copter documentation. link
- ArduPilot Dev Team. Inclusion and exclusion fences. ArduPilot Copter documentation. link
- ArduPilot Dev Team. ArduPilot source code, tag Copter-4.6.3 [Computer software]. GitHub. link
Further reading
Study the assigned knowledge units in advance, review media and take the module quiz
Planning survey image-capture flights
QGroundControl
Coordinates, direction, time and height
In class / field
Lecture, case discussion and in-class problem solving
Learning evidence: Quiz results and submitted exercises