control//controller design//MPC//MPC computation
MPC computation is the online work of solving a model predictive controller's optimization within one sampling period, and it is the line item that decides whether an MPC fits on a given processor at a given rate. An LQR is a matrix-vector product, microseconds; an MPC solves a problem with \(N\cdot m\) decision variables (\(m\) inputs over \(N\) steps) and must finish before the next period. For the lateral drone axis (four states, one input) with \(N=20\) that is 20 variables and 40 torque bounds plus the state limits: a small quadratic program. A chemical plant with 30 inputs and \(N=50\) would reach 1500 variables, but it has a minute.
MPC computation is the online work of solving a model predictive controller's optimization within one sampling period, and it is the line item that decides whether an MPC fits on a given processor at a given rate. An LQR is a matrix-vector product, microseconds; an MPC solves a problem with N⋅mN\cdot mN⋅m decision variables (mmm inputs over NNN steps) and must finish before the next period. For the lateral drone axis (four states, one input) with N=20N=20N=20 that is 20 variables and 40 torque bounds plus the state limits: a small quadratic program. A chemical plant with 30 inputs and N=50N=50N=50 would reach 1500 variables, but it has a minute.
A QP of a few dozen variables solves on a powerful microcontroller in a fraction of a millisecond to a few milliseconds, depending on solver and precision, and much faster on a PC. OSQP and qpOASES are common open solvers for QPs; acados generates C code for nonlinear MPC.
Computation is delay.
If solving takes 5 ms, the input will be applied 5 ms after the state it was computed from, and that lag eats phase like any other (loop delay). Optimize from the state predicted for the instant the input will apply, and design for the worst case of the solve time, never its mean (worst-case execution time).
Warm start: begin from the previous plan shifted one step, which usually leaves the solution a few iterations away.
Structure-exploiting solvers use the stage-by-stage shape of the problem, so the cost grows roughly linearly with NNN instead of cubically; capping iterations and accepting a feasible, near-optimal answer is standard. Move blocking lets the input change only in the first steps of the horizon and holds it afterwards, cutting the decision variables (process MPC does this routinely).
Explicit MPC solves the QP offline for every possible state, which yields a piecewise-affine law stored as a table, so the microcontroller only looks up a region and multiplies. It suits two to four states and few constraints, because the number of regions explodes with problem size.
Plan for the solver not finishing: apply the next input of the previous plan (it was feasible a period ago) or fall back to an LQR. A real-time design must say which, and test it.
The solve time a browser simulation shows is the browser's; the embedded target needs its own measurement, which is why MPC on a drone usually runs on an ARM companion computer while the attitude loop stays on the flight microcontroller (time-scale separation).