Export Simulink Model to Standalone FMU with Source Code
This example shows how to export Simulink® model to a standalone Co-Simulation FMU 2.0 with C++ source code using Simulink Compiler™ and Simulink Coder™. You can generate C or C++ source code for the FMU. 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
andTop-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 C++ 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: https://fmi-standard.org/tools/.
Export Aircraft Longitudinal Flight Control Plant Component to FMU with Source Code
Open the fmuexport_aircraft_flight_control_plant
example model.
open_system('fmuexport_aircraft_flight_control_plant');
Set the desired language for the source code as C++ by specifying it as the target language for code generation.
set_param('fmuexport_aircraft_flight_control_plant','TargetLang','C++');
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; set_param(0,'AutoSaveOptions',newval); restoreOC = onCleanup(@()set_param(0,'AutoSaveOptions',restoreval)); % Export model to Standalone Co-Simulation FMU 2.0 with source code evalc('exportToFMU(''fmuexport_aircraft_flight_control_plant'',''FMIVersion'',''2.0'',''FMUType'',''CS'',''SaveDirectory'',pwd,''SaveSourceCodeToFMU'',''on'',''AddIcon'',''off'')'); restoreOC.delete; 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 exportToFMU
command:
% Export model to Standalone Co-Simulation FMU 2.0 exportToFMU('fmuexport_aircraft_flight_control_plant', 'FMIVersion', '2.0', 'FMUType', 'CS,''SaveDirectory', pwd, 'SaveSourceCodeToFMU', 'on');
You can use optional arguments CreateModelAfterGeneratingFMU, AddIcon, and SaveDirectory to configure FMU export settings. For more information, see exportToFMU
.
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.
open_system('fmuexport_aircraft_flight_control_top'); set_param('fmuexport_aircraft_flight_control_top', 'SimulationCommand', 'Update'); sim('fmuexport_aircraft_flight_control_top');
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.
%Example g++ -I<directoryWithFMUHeader> -I<directoryWithSourceCode> -c <source files in model description.xml> -fPIC fmuexport_aircraft_flight_control_plant.cpp fmuexport_aircraft_flight_control_plant_data.cpp RTWCG_FMU_util.c fmuexport_aircraft_flight_control_plant_fmu.cpp -fPIC g++ -shared -o fmuexport_aircraft_flight_control_plant.so fmuexport_aircraft_flight_control_plant.o fmuexport_aircraft_flight_control_plant_data.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