- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » How to use a C function in...
How to use a C function in OpenModelica model
How to use a C function in OpenModelica model
I am working on a model given on https://mbe.modelica.university/behavio … ntroller/. The name of this model is HysteresisEmbeddedControl. In this model, there is a function in Modelica as given below:
Code:
impure function computeHeat "Modelica wrapper for an embedded C controller"
input Real T;
input Real Tbar;
input Real Q;
output Real heat;
external "C" annotation (Include="#include \"ComputeHeat.c\"",
IncludeDirectory="modelica://ModelicaByExample.Functions.ImpureFunctions/source");
end computeHeat;
As given above, the definition of this function is given as below:
Code:
#ifndef _COMPUTE_HEAT_C_
#define _COMPUTE_HEAT_C_
#define UNINITIALIZED -1
#define ON 1
#define OFF 0
double
computeHeat(double T, double Tbar, double Q) {
static int state = UNINITIALIZED;
if (state==UNINITIALIZED) {
if (T>Tbar) state = OFF;
else state = ON;
}
if (state==OFF && T<Tbar-2) state = ON;
if (state==ON && T>Tbar+2) state = OFF;
if (state==ON) return Q;
else return 0;
}
#endif
To reproduce this model, I have created a new Model named HysteresisEmbeddedControl in OMEdit. Then, I create a function named computeHeat and insert it into this model. Now, I am not sure how to use the C function in my model. I am having OpenModelica 1.17.0 on Ubuntu 20.04. Could anyone please provide some insights into this?
Re: How to use a C function in OpenModelica model
Hi, I'm interested in use C functions in OME but 'till now I haven't success, I think the problem is thtat I don't write the adress of the C file correctly, could you be so kind and post how you wrote it? I mean write here the complete path.
Thank you in advance.
Re: How to use a C function in OpenModelica model
Thanks for your response. Let us say, you have a C code, named as ComputeHeat.c and it is located in a folder whose path is /home/fossee/Desktop/testSim on a Linux Machine. So, I will use the following code chunk to include the C code in my model.
Code:
external "C" annotation(
Include = "#include \"ComputeHeat.c\"",
ncludeDirectory = "file:///home/fossee/Desktop/testSim");
Hope this helps!
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » How to use a C function in...