MPC

MPC Controller

Advanced
Run the live example Free · no sign-up · runs in your browser

The MPCController block is a discrete-time Model Predictive Controller for linear state-space systems. At every simulation step, it solves a constrained finite-horizon optimal control problem using an interior-point Quadratic Programming (QP) solver, and applies the first optimal control move u[0]u[0] to the actuator.

Where an LQR controller computes an unconstrained feedback law u=−Kxu = -\mathbf{K}\mathbf{x}, MPC explicitly respects actuator limits (umin⁡≤u≤umax⁡u_{\min} \le u \le u_{\max}) and state boundaries (xmin⁡≤x≤xmax⁡x_{\min} \le x \le x_{\max}). When the terminal cost P\mathbf{P} is left empty, the block computes the infinite-horizon Discrete Algebraic Riccati Equation (DARE) solution, ensuring closed-loop stability even with finite prediction horizons.

Wire each plant state to the inputs x0,x1,…x_0, x_1, \dots (in1, in2, ...) in the order of A\mathbf{A}; up to six states. When reference tracking is enabled, provide setpoints to r0,r1,…r_0, r_1, \dots (in7, in8, ...) to track nonzero references.

Mathematical Model

The controller models the plant as a discrete-time linear state-space system:

x[k+1]=Ax[k]+Bu[k]\mathbf{x}[k+1] = \mathbf{A} \mathbf{x}[k] + \mathbf{B} u[k]

At step tt, given current state x0=x[t]\mathbf{x}_0 = \mathbf{x}[t], the block solves the quadratic program over prediction horizon NN:

min⁡u[0],…,u[N−1]∑k=0N−1(x[k]TQx[k]+Ru[k]2)+x[N]TPx[N]\min_{u[0], \dots, u[N-1]} \sum_{k=0}^{N-1} \left( \mathbf{x}[k]^T \mathbf{Q} \mathbf{x}[k] + R u[k]^2 \right) + \mathbf{x}[N]^T \mathbf{P} \mathbf{x}[N]

Subject to:

x[k+1]=Ax[k]+Bu[k],k=0,…,N−1\mathbf{x}[k+1] = \mathbf{A}\mathbf{x}[k] + \mathbf{B}u[k], \quad k = 0, \dots, N-1
umin⁡≤u[k]≤umax⁡,k=0,…,N−1u_{\min} \le u[k] \le u_{\max}, \quad k = 0, \dots, N-1
xmin⁡≤x[k]≤xmax⁡,k=1,…,N(optional)\mathbf{x}_{\min} \le \mathbf{x}[k] \le \mathbf{x}_{\max}, \quad k = 1, \dots, N \quad \text{(optional)}

The terminal cost matrix P\mathbf{P} satisfies the discrete algebraic Riccati equation:

P=ATPA−ATPB (R+BTPB)−1BTPA+Q\mathbf{P} = \mathbf{A}^T\mathbf{P}\mathbf{A} - \mathbf{A}^T\mathbf{P}\mathbf{B}\,(R + \mathbf{B}^T\mathbf{P}\mathbf{B})^{-1}\mathbf{B}^T\mathbf{P}\mathbf{A} + \mathbf{Q}

The first element u[0]u[0] is emitted on output port out at each time step (receding horizon control).

Inputs & Outputs

Direction ID Label Type Status
→ In in1 x₀ number Required
→ In in2 x₁ number Optional
→ In in3 x₂ number Optional
→ In in4 x₃ number Optional
→ In in5 x₄ number Optional
→ In in6 x₅ number Optional
→ In in7 r₀ number Optional
→ In in8 r₁ number Optional
→ In in9 r₂ number Optional
→ In in10 r₃ number Optional
→ In in11 r₄ number Optional
→ In in12 r₅ number Optional
← Out out u number Output
← Out valid Valid number Output
← Out status Status number Output
← Out iterations Iterations number Output
← Out cost Cost number Output

Parameters

Parameters Label Type Default Description
nx Number of States (nₓ) number 2 Number of states in the model (1-6). Determines how many x inputs are used.
horizon Prediction Horizon (N) number 10 Number of steps to predict. Longer horizon → better performance but more computation.
A A (nₓ×nₓ) matrix 1 0.1; 0 1 State transition matrix. Rows separated by ';'. Example: '1 dt; 0 1' for double integrator with dt=0.1
B B (nₓ×1) matrix 0.005; 0.1 Input matrix. One entry per state. Example: 'dt²/2; dt' for double integrator
sampleTime Sample Time number 0.1
Q Q (nₓ×nₓ) matrix 10 0; 0 1 State cost matrix. Diagonal entries penalize state deviations. Larger = faster settling.
R R (1×1) matrix 1 Input cost weight. Larger R = gentler control effort. Must be positive.
P Terminal Cost P (nₓ×nₓ) matrix Optional terminal cost. If empty, computed automatically from DARE (infinite-horizon LQR cost-to-go).
uMin Input Min number -100 Lower bound on control input u
uMax Input Max number 100 Upper bound on control input u
xMin State Min (per state) matrix Optional lower bounds on states (1×nₓ matrix). Leave empty to disable state constraints.
xMax State Max (per state) matrix Optional upper bounds on states (1×nₓ matrix). Leave empty to disable state constraints.
slackWeight State Slack Weight number 10000 Penalty weight for soft state constraints. Values > 0 guarantee solver feasibility under disturbances. Set to 0 for hard constraints.
reference Enable Reference Tracking boolean false When enabled, ref0..ref(n-1) inputs are used for offset-free tracking. The controller regulates the deviation from reference.

Usage Examples

Basic MPCController: Constrained Double Integrator

A cart positioning system (x1=px_1 = p, x2=vx_2 = v) with sample time Ts=0.1 sT_s = 0.1\,\text{s} has actuator limits u∈[−5,5]u \in [-5, 5]. Starting with an initial displacement of 1 m1\,\text{m}, the MPC controller drives the state to the origin while strictly clamping the motor force within [−5,5] N[-5, 5]\,\text{N}.

Constrained Thermal Chamber Setpoint Tracking

A temperature regulation process where heater power is strictly bounded u∈[0,50] Wu \in [0, 50]\,\text{W} and states must track an elevated temperature setpoint with zero overshoot while respecting thermal slew rate constraints.

Remarks & Best Practices

  • Tuning: Start with diagonal Q=I\mathbf{Q} = \mathbf{I}, R=1R = 1, and prediction horizon N=10N = 10. Increasing NN expands the feasible region and improves tracking, but increases per-step QP solve time.
  • Constraints: Actuator bounds umin⁡u_{\min} and umax⁡u_{\max} are hard constraints enforced at every step of the prediction horizon. State constraints (xmin⁡x_{\min}, xmax⁡x_{\max}) are softened using slack variables with quadratic weight slackWeight (default 10410^4), guaranteeing solver feasibility even when external disturbances push states beyond the boundaries. Set slackWeight to 0 for strict hard constraints.
  • Warm Starting: The optimal control trajectory is automatically shifted forward and cached across simulation steps, accelerating interior-point QP convergence from ~8 to ~2-3 iterations per step.
  • Terminal Cost: Leaving P\mathbf{P} empty automatically computes the stabilizing infinite-horizon DARE solution, ensuring stability for asymptotically controllable systems.
  • Computation: Solved at each time step via a Mehrotra predictor-corrector primal-dual interior-point algorithm in Rust/WASM.

Related Components