Part 1 · Control
The receding-horizon idea
Model predictive control answers one question at every control step: given where the system is now and a model of how it moves, what is the best sequence of inputs over the next steps? The controller solves that optimization, applies only the first input, measures again and repeats.
This receding horizon is what lets MPC handle constraints such as steering limits, acceleration limits and lane boundaries directly, inside the optimization, instead of patching them in afterwards with saturation blocks and anti-windup logic.
Why not just a PID?
A well-tuned PID controller reacts to the error it sees now. It has no idea that a sharp turn is coming in two seconds, and it cannot know that the actuator will saturate. MPC looks ahead: it uses the model to predict the consequences of its decisions, and it treats limits as part of the problem rather than as surprises.
The price is computation. Every control period, MPC solves an optimization problem. Most of the engineering in a real MPC is about making that solve fast and reliable, which is what later parts of this series are about.
The optimization at each step
For a linear model and a reference , the problem solved at time is a quadratic program:
- and trade tracking accuracy against control effort. Larger tracks harder; larger gives smoother, lazier inputs.
- weights the end of the horizon so the plan does not drift once the horizon runs out.
- is how far the controller looks ahead. Too short and it is surprised by turns; too long and the problem gets expensive.
In code
A complete, runnable example: a double integrator (position and velocity, with acceleration as the input) that has to reach position 5 and stop, with acceleration limited to .
import numpy as np
import cvxpy as cp
dt, N = 0.1, 20
A = np.array([[1, dt], [0, 1]]) # state: position, velocity
B = np.array([[0.5 * dt**2], [dt]]) # input: acceleration
Q = np.diag([10.0, 1.0])
R = np.array([[0.1]])
P = Q
u_max = 2.0
nx, nu = 2, 1
def mpc_step(x0, ref):
x = cp.Variable((nx, N + 1))
u = cp.Variable((nu, N))
cost, cons = 0, [x[:, 0] == x0]
for k in range(N):
cost += cp.quad_form(x[:, k] - ref, Q) + cp.quad_form(u[:, k], R)
cons += [x[:, k + 1] == A @ x[:, k] + B @ u[:, k],
cp.abs(u[:, k]) <= u_max]
cost += cp.quad_form(x[:, N] - ref, P)
cp.Problem(cp.Minimize(cost), cons).solve()
return u[:, 0].value
x = np.array([0.0, 0.0])
ref = np.array([5.0, 0.0]) # reach position 5 and stop
for t in range(60):
u0 = mpc_step(x, ref) # plan N steps ahead...
x = A @ x + B @ u0 # ...but apply only the first input
print(x) # close to [5, 0]
Run it with pip install cvxpy numpy. Plot u0 over time and you will see it sit exactly on the limit at the start, then ease off as the target approaches. Nobody wrote a saturation rule; the limit is simply part of the problem.
In practice
On a vehicle, this problem has to be solved inside the control period, every period, without exception. Three habits matter more than the formulation:
- Warm-start from the previous solution, shifted by one step.
- Cap the solver’s iterations and use the best feasible answer when time runs out.
- Keep a simple fallback controller for the period when no answer arrives at all.
Building the problem from scratch every step, as the example above does, is fine for learning and far too slow for a real controller. Real implementations build the problem once and only update the initial state and reference.
Exercises
- Halve the horizon . What happens to the approach to the target, and why?
- Replace the hard limit with a heavy penalty in the cost. When does the controller now violate the limit?
- Add a velocity limit . Which constraint becomes active first?
Discussion
Questions or corrections? Send me an email.