Main Content

Export Simulink Model to Standalone FMU with Source Code

This example shows how to export Simulink® component to standalone Co-Simulation FMU 2.0 with source code using Simulink Compiler™ and Simulink Coder™. The source code is packed inside the sources folder of the Standalone FMU. A documentation file, index.html is also generated. It lists the steps to regenerate the binaries on another platform. This documentation file is located inside documentation folder of the Standalone FMU. The source code can be used for cross platform workflow.

A user may desire to export Simulink component to standalone FMU with source code for the following scenarios:

  • Cross-platform FMU support.

  • Hardware-in-Loop (HIL) simulation.

In this example, the aircraft flight control system is composed of two simulink models:

  • Aircraft Longitudinal Flight Control Plant Component: fmuexport_aircraft_flight_control_plant and

  • Top-level model: fmuexport_aircraft_flight_control_top.

fmuexport_aircraft_flight_control_plant models flight control for the longitudinal motion of an aircraft. First order linear approximations of the aircraft and actuator behavior are connected to an analog flight control design that uses the pilot's stick pitch command as the set point for the aircraft's pitch attitude and uses aircraft pitch angle along with pitch rate to determine commands. A simplified Dryden wind gust model is incorporated to perturb the system.

fmuexport_aircraft_flight_control_top is used to observe the change in the angle of attack reported by Aircraft Longitudinal Flight Control Plant Component with respect to the applied pilot's stick pitch command.

This examples exports fmuexport_aircraft_flight_control_plant to Standalone Co-Simulation FMU 2.0 with source code and list steps to compile the source code on 64-bit Linux® platform to generate binaries for reuse in fmuexport_aircraft_flight_control_top. For a list of tools that support FMI, see:

Export Aircraft Longitudinal Flight Control Plant Component to FMU with Source Code

Open the fmuexport_aircraft_flight_control_plant example model.


From Simulation tab, click drop-down button for Save. In Export Model To section, click Standalone FMU.... In FMU Export dialog, check the Access Source Code from FMU, configure wrapper model and icon settings, and specify save location for generated FMU.

Click Create to export to FMU. The fmuexport_aircraft_flight_control_plant.fmu file can be found at specified save location.

restoreval = get_param(0,'AutoSaveOptions');
newval = restoreval;
newval.SaveOnModelUpdate = false;
restoreOC = onCleanup(@()set_param(0,'AutoSaveOptions',restoreval));
% Export model to Standalone Co-Simulation FMU 2.0 with source code
evalc('exportToFMU2CS(''fmuexport_aircraft_flight_control_plant'',''SaveDirectory'', pwd, ''SaveSourceCodeToFMU'', ''on'',''AddIcon'',''off'')');
close_system('fmuexport_aircraft_flight_control_plant', 0);

FMU can also be exported with source code using command-line. In the MATLAB® command-line window, use exportToFMU2CS command:

% Export model to Standalone Co-Simulation FMU 2.0
exportToFMU2CS('fmuexport_aircraft_flight_control_plant', 'SaveDirectory', pwd, 'SaveSourceCodeToFMU', 'on');

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

The generated standalone Co-Simulation FMU 2.0 has source code in the sources folder inside the FMU

A documentation file(index.html) is also generated with steps to regenerate the binaries on Linux, Windows® and macOS platform. This documentation file is located at documentation/index.html

Integrate FMU Components in Simulink

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

set_param('fmuexport_aircraft_flight_control_top', 'SimulationCommand', 'Update');

close_system('fmuexport_aircraft_flight_control_top', 0);

The generated simulation result might be slightly different than simulation results observed for fmuexport_aircraft_flight_control_plant model in Simulink. This is expected, for more information refer to Co-Simulation Execution

Reuse the Standalone Co-Simulation FMU with Source Code for Cross Platform Workflow

Users can reuse the standalone co-simulation FMU with source code on another platform by regenerating the binaries and packing the generated binaries in the FMU. Given below are the steps to reuse the FMU on another platform.

1. Unzip the standalone co-simulation FMU with source code and compile the source code to generate the platform specific binary files.

gcc -I<directoryWithFMUHeader> -I<directoryWithSourceCode> -c
fmuexport_aircraft_flight_control_plant.c fmuexport_aircraft_flight_control_plant_data.c rtGetInf.c rtGetNaN.c rt_nonfinite.c RTWCG_FMU_util.c fmuexport_aircraft_flight_control_plant_fmu.c -fPIC
gcc -shared -o fmuexport_aircraft_flight_control_plant.o fmuexport_aircraft_flight_control_plant_data.o rtGetInf.o rtGetNaN.o rt_nonfinite.o RTWCG_FMU_util.o fmuexport_aircraft_flight_control_plant_fmu.o -lm

2. Move the generated binary file in the binaries folder.

% Example of folder structure when binaries were packed in FMU on 64-bit
% Linux platform

3. Repackage the files in FMU for cross-platform use