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 reference targets 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 reference targets to update.
### Starting serial model reference code generation build.
### Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'Controller'.
Finished creating protected model '/tmp/Bdoc24b_2725827_1563751/tp80f8f436/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
exportToFMU(model, 'FMIVersion', '3.0', 'FMUType', 'CS', '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:

Model                    Build Reason                                         Status                        Build Duration
==========================================================================================================================
Aircraft_Dynamics_Model  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 14.776s 

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 15.459s
### Model was successfully exported to 'Co-Simulation' FMU: '/tmp/Bdoc24b_2725827_1563751/tp80f8f436/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_param('f14_flight_control/Aircraft Dynamics Model','Md', 'Md');
set_param('f14_flight_control/Aircraft Dynamics Model','Mw', 'Mw');
set_param('f14_flight_control/Aircraft Dynamics Model','Uo', 'Uo');
set_param('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
exportToFMU(model,'FMIVersion','2.0','FMUType','CS','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);