- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Calling FMU cosimulation code from C:...
Calling FMU cosimulation code from C: working example
Calling FMU cosimulation code from C: working example
Does anyone have a working example of a C code that calls an FMU library for cosimulation?
I tried to follow the Pseudo-code example that we can find in the FMI standard documentation (BTW, I do not really understand why, in a manual, you do not write a REAL code, but a pseudo one, that gives space to a lot of misunderstanding, just for saving a bunch of pages... damn)
I successfully call fmi2GetVersion, but I'm hitting exceptions when I call fmi2Instantiate. I'm sure that I'm wrongly initializing some variable, but I do not have any clue on how to do the right thing.
Moreover, from OpenModelica I do not have any resource folder: it has not been generated at the time I called build/translateModelFMU as I already said here.
The code below is linked to binaries/win64/model.lib
and I have binaries/win64 in my path, so the binaries/win64/model.dll is reached.
In the attachments there is a CMake minimal example from which I'm calling the FMU generated library.
I really appreciate any help.
Dario
Code:
#include "fmi2/fmi2Functions.h"
#include "fmi2/fmi2FunctionTypes.h"
int main(int argc, char* argv[])
{
//Set callback functions
fmi2CallbackAllocateMemory allocateMemory = calloc;
fmi2CallbackFreeMemory freeMemory = free;
fmi2StepFinished stepFinished = NULL;
fmi2ComponentEnvironment componentEnvironment = NULL;
fmi2Status loggerStatus = fmi2OK;
fmi2CallbackLogger logger = (componentEnvironment, "fullvehicle3D_Pacejka", loggerStatus, "category", "message");
fmi2CallbackFunctions cbf = { logger, allocateMemory, freeMemory, stepFinished, componentEnvironment };
const char* ver = fmi2GetVersion(); // it works!
fmi2Component mycomp = fmi2Instantiate(
"test",
fmi2CoSimulation,
"21491c75-15e3-4b05-903a-d801733579d9", // my guid
"path", // dummy, because I do not have any resource folder...
&cbf,
fmi2True,
fmi2False);
return 0;
}
- DarioMangoni
- 45 Posts
Re: Calling FMU cosimulation code from C: working example
Hi,
I am doing fmu instantiating now and confronted with the same problem as yours. I am wondering have you solved the problem?
When I instantiate an fmu, I need to initialize the callbackfunction, but what should be fmi2componentEnvironment in vc2015?
Thank you very much.
Re: Calling FMU cosimulation code from C: working example
Hi zb2865493,
yes, I solved the problem, but now my configuration is a bit more complex than this.
I make run-time linking of the DLL that requires some more steps...
Code:
SetDllDirectory(<directory to dll>);
HINSTANCE myGetProcIDDLL = LoadLibrary(<path to dll>);
and for any fmi function you are interested in...
Code:
fmi2InstantiateTYPE* _fmi2Instantiate = (fmi2InstantiateTYPE*)GetProcAddress(myGetProcIDDLL, "fmi2Instantiate");
fmi2DoStepTYPE* _fmi2DoStep = (fmi2DoStepTYPE*)GetProcAddress(myGetProcIDDLL, "fmi2DoStep");
build your own logger...
Code:
void mylogger(fmi2ComponentEnvironment c, fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message, ...)
{
...
}
typedef struct {
fmi2CallbackLogger logger;
fmi2CallbackAllocateMemory allocateMemory;
fmi2CallbackFreeMemory freeMemory;
fmi2StepFinished stepFinished;
fmi2ComponentEnvironment componentEnvironment;
} fmi2_callback_functions_t_aux;
fmi2_callback_functions_t_aux mycallbacks;
mycallbacks.logger = mylogger;
mycallbacks.allocateMemory = calloc;
mycallbacks.freeMemory = free;
mycallbacks.stepFinished = NULL;
mycallbacks.componentEnvironment = NULL;
fmi2Component component = _fmi2Instantiate(
"mytool",
fmi2CoSimulation,
<guid name>,
<path to resource folder>,
(const fmi2CallbackFunctions*)&mycallbacks,
fmi2False,
fmi2True);
I'm sorry but I cannot share with you a compilable snippet...
- DarioMangoni
- 45 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » Calling FMU cosimulation code from C:...