Main Content

Export Simulink Model to Standalone FMU with User Specified Files and Archived Project with Harness Model

This example shows how to export Simulink® component to standalone Co-Simulation FMU 2.0 with user specified documentation and harness model using Simulink Compiler™. This example demonstrate following features of Standalone FMU Export:

  • User file packing.

  • Archived project generation with harness model.

You may want too add files in the generated Standalone FMU for the following scenarios:

  • FMU requires documentation and/or license information

  • FMU needs additional files to determine its initial states

  • FMU requires additional meta-data information for compliance.

Similarly, you may want to generated archived project with harness model for:

  • Cross team collaboration for System Design in Simulink

  • SIL and functional testing using Simulink Test™.

This example exports fmuexport_fuelsys_controller to standalone FMU with specified documentation. The documentation is in form of index.html with supporting image files inside images folders. This documentation outlines the behavior of component modeled using standalone FMU. The archived project generated can be shared for reuse in Simulink. For a detailed explanation of the model, see:

In this example, the air-fuel ratio control system is composed of three Simulink models:

  • Fuel Rate Control Component: fmuexport_fuelsys_controller,

  • Engine Gas Dynamics Component: fmuexport_fuelsys_plant, and

  • top-level model fmuexport_fuelsys_top.

For a list of tools that support FMI, see: https://fmi-standard.org/tools/.

Export Fuel Rate Control Component to FMU with User Specified Documentation

Open the fmuexport_fuelsys_controller example model.

open_system('fmuexport_fuelsys_controller');

From Simulation tab, click drop-down button for Save. In Export Model To section, click Standalone FMU.... In FMU Export dialog, click Access Additional Resource to expand the section for adding user specified files

Click Add File to add index.html in DocumentFiles folder

Click the dropdown associated with Add File and select Add Folder to add images folder inside DocumentFiles

Click Select All button to select the entries in the spreadsheet. Click one of the rows under Destination column and update the destination to documentation

Click Create button to export standalone FMU for Fuel Rate Control Component

After FMU is generated, unzip the generated FMU and notice the presence of index.html and images folder under documentation

restoreval = get_param(0,'AutoSaveOptions');
newval = restoreval;
% Disable auto save
newval.SaveOnModelUpdate = false;
set_param(0,'AutoSaveOptions',newval);
restoreOC = onCleanup(@()set_param(0,'AutoSaveOptions',restoreval));
% Export Simulink model to Standalone Co-Simulation FMU 2.0
evalc('exportToFMU2CS(''fmuexport_fuelsys_controller'', ''Package'', {''documentation'',{fullfile(pwd,''DocumentFiles'',''index.html''),fullfile(pwd,''DocumentFiles'',''images'')}},''ExportedContent'',''project'',''AddIcon'',''off'')');
restoreOC.delete;
close_system('fmuexport_fuelsys_controller', 0);

You can also export FMU and archived project using command-line. At the MATLAB® command line, use exportToFMU2CS command.

% Export model to Standalone Co-Simulation FMU 2.0 with user specified
% file
exportToFMU2CS('fmuexport_fuelsys_controller', 'Package', {'documentation',{fullfile(pwd,'DocumentFiles','index.html'),fullfile(pwd,'DocumentFiles','images')}},'ExportedContent','project');

You can use optional arguments ProjectName, AddIcon, and SaveDirectory to configure FMU export settings. For more information, call help ExportToFMU2CS.

Integrate Standalone FMU with Simulink Model

Once the FMU is successfully exported, you may use the top model fmuexport_fuelsys_top to fully integrate the system for testing.

clear Atm PumpCon RampRateKiZ SpeedVect max_press min_press p0 st_range ...
    PressEst RampRateKiX SonicFlow ThrotEst hys max_speed min_speed ...
    restoreOC zero_thresh PressVect RampRateKiY SpeedEst ThrotVect ...
    max_ego max_throt min_throt s;
open_system('fmuexport_fuelsys_top');

In the fmuexport_fuelsys_top, you can open the documentation file packed in the FMU by launching the block dialog for Fuel Rate Control and clicking Open FMU Documentation File.

set_param('fmuexport_fuelsys_top', 'SimulationCommand', 'Update');
set_param('fmuexport_fuelsys_top', 'StopTime', '16');
sim('fmuexport_fuelsys_top');

bdclose('fmuexport_fuelsys_top');
clear ans sldemo_fuelsys_output;

The generated simulation result might be slightly different than Model Fault-Tolerant Fuel Control System. This is expected, for more information see, Co-Simulation Execution

Integrate Harness Model in Archived Project with Simulink Model

You may also integrate the harness model inside the archived project in fmuexport_fuelsys_top_harness_model by unzipping fmuexport_fuelsys_controller_fmu.mlproj archived project file in the current directory and executing the following command in the MATLAB command window.

%% Script to simulate the extracted harness model in Simulink
open_system('fmuexport_fuelsys_top_harness_model');
set_param('fmuexport_fuelsys_top_harness_model', 'SimulationCommand', 'Update');
set_param('fmuexport_fuelsys_top_harness_model', 'StopTime', '16');
sim('fmuexport_fuelsys_top_harness_model');

You can also use the harness model for functional verification in Simulink. For more information, see Create Test Harnesses from Standalone Models (Simulink Test).