30% off your first 3 months

Get the deal

Drone flight controllers and stabilization

by DuckyHelperNov 12, 20265 min read
Robotics

TL;DR:

  • A flight controller reads an IMU thousands of times a second, compares the drone's tilt and rotation rate with targets, and adjusts four motors to correct them.

  • Each axis (roll, pitch, yaw) usually has a PID loop on angle feeding an inner PID loop on rotation rate.

  • Flight modes decide what the targets are: hold the stick's rate, hold a level angle, or hold a position and height.

A gust of wind hits a hovering drone from the left. Within a few hundredths of a second the right motors speed up and the drone is level again. The pilot did nothing. A small circuit board, the flight controller, did the whole job. It is a feedback loop, and one of the best examples of one.

What a flight controller does

A flight controller is a small computer with sensors, running a loop many times per second. In each pass of the loop it does four things:

  1. Read the sensors. The IMU (inertial measurement unit) has gyroscopes, which measure rotation rate about each axis, and accelerometers, which measure acceleration including gravity. Many controllers also have a barometer for height and a compass; GPS can give position.

  2. Estimate the state. It combines the sensor readings (a gyroscope is smooth but drifts, an accelerometer is noisy but does not drift) into estimates of roll, pitch and yaw angles. See sensor noise and how to smooth it for the basic idea of filtering.

  3. Compute corrections. It compares the estimate with the target and uses a PID controller to decide how much correction to apply to each axis.

  4. Mix and output. The mixer turns the roll, pitch, yaw and throttle commands into four motor commands, as described in how a quadcopter flies.

Control loops for the inner stabilization commonly run at some kilohertz. The exact rate depends on the hardware, but the point is that each loop is only a millisecond or so, since the drone can tilt noticeably in a few hundredths of a second.

Nested loops

Most controllers use two loops for each axis, one inside the other:

  • The outer (angle) loop compares the desired angle with the measured angle. Its output is a desired rotation rate.

  • The inner (rate) loop compares the desired rate with the gyroscope's measured rate. Its output is the torque command that goes to the mixer.

The inner loop is the fast one and does most of the work of stabilization. The outer loop gives it targets. In the "acro" mode, the pilot's stick sets the rate directly, and the outer loop is skipped.

Worked example: holding level in a gust

A gust has tilted the drone 5° to the right (roll = +5°), and it is still rolling right at +10°/s. The pilot wants level, so the target angle is 0°. The gains below are examples, since real ones are tuned for each aircraft.

  1. Outer loop. Angle error = 0 − 5 = −5°. With an angle gain of 4 per second, the desired rate is 4 × (−5) = −20°/s: roll back to the left at 20°/s.

  2. Inner loop. Rate error = desired − measured = −20 − 10 = −30°/s. With a rate gain of 0.01 (in normalized command units per °/s), the correction is 0.01 × (−30) = −0.30: a strong command to roll left. (A D term and an I term would be added in the same way.)

  3. Mixer. A left-roll command speeds up the right-hand motors and slows the left-hand ones. If the base command is 0.50 for each, the right motors might receive 0.50 + 0.15 = 0.65 and the left motors 0.50 − 0.15 = 0.35, with the split depending on how the mixer scales the roll command. The total stays near the same so the height holds.

  4. Next loop, 1 ms later: the rate has turned negative and the angle error shrinks. The correction shrinks too, and by about the time the angle reaches 0° the D term in the inner loop damps the motion, so the drone stops level.

Check the signs: the drone is rolling right (positive), the desired rate is negative, so the rate error is more negative, and the command pushes it left. Each stage points the right way.

Flight modes

The modes decide where the targets come from:

  • Acro (rate) mode: the stick sets a rotation rate. There is no self-leveling. This is for experienced pilots.

  • Angle (self-level) mode: the stick sets a tilt angle, and the sticks centered means level.

  • Altitude hold: the controller adds a loop on height using the barometer or another sensor and adjusts the throttle.

  • Position hold: with GPS or a camera, the controller holds a location, tilting against drift.

  • Failsafe or return-to-home: if the radio link is lost, the controller follows a pre-set action such as landing or returning.

A higher-level planner, such as the grid search in path planning with A* on a grid, can generate waypoints for the position-hold mode to follow.

Where it goes wrong

  • Gains too high. The drone oscillates, sometimes with a high-pitched buzz, and the motors get hot.

  • Gains too low. The drone feels mushy and wobbles in the wind.

  • Vibration. Unbalanced propellers shake the gyro readings. Mount the controller on soft mounts and balance the props.

  • Sensor calibration. An accelerometer that is not level-calibrated makes the drone drift in angle mode.

  • Wrong orientation or motor order. If the board is mounted rotated, or a motor is wired to the wrong output, the controller pushes the wrong way and the drone flips on take-off.

  • Loop timing. An irregular loop rate breaks the D and I terms.

Safety: Remove the propellers before connecting the battery for any configuration, calibration or motor test. Set up an arming switch and a clear failsafe, and check that the motors spin in the right direction with the props off. Fly in an open area away from people, with the drone in view, and know the local rules for flying drones. Handle LiPo batteries carefully and do not fly with a damaged pack.

How real drones use it

Almost every multirotor has a flight controller running this structure, from toy drones to larger camera drones. The tuning approach is the same as for a wheel: change one gain at a time and watch the response (see tuning a PID controller step by step). What changes is the speed and the stakes: when a wheel controller fails the robot stops, but when a flight controller fails the drone falls.

Try it yourself

Without a drone, simulate the loop. Write a small program with an angle that changes under a random push each step, an outer P gain and an inner P gain, and print the angle over time. Try different gains and see which oscillate. Then add the D term. You have rebuilt the core of a flight controller in about twenty lines.

Frequently asked questions

Why use two loops instead of one?

The inner rate loop reacts quickly to disturbances, and the outer angle loop sets targets for it. Splitting the problem gives a more stable response than a single loop on angle.

What is the mixer?

It turns the roll, pitch, yaw and throttle commands into individual commands for each of the four motors, adding and subtracting so that each command affects only its own axis.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.