30% off your first 3 months

Get the deal

Model predictive control in plain words

by DuckyHelperNov 12, 20265 min read
Robotics

TL;DR:

  • Model predictive control (MPC) uses a model to predict what several candidate actions would do over a short horizon, picks the one with the lowest cost, applies only its first step, then repeats.

  • The cost function says what you want: stay on the line, keep steering gentle. Constraints say what is not allowed.

  • It is flexible and handles limits well, but it needs a decent model and enough computing time each cycle.

A simple controller reacts to the present: you are 10 cm left of the line, so steer right in proportion. A driver does more. They look ahead, see a bend, and start steering early. Model predictive control (MPC) gives a robot that kind of look-ahead, by trying out futures in a model before committing to one.

Predict, optimize, act, repeat

  1. Predict. Use a mathematical model of the robot to forecast its state over a short future window, called the horizon, for a given sequence of actions.

  2. Optimize. Search for the action sequence whose predicted future has the lowest cost, while keeping to the constraints, such as a maximum steering angle.

  3. Act. Apply only the first action of that sequence.

  4. Repeat. At the next time step, measure the real state and do the whole thing again, with the horizon shifted forward.

Re-planning each cycle is what makes MPC cope with errors. If the car did not move quite as predicted, the new measurement fixes the starting point and the plan adjusts. This repeated re-planning is called a receding horizon.

Worked example: a robot car planning its next second of steering

A small car has a wheelbase L = 0.25 m and drives at v = 1.0 m/s. It is y₀ = 0.30 m to the left of the lane centre and points straight along the lane (heading ψ₀ = 0). Use a simple model for small angles: the heading changes at ψ̇ = (v / L) × δ, where δ is the steering angle, and the sideways position changes at ẏ ≈ v × ψ. Positive δ steers left.

Keep the steering constant over the 1 s horizon, to keep the arithmetic short. Then v / L = 1.0 / 0.25 = 4 s⁻¹, and after T = 1 s:

heading:   ψ(T) = 4 × δ × T          = 4δ
position:  y(T) = y0 + v × (4δ) × T²/2 = 0.30 + 2δ

Define the cost as J = y² + 0.1 ψ² + 0.2 δ²: stay near the centre, finish pointing along the lane, and do not steer harder than needed. The constraint is |δ| ≤ 0.3 rad. Evaluate three candidates.

Steering δ (rad)

Predicted y (m)

Predicted ψ (rad)

Cost J

0

0.30

0

0.09 + 0 + 0 = 0.090

−0.10

0.10

−0.40

0.01 + 0.016 + 0.002 = 0.028

−0.15

0.00

−0.60

0 + 0.036 + 0.0045 = 0.0405

Three candidate steering commands evaluated over a 1 second horizon.

The lowest cost is the second row, δ = −0.10 rad (steering right about 5.7°). Going straight leaves the car 30 cm off the centre. Steering harder at −0.15 reaches the centre exactly but ends with the car pointing sharply right, so it would immediately cross the line, which the heading term punishes.

Check the numbers. Row 2: y = 0.30 + 2 × (−0.10) = 0.10, ψ = 4 × (−0.10) = −0.40. Cost: 0.10² = 0.01, 0.1 × 0.40² = 0.016, 0.2 × 0.10² = 0.002, sum 0.028. The small-angle model is approximate at 0.4 rad (about 23°), where sin 0.4 = 0.389, so the approximation sin ψ ≈ ψ is off by only about 0.011, fine for this purpose.

A real MPC does not test three constant steering values. It optimizes a whole sequence, for example 10 steps of 0.1 s each, with a numerical solver, and may include speed as well. The robot applies only the first step of the best sequence, then measures again and repeats. After 0.1 s the car is at a new state, and the plan changes.

Where it goes wrong

  • Poor model. If the model ignores tyre slip or delay, predictions drift from reality. Feedback helps but cannot fix everything.

  • Too slow to compute. The solver must finish well within one control period. If it takes longer, the plan is stale.

  • Horizon too short or too long. Too short and the car cannot see the bend coming. Too long and the model's errors grow and computing time rises.

  • Badly chosen cost. The robot does exactly what the cost says. Forget the steering penalty and it may jerk the wheel.

  • Infeasible constraints. If the limits make every option illegal, the solver fails. Plan a fallback, such as slowing down.

  • Local minima. For hard, non-convex problems the solver can settle for a poor solution.

Safety: Test a new controller in simulation first, then on the real vehicle at low speed with the wheels off the ground or in a clear area, and keep a hardware emergency stop. Clamp steering and speed outside the optimizer, so a solver error cannot command a wild value.

How real robots use it

MPC is used for car and drone path tracking, for legged robots deciding footholds, and in industrial process control. It builds on feedback ideas like those in visual servoing and the PID controller, but adds prediction and explicit limits. For arms, a smooth reference path is often given to the controller, as in smooth trajectories for robot arms.

Try it yourself

Use a spreadsheet. Put y₀, v and L in cells, list ten candidate steering values from −0.3 to 0.3, and compute y, ψ and cost for each with the formulas above. Find the minimum. Then change the weight on ψ from 0.1 to 0.5 and watch the best steering move toward zero.

Frequently asked questions

Why apply only the first action?

The rest of the plan was based on predictions that will be slightly wrong. Measuring again and re-planning every step corrects those errors.

Is MPC always better than PID?

No. PID is simpler, cheaper to run and often enough. MPC helps when you have limits to respect or need to anticipate what is coming.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.