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 Coder™
Embedded Coder^{®}
Simulink PLC Coder™
Simulink Real-Time™
The sampling rate that a controller can achieve in real-time environment is system-dependent. For example, for a typical small MIMO control application running on Simulink Real-Time, the sampling rate can go as low as 1–10 ms. To determine the sampling rate, first test a less aggressive controller whose sampling rate produces acceptable performance on the target. Next, increase the sampling rate and monitor the execution time used by the controller. You can further decrease the sampling rate as long as the optimization safely completes within each sampling period under the normal plant operations. To reduce the sampling rate, you can also consider using explicit MPC. However, explicit MPC controllers have a larger memory footprint, since they store precomputed control laws.
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.
Note:
The MPC Controller block is implemented using
the MATLAB
Function (Simulink) block. To see the structure, right-click the block
and select Mask > Look
Under Mask. Open the |
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 getCodeGenerationData
.
To verify that your controller produces the expected
closed-loop results, simulate it using mpcmoveCodeGeneration
in
place of mpcmove
.
Generate code for mpcmoveCodeGeneration
using codegen
. This step requires MATLAB Coder software.
For more information, see Generate Code To Compute Optimal MPC Moves in MATLAB.
At each control interval, an MPC controller constructs a new QP problem, which is defined as:
$$\underset{x}{Min}(\frac{1}{2}{x}^{\u22ba}Hx+{f}^{\u22ba}x)$$
subject to the linear inequality constraints
$$Ax\ge b$$
where
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 |
---|---|---|---|---|---|
Hinv | N_{M}-by-N_{M} | Inverse of the Hessian matrix, H | Constant | Regenerated | Regenerated |
Linv | N_{M}-by-N_{M} | Inverse of the lower-triangular Cholesky decomposition of H | |||
Ac | N_{C}-by-N_{M} | Linear constraint coefficients, A | Constant | ||
Kx | N_{xqp}-by-(N_{M}–1) | Used to generate f | Regenerated | ||
Kr | p*N_{y}-by-(N_{M}–1) | ||||
Ku1 | N_{mv}-by-(N_{M}–1) | ||||
Kv | (N_{md}+1)*(p+1)-by-(N_{M}–1) | ||||
Kut | p*N_{mv}-by-(N_{M}–1) | ||||
Mlim | N_{C}-by-1 | Used to generate b | Constant | Constant, except when there are custom constraints | |
Mx | N_{C}-by-N_{xqp} | Regenerated | |||
Mu1 | N_{C}-by-N_{mv} | ||||
Mv | N_{C}-by-(N_{md}+1)*(p+1) |
Here
p is the prediction horizon.
N_{mv} is the number of manipulated variables.
N_{md} is the number of measured disturbances.
N_{y} is the number of output variables.
N_{M} is the number of optimization variables (m*N_{mv}+1, where m is the control horizon).
N_{xqp} is the number of states used for the QP problem; that is, the total number of the plant states and disturbance model states.
N_{C} is the total number of constraints.
At each control interval, the generated C code computes f and b as:
$$f=K{x}^{\u22ba}\ast {x}_{q}+K{r}^{\u22ba}\ast {r}_{p}+Ku{1}^{\u22ba}\ast {m}_{l}+K{v}^{\u22ba}\ast {v}_{p}+Ku{t}^{\u22ba}\ast {u}_{t}$$
$$b=-\left(Mlim+Mx\ast {x}_{q}+Mu1\ast {m}_{l}+Mv\ast {v}_{p}\right)$$
where
x_{q} is the vector of plant and disturbance model states estimated by the Kalman filter.
m_{l} is the manipulated variable move from the previous control interval.
u_{t} is the manipulated variable target.
v_{p} is the sequence of measured disturbance signals across the prediction horizon.
r_{p} is the sequence of reference signals across the prediction horizon.
Note:
When generating code in MATLAB, the |