Nandha
Nandha

Reputation: 882

How to export c file/function to FMU using fmusdk

This is my function. I am trying to export this code to fmu. I am using fmusdk.

For each cycle(time step),

  1. my input should be changed to the value given during simulation.
  2. myexecute() should be called.
  3. the values of input and pout should be stored during simulation, so that we can plot the values after the simulation.

I tried the examples(BouncingBall and values) given in fmusdk. I have created corresponding fmus and imported them in Amesim. They are working fine. But i am unable to figure out how to do the same for my C file/function

/*
 * Execution function
 */
void  myexecute(double *input, double *pout) 
{
  (*pout) = 2 * (*input);
}

I examined the bouncingBall.c and values.c, they have only four methods

setStartValues(ModelInstance *comp)
calculateValues(ModelInstance *comp)
getReal(ModelInstance *comp, fmi2ValueReference vr)
void eventUpdate(ModelInstance *comp, fmi2EventInfo *eventInfo, int isTimeEvent, int isNewEventIteration) 

Can someone help me in accomplishing this fmi export? In short i am looking for the contents for the above 4 functions. Explaination for the above 4 methods will also be sufficient. I can create the contents for the functions.

Upvotes: 2

Views: 1868

Answers (1)

user7460932
user7460932

Reputation:

Consider the C file given below similar to the files you mentioned bouncingBall.c or valus.c . Since you have referred the function myexecute from another c file, replace yourCFile.c and/or yourHFile.h with the correct files. Also your modelDescription.xml should be in sync with this C file. For ex) guid value should be the same in both files. Also the value reference for the scalar variables should be the same. Analyse <ScalarVariable name="pin" valueReference="0"> and #define pin_ 0 . Similarly for pout. Create a folder structure similar to bouncingBall. Create a separate batch file, since we have to include additional files(yourCFile.c and/or yourHFile.h)

// define class name and unique id
#define MODEL_IDENTIFIER modelName
#define MODEL_GUID "{8c4e810f-3df3-4a00-8276-176fa3c90123}"

// define model size
#define NUMBER_OF_REALS 2
#define NUMBER_OF_INTEGERS 0
#define NUMBER_OF_BOOLEANS 0
#define NUMBER_OF_STRINGS 0
#define NUMBER_OF_STATES 1
#define NUMBER_OF_EVENT_INDICATORS 0

// include fmu header files, typedefs and macros
#include "fmuTemplate.h"
#include "yourHFile.h"
#include "yourCFile.c"

// define all model variables and their value references
// conventions used here:
// - if x is a variable, then macro x_ is its variable reference
// - the vr of a variable is its index in array  r, i, b or s
// - if k is the vr of a real state, then k+1 is the vr of its derivative
#define pin_        0
#define pout_       1

// define state vector as vector of value references
#define STATES { pout_ }

// called by fmi2Instantiate
// Set values for all variables that define a start value
// Settings used unless changed by fmi2SetX before fmi2EnterInitializationMode
void setStartValues(ModelInstance *comp) {
    r(pin_) = 2;
    r(pout_) = 4;
}

// called by fmi2GetReal, fmi2GetInteger, fmi2GetBoolean, fmi2GetString, fmi2ExitInitialization
// if setStartValues or environment set new values through fmi2SetXXX.
// Lazy set values for all variable that are computed from other variables.
void calculateValues(ModelInstance *comp) {
    if (comp->state == modelInitializationMode) {
        // set first time event
        comp->eventInfo.nextEventTimeDefined = fmi2True;
    }
}

// called by fmi2GetReal, fmi2GetContinuousStates and fmi2GetDerivatives
fmi2Real getReal(ModelInstance *comp, fmi2ValueReference vr){
    switch (vr) {
        case pin_  : return  r(pin_);
        case pout_ : return     r(pout_);
        default: return 0;
    }
}

// used to set the next time event, if any.
void eventUpdate(ModelInstance *comp, fmi2EventInfo *eventInfo, int isTimeEvent, int isNewEventIteration) {
    myexecute(&r(pin_), &r(pout_));
}

#include "fmuTemplate.c"

Upvotes: 2

Related Questions