Model Predictive Control Toolbox™ software provides code generation functionality for controllers designed in Simulink® and MATLAB®.
After designing a controller in Simulink using any of the MPC blocks, you can generate code and deploy it for real-time control. You can deploy controllers to all targets supported by the following products:
Simulink PLC Coder™
You can generate code for any of the Model Predictive Control Toolbox Simulink blocks:
For more information, see Simulation and Code Generation Using Simulink Coder and Simulation and Structured Text Generation Using PLC Coder.
The MPC Controller, Explicit MPC Controller, and Adaptive MPC Controller blocks are implemented using the MATLAB Function block. To see the structure, right-click the block, and select Mask > Look Under Mask. Then, open the MPC subsystem underneath.
After designing an MPC controller in MATLAB, you can generate C code using MATLAB Coder and deploy it for real-time control.
To generate code for computing optimal MPC control moves:
Generate data structures from an MPC or explicit MPC controller using
For more information, see Generate Code To Compute Optimal MPC Moves in MATLAB.
The sampling rate that a controller can achieve in a real-time environment is system-dependent. For example, for a typical small MIMO control application running on Simulink Real-Time, the sample time can go as low as 1–10 ms. To determine the sample time, first test a less-aggressive controller whose sampling rate produces acceptable performance on the target. Next, decrease the sample time and monitor the execution time of the controller. You can further decrease the sample time as long as the optimization safely completes within each sampling period under normal plant operating conditions. To reduce the sample time, you can also consider using:
Explicit MPC. While explicit MPC controllers have a faster execution time, they also have a larger memory footprint, since they store precomputed control laws. For more information, see Explicit MPC Design.
A suboptimal QP solution after a specified number of maximum solver iterations. For more information, see Suboptimal QP Solution.
A lower controller sample time does not necessarily provide better performance. In fact, you want to choose a sample time that is small enough to give you good performance but no smaller. For the same prediction time, smaller sample times result in larger prediction steps, which in turn produces a larger memory footprint and more complex optimization problem.
At each control interval, an MPC controller constructs a new QP problem, which is defined as:
subject to the linear inequality constraints
x is the solution vector.
H is the Hessian matrix.
A is a matrix of linear constraint coefficients.
f and b are vectors.
In generated C code, the following matrices are used to provide H, A, f, and b. Depending on the type and configuration of the MPC controller, these matrices are either constant or regenerated at each control interval.
|Constant Matrix||Size||Purpose||Implicit MPC||Implicit MPC with Online Weight Tuning||Adaptive MPC or LTV MPC|
|NM-by-NM||Inverse of the Hessian matrix, H||Constant||Regenerated||Regenerated|
|NM-by-NM||Inverse of the lower-triangular Cholesky decomposition of H|
|NC-by-NM||Linear constraint coefficients, A||Constant|
|Nxqp-by-(NM–1)||Used to generate f||Regenerated|
|NC-by-1||Used to generate b||Constant||Constant, except when there are custom constraints|
p is the prediction horizon.
Nmv is the number of manipulated variables.
Nmd is the number of measured disturbances.
Ny is the number of output variables.
NM is the number of optimization variables (m*Nmv+1, where m is the control horizon).
Nxqp is the number of states used for the QP problem; that is, the total number of the plant states and disturbance model states.
NC is the total number of constraints.
At each control interval, the generated C code computes f and b as:
xq is the vector of plant and disturbance model states estimated by the Kalman filter.
ml is the manipulated variable move from the previous control interval.
ut is the manipulated variable target.
vp is the sequence of measured disturbance signals across the prediction horizon.
rp is the sequence of reference signals across the prediction horizon.
When generating code in MATLAB, the
command generates these matrices and returns them in
You can generate code for MPC controllers that use a custom QP solver written in either C or Embedded MATLAB®. The controller calls this solver in place of the built-in QP solver at each control interval.
For an example, see Simulate and Generate Code for MPC Controller with Custom QP Solver. For more information on custom QP solvers, see Custom QP Solver.