Skip to content

Control

Every robot has a controller, which is a loop that runs at a fixed rate (hundreds of times per second on a drone). On each pass it compares the state of the vehicle (its altitude and velocity) with a setpoint, which is the desired state (an altitude to hold), and computes the motor commands that bring the state to the setpoint.

This page develops the main ideas in order, using one small quadrotor as the running example. It starts with feedback and the PID controller, then shows what the limit on motor thrust does to it, and ends with a controller that plans ahead with a model of the vehicle and whose settings can be trained. Each figure is interactive and can be rotated by dragging. The figures run a small simulation of a 1 kg quadrotor that can produce twice its weight in thrust, not a flight controller.

Feedback

The simplest controller commands the thrust that a model says will hold the vehicle in the air (its weight) and never checks the result. This is called open-loop control, and it is only as accurate as the model. Feedback control instead measures the altitude and computes the thrust from the error,

e = setpoint − altitude

so it keeps correcting for as long as an error remains, whatever caused it.

The dark line is the altitude over the last eight seconds and the teal line is the setpoint. The red bar beside the drone is the current error.

In the figure the vehicle is 5% heavier than the controller assumes. With feedback disabled, the hover thrust falls short and the drone sinks to the ground. With it enabled, the drone follows the setpoint at every mass error, although the controller is never told the mass.

The PID controller

Reaching the setpoint is not enough. The vehicle should arrive without swinging past it and without correcting so hard that it thrashes. The most common feedback controller, the PID controller, does this with three terms added to the hover thrust. Each term is a gain multiplied by the error, the accumulated error, or the rate of change of the error, and each one fixes a fault that the others leave:

u = + + + thrust uhover hover thrust holds the weight Kp e proportional how far off it is now Ki ∫ e dt integral how long it has been off Kd de/dt derivative how fast that is changing
The PID control law. Each bracket marks one term and the question about the error that it answers.

The thrust u is written as the acceleration it gives the vehicle, so the hover thrust is 9.8 m/s². In code, this is the body of the loop, run once per pass. The integral is a running sum and the derivative is a difference:

error = setpoint - altitude
total += error * dt                 # the integral of the error
rate = (error - last_error) / dt    # the derivative of the error
last_error = error
thrust = hover + kp * error + ki * total + kd * rate
The drone carries a payload of 10% of its mass that the controller does not know about, and climbs 2 m from rest. The readout shows how much thrust each of the three terms is adding. The motors in this figure respond instantly.

The buttons add the terms one at a time, which shows what each one is for. With the proportional term alone (P only), the thrust depends only on how far away the drone is, so the controller keeps pushing until the drone is at the setpoint, where it is still moving at full speed. It passes the setpoint and swings around it without settling, because the term acts as a spring and a spring removes no energy.

The derivative term is negative while the error is shrinking, so it brakes the drone and stops the swing. The drone then rests about 25 cm below the setpoint, because at rest only the proportional term can supply the extra thrust that the payload requires, and it produces thrust only when there is an error.

The integral term removes this offset. It keeps growing for as long as any error remains, so the drone can only come to rest where the error is zero, and the integral term ends up holding the thrust that the model left out. Its cost is that it responds to the past, so it keeps pushing after the error has changed sign, and the drone passes the setpoint before it settles. The distance by which it passes is called the overshoot, given as a fraction of the step (the change in the setpoint), and it is about 20% here. With Ki above about 15 the swings grow with every cycle, and such a loop is called unstable.

The thrust limit

The motors cannot deliver whatever thrust the controller requests. This vehicle produces at most twice its weight in thrust, and when the controller requests more, the motors are said to saturate.

In the chart, red is the thrust the controller requests, dark is the thrust the motors deliver, and the dashed line is the most they can produce.

Saturation first affects the integral term. During a long climb the motors are saturated and the error stays large, so the accumulated error grows far beyond any useful value, and the drone overshoots a 3 m step by about 65%. This is called windup. Enabling integrator clamp stops the accumulation while the motors are saturated and halves the overshoot.

The rest of the overshoot comes from braking, and it grows with the step: with integral action disabled it is 9% on the 1 m step and 55% on the 10 m step. The controller begins to brake at a distance from the setpoint that is proportional to its speed, while the distance needed to stop under a thrust limit grows with the square of the speed. On the 10 m step the drone reaches 12 m/s, begins to brake 3.4 m from the setpoint, and needs more than 7 m. A controller that reacts to the current error has no knowledge of its own braking distance.

Limits on the gain

Higher gains make errors smaller, but three properties of a real vehicle limit them. The altitude measurement is noisy, the motors take about 50 ms to change speed (lag), and there is a delay of 30 to 40 ms between a measurement and the thrust that results from it.1

The loop speed slider sets the proportional and derivative gains together; there is no integral term. The noise is 1 cm on each altitude measurement.

The derivative term amplifies noise, because the difference between two noisy measurements taken 10 ms apart is mostly noise. In the default state, 1 cm of noise becomes about 10 m/s² of variation in the thrust command. A derivative filter of 20 ms, which averages the derivative over that time, reduces this by a factor of three, but a longer filter adds a delay of its own.

Lag and delay limit the gains directly. Raising loop speed from 5 to 10 raises the overshoot from 10% to about 60%, because the controller now reacts faster than the thrust can follow, and raising delay to 150 ms makes the default loop unstable. The usable gain is therefore set by the slowest element between the measurement and the thrust.

Cascaded loops

A quadrotor has no motor that pushes sideways, so it moves horizontally by tilting. Position is therefore controlled through attitude (the orientation of the vehicle, which in the side view below is a single tilt angle) by a cascade of two loops: an outer loop converts the position error into a tilt setpoint, and an inner loop tilts the vehicle to match it.

The dark arrow is the thrust, and the teal arrow is the direction the position loop asks for. The drone is seen from the side.

The cascade works only if the inner loop is faster than the outer one, because to the position loop the attitude loop is a lag. With attitude loop speed at four times the position loop, the move overshoots by 6%, and at equal speeds it overshoots by 45%. Tilting also reduces the vertical part of the thrust, so with more thrust when tilted disabled the drone sinks during every move.

Flight controllers are built this way. PX4, a widely used open-source autopilot, runs four nested loops (position, velocity, attitude, and rotation rate), and every gain and limit in that structure is chosen by hand.2

The cost

Every controller so far reacts to the current error, with gains chosen by trial. The rest of this page takes a different approach: state what a good flight is, and compute the inputs that achieve it. The statement is called a cost, which is a single number that scores a whole flight, where lower is better. Here the drone moves along one horizontal axis, its state is the position error p and the velocity v, and the input u is an acceleration:

J = + + cost Σ sum over every time step ( ) qp p2 position far from the target qv v2 velocity moving fast r u2 effort pushing hard
The cost that LQR and MPC both minimize. Each bracket marks one term and what it penalizes. The weights are the three numbers written before the squares.

The weights qp, qv, and r say how much each term matters relative to the others. The controller is now whatever choice of inputs makes the cost smallest, and the two controllers that follow differ only in how they find it.

The first ignores the limit on the input. The inputs that minimize the cost are then given by a formula, and they are a fixed gain on each part of the state, u = −Kp p − Kv v. This controller is called the linear quadratic regulator (LQR).

The dark line behind the drone is the path already flown, with time running into the screen, and the teal line is the target. The acceleration is limited to 5 m/s², which corresponds to a tilt of about 27°.

The result has the form of the proportional and derivative terms of the PID controller, but the gains are now computed from the weights. Raising position weight raises both gains together, and the response settles without swinging at every setting, so the weights are easier to choose than the gains.

The drone's acceleration is limited to 5 m/s², and LQR does not know it. On the 20 m move the drone reaches almost 12 m/s and overshoots the target by 7.3 m, for the reason given in the section on the thrust limit.

Model predictive control

Model predictive control (MPC) minimizes the same cost with the limit included. No formula gives the answer in that case, so MPC computes it numerically. At every step, it uses the model to find the sequence of inputs that minimizes the cost over the next few seconds while respecting the limit. That sequence is called the plan, and the time it covers is called the horizon. MPC applies the first input of the plan, discards the rest, and solves again from the next measured state.

Dark is the drone flown by MPC, and the blue ribbon ahead of it is its current plan. Brown is LQR from the previous section, with the same weights and the same limit. Teal is the target. The lines behind each drone are the paths already flown, with time running into the screen.

Because the plan respects the limit, it accounts for the braking distance: with a horizon of 2 s the drone overshoots by 0.8 m where LQR overshoots by 7.3 m. The horizon must be long enough to contain the stop, and lowering horizon to 1 s raises the overshoot to 2.9 m. Pressing shove changes the plan at the next step, because every solve begins from the measured state, and this is what makes MPC a feedback controller.

MPC is limited by its model. With wind enabled, the drone rests about 25 cm from the target, because the model does not include the wind. Estimate the wind attributes the difference between the predicted and the measured velocity to an unmodeled force and adds that force to the model, which removes the offset. MPC also costs computation: on a racing quadrotor it needed 2.7 ms per command against 0.02 ms for a cascaded controller.3

Trained weights

MPC still depends on numbers chosen by hand, namely the weights of its cost. They can be trained instead. A batch of flights is flown and reduced to a single score, each weight is changed slightly to see whether the score improves, and the weights are moved in the direction that improves it. This is the procedure used to train a neural network, applied to two parameters in place of millions.

The height of the surface is the score of a batch of 20 m moves at each pair of weights, where lower is better. Brown is the hand-tuned starting point and blue is the path that training follows.

In the figure, each of the eight flights in the batch has a tailwind gust, a motor lag, and a delay that the controller's model omits, and a wall stands 1.5 m past the target. The hand-tuned weights hit the wall in all eight flights. Pressing train lowers the position weight and raises the velocity weight, which makes the drone approach more slowly, and it then stops short of the wall in all eight. With train on gusts, lag, and delay disabled, the training flights contain no disturbances, and the resulting weights still hit the wall in all eight.

DiffMPC, from Toyota Research Institute, computes the effect of every parameter on the score directly from the solver, which makes this training practical for controllers with many parameters.4 Its authors trained the 14 parameters of a controller for a drifting car, and on the real car the trained controller completed a maneuver on which the hand-tuned one spun out. It has not been shown that the same holds for a drone.

Running an experiment

diffmpc-drones is the repo for control experiments. It currently reproduces DiffMPC's quadrotor example, which trains the cost weights of a hover controller:

uv run diffmpc-drones

The simulated drone in that example is the same model the controller plans with, so nothing in the training resembles the gusts, lag, and thrust limit on this page. Adding a separate simulator that has them is the first piece of work in the control area.