2017-07-07 62 views
2

這是我的功能。我正嘗試將此代碼導出至fmu。我正在使用fmusdk如何使用fmusdk將c文件/函數導出到FMU

對於每個循環(時間步長),

  1. input應改爲模擬期間給定的值。
  2. myexecute()應該被調用。
  3. inputpout的值應在模擬過程中存儲,以便我們可以在模擬之後繪製值。

我試過fmusdk中給出的例子(BouncingBall和values)。我創建了相應的Fmus,並將它們導入Amesim。他們工作正常。但我無法弄清楚如何爲我的C文件做同樣的/功能

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

我檢查了bouncingBall.c和values.c,他們只有四種方法

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

可有人幫助我完成這個fmi出口?總之,我正在尋找上述4個功能的內容。上述4種方法的解釋也就足夠了。我可以爲這些功能創建內容。

回答

0

考慮下面給出的C文件,類似於您提到的文件bouncingBall.c或valus.c。由於您從另一個c文件引用了功能myexecute,因此請將yourCFile.c和/或yourHFile.h替換爲正確的文件。 此外,您的modelDescription.xml應與此C文件同步。對於ex)​​這兩個文件中的值應該是相同的。標量變量的值引用也應該是相同的。分析<ScalarVariable name="pin" valueReference="0">#define pin_ 0。同樣爲pout。創建一個類似於bouncingBall的文件夾結構。創建一個單獨的批處理文件,因爲我們必須包括其他文件(yourCFile.c和/或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" 
相關問題