When a state-space model of the plant is available, the Linear Quadratic Regulator provides the mathematically optimal state-feedback gain. Instead of manually tuning three knobs (as with PID), LQR asks a more principled question: given how much I care about state deviation versus control effort, what is the cheapest way to drive the state to zero?
The answer is a constant gain matrix
For embedded systems, the gain
where
The LQR minimizes the infinite-horizon quadratic cost:
where:
-
$Q \succeq 0$ (positive semi-definite) penalizes state deviation. -
$R \succ 0$ (positive definite) penalizes control effort.
The solution requires finding the matrix
The optimal gain is then:
and the optimal control law is
A unique stabilizing solution
-
$(A, B)$ is stabilizable (all unstable modes are controllable). -
$(A, C)$ is detectable (where$Q = C^T C$ ).
| Phase | Time | Space | Notes |
|---|---|---|---|
| DARE solve (offline) |
|
||
| Control step (online) | Single matrix-vector multiply |
The DARE iteration is the expensive part, but it is done once (offline or at initialization). The per-sample cost is just a matrix-vector product.
System: Double integrator with
Step 1 — Initialize
Step 2 — DARE iteration (showing first iteration):
- $S = R + B^T P_0 B = 1 + [0.00005; 0.01] \begin{bmatrix}100 & 0\0 & 1\end{bmatrix} \begin{bmatrix}0.00005\0.01\end{bmatrix} = 1.0001$
- Solve
$S \cdot K_{\text{part}} = B^T P_0 A$ via Gaussian elimination - Update
$P_1 = A^T P_0 A - A^T P_0 B \cdot K_{\text{part}} + Q$ - Check convergence:
$|P_1 - P_0|$
Step 3 — Repeat until
Step 4 — Extract gain
Runtime:
-
Uncontrollable modes. If
$(A, B)$ is not stabilizable, the DARE has no stabilizing solution. Verify controllability before calling the solver. -
Ill-conditioned
$R$ . If$R$ is near-singular, the inversion in$K$ becomes numerically unstable. Ensure$R$ is strictly positive definite. -
Q/R scaling. Only the ratio of
$Q$ to$R$ matters. Scaling both by the same factor does not change$K$ . - Full state required. LQR assumes all states are measured. If only partial measurements are available, combine LQR with a Kalman Filter to form an LQG controller.
-
Fixed-point limitations. The DARE involves matrix inversions and multiplications that can overflow Q15/Q31 ranges. Prefer floating-point for the offline solve; use the pre-computed
$K$ constructor for fixed-point runtime. -
Slow DARE convergence. Systems with eigenvalues near the unit circle (e.g., high-frequency sampling, integrating plants) may require far more than the default 300 iterations to converge. Use the
MaxIterationstemplate parameter to raise the cap:Lqr<float, StateSize, InputSize, 30000>. A hard-assert fires if the solver does not converge within the specified limit.
| Variant | Key Difference |
|---|---|
| Continuous-time LQR | Solves the Continuous ARE instead of DARE; used for continuous-time plant models |
| LQG (Linear Quadratic Gaussian) | Combines LQR with Kalman filter for systems with noisy, partial observations |
| LQR with integral action | Augments the state with integral of tracking error to eliminate steady-state offset |
| Finite-horizon LQR | Time-varying gain |
| Robust LQR (H∞) | Accounts for model uncertainty by optimizing the worst-case cost |
| Pre-computed gain |
dlqr) and hard-coded for embedded targets |
- Cart-pole balancing — Classic underactuated system; LQR linearized around the upright equilibrium.
- Quadrotor attitude control — Regulating roll, pitch, yaw with motor thrust as input.
- Satellite station-keeping — Maintaining orbital position with minimal thruster fuel.
- Industrial process control — Multi-input multi-output (MIMO) systems where PID struggles.
- Autonomous vehicles — Lateral and longitudinal control using a linearized vehicle model.
graph LR
LQR["LQR Controller"]
DARE["DARE Solver"]
GE["Gaussian Elimination"]
KF["Kalman Filter"]
PID["PID Controller"]
DARE --> LQR
GE --> DARE
KF -->|"state estimate"| LQR
PID -.->|"model-free alternative"| LQR
| Algorithm | Relationship |
|---|---|
| DARE Solver | Computes the cost-to-go matrix |
| Gaussian Elimination | Used inside DARE iteration to solve linear sub-systems |
| Kalman Filter | Provides state estimates when direct measurement is unavailable (forming LQG) |
| PID Controller | Model-free alternative; simpler to deploy but not optimal |
- Anderson, B.D.O. and Moore, J.B., Optimal Control: Linear Quadratic Methods, Prentice Hall, 1990.
- Franklin, G.F., Powell, J.D. and Emami-Naeini, A., Feedback Control of Dynamic Systems, 8th ed., Pearson, 2019 — Chapter 9.
- Bryson, A.E. and Ho, Y.-C., Applied Optimal Control, Hemisphere, 1975.