Frequently Asked Question

How to export FMU file from Simulink model and import it in Typhoon HIL toolchain (for MATLAB release 2022a)?
Last Updated 2 years ago

From the 2021a version, Matlab enables its users to export a Simulink model to FMU along with C source code. Although FMU could be exported in previous versions as well, those versions didn't support exporting C source code along with FMU.

Having C source code included in FMU is crucial for running real-time simulations in the Typhoon HIL toolchain.

Exporting FMU from Simulink

1. Open your model in Simulink. It is recommended that the path to your model does not contain empty spaces, so we recommend that you place _ instead of empty space.

2. Comment out parts of the model you don't want to export. Place Import components to the places inputs to the system and Outport components to the places where are outputs of the system.

3. There are two parameters that should be set in Model settings (Modeling tab)

  • Type of Solver (Model Settings->Solver->Solver Selection->Type), should be set to Fixed-step
  • Fixed step size (Model Settings->Solver->Solver details->Fixed step size), should be defined. (This value is important for parametrization of the FMU unit when it is imported in Typhoon HIL tool-chain)

4. Next step is to export FMU. This can be done in Save drop-down menu in the section Export Model To. Pressing Standalone FMU... will open the FMU Export window.

5. In the FMU export model you have to select Save source code to FMU, while other parameters can be selected as you want. When everything is selected, press Create and FMU will be generated in the folder you set.

Import FMU in Typhoon HIL tool-chain

Before you can import created FMU file, a small adjustment to the FMU file must be done.

1. Open the FMU file as an archive

2. Edit the "modelDescription.xml" file


3. Remove line: with "File name="lccstub.c" (This library is not supported by the HIL device, so without removing it, the model will not be able to compile successfully.)


4.  Save "modelDescription.xml"

5. Go inside the "SimpleFMU.fmu/sources/SimpleFMU_fmu.c" file (the path depends on the name of generated FMU file) and instead of this code:

if (fmuResourceLocation) {
modelData->resourceLocation = uriToLocal(fmuResourceLocation);
if (!modelData->resourceLocation) {
functions->logger(functions->componentEnvironment, instanceName, fmi2Error, "error",
"fmi2Instantiate: Memory allocation error.");
return NULL;
}
else if (strlen(modelData->resourceLocation) == 0){
functions->logger(functions->componentEnvironment, instanceName, fmi2Error, "error",
"fmi2Instantiate: uriToLocal cannot handle fmuResourceLocation.");
return NULL;
}
} else {
modelData->resourceLocation = fmuResourceLocation;
}

insert the following:

if (fmuResourceLocation) {
modelData->resourceLocation = uriToLocal(fmuResourceLocation);
if (!modelData->resourceLocation) {
functions->logger(functions->componentEnvironment, instanceName, fmi2Error, "error",
"fmi2Instantiate: Memory allocation error.");
return NULL;
}
} else {
modelData->resourceLocation = fmuResourceLocation;

}

6. Save the modified file.

7. Go inside the "SimpleFMU.fmu/sources/RTWCG_FMU_util.c file (the path depends on the name of generated FMU file) and instead of this code:

#ifdef _WIN64
const char* separator = "\\";
const char* platform = "win64";
#elif _WIN32
const char* separator = "\\";
const char* platform = "win32";
#elif __APPLE__
const char* separator = "/";
const char* platform = "darwin64";
#elif __linux__
const char* separator = "/";
const char* platform = "linux64";
#else
//# error Must specify OS.
#endif

Insert the following:

#ifdef _WIN64
const char* separator = "\\";
const char* platform = "win64";
#elif _WIN32
const char* separator = "\\";
const char* platform = "win32";
#elif __APPLE__
const char* separator = "/";
const char* platform = "darwin64";
#elif __linux__
const char* separator = "/";
const char* platform = "linux64";
#else
const char* separator = "/";
const char* platform = "linux64";
#endif

7. Save the modified file.

8. Import it in Typhoon HIL by dragging FMU import from the core library (core/Signal Processing/Extras/FMU Import)


9. In the properties of FMU Import, choose the FMU file you exported and modified.

10. You should choose the Execution rate to be the same or larger than the one you selected the value of Fixed step size in Simulink. If the execution rate is smaller than the one you set in Simulink, FMU behavior will not be correct.

This concludes the steps needed for the proper setup of the Functional Mock-up Unit. You can now start the simulation.

Please Wait!

Please wait... it will take a second!