← Projects · Real-time MPC › Car

Real-time MPC for a car at the handling limit

A nonlinear predictive controller that drives a car around a track close to the tyres' limit, solving its optimisation in about a millisecond, and runs as a C++ ROS 2 node. On the same speed profile, the linear MPC used in production stacks spins the car. On a slippery road, the controller plans with a grip level certified from six seconds of driving data, and finishes where planning with the nominal grip spins.

Nonlinear MPC · Real-time · Vehicle dynamics · acados · ROS 2 · C++

Code on GitHub →Stack: acados · CasADi · OSQP · IPOPT · Python · C++ · ROS 2 Jazzy · ddma · pytest · GitHub Actions · Docker

≈ 1 msmedian solve per control step at 50 Hz; no missed deadline over a lap
9 %faster lap than linear MPC, which spins on the same speed profile
0 spinson a slippery road, planning with the grip certified from data
≈ 1.6 msfrom state message to steering and throttle command in ROS 2

Two NMPCs on a road with friction 0.6

Road friction 0.6: planning with the nominal friction 1.0, the car spins after 344 m; planning with the friction certified by ddma (0.57), it finishes the lap in 72.6 s.

What it does

Near the tyres’ limit a car stops behaving linearly: braking or accelerating takes lateral grip away from that axle, and the rear can step out. A controller that plans with a linear model and a separate speed loop does not see this coming. This one plans with the nonlinear model, every 20 ms:

  1. ModelDynamic bicycle model along the track, magic-formula tyres, and a friction circle per axle: grip used for braking is not available for cornering.
  2. Optimise in real timeA 2-second plan for steering and acceleration, re-solved every step with acados: one fast iteration per step, warm-started from the last plan.
  3. Know the gripddma turns a short calibration drive into a grip level that is guaranteed to be available.
  4. DeployGenerated C code inside a ROS 2 C++ node, next to a 1 kHz vehicle simulator; tested against the Python version.

The plan keeps the car in its corridor, within a friction ellipse, on the stable side of each tyre’s peak, and within the steering and per-axle traction limits. Because the model knows how braking and throttle eat into cornering grip, the controller brakes before a corner and feeds the throttle in on the way out.

Demo: a lap at the limit

A 1.3 km track with fast sweepers and an 11 m hairpin; the car may use 85 % of the available grip.

Controller Lap time Max distance from the centre line Result
Nonlinear MPC (one iteration per step) 57.4 s 0.23 m finishes
Nonlinear MPC, solved to convergence every step 57.4 s 0.24 m finishes: one iteration loses nothing here
Linear MPC (OSQP), same speed profile – – spins on a corner exit (power oversteer)
Linear MPC, slowest profile that finishes (65 %) 63.1 s 0.33 m finishes

The linear baseline is the classic production design: linear tyres along the predicted speed and curvature, a separate speed loop, the same horizon, corridor and slip limits, solved by OSQP with warm start.

Demo: a slippery road

The road’s friction is 0.6 instead of the nominal 1.0, and the controller is not told. Before driving, the car does a short steering ramp at 12 m/s. From the measured slip angles and lateral forces (with sensor noise), ddma computes bounds on the tyre curve that hold for every curve consistent with the data and two physical priors. The largest guaranteed grip is 0.57, below the true 0.6.

The controller plans with Lap time Result
the nominal grip, 1.0 – spins after 344 m
the grip certified by ddma, 0.57 72.6 s finishes, never asks for more grip than there is
the true grip, 0.6 (oracle) 71.1 s finishes

The certificate costs 2 % of lap time against a controller that knows the road.

Runs on the slippery road, and the guaranteed band around the tyre curve from the calibration data

Algorithms and performance

  • Solver: acados, multiple shooting with an exact-Hessian SQP (projected to stay convex), HPIPM for the quadratic programs, one real-time iteration per control step. A Gauss-Newton Hessian made the full steps oscillate on this model.
  • Horizon: 2 s on a non-uniform grid (ten 20 ms steps, then 60 ms steps), so the first move is resolved at the control rate and the tail sees far enough to brake for corners.
  • Checked against an independent solver: CasADi + IPOPT on the same problem return the same first input and the same optimal cost.
Solver (closed loop over a lap) Median 99th percentile Within 20 ms
acados, 20 / 40 / 80 shooting intervals about 0.7 / 1.2 / 2 ms 1 / 2 / 6 ms 100 %
acados at 100 Hz about 1.4 ms 2.5 ms 100 %
ROS 2 C++ node, state → command about 1.6 ms 3 ms 100 %
Solved to convergence every step about 6 ms 180 ms 91 %
IPOPT, warm-started about 130 ms 210 ms 0 %

All three grids give the same lap: the horizon length matters, not the number of intervals.

Solve time per control step for each solver

Engineering specific to this project

  • One problem, three solvers: acados (generated C, used in the loop), IPOPT (reference) and OSQP (linear baseline); tests check that acados and IPOPT agree.
  • ROS 2: the road’s friction can be changed at run time; the launch test drives the car for 20 s and checks progress, corridor, solver status and latency.

Next

Load transfer and actuator lag in the simulated car; updating the grip certificate while driving; a tube variant with a certified error bound; F1TENTH-scale parameters and a hardware run.