Skip to the content
FixTheRobot Explore Robotics / UAV / Module 5 / M5-T7

UAV Systems Engineering · Module 5

M5-T7: LQR control

LQR trades state error against effort optimally for a linear model: pick the weights, solve one equation, and get coordinated multi-state feedback.

  • Lesson 7 of 17
  • UAV Guidance, Navigation and Control (GNC)
  • Established engineering
  • About 6 minutes to read

Where PID tunes loops one at a time, LQR designs all gains together from a model and a statement of what you care about. For a linearised quadrotor it is a few lines of code and a very good baseline.

Learning objectives

After this lesson, you should be able to:

  • State the LQR problem and its optimal feedback law.
  • Linearise a quadrotor about hover and build the A and BB matrices of a position-attitude subsystem.
  • Solve the discrete Riccati equation iteratively and check closed-loop stability.
  • Explain how QQ and RR shape the response and how LQR compares with PID.

Conceptual explanation

The linear-quadratic regulator minimises an infinite-horizon cost that adds up squared state error and squared control effort. For a linear model the solution is a constant state-feedback gain KK: u=−K xu=-K\,x. The tuning knobs are two weight matrices: QQ says how much each state error matters, RR says how expensive each input is.

LQR structureState feedback u equals minus K times the state error. K comes from solving the Riccati equation given the model A and B and the cost weights Q and R.Model A, Bweights Q, R Riccati PK = R⁻¹ Bᵀ P u = −K (x − x_ref)optimal state feedback offline design, trivial online cost: one matrix-vector product
LQR: the gain is designed offline from the model and weights; online it is a matrix multiplication.

Unlike separate PID loops, LQR uses all states at once: it knows, for instance, that position error should be corrected through a coordinated change of velocity and tilt rather than by each loop independently.

Animation of how the control weight in a linear quadratic regulator moves the closed loop along a trade-off curve. For a double integrator started 5 metres from its target, a cheap control weight gives fast convergence with large control effort and a costly weight gives gentle, slow convergence. A Pareto chart plots state cost against control cost for 25 weights with a moving point, and the time response for the current weight is drawn beside it.
LQR minimises Σ x⊤\Sigma \,x^{\top}Qx + u⊤u^{\top}Ru: increasing RR trades convergence speed for smaller control effort, tracing a Pareto curve. Bryson’s rule gives a starting point. Double integrator, Q=diag⁡(10, 1)Q=\operatorname{diag} (10,\,1), RR swept from 0.01 to 100; the Riccati equation is solved for each weight. (Looping, about 6 seconds; drawn and computed for this course with illustrative values.)

Engineering intuition

Large QQ on a state says “keep this near zero, even at the cost of effort”; large RR says “be gentle with the actuator”. The ratio sets the speed of response: heavier QQ gives a faster, more energetic controller. Because the design is optimal for the model, it is only as good as the model: it inherits guaranteed stability margins for the ideal linear case, but real motor lag, delays and saturation reduce them, so verify in simulation with those effects before flying.

Mathematical formulation

Discrete model xk+1=A xk+B ukx_{k+1}=A\,x_{k}+B\,u_{k} and cost J=∑(xk⊤ Q xk+uk⊤ R uk)J=\sum (x_{k}^{\top}\,Q\,x_{k}+u_{k}^{\top}\,R\,u_{k}) with QQ ⪰ 0, RR ≻ 0:

Discrete LQR
P=Q+A⊤PA−A⊤PB(R+B⊤PB)−1B⊤PA\mathbf P = \mathbf Q + \mathbf A^{\top}\mathbf P\mathbf A - \mathbf A^{\top}\mathbf P\mathbf B\left(\mathbf R + \mathbf B^{\top}\mathbf P\mathbf B\right)^{-1}\mathbf B^{\top}\mathbf P\mathbf A
The discrete algebraic Riccati equation.
Gain and control law
K=(R+B⊤PB)−1B⊤PA,uk=−K xk\mathbf K = \left(\mathbf R + \mathbf B^{\top}\mathbf P\mathbf B\right)^{-1}\mathbf B^{\top}\mathbf P\mathbf A, \qquad \mathbf u_k = -\mathbf K\,\mathbf x_k
xx
state error relative to the reference (for position control: position, velocity, attitude, rate errors).
QQ
state weights, diagonal in practice; Bryson’s rule sets Qii=1Q_{ii}=1/(max acceptable xix_{i})2^{2}.
RR
input weights; Bryson’s rule sets Rjj=1R_{jj}=1/(max acceptable uju_{j})2^{2}.
PP
cost-to-go matrix: V(x)=x⊤ P xV(x)=x^{\top}\,P\,x.

Linearised hover (one horizontal axis)

About hover, with small angles and xx forward, x¨=g θ\ddot{x}=g\,\theta , θ¨=τ/J\ddot{\theta }=\tau /J. With state [x, x˙, θ, ω][x,\,\dot{x},\,\theta ,\,\omega ] and input the normalised torque u=τ/Ju=\tau /J:

Linearised planar hover model
A=[010000g000010000],B=[0001]\mathbf A = \begin{bmatrix}0 & 1 & 0 & 0\\ 0 & 0 & g & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0\end{bmatrix}, \qquad \mathbf B = \begin{bmatrix}0\\ 0\\ 0\\ 1\end{bmatrix}
Continuous time; discretise with Δt.

Four integrator-chain states, one input: the pair (AA, BB) is controllable, so an LQR exists. The solution has the cascaded look of PID (position error to velocity to tilt to rate) but with gains chosen jointly.

For reference tracking, define the error relative to the reference trajectory and add feedforward, or augment with an integrator state to reject constant disturbances (LQI).

Architecture and data flow

Design offline: linearise, discretise, choose QQ and RR, solve the Riccati equation, store KK. Online: compute the state error from the estimator, u=−K eu=-K\,e, pass the torque (or acceleration) command to the next loop. If the estimator’s delay is significant, include it in the model, because LQR assumes the state is known exactly and instantly.

Real-world UAV applications

  • Hover and position hold with coordinated multi-state feedback.
  • Gain-scheduled controllers across payload or speed.
  • Terminal controller and cost-to-go in MPC (the same PP matrix).
  • Quick controller design for new airframes from CAD-derived inertia.

Implementation example

This runs as shown: it builds the discretised hover model, solves the Riccati equation by iteration (no SciPy required), and simulates the closed loop from a 2 m position error, comparing with an arbitrary hand-picked gain set.

import numpy as np
g, dt = 9.81, 0.01
Ac = np.array([[0, 1, 0, 0], [0, 0, g, 0], [0, 0, 0, 1], [0, 0, 0, 0.0]]); Bc = np.array([[0], [0], [0], [1.0]])
# zero-order-hold discretisation: A = exp(Ac dt), B = (integral of exp(Ac s) ds from 0 to dt) Bc, both by their power series
A = np.eye(4); term = np.eye(4)
for k in range(1, 6):
    term = term @ (Ac * dt) / k; A = A + term
B = np.zeros((4, 1)); Mt = np.eye(4) * dt
for k in range(6):
    B += Mt @ Bc; Mt = Mt @ (Ac * dt) / (k + 2)

def dlqr(A, B, Q, R, iters=20000):
    P = Q.copy()
    for _ in range(iters):
        K = np.linalg.solve(R + B.T @ P @ B, B.T @ P @ A)
        Pn = Q + A.T @ P @ (A - B @ K)
        if np.max(np.abs(Pn - P)) < 1e-11: P = Pn; break
        P = Pn
    return np.linalg.solve(R + B.T @ P @ B, B.T @ P @ A), P

# Bryson's rule: 1 m position, 1 m/s, 0.2 rad, 1 rad/s acceptable; input 5 rad/s^2
Q = np.diag([1 / 1.0**2, 1 / 1.0**2, 1 / 0.2**2, 1 / 1.0**2]); R = np.array([[1 / 5.0**2]])
K, P = dlqr(A, B, Q, R)
print("K =", np.round(K, 3))
rho = max(abs(np.linalg.eigvals(A - B @ K)))
assert rho < 1                                                 # closed loop is stable

def run(K, T=12.0):
    x = np.array([2.0, 0, 0, 0]); out = []
    for _ in range(int(T / dt)):
        u = np.clip(-K @ x, -30, 30)
        x = A @ x + (B @ u).ravel(); out.append(x[0])
    return np.array(out)
traj = run(K)
t_settle = np.argmax(np.abs(traj[::-1]) > 0.1 * 2.0)            # last time |x| exceeds 10% of the step
print(f"closed-loop spectral radius {rho:.4f}; position error back inside 10% after {(len(traj) - t_settle) * dt:.1f} s; final {traj[-1]:.4f} m")
assert abs(traj[-1]) < 0.01 and abs(traj.min()) < 1.0          # converges without wild undershoot
Discrete LQR for a planar hover model, solved by Riccati iteration, with Bryson’s-rule weights.

Common engineering mistakes

Treating Q and R as arbitrary numbers

Use Bryson’s rule and normalise units so weights reflect acceptable errors and inputs.

Ignoring actuator limits

LQR is unconstrained; a large step saturates the motors and the guarantees disappear. Limit the setpoint or use MPC.

Using a linearisation far from its point

The hover model is valid for small angles only; schedule gains or use MPC for aggressive flight.

No integral action

A constant wind leaves a steady-state offset; use an integrator state.

Assuming perfect state knowledge

Estimator delay and noise reduce margins: simulate with them.

Failure modes

Failure modes, symptoms and causes
Failure modeHow it shows upLikely cause
Oscillation after saturationRinging after an aggressive commandActuator limits violate the linear assumption
Steady-state offset in windPersistent position errorNo integral state
Poor performance at high speedTracking degrades as tilt growsLinearisation invalid; need scheduling or nonlinear control
Model mismatch instabilityReal vehicle oscillates while simulation was cleanUnmodelled motor lag or delay

Debugging approach

  1. Check controllability (rank of [BB AB A2B…A^{2}B\ldots ]) and the eigenvalues of A − BK.
  2. Validate in simulation with the real sensor delay and motor lag before flying.
  3. Compare step responses with those of the PID baseline on the same plant.
  4. Sweep the Q/R ratio and plot response time against control effort to find a sensible compromise.

Practical exercises

  1. Apply Bryson’s rule for a 0.5 m position tolerance and a 0.1 rad tilt limit; compare KK with the example.
  2. Add an integral of position error as a fifth state (LQI) and test against constant wind.
  3. Show that scaling QQ and RR by the same factor does not change KK.
  4. Compare closed-loop poles of the LQR with those of a PD cascade you tuned in lesson M5-T6.

Interview questions

Write an answer in your own words, then reveal the model answer. Nothing is sent anywhere.

Question 1

What does LQR optimise, and what do QQ and RR mean?

Model answer

It minimises the sum of squared state errors weighted by QQ plus squared control effort weighted by RR. Larger QQ relative to RR gives a faster, more energetic response.

Question 2

What are LQR’s main limitations for UAVs?

Model answer

It assumes a linear model and no constraints. Aggressive flight, saturation, delays and model error reduce performance and margins; MPC handles constraints explicitly.

Question 3

How does LQR differ from cascaded PID?

Model answer

LQR designs all state-feedback gains jointly and optimally from a model, giving coordinated corrections; cascaded PID tunes loops independently, which is simpler and robust but not optimal.

References and further reading

Primary papers, official documentation and standards. Each claim above that is specific to a tool or version points here.

  • BB. DD. OO. Anderson, JJ. BB. Moore, Optimal Control: Linear Quadratic Methods, Prentice-Hall, 1990.
  • KK. JJ. Åström, RR. MM. Murray, Feedback Systems, Princeton University Press, 2008, chapter 6.
  • R. Mahony, V. Kumar, P. Corke, IEEE RAM 2012

What’s next

Mark this lesson

Saved in this browser and synced to your account when you are signed in.

Practice this lesson

M5-L2: Implement LQR

Solve the Riccati equation for a hover model.

Practice this lesson

M5-L4: Compare PID, LQR and MPC

Compare the three on one reference.

Next lesson

M5-T8: Model Predictive Control

Formulate MPC with velocity and acceleration constraints, choose horizons, and judge computational and model-mismatch risks.