Wiki

Clone wiki

C++ FMU Builder / Home

Welcome

This project is devoted to allow the construction of FMUs based on C++ simulators. You can find the releases in the download section. Please note we have created a tutorial to show how to develop your FMUs from c++ code:

Creating car simulator from the scratch and testing the FMU

License

C++ FMU builder is licensed under the LGPL v3, the LGPL guarantees that this library will stay open source, protecting your work.

Brief how to

Once you have downloaded the releases in the download section you can open the jar file with double click (note jar files must be associated to the JVM in your OS). Once you open it for the first time you will see the following screen:

Initial screen of the FPD

FPD is the FMU project designer. In this tool you can specify the needs of the FMU you want to create. To do so, you will be assisted by the editor all times. Basically, using this editor you will need to define which is the interface of your future FMU which mostly regards the definition of the variables. Here you have an example of how a definition could be:

#!bash
[FMU]
name: Car
description: Simulation model of Car system

[Variable]
name: speed
type: Real
causality: input
variability: discrete
startValue: 0

[Variable]
name: distance
type: Real
causality: output
variability: continuous
initial: exact
startValue: 0

[Variable]
name: coordinates[0]
type: Real
causality: output
variability: continuous
initial: exact
startValue: 0

[Variable]
name: coordinates[1]
type: Real
causality: output
variability: continuous
initial: exact
startValue: 0

[Variable]
name: numberOfDoors
type: Integer
causality: parameter
variability: fixed
initial: exact
startValue: 5

[Variable]
name: maxPower
type: Integer
causality: parameter
variability: fixed
initial: exact
startValue: 90

[Variable]
name: abs
type: Boolean
causality: parameter
variability: fixed
initial: exact
startValue: true

[Variable]
name: mp3
type: Boolean
causality: parameter
variability: fixed
initial: exact
startValue: true

[Variable]
name: fuel
type: String
causality: parameter
variability: fixed
initial: exact
startValue: Diesel

After you have finished the design, FPD will generate the project according to the structure in the figure below. To do so you must click in "tools/generate project". This project is provided to be opened in the C++ IDE: Qt creator, but it also provides a makefile to build the project.

tree fpd.PNG

The folder fmi is part of the framework to develop the fmus and consists of the classes that give support to the exchange of information through the dynamic library API. These files must be considered as autogenerated code and should not be modified. callbacks.h and callbacks.cpp files contain the functions to allocate memory, free memory and log.

In the context of the FMU, it is mandatory to use these functions. The way these functions are used is seen in the Simulation.h (gen folder). In fact, it is advisable to follow the guideline presented in Simulation.h of overriding the new and delete methods to use the callback functions in your own classes. This file is autogenerated and should not be modified. skeleton.h and cpp contains the interface that must be followed by the inheriting class of the Car. This interface are the functions that are required to be filled so that the skeleton use them to communicate the master algorithm (MA) with the simulator made in C++. This file is autogenerated and should not be modified.

car.h (in gen folder) and car.cpp (in root folder) contain the example simulator that is to be embedded in the FMU. The header is fully autogenerated and should not be modified. It contains the definition of all the interface to be filled in the cpp file and the variables that were defined in the FPD. The cpp contains the implementations that must be made by the developer of the simulator. In this file, all methods must be filled according to the needs of the simulator and they are the point of integration with the FMU framework. This is the development of init, doStep, terminate and reset. The methods below of getXXX and setXXX are also autogenerated and should not be modified.

This project is fully openable and editable with QT. If you open it in QT you will be able to make the implementation you need for your simulation and to compile it generating the dynamic library for the OS in which you are executing it. Once you locate the dynamic library, you can place it in the root folder of the project under the folder "binaries/<system>/", being system the OS you are using (e.g. win64, linux64...). After doing that, you can use the "tools/build FMU" in the FPD interface to build the FMU.

At that point you will have your FMU, congratulations!!

  • NOTE: since version 1.2.0, FPD generates also a makefile (dependant on your system) so that you can use it to build the project and generate the dynamic libraries. Also take into account that FPD will try to invoke the makefile when you click on "build". Makefile can be edited to customise to the project needs.

Command line interface (CLI)

In case you want to have this procedure automated, you can directly generate the fpd file (it is a simple text file) and use the CLI to generate the project and to build the FMU. To do so, you can invoke the jar of the download folder in the terminal like this:

#!bash

java -jar fpd.jar operation=generate fpdFile=D:/Car/Car.fpd #Generates project structure
java -jar fpd.jar operation=build fpdFile=D:/Car/Car.fpd #Builds the FMU once the binaries are correctly placed

Troubleshooting

Dependency management

The previous generated FMU can fail when opened in a FMU Master algorithm since the dynamic library may depend on other dynamic libraries of your system (depending on the system). This guide explains how to check the dependencies required by a dynamic libraries so that you can collect them and include it in the FMU to avoid the mentioned loading problems. To do this, we recommend this tool in windows. In linux you have the "ldd" command. If we use this tool with the generated FMU we will see that the Car.dll depends on:

dependencies.png

In case the FMU is not correctly loaded, these dynamic libraries must be collected and added to the binaries/<yoursystem>/ folder so that the FMU is built considering these dynamic libraries. Note that dependencies can be transitives. That is, these dynamic libraries may depend on other libraries that must be also collected and included. Regarding this case, the list of dynamic libraries required for the win64 system are:

  • libgcc_s_seh-1.dll
  • libstdc++-6.dll
  • libwinpthread-1.dll

Contributors

José Évora Gómez (Monentia)

Updated