Model Predictive Control for Robots
Model Predictive Control (MPC) optimizes robot commands across a future time horizon using a dynamic model, executes only the immediate first step, and replans at the next cycle with updated state feedback to handle constraints gracefully.
Why Does This Exist?
Standard PID feedback control is purely reactive: it looks at present error and past accumulated error to calculate motor torque. It cannot anticipate an upcoming sharp bend in a path, cannot look ahead to decelerate before hitting a hard joint limit, and cannot enforce complex inequality constraints (like maximum torque or obstacle boundaries).
Global motion planners (like A* or RRT) solve for long collision-free paths, but take hundreds of milliseconds to compute and assume a static world: if a human steps in front of the robot, the global planner stalls.
Model Predictive Control (MPC), also called receding horizon control, fills the vital middle tier between high-level global planning and low-level joint reflexes. By using a predictive mathematical model of robot physics over a short future window (such as to ), MPC optimizes candidate control sequences subject to hard physical constraints, executes only the first commanded step, and replans continuously with fresh sensor feedback.
Basics first: the low-level PID loops that track MPC setpoints live in robot-feedback-control. Global path planning across configuration space lives in robot-motion-planning. Learning action-conditioned predictive dynamics from data lives in robot-world-models.
Think of It Like This
Sprinting through a crowded train station while looking five paces ahead
Imagine rushing through a crowded train concourse to catch your departure. You do not stare straight down at your toes (purely reactive PID), nor do you freeze to calculate a rigid, fixed route across the entire building (global planning).
Instead, you focus your gaze five paces ahead (the prediction horizon). As you run, you predict where walking passengers will be in two seconds, steer smoothly around them, and avoid brushing against luggage carts (constraint handling).
Crucially, you only execute your very next stride. As your foot hits the ground, you shift your gaze forward another five paces and adjust your heading based on what actually moved.
Where it stops: human runners have vast situational awareness and peripheral vision. Robot MPC must solve a numerical mathematical optimization problem (such as a quadratic program) within strict real-time deadlines (e.g. every ). If the solver fails to converge before the deadline, the robot has no command to send to its motors.
How It Actually Works
The receding horizon optimization problem
At discrete time , given current measured state , MPC solves a constrained finite-horizon optimal control problem over a prediction horizon of steps:
subject to:
- Dynamic model: with .
- State constraints: (e.g. maximum velocity, position boundaries).
- Actuator constraints: (e.g. motor torque limits, acceleration bounds).
where penalizes state tracking errors, penalizes control effort, and is the terminal cost.
The receding horizon loop
- Measure current state from robot state estimation.
- Solve the optimization problem over horizon to obtain optimal sequence .
- Apply only the first control action to the robot motor controllers.
- Discard the remaining planned actions .
- Advance the clock to , obtain fresh sensor feedback, and repeat the optimization.
Because the optimization repeats every cycle with real-world state feedback, MPC continuously corrects for model inaccuracies, wheel slip, and unexpected disturbances.
Worked example: 1D cart regulation over a 3-step horizon
Consider a 1D robotic delivery cart tracking a target position .
- Discrete state dynamics: , , with .
- Initial state at : position , velocity .
- Horizon length: steps ().
- Actuator constraint: acceleration .
- Velocity constraint: .
- Cost function weights: (tracking error), (velocity damping), (control effort).
Evaluate candidate control sequence :
-
Step ():
-
Step ():
-
Step ():
Total horizon cost:
All constraints are satisfied (, ). The controller executes only the immediate action , commands the motor drive, and re-evaluates the optimization at from the newly measured position and velocity.
Code
This Python snippet defines a discrete MPC forward simulator, computes state rollouts, evaluates cost functions, and verifies the receding horizon first-step selection.
def evaluate_mpc_candidate( u_seq: list[float], x_init: float = 0.0, v_init: float = 0.0, x_target: float = 1.0, dt: float = 0.1,) -> tuple[float, float, float]: """Simulate candidate control sequence over horizon and return (cost, final_x, final_v).""" x: float = x_init v: float = v_init total_cost: float = 0.0
for u in u_seq: x_next: float = x + v * dt v_next: float = v + u * dt # Cost: tracking error + velocity penalty + control effort stage_cost: float = (x_next - x_target) ** 2 + 0.5 * (v_next ** 2) + 0.1 * (u ** 2) total_cost += stage_cost x, v = x_next, v_next
return round(total_cost, 4), round(x, 4), round(v, 4)
cost, x_end, v_end = evaluate_mpc_candidate(u_seq=[1.2, 1.2, 0.0])print(cost) # -> 3.2582print(x_end) # -> 0.036print(v_end) # -> 0.24Watch Out For
Optimization solver exceeding real-time loop deadlines
If your robot's control loop runs at ( period), an MPC solver (such as a nonlinear interior-point optimizer) must guarantee completion in well under . If the optimizer gets stuck resolving non-convex obstacles, it misses its compute deadline.
The symptom is the robot stuttering, hesitating, or executing outdated commands while the control loop stalls. Fix it by using warm-starting (seeding the solver with the shifted solution from the previous cycle), setting strict maximum iteration caps, or using high-speed convex quadratic programming (OSQP, qpOASES).
Model mismatch inducing high-frequency chatter
If the internal predictive model assumes an idealized frictionless surface while the real robot operates on thick carpet, the predicted state trajectory diverges from physical reality within two steps.
The symptom is aggressive command chattering: at step the solver commands large positive torque expecting rapid motion, but at discovers the robot moved only half as far, causing it to command erratic corrections. Fix it by adding state-dependent disturbance estimators or learning data-driven robot world models that predict physical dynamics under complex contacts.
The Quick Version
- Model Predictive Control optimizes control sequences across a finite future time horizon using a dynamic system model.
- At each cycle, MPC solves a constrained optimization problem, executes only the first action (), and replans with fresh feedback.
- For a 1D cart tracking over a 3-step horizon (), candidate sequence yields a cost of .
- MPC inherently respects physical inequality constraints, such as motor torque limits, maximum velocity envelopes, and obstacle clearance margins.
- Real-time feasibility requires fast convex quadratic solvers and warm-starting to ensure the optimizer consistently finishes within its strict loop deadline.