Main Content

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

This example shows how to export 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.

Simulink Compiler™ license is required for standalone FMU Export and 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 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. Use Simulink.ProtectedModel.getSupportedTargets to get a list of targets that protected model supports.

This example opens model Controller, sets system target file to fmu2cs.tlc, and exports model to protected model.

% Open example model Controller
model = "Controller";

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');

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

Generate Protected Model from Simulation tab and select Save > Protected Model. A dialog box opens where user can select options for creating a protected model.

To enable code generation for standalone FMU export, check Use Generated Code and select Obfuscated source code in Content type.

% generate protected model
### Starting serial model reference simulation build.
### Successfully updated the model reference simulation target for: Controller

Build Summary

Simulation targets built:

Model       Action                        Rebuild Reason                         
Controller  Code generated and compiled.  Controller_msp.mexa64 does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 20.883s
### 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/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981/slprj/fmu2cs/Controller
### Invoking Target Language Compiler on Controller.rtw
### Using System Target File: /mathworks/devel/bat/Bdoc22b/build/matlab/toolbox/shared/simulink/fmuexport/fmu2cs.tlc
.................................................................### Saving binary information cache.
### Using toolchain: GNU gcc/g++ | gmake (64-bit Linux)
### Creating '/tmp/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981/slprj/fmu2cs/Controller/' ...
### Building 'Controller_rtwlib': "/mathworks/devel/bat/Bdoc22b/build/matlab/bin/glnxa64/gmake"  -f all
gcc -c -fwrapv -fPIC -O0 -msse2 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DMAT_FILE=0 -DONESTEPFCN=1 -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/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981 -I/tmp/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981/slprj/fmu2cs/Controller -I/mathworks/devel/bat/Bdoc22b/build/matlab/extern/include -I/mathworks/devel/bat/Bdoc22b/build/matlab/simulink/include -I/mathworks/devel/bat/Bdoc22b/build/matlab/rtw/c/src -I/mathworks/devel/bat/Bdoc22b/build/matlab/rtw/c/src/ext_mode/common -I/tmp/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981/slprj/fmu2cs/_sharedutils -I/mathworks/devel/bat/Bdoc22b/build/matlab/toolbox/shared/simulink/fmuexport/fmi2_noprefix -o "Controller.o" "/tmp/Bdoc22b_2054784_2358925/tpb1f35ea7_41b6_4d35_9058_682db8b23981/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'.

Build Summary

Code generation targets built:

Model       Action                        Rebuild Reason                
Controller  Code generated and compiled.  Controller.c does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 8.324s
% close model after protected model is generated

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.

% open model Aircraft_dynamics_Model
model = "Aircraft_Dynamics_Model";

The figure below shows the Export Standalone FMU dialog. User 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 Standalone FMU Export.
Warning: The following variable names are not unique, too long, or contain invalid characters: 'Elevator
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 11.229s
### Model was successfully exported to co-simulation standalone FMU: '/tmp/Bdoc22b_2054784_2358925/tp440cf97a/simulinkcompiler-ex90564728/Aircraft_Dynamics_Model.fmu'.

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.

% open top model that reference the protedted model and FMU
model = "f14_flight_control";

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 the user selects Create model after generating standalone FMU.