Skip to content

Control Systems

Build a drone altitude hold with PID: climb, step, gust

A drone altitude hold is one PID loop on height: thrust = hover + Kp × error + Ki × integral − Kd × v, run many times a second. This build tunes one for a hard mission: carry a parcel the code doesn't know about, climb to 10 m and settle within four seconds, move to 15 m on command, and recover from a downdraft, all without overshooting.

The mission

  • The drone carries a parcel: gravity is effectively 10.8, but the code assumes 9.8 for hover.
  • Climb to 10 m and settle cleanly within 4 s.
  • At 4 s the target becomes 15 m: settle again, no overshoot.
  • At 7 s a downdraft knocks 3 m/s off its speed: recover.

"Clean" means it never goes more than 1.5 m past the target, and "settled" means within 0.3 m and nearly still for a whole second.

The controller

Two lines, run every 10 ms:

integral = constrain(integral + error * 0.01, -3, 3);
thrust = 9.8 + 16.0 * error + 1.0 * integral - 7.0 * v;

The cap on integral is a simple anti-windup: during the climb the error is large for a while, and an unlimited total would store a push that carries the drone past 10 m.

Tune it in order

  1. P first. Raise Kp until the drone arrives fast. Kp 16 is bold: alone, it rockets far past the target.
  2. D next. Raise Kd until neither the climb nor the step to 15 m overshoots. About 7 with Kp 16.
  3. I last, and small. About 1, to take out the parcel's droop.
  4. One gain at a time, so you can see what each change did.

Live experiment

Fly the whole mission

The loaded drone climbs to 10 m, gets a new target of 15 m at 4 s and a downdraft at 7 s, under thrust = 9.8 + Kp × error + Ki × integral − Kd × v. Set the three gains and fly; the three ticks are the build's checks. Kp 16, Ki 1, Kd 7 passes all three.

Try it yourself: free lesson

“Project: Hold the hover” is the project that ends Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Read the result the same way each time. Overshoot means less P or more D. No settle means more P or less D. A standing gap means a little more I.

One loop, three jobs

The controller never sees the mission. It sees error. A new target is a new error, so the move to 15 m is just the climb again, 5 m long. The gust is a sudden drop that shows up as error and a burst of speed, which P and D answer. The parcel is a small error that won't go away, which is exactly what I is for. That's why the same three numbers handle all of it.

Real flight controllers run nested loops like this, often with an inner loop on vertical speed inside an outer one on height, and they read height from a barometer, a rangefinder or both.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Project: Hold the hover” is the project that ends Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3At four seconds the target changes from 10 to 15. What does the controller see?
    Show the answer

    Answer: The error jumps to 5 and the loop climbs, exactly as it did from the ground. The controller only ever sees error = target − h. A new target is a new error, and the same loop handles it the same way. That's why one controller can follow any command.

    • Why not “An error of 15”? The drone is already at 10 m, so the error is 15 − 10 = 5, not 15.
    • Why not “Nothing: the target is fixed”? The target is part of the error, target minus height, so changing it changes the error at once.
    • Why not “A gust”? A gust is a push on the drone. A new target moves the goal instead, and shows up as a jump in error with the drone still where it was.
  2. Question 2 of 3Which combination fails the project: settles clean, but not within 4 seconds?
    Show the answer

    Answer: A small, gentle gain. A gentle gain arrives clean but slow, and the project wants the first settle inside 4 seconds. Overshoot problems come from the other three.

    • Why not “Too much I”? A big integral winds up during the climb and carries the drone past the target, which is overshoot again.
    • Why not “Too little damping”? Too little damping lets the drone swing past the target: an overshoot problem, not a slow one.
    • Why not “Too much gain”? Too much gain gets there quickly but overshoots, so it fails the clean check, not the time limit.
  3. Question 3 of 3The integral term fixes…
    Show the answer

    Answer: A steady leftover error, like a heavier load. It adds up the droop until it's gone.

    • Why not “Overshoot”? Overshoot is braked by the D term. The integral, if anything, adds to it when it winds up.
    • Why not “Noise”? Noise is fast and random. Adding it up mostly cancels it, but filtering is the tool for noise.
    • Why not “Fast gusts”? A sudden knock is handled by P and D. The integral is slow and works on error that lasts.
Keep practising: free lesson

“Project: Hold the hover” is the project that ends Feedback Falls, Control Systems World 1. Free with an account. Lessons unlock in order, so you’ll do the 6 before it first.

Questions people ask

What sensors does a drone use to hold altitude?

Usually a barometer, which reads air pressure and works at any height but drifts with the weather, plus an accelerometer for fast changes. Low down, many add a downward rangefinder, sonar or a small lidar, which measures height above the ground directly.

How do you get the vertical speed for the D term?

From how fast the height reading changes, or better, by fusing the height sensor with the accelerometer. Using the measured speed, rather than the change in error, also stops the D term kicking when the target suddenly jumps.

Why does the drone settle low with P and D only?

Because the code's hover thrust is wrong for the load. With a parcel needing 1.0 more thrust and Kp 16, it hangs 1.0 ÷ 16, about 6 cm, low. A small integral term adds up that gap and removes it.

Every topic