simulation and code generation using simulink coder -凯发k8网页登录
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.
required products
to run this example, simulink and simulink coder are required.
if ~mpcchecktoolboxinstalled('simulink') disp('simulink is required to run this example.') return end if ~mpcchecktoolboxinstalled('simulinkcoder') disp('simulink coder is required to run this example.'); return end
configure environment
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 plant model and mpc controller
define a siso plant.
plant = ss(tf([3 1],[1 0.6 1]));
define the mpc controller for the plant.
ts = 0.1; %sample time p = 10; %prediction horizon m = 2; %control horizon weights = struct('mv',0,'mvrate',0.01,'ov',1); % weights mv = struct('min',-inf,'max',inf,'ratemin',-100,'ratemax',100); % input constraints ov = struct('min',-2,'max',2); % output constraints mpcobj = mpc(plant,ts,p,m,weights,mv,ov);
simulate and generate code in double-precision
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 #1 is integrated white noise. -->"model.noise" is empty. assuming white noise on each measured output.
the controller effort and the plant output are saved into base workspace as variables u
and y
, respectively.
build the model with the slbuild
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 39.493s
on a windows® system, an executable file named mpc_rtwdemo.exe
appears in the temporary directory after the build process finishes.
run the executable.
if ispc disp('running executable...') status = system(mdl1); else disp('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 named mpc_rtwdemo.mat
appears in the temporary directory.
compare the responses from the generated code (rt_u
and rt_y
) with the responses from the previous simulation in simulink (u
and y
).
the responses are numerically equal.
simulate and generate code in single-precision
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 the output data type property of the mpc controller block to single
.
simulate the model in simulink.
sim(mdl2)
the controller effort and the plant output are saved into base workspace as variables u1
and y1
, respectively.
build the model with the slbuild
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 24.288s
on a windows system, an executable file named mpc_rtwdemo_single.exe
appears in the temporary directory after the build process finishes
run the executable.
if ispc disp('running executable...') status = system(mdl2); else disp('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 named mpc_rtwdemo_single.mat
appears in the temporary directory.
compare the responses from the generated code (rt_u1
and rt_y1
) with the responses from the previous simulation in simulink (u1
and y1
).
the responses are numerically equal.
close the simulink models, and return to the original directory.
bdclose(mdl1) bdclose(mdl2) cd(cwd)