Operations: links, logs, rules and fleets in service · 6 MIN READ

Energy planning before take-off: calibrating consumption, predicting a mission and go/no-go

Calibrate a drain model from short test legs at known airspeeds, fit it by least squares, predict a planned route leg by leg in several wind cases including the airspeed limit, decide go or no-go with a landing reserve and a model margin, then fly the mission and compare prediction with use.

The failsafes lesson budgeted the way home in flight, every second. Before take-off the question is wider: will this whole mission, in today's wind, land with the reserve, and how sure are we? Answering it needs a consumption model of this airframe and pack, measured rather than guessed, a prediction for every leg, and an honest margin for what the model does not know. The same model then tells you after the flight where it was wrong.

drones · A MAP OF THE IDEA

  1. Fly calibration legs
  2. Fit drain vs airspeed
  3. Predict legs per wind case
  4. Go if margin and reserve fit

Useful when

  • Turns a wind forecast into a go or no-go before take-off.
  • Per-leg comparison shows where the model is wrong.

Watch out

  • Outside the calibrated airspeeds the fit is a guess.
  • Cold or ageing packs drain faster than the calibration.

Implementation & operation cost: A calibration flight per airframe and pack type, a small least-squares fit, and a planner that predicts every leg.

01

What drains a multicopter

Hovering costs power because the rotors hold the weight up; flying through the air costs more, because the airframe must tilt and push against drag, and drag grows with the square of the airspeed. Airspeed, not ground speed: the same leg costs more into a headwind, both because the airspeed is higher and because it takes longer. Climbing adds the work of lifting. Measuring what was used is its own problem. PX4 recommends fusing a voltage-based estimate, load-compensated with the pack's internal resistance, with a current-based count of the charge consumed, and setting BAT1_CAPACITY to about 90 percent of the label. The simulator reports an exact percentage, which here stands in for such an estimate.

02

Calibrate with short legs

Fly a calibration in a known, steady wind: a hover, then legs at steady velocity downwind, upwind and across, at the speeds the missions will use and faster. On each leg, wait for the acceleration to settle, then measure the drain over a fixed window. Each leg gives one point, airspeed against drain in percent per second. Fit drain = A + B times airspeed squared by least squares: an ordinary straight-line regression on airspeed squared, a few lines of Go. Check the residuals, and write down the range of airspeeds the calibration covered, because outside it the fit is a guess. Measure the take-off and the landing separately, as costs in points. Repeat the calibration per airframe and pack type, in the cold, and as packs age.

03

Predict a route in several winds

For each leg, split the wind into the part along the leg and the part across it. The autopilot flies the cruise ground speed unless the top airspeed cannot reach it: the ground speed is the smaller of the cruise speed and the along-wind component plus the square root of the top airspeed squared minus the crosswind squared, as in the failsafes lesson. The airspeed is the ground velocity minus the wind; the time is the length over the ground speed plus a few seconds for braking into the waypoint and accelerating out. The leg costs the drain at that airspeed times the time. Run it for every wind the forecast allows, not just the mean. One result surprises people: on a closed loop flown at one ground speed, the quadratic model makes the wind's direction cancel, because the cross term sums to the route's total displacement, which is zero. Only the wind's speed costs, B times its square every second, until a headwind leg hits the airspeed limit and slows down.

04

Calibrate, plan, fly, compare

The program calibrates in a 3 m/s wind with seven legs, fits the model and prints it. It then predicts a 6-leg route at 10 m/s and 25 m in five wind cases, and calls the flight go when the prediction plus 10 percent still lands with 25 percent, above the 20 percent return-home failsafe; it flags cases that need airspeeds beyond the calibration. Finally it flies the route in 4 m/s from the west with 1.5 m/s gusts and compares each step with its prediction.

go
package main

import (
	"fmt"
	"log"
	"math"

	"gopherforge.app/drone"
)

const (
	alt     = 25.0 // m, mission altitude
	cruise  = 10.0 // m/s ground speed the autopilot flies
	maxAir  = 18.0 // m/s, the airframe's top airspeed
	stop    = 2.5  // s per waypoint: braking into it and accelerating out
	reserve = 25.0 // % on the ground, above the 20% return-home failsafe
	margin  = 0.10 // model error allowance on the predicted use
)

// Model is drain = A + B*airspeed^2 in %/s, plus measured take-off and
// landing costs in points.
type Model struct{ A, B, Climb, Descent, MaxAir float64 }

func (m Model) Drain(air float64) float64 { return m.A + m.B*air*air }

// Leg predicts one straight leg: the ground speed the autopilot can hold
// (the airspeed limit caps it against a headwind), the airspeed, the cost.
func (m Model) Leg(from, to, wind drone.Vec3) (cost, secs, air float64) {
	seg := to.Sub(from).Horizontal()
	u := seg.Scale(1 / seg.Len())
	along, across := wind.X*u.X+wind.Y*u.Y, wind.X*u.Y-wind.Y*u.X
	gs := math.Min(cruise, along+math.Sqrt(math.Max(0, maxAir*maxAir-across*across)))
	if gs < 1 {
		return math.Inf(1), math.Inf(1), maxAir
	}
	air = u.Scale(gs).Sub(wind).Len()
	secs = seg.Len()/gs + stop
	return m.Drain(air)*seg.Len()/gs + m.Drain(wind.Len())*stop, secs, air
}

// Predict returns the take-off, each leg and the landing, in points.
func (m Model) Predict(route []drone.Vec3, wind drone.Vec3) (costs []float64, secs, worstAir float64) {
	costs = append(costs, m.Climb)
	for i := 1; i < len(route); i++ {
		c, s, air := m.Leg(route[i-1], route[i], wind)
		costs, secs, worstAir = append(costs, c), secs+s, math.Max(worstAir, air)
	}
	return append(costs, m.Descent), secs, worstAir
}

// Calibrate flies short legs at steady velocity in a known wind, measures
// the drain on each after it settles, and fits A and B by least squares.
func Calibrate(wind drone.Vec3) Model {
	sim := drone.New(drone.Config{Wind: wind, Gust: 0.5, MaxSpeed: 15, Seed: 4})
	d := sim.Drone()
	if err := d.Arm(); err != nil {
		log.Fatal(err)
	}
	var m Model
	b := d.State().Battery
	d.TakeOff(alt)
	sim.RunUntil(30, func(float64) bool { return d.Arrived() })
	m.Climb = b - d.State().Battery
	var xs, ys []float64
	fmt.Printf("calibration in %.0f m/s wind from the west, %.0f m up\n  %-22s %8s %10s\n", wind.Len(), alt, "leg", "airspeed", "drain")
	for _, leg := range []struct {
		name string
		v    drone.Vec3
	}{{"hover", drone.Vec3{}}, {"east 10 m/s, downwind", drone.Vec3{X: 10}}, {"west 10 m/s, upwind", drone.Vec3{X: -10}},
		{"north 10 m/s, across", drone.Vec3{Y: 10}}, {"south 10 m/s, across", drone.Vec3{Y: -10}},
		{"west 14 m/s, upwind", drone.Vec3{X: -14}}, {"east 14 m/s, downwind", drone.Vec3{X: 14}}} {
		if leg.v.Len() == 0 {
			d.Hold()
		} else {
			d.SetVelocity(leg.v, 0)
		}
		sim.Run(4, nil) // settle
		b, t := d.State().Battery, sim.Time()
		sim.Run(10, nil)
		drain := (b - d.State().Battery) / (sim.Time() - t)
		air := leg.v.Sub(wind).Len()
		xs, ys, m.MaxAir = append(xs, air*air), append(ys, drain), math.Max(m.MaxAir, air)
		fmt.Printf("  %-22s %6.1f m/s %6.3f %%/s\n", leg.name, air, drain)
	}
	d.GoTo(drone.Vec3{Z: alt})
	sim.RunUntil(60, func(float64) bool { return d.Arrived() })
	b = d.State().Battery
	d.Land()
	sim.RunUntil(60, func(float64) bool { return d.State().Landed })
	m.Descent = b - d.State().Battery

	var mx, my, sxy, sxx, rms float64
	for i := range xs {
		mx, my = mx+xs[i]/float64(len(xs)), my+ys[i]/float64(len(xs))
	}
	for i := range xs {
		sxy, sxx = sxy+(xs[i]-mx)*(ys[i]-my), sxx+(xs[i]-mx)*(xs[i]-mx)
	}
	m.B = sxy / sxx
	m.A = my - m.B*mx
	for i := range xs {
		rms += math.Pow(ys[i]-m.Drain(math.Sqrt(xs[i])), 2) / float64(len(xs))
	}
	fmt.Printf("  take-off to %.0f m %.1f points, landing %.1f points\n", alt, m.Climb, m.Descent)
	fmt.Printf("  fit: drain = %.3f + %.5f * airspeed^2 %%/s, rms error %.3f, calibrated up to %.0f m/s\n", m.A, m.B, math.Sqrt(rms), m.MaxAir)
	if err := sim.SaveAs("calibration"); err != nil {
		log.Fatal(err)
	}
	return m
}

func sum(xs []float64) (s float64) {
	for _, x := range xs {
		s += x
	}
	return s
}

func main() {
	m := Calibrate(drone.Vec3{X: 3})
	route := []drone.Vec3{{Z: alt}, {X: 300, Z: alt}, {X: 300, Y: 100, Z: alt}, {Y: 100, Z: alt},
		{Y: 200, Z: alt}, {X: 300, Y: 200, Z: alt}, {Z: alt}}
	length := 0.0
	for i := 1; i < len(route); i++ {
		length += route[i].Sub(route[i-1]).Len()
	}
	fmt.Printf("\nplan: %d legs, %.0f m at %.0f m/s; go if %.0f%% more than predicted still lands with %.0f%%\n",
		len(route)-1, length, cruise, margin*100, reserve)
	fmt.Printf("  %-18s %7s %10s %9s\n", "wind", "time", "predicted", "verdict")
	for _, c := range []struct {
		name string
		w    drone.Vec3
	}{{"calm", drone.Vec3{}}, {"4 m/s from west", drone.Vec3{X: 4}}, {"8 m/s from north", drone.Vec3{Y: -8}},
		{"8 m/s from west", drone.Vec3{X: 8}}, {"12 m/s from west", drone.Vec3{X: 12}}} {
		costs, secs, worst := m.Predict(route, c.w)
		need := sum(costs)
		verdict := "go"
		if need*(1+margin)+reserve > 100 {
			verdict = "NO-GO"
		}
		if worst > m.MaxAir+0.5 {
			verdict += fmt.Sprintf(", extrapolated to %.0f m/s", worst)
		}
		fmt.Printf("  %-18s %5.0f s %9.1f%%  %s\n", c.name, secs, need, verdict)
	}

	wind := drone.Vec3{X: 4}
	fmt.Println("\nflown in 4 m/s from the west with 1.5 m/s gusts")
	predicted, _, _ := m.Predict(route, wind)
	sim := drone.New(drone.Config{Wind: wind, Gust: 1.5, MaxSpeed: cruise, Seed: 6})
	d := sim.Drone()
	if err := d.Arm(); err != nil {
		log.Fatal(err)
	}
	actual := []float64{d.State().Battery}
	d.TakeOff(alt)
	sim.RunUntil(30, func(float64) bool { return d.Arrived() })
	actual = append(actual, d.State().Battery)
	for _, p := range route[1:] {
		d.GoTo(p)
		sim.RunUntil(120, func(float64) bool { return d.Arrived() })
		actual = append(actual, d.State().Battery)
	}
	d.Land()
	sim.RunUntil(60, func(float64) bool { return d.State().Landed })
	actual = append(actual, d.State().Battery)
	fmt.Printf("  %-12s %9s %7s\n", "step", "predicted", "actual")
	for i, p := range predicted {
		name := fmt.Sprintf("leg %d", i)
		if i == 0 {
			name = "take-off"
		} else if i == len(predicted)-1 {
			name = "landing"
		}
		fmt.Printf("  %-12s %9.1f %7.1f\n", name, p, actual[i]-actual[i+1])
	}
	used := actual[0] - actual[len(actual)-1]
	fmt.Printf("  total %.1f predicted, %.1f used (%+.1f%%), landed with %.1f%%\n", sum(predicted), used, (used/sum(predicted)-1)*100, d.State().Battery)
	if err := sim.Save(); err != nil {
		log.Fatal(err)
	}
}
Read the code

The hover in 3 m/s of wind drains 0.194 %/s, the 10 m/s legs 0.255 downwind (7.0 m/s airspeed), 0.447 upwind (13.0) and 0.351 and 0.349 across (10.4), and the 14 m/s legs 0.658 upwind (17.0) and 0.365 downwind (11.0). The fit is drain = 0.172 + 0.00166 * airspeed^2 with an rms error of 0.005, calibrated up to 17 m/s; the take-off to 25 m costs 2.5 points and the landing 2.9. The 1461 m plan takes 161 s and needs 57.3% in calm air and 61.5% in 4 m/s from the west: go. Both 8 m/s cases need 74.3%, because the loop cancels the direction, and are NO-GO, extrapolated to 18 m/s. At 12 m/s the upwind legs slow down, the flight takes 198 s and needs 107.1%. In the flown mission every step is within 0.5 points of its prediction; the largest miss is leg 3, the 300 m upwind leg, 15.4 against 15.9. In total 61.5 was predicted and 62.4 used (+1.4%), and the drone landed with 37.6%.

05

Go, no-go and the margin

Write the rule down before looking at the number: go if the predicted use times one plus the margin, plus the reserve, fits in the charge at take-off. The reserve is what you want on the ground and must sit above the return-home threshold, or the failsafe ends the mission early. The margin covers what the model ignores: gusts above the forecast, a colder or older pack, a detour, a go-around at the landing site. Start with 10 to 20 percent and tighten it only as logged flights show the model's real error, as the per-leg comparison here does. Plan with the forecast's upper wind, not its mean, and treat a case outside the calibrated airspeeds as no-go until it is calibrated. Log the decision with its inputs. In flight, the failsafes lesson's budget takes over: a plan that was go at take-off can still turn back early if the wind rises.

06

Fly it in GopherForge

In GopherForge choose New project > Robotics and simulation > Drone flight, replace main.go with this program and keep mission.go, then tap Run. Output lists calibration.gfflight, the hover and the out-and-back test legs at two speeds, and flight.gfflight, the survey loop flown in 4 m/s of wind; the HUD's battery shows where the upwind legs cost more. Fly the mission in 8 m/s from the west instead and compare the actual use with the 74.3% the model predicted outside its calibration.

TAKE THIS WITH YOU

Calibrate drain against airspeed with short steady legs, fit it by least squares and note the range it covers. Predict every leg from the wind, the airspeed limit and the stops, decide go or no-go with a written rule of margin plus a reserve above the return threshold, and compare each flight with its prediction to learn the model's real error.

Check your understanding

A closed loop is flown at one ground speed and drain is modelled as A + B times airspeed squared. How does the direction of an 8 m/s wind change the predicted use?

Go to the source

Progress stays in this browser. No account needed.