This example shows how to simulate and generate real-time code for an MPC Controller block with Simulink® Coder™. Code can be generated in both single and double precisions.
To run this example, Simulink and Simulink Coder are required.
if~mpcchecktoolboxinstalled('simulink') disp('Simulink is required to run this example.')returnendif~mpcchecktoolboxinstalled('simulinkcoder') disp('Simulink Coder is required to run this example.');returnend
You must have write-permission to generate the relevant files and the executable. Therefore, before starting simulation and code generation, change the current directory to a temporary directory.
cwd = pwd; tmpdir = tempname; mkdir(tmpdir); cd(tmpdir);
Define a SISO plant.
plant = ss(tf([3 1],[1 0.6 1]));
Define the MPC controller for the plant.
Ts = 0.1;%Sample timep = 10;%Prediction horizonm = 2;%Control horizonWeights = struct(“MV”,0,'MVRate',0.01,'OV',1);% WeightsMV = struct('Min',-Inf,'Max',Inf,'RateMin',-100,'RateMax',100);% Input constraintsOV = struct('Min',-2,'Max',2);% Output constraintsmpcobj = mpc(plant,Ts,p,m,Weights,MV,OV);
By default, MPC Controller blocks use double-precision data for simulation and code generation.
Simulate the model in Simulink.
mdl1 ='mpc_rtwdemo'; open_system(mdl1) sim(mdl1)
-->Converting model to discrete time. -->Assuming output disturbance added to measured output channel #1 is integrated white noise. -->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.
The controller effort and the plant output are saved into base workspace as variablesu
andy
, respectively.
Build the model with theslbuild
command.
disp('Generating C code... Please wait until it finishes.') set_param(mdl1,'RTWVerbose','off') slbuild(mdl1);
Generating C code... Please wait until it finishes. ### Starting build procedure for: mpc_rtwdemo ### Successful completion of build procedure for: mpc_rtwdemo Build Summary Top model targets built: Model Action Rebuild Reason ============================================================================================ mpc_rtwdemo Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 42.282s
On a Windows® system, an executable file namedmpc_rtwdemo.exe
appears in the temporary directory after the build process finishes.
Run the executable.
ifispc disp('Running executable...') status = system(mdl1);elsedisp('The example only runs the executable on Windows system.')end
The example only runs the executable on Windows system.
After the executable completes successfully (status=0), a data file namedmpc_rtwdemo.mat
appears in the temporary directory.
Compare the responses from the generated code (rt_u
andrt_y
) with the responses from the previous simulation in Simulink (u
andy
).
The responses are numerically equal.
You can also configure the MPC block to use single-precision data in simulation and code generation.
mdl2 ='mpc_rtwdemo_single'; open_system(mdl2)
To do so, set theOutput data typeproperty of the MPC Controller block tosingle
.
Simulate the model in Simulink.
sim(mdl2)
The controller effort and the plant output are saved into base workspace as variablesu1
andy1
, respectively.
Build the model with theslbuild
command.
disp('Generating C code... Please wait until it finishes.') set_param(mdl2,'RTWVerbose','off') slbuild(mdl2);
Generating C code... Please wait until it finishes. ### Starting build procedure for: mpc_rtwdemo_single ### Successful completion of build procedure for: mpc_rtwdemo_single Build Summary Top model targets built: Model Action Rebuild Reason =================================================================================================== mpc_rtwdemo_single Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 28.411s
On a Windows system, an executable file namedmpc_rtwdemo_single.exe
appears in the temporary directory after the build process finishes
Run the executable.
ifispc disp('Running executable...') status = system(mdl2);elsedisp('The example only runs the executable on Windows system.')end
The example only runs the executable on Windows system.
After the executable completes successfully (status=0), a data file namedmpc_rtwdemo_single.mat
appears in the temporary directory.
Compare the responses from the generated code (rt_u1
andrt_y1
) with the responses from the previous simulation in Simulink (u1
andy1
).
The responses are numerically equal.
Close the Simulink models, and return to the original directory.
bdclose(mdl1) bdclose(mdl2) cd(cwd)