Main Content

Export Simulink Model with Protected Model and FMU Import Block to Standalone FMU

This example shows how to export a Simulink® model with external references to Standalone FMU. In this example, the model f14_flight_control demonstrates exporting protected model and FMU Import block to a Standalone FMU.

A Simulink Compiler™ license is required for standalone FMU Export. A Simulink Coder™ license is required to create protected models.

The example consists of three steps:

Export Simulink Model to Protected Model with FMU Code Generation Capability

In this section, the following steps illustrate how to enable FMU Code Generation capability of a protected model for FMU export. FMU code generation artifacts must be packed into the protected model before exporting to standalone FMU. Protected model author can use the following steps to create a protected model with FMU code generation artifacts. Alternatively, protected model author can add the code generation artifacts to an existing protected model with API Simulink.ProtectedModel.addTarget. If your protected model does not contain FMU code generation artifacts, please contact the protected model author. FMU export supports protected model code generation with grt and fmu2cs artifacts. Use Simulink.ProtectedModel.getSupportedTargets to get a list of targets that protected model supports.

The following steps use fmu2cs system target file as a example to generate protected model. This example opens model Controller, sets system target file to fmu2cs.tlc, and exports model to a protected model. System target file grt.tlc is also supported for this workflow. Compare to grt.tlc, fmu2cs.tlc supports FMU import block code generation, which allow you to export a model with FMU import blocks to a protected model. If your protected model contains artifacts for both system target file, fmu2cs will take effect when exporting standalone FMU. To learn more about protected model code generation, see explore protected model capabilities.

Controller model

% Open example model Controller
model = "Controller";
open_system(model);

After the model is opened, go to the configuration dialog and update System target file to fmu2cs.tlc in Configuration Parameter > Code Generation.

% alternative command-line option to set protected model code generation target to fmu2cs.tlc
% this allows the protected model to be exported as standalone FMU
set_param(model, 'SystemTargetFile', 'fmu2cs.tlc');
set_param(model, 'GRTInterface', 'off');
set_param(model, 'UseToolchainInfoCompliant', 'on');

% to use grt code generation, use command:
% set_param(model, 'SystemTargetFile', 'grt.tlc');

Inconsistent hardware implementation of hardware attributes can result in failure when exporting standalone FMU. To configure these parameters, open Configuration Parameters > Hardware Implementation. Model Controller uses the following setting for code generation. The same setting is used in model f14_flight_control.

To generate a protected model, in the Simulink Toolstrip, on the Simulation tab, select Save > Protected Model. The Protected Model Creator opens and provides options for creating a protected model.

To enable code generation for standalone FMU export, select Code generation and set Content type to Obfuscated source code.

Protected Model Creator

% generate protected model
Simulink.ModelReference.protect(model,'Mode','CodeGeneration','ObfuscateCode',true);
Creating protected model for 'Controller'.
### Searching for referenced models in model 'Controller'.
### Found 1 model references to update.
### Starting serial model reference simulation build.
### Successfully updated the model reference simulation target for: Controller
### Searching for referenced models in model 'Controller'.
### Found 1 model references to update.
### Starting serial model reference code generation build.
### Checking status of model reference code generation target for model 'Controller'.
### Model reference code generation target (Controller.c) for model Controller is out of date because Controller.c does not exist.
### Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'Controller'.
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: /tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72/slprj/fmu2cs/Controller
### Invoking Target Language Compiler on Controller.rtw
### Using System Target File: /mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/toolbox/shared/simulink/fmuexport/fmu2cs.tlc
.................................................................### Saving binary information cache.
### Using toolchain: GNU gcc/g++ | gmake (64-bit Linux)
### Creating '/tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72/slprj/fmu2cs/Controller/Controller.mk' ...
### Building 'Controller_rtwlib': "/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/bin/glnxa64/gmake"  -f Controller.mk all
gcc -c -fwrapv -fPIC -O0 -msse2 -fno-predictive-commoning -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DMAT_FILE=0 -DONESTEPFCN=0 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0  -DTID01EQ=0 -DMODEL=Controller -DNUMST=1 -DNCSTATES=4 -DHAVESTDIO -DRT -DUSE_RTMODEL -I/tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72 -I/tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72/slprj/fmu2cs/Controller -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/extern/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/simulink/include -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src/ext_mode/common -I/tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72/slprj/fmu2cs/_sharedutils -I/mathworks/devel/bat/filer/batfs1904-0/Bdoc24a.2528353/build/matlab/rtw/c/src/rapid/fmu/fmi2 -o "Controller.o" "/tmp/Bdoc24a_2528353_479333/tpbec330fa_c6cc_4a8e_80e7_d7a30813df72/slprj/fmu2cs/Controller/Controller.c"
### Creating static library ./Controller_rtwlib.a ...
ar ruvs  ./Controller_rtwlib.a Controller.o
ar: creating ./Controller_rtwlib.a
a - Controller.o
### Created: ./Controller_rtwlib.a
### Successfully generated all binary outputs.
gmake: Nothing to be done for `all'.
Finished creating protected model '/tmp/Bdoc24a_2528353_479333/tpf67a1df1/simulinkcompiler-ex90564728/Controller.slxp'.
% close model after protected model is generated
close_system(model);

Export Simulink Model to Standalone FMU with Tunable Parameters

This section exports a reference model Aircraft_Dynamics_Model as a standalone FMU that will be used in nested FMU export workflow.

Aircraft_Dynamics_Model

% open model Aircraft_dynamics_Model
model = "Aircraft_Dynamics_Model";
open_system(model);

The figure below shows the Export Standalone FMU dialog. You can manually select model variables exposed to FMU interface by using Parameter Table under Access the Parameters of FMU... in FMU export dialog. In this example, we expose variables Md, Mw, Uo, and Zd. Other features like pack source code, configure model variables, add additional resources to FMU package are available via UI. Read more about the Standalone FMU export functionality: Export Simulink Model to Standalone FMU.

Note: You may see a warning message indicating variable names are not unique, too long, or contain invalid characters and they will be renamed. This is expected if you have variable names meet above conditions, and you will see the renamed variables when you import the FMU back to a FMU block.

% export model Aircraft dynamics to standalone FMU
exportToFMU2CS(model,'CreateModelAfterGeneratingFMU', 'off');
Setting System Target to FMU 'Co-Simulation' for model 'Aircraft_Dynamics_Model'.
Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'Aircraft_Dynamics_Model'.
### 'GenerateComments' is disabled for 'Co-Simulation' FMU Export.
Warning: The following variable names are not unique, too long, or contain invalid characters: 'Elevator
Deflection
d (deg),Vertical Gust
wGust (ft/sec),Rotary Gust
qGust (rad/sec),Vertical Velocity
w (ft/sec),Pitch Rate
q (rad/sec)'. They will be renamed to: 'ElevatorDeflectionD_deg_,VerticalGustWGust_ft_sec_,RotaryGustQGust_rad_sec_,VerticalVelocityW_ft_sec_,PitchRateQ_rad_sec_'.
Build Summary

Top model targets built:

Model                    Action                        Rebuild Reason                                    
=========================================================================================================
Aircraft_Dynamics_Model  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 20.428s
### Model was successfully exported to 'Co-Simulation' FMU: '/tmp/Bdoc24a_2528353_479333/tpf67a1df1/simulinkcompiler-ex90564728/Aircraft_Dynamics_Model.fmu'.
close_system(model);

Export Simulink Model with External References to Standalone FMU

This section illustrates how to export Simulink model f14_flight_control with external references to a Standalone FMU with tunable parameters. Model f14_flight_control uses the protected model and FMU generated in the previous step, Model block Controller references the protected model Controller.slxp, and FMU Import block Aircraft Dynamics Model references Aircraft_Dynamics_Model.fmu.

f14_flight_control

% open top model that reference the protected model and FMU
model = "f14_flight_control";
open_system(model);

Open FMU Import Block dialog and configure tunable parameter to be exported in the nested FMU. Parameters Md, Mw, Uo, Zd are Simulink.Parameter objects defined in model workspace.

% Associate parameter in FMU export dialog with model variables
% This allows the variables to be export as tunable parameter in the generated nested FMU
set('f14_flight_control/Aircraft Dynamics Model','Md', 'Md');
set('f14_flight_control/Aircraft Dynamics Model','Mw', 'Mw');
set('f14_flight_control/Aircraft Dynamics Model','Uo', 'Uo');
set('f14_flight_control/Aircraft Dynamics Model','Zd', 'Zd');

To build and export model f14_flight_control to a standalone FMU, click drop-down button for Save from Simulation tab and select Standalone FMU. Follow the export procedure as described in previous section.

% Export model to Standalone Co-Simulation FMU 2.0
exportToFMU2CS(model,'CreateModelAfterGeneratingFMU', 'on');

A standalone FMU is generated in the Destination folder specified from the export dialog. A harness model is created and opened if you select Create model after generating standalone FMU.

f14_flight_control_harness

sdfdw

close_system(model);