|
| 1 | +# Model Predictive Controller (MPC) |
| 2 | + |
| 3 | +## Overview & Motivation |
| 4 | + |
| 5 | +Model Predictive Control (MPC) is a receding-horizon optimal control strategy that computes control inputs by solving a finite-horizon optimization problem at each time step. Unlike LQR, which applies a fixed gain computed offline, MPC re-solves the optimization online, enabling it to handle constraints on control inputs directly. |
| 6 | + |
| 7 | +At each sample instant, MPC predicts the future system trajectory over a prediction horizon $N_p$, optimizes a sequence of control moves over a control horizon $N_c \leq N_p$, applies only the first control move, and repeats at the next step. This receding-horizon approach provides feedback and robustness to disturbances. |
| 8 | + |
| 9 | +**Key advantages over LQR:** |
| 10 | +- Explicit handling of input constraints (actuator limits) |
| 11 | +- Preview and anticipation of future reference changes |
| 12 | +- Tunable trade-off between horizon length and computational cost |
| 13 | + |
| 14 | +**When to use MPC:** |
| 15 | +- Systems with actuator saturation or operational limits |
| 16 | +- Multi-input multi-output (MIMO) systems requiring coordinated control |
| 17 | +- Applications where the computational budget allows online optimization (typically $N_c \cdot m \leq 20$) |
| 18 | + |
| 19 | +## Mathematical Theory |
| 20 | + |
| 21 | +### Prerequisites |
| 22 | + |
| 23 | +- Discrete-time linear state-space model: $x_{k+1} = Ax_k + Bu_k$ |
| 24 | +- Quadratic cost weighting matrices $Q \succeq 0$ (state), $R \succ 0$ (control) |
| 25 | +- Optional terminal cost matrix $P \succeq 0$ (typically from DARE) |
| 26 | + |
| 27 | +### Core Definitions |
| 28 | + |
| 29 | +**Cost function** over prediction horizon $N_p$ with control horizon $N_c$: |
| 30 | + |
| 31 | +$$J = \sum_{k=0}^{N_p-1} x_k^T Q\, x_k + \sum_{k=0}^{N_c-1} u_k^T R\, u_k + x_{N_p}^T P\, x_{N_p}$$ |
| 32 | + |
| 33 | +For $k \geq N_c$, the control input is held at $u_{N_c-1}$ or zero (our implementation sets it to zero beyond $N_c$). |
| 34 | + |
| 35 | +### Prediction Matrices |
| 36 | + |
| 37 | +The future state trajectory can be expressed as: |
| 38 | + |
| 39 | +$$\mathbf{X} = \Psi\, x_0 + \Theta\, \mathbf{U}$$ |
| 40 | + |
| 41 | +where $\mathbf{X} = [x_1^T, x_2^T, \ldots, x_{N_p}^T]^T$ and $\mathbf{U} = [u_0^T, u_1^T, \ldots, u_{N_c-1}^T]^T$. |
| 42 | + |
| 43 | +**State propagation matrix** $\Psi$ ($N_p n \times n$): |
| 44 | + |
| 45 | +$$\Psi = \begin{bmatrix} A \\ A^2 \\ \vdots \\ A^{N_p} \end{bmatrix}$$ |
| 46 | + |
| 47 | +**Control-to-state matrix** $\Theta$ ($N_p n \times N_c m$): |
| 48 | + |
| 49 | +$$\Theta = \begin{bmatrix} B & 0 & \cdots & 0 \\ AB & B & \cdots & 0 \\ A^2B & AB & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ A^{N_p-1}B & A^{N_p-2}B & \cdots & A^{N_p-N_c}B \end{bmatrix}$$ |
| 50 | + |
| 51 | +### QP Formulation |
| 52 | + |
| 53 | +Substituting predictions into the cost yields a quadratic program in $\mathbf{U}$: |
| 54 | + |
| 55 | +$$J = \mathbf{U}^T H\, \mathbf{U} + 2\, x_0^T F^T \mathbf{U} + \text{const}$$ |
| 56 | + |
| 57 | +where: |
| 58 | + |
| 59 | +$$H = \Theta^T \bar{Q}\, \Theta + \bar{R}$$ |
| 60 | +$$F = \Theta^T \bar{Q}\, \Psi$$ |
| 61 | + |
| 62 | +$\bar{Q}$ is block-diagonal with $Q$ on the first $N_p - 1$ blocks and $P$ on the last block. $\bar{R}$ is block-diagonal with $R$ repeated $N_c$ times. |
| 63 | + |
| 64 | +### Unconstrained Solution |
| 65 | + |
| 66 | +Setting $\nabla_{\mathbf{U}} J = 0$: |
| 67 | + |
| 68 | +$$\mathbf{U}^* = -H^{-1} F\, x_0$$ |
| 69 | + |
| 70 | +Solved via Gaussian elimination ($H\, \mathbf{U}^* = -F\, x_0$) rather than explicit matrix inversion. |
| 71 | + |
| 72 | +### Box Constraints |
| 73 | + |
| 74 | +For control input constraints $u_{\min} \leq u_k \leq u_{\max}$, a simple projected approach clamps each element of the unconstrained solution. This is a first-order approximation; for tight constraints or state constraints, a full QP solver would be required. |
| 75 | + |
| 76 | +## Complexity Analysis |
| 77 | + |
| 78 | +| Phase | Time | Space | Notes | |
| 79 | +|----------|----------------------|------------------------------------|----------------------------------------------------| |
| 80 | +| Offline | $O(N_p^2 \cdot n^3)$ | $O((N_c m)^2 + N_p n \cdot N_c m)$ | Prediction matrices, Hessian, gradient precomputed | |
| 81 | +| Online | $O((N_c m)^3)$ | $O((N_c m)^2)$ | Gaussian elimination to solve $H\mathbf{U}=-Fx$ | |
| 82 | +| Per step | $O(N_c m \cdot n)$ | $O(N_c m)$ | Matrix-vector product $Fx$ + constraint clamping | |
| 83 | + |
| 84 | +For typical embedded use ($n=2, m=1, N_c=10$): the online solve is a $10 \times 10$ linear system, well within real-time budgets. |
| 85 | + |
| 86 | +## Step-by-Step Walkthrough |
| 87 | + |
| 88 | +**System:** Double integrator with $dt = 0.1$ s |
| 89 | + |
| 90 | +$$A = \begin{bmatrix} 1 & 0.1 \\ 0 & 1 \end{bmatrix}, \quad B = \begin{bmatrix} 0.005 \\ 0.1 \end{bmatrix}$$ |
| 91 | + |
| 92 | +$$Q = \begin{bmatrix} 10 & 0 \\ 0 & 1 \end{bmatrix}, \quad R = [0.1], \quad N_p = N_c = 3$$ |
| 93 | + |
| 94 | +**Step 1 — Build $\Psi$:** |
| 95 | + |
| 96 | +$$\Psi = \begin{bmatrix} A \\ A^2 \\ A^3 \end{bmatrix} = \begin{bmatrix} 1 & 0.1 \\ 0 & 1 \\ 1 & 0.2 \\ 0 & 1 \\ 1 & 0.3 \\ 0 & 1 \end{bmatrix}$$ |
| 97 | + |
| 98 | +**Step 2 — Build $\Theta$:** |
| 99 | + |
| 100 | +$$\Theta = \begin{bmatrix} 0.005 & 0 & 0 \\ 0.1 & 0 & 0 \\ 0.015 & 0.005 & 0 \\ 0.2 & 0.1 & 0 \\ 0.03 & 0.015 & 0.005 \\ 0.3 & 0.2 & 0.1 \end{bmatrix}$$ |
| 101 | + |
| 102 | +**Step 3 — Compute $H$ and $F$:** |
| 103 | + |
| 104 | +$H = \Theta^T \bar{Q} \Theta + \bar{R}$ produces a $3 \times 3$ positive definite matrix. |
| 105 | + |
| 106 | +$F = \Theta^T \bar{Q} \Psi$ produces a $3 \times 2$ matrix. |
| 107 | + |
| 108 | +**Step 4 — Online solve for $x_0 = [1, 0]^T$:** |
| 109 | + |
| 110 | +Solve $H\, \mathbf{U}^* = -F \cdot [1, 0]^T$ via Gaussian elimination. |
| 111 | + |
| 112 | +Apply first element $u_0^*$ to the plant. |
| 113 | + |
| 114 | +## Pitfalls & Edge Cases |
| 115 | + |
| 116 | +- **Horizon too short:** If $N_p$ is too small, the controller is myopic and may produce oscillatory or unstable behavior. Use DARE terminal cost $P$ to mitigate. |
| 117 | +- **Ill-conditioned Hessian:** Very large $Q/R$ ratios create poorly conditioned $H$. Keep $Q$ and $R$ within 3–4 orders of magnitude of each other. |
| 118 | +- **Constraint infeasibility:** Box constraints that are too tight for the required control effort will clamp every step, degrading performance. Monitor constraint activity. |
| 119 | +- **Fixed-point overflow:** For Q15/Q31 types, the Hessian elements grow with horizon length. Keep $N_c \cdot m$ small and scale weights carefully. |
| 120 | +- **Stack usage:** All matrices are stack-allocated. A system with $n=3, m=2, N_c=10$ creates a $20 \times 20$ Hessian (1600 bytes for float). Monitor stack budget. |
| 121 | +- **Terminal cost omission:** Without terminal cost $P$, the controller ignores cost-to-go beyond the horizon, leading to suboptimal or unstable behavior for short horizons. |
| 122 | + |
| 123 | +## Variants & Generalizations |
| 124 | + |
| 125 | +| Variant | Key Difference | Use Case | |
| 126 | +|----------------------|-------------------------------------------------------------|-----------------------------------| |
| 127 | +| Nonlinear MPC (NMPC) | Nonlinear prediction model, requires iterative optimization | Nonlinear plants, high-accuracy | |
| 128 | +| Explicit MPC | Precomputes control law as piecewise affine function | Very fast online evaluation | |
| 129 | +| Move-Blocking MPC | Constrains control moves to change only at specific steps | Reduces QP size | |
| 130 | +| Adaptive MPC | Updates plant model online | Time-varying or uncertain systems | |
| 131 | +| Distributed MPC | Decomposes problem across subsystems | Large-scale multi-agent systems | |
| 132 | +| Robust MPC | Accounts for bounded disturbances in constraints | Safety-critical applications | |
| 133 | + |
| 134 | +## Applications |
| 135 | + |
| 136 | +- **Autonomous vehicles:** Path tracking with steering and acceleration limits |
| 137 | +- **Process control:** Chemical reactor temperature/pressure regulation with valve constraints |
| 138 | +- **Robotics:** Joint torque-constrained trajectory tracking |
| 139 | +- **Power electronics:** Voltage/current regulation in converters with switching constraints |
| 140 | +- **HVAC systems:** Energy-efficient building climate control with comfort bounds |
| 141 | +- **Quadrotor control:** Attitude and position control with thrust limits |
| 142 | + |
| 143 | +## Connections to Other Algorithms |
| 144 | + |
| 145 | +```mermaid |
| 146 | +graph LR |
| 147 | + DARE["Discrete Algebraic<br/>Riccati Equation"] -->|terminal cost P| MPC["Model Predictive<br/>Controller"] |
| 148 | + GE["Gaussian<br/>Elimination"] -->|solves H·U = -F·x| MPC |
| 149 | + LQR["LQR Controller"] -.->|special case<br/>N→∞, no constraints| MPC |
| 150 | + MPC -->|first control move| Plant["Plant Model"] |
| 151 | + Plant -->|state measurement| MPC |
| 152 | +``` |
| 153 | + |
| 154 | +- **LQR** is the infinite-horizon, unconstrained special case of MPC. As $N_p \to \infty$ with DARE terminal cost, the MPC gain converges to the LQR gain. |
| 155 | +- **DARE** computes the terminal cost matrix $P$, ensuring the finite-horizon problem approximates the infinite-horizon solution. |
| 156 | +- **Gaussian Elimination** solves the dense linear system $H\mathbf{U} = -Fx$ at each step. For constrained problems, iterative QP solvers could replace this. |
| 157 | +- **Kalman Filter** provides state estimates when full state measurement is unavailable (MPC + KF = output-feedback MPC). |
| 158 | + |
| 159 | +## References & Further Reading |
| 160 | + |
| 161 | +- Maciejowski, J.M., *Predictive Control with Constraints*, Prentice Hall, 2002. |
| 162 | +- Rawlings, J.B. and Mayne, D.Q., *Model Predictive Control: Theory and Design*, Nob Hill Publishing, 2009. |
| 163 | +- Camacho, E.F. and Bordons, C., *Model Predictive Control*, 2nd ed., Springer, 2007. |
| 164 | +- Borrelli, F., Bemporad, A., and Morari, M., *Predictive Control for Linear and Hybrid Systems*, Cambridge University Press, 2017. |
| 165 | +- Wang, L., *Model Predictive Control System Design and Implementation Using MATLAB*, Springer, 2009. |
0 commit comments