2

这是我的功能。我正在尝试将此代码导出到fmu。我正在使用fmusdk

对于每个周期(时间步长),

  1. myinput应更改为模拟期间给出的值。
  2. myexecute()应该调用。
  3. input和的值pout应在模拟过程中存储,以便我们可以在模拟后绘制值。

我尝试了fmusdk中给出的示例(BouncingBall 和值)。我已经创建了相应的 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 种方法的解释也足够了。我可以为函数创建内容。

4

1 回答 1

2

考虑下面给出的 C 文件,类似于您提到的文件 bouncingBall.c 或 values.c 。由于您已经myexecute从另一个 c 文件中引用了该函数,因此请替换yourCFile.c和/或yourHFile.h使用正确的文件。此外,您的 modelDescription.xml 应该与此 C 文件同步。例如)guid两个文件中的值应该相同。标量变量的值引用也应该相同。分析<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"
于 2017-07-07T11:32:40.123 回答