- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » How to call an external C function?
How to call an external C function?
How to call an external C function?
I've seen many examples on how to implement it and it seems so simple, but every time I try to run an external function written in C an error happens.
My goal is to apply it on a predictive control system, but first I'd like to do an simple example to learn how to do it. Can someone help me?
I've tried this example:
class ChirpSignal
Modelica.Blocks.Interfaces.RealOutput u;
parameter Modelica.SIunits.AngularVelocity w_start=0;
parameter Modelica.SIunits.AngularVelocity w_end=10;
parameter Real A=1;
parameter Real M=10;
equation
u=Chirp(w_start, w_end, A, M, time);
end ChirpSignal;
With the Chirp function being:
function Chirp
input Modelica.SIunits.AngularVelocity w_start;
input Modelica.SIunits.AngularVelocity w_end;
input Real A;
input Real M;
input Real t;
output Real u "output signal";
external "C" annotation(Include="#include \"Chirp.c\"");
end Chirp;
And the .c file as:
double Chirp(double w1, double w2, double A, double M, double time)
{
double res;
res=A*w1*w2*M*time;
return res;
}
The error is as shown bellow:
I'm not using a header file, but already tried to use it. What should I do?
Thank you!
Re: How to call an external C function?
Where did you put the file? The only location that is searched is Teste26-02/Resources/Include
- sjoelund.se
- 1700 Posts
Re: How to call an external C function?
You can use also other directories if you include them, as follows:
package ChirpP
class ChirpSignal
Modelica.Blocks.Interfaces.RealOutput u;
parameter Modelica.SIunits.AngularVelocity w_start = 1;
parameter Modelica.SIunits.AngularVelocity w_end = 10;
parameter Real A = 1;
parameter Real M = 10;
equation
u = Chirp(w_start, w_end, A, M, time);
end ChirpSignal;
function Chirp
input Modelica.SIunits.AngularVelocity w_start;
input Modelica.SIunits.AngularVelocity w_end;
input Real A;
input Real M;
input Real t;
output Real u "output signal";
external "C" annotation(
IncludeDirectory = "modelica://ChirpP",
Include = "#include \"Chirp.c\"");
end Chirp;
end ChirpP;
"modelica://ChirpP" will place you in the directory where ChirpP package is, and you can put the c file there. Or indicate a relative path, for example: IncludeDirectory = "modelica://ChirpP/Resources" and put the C files in Resources, that is the default.
I have changed w_start from 0 to 1, because if it 0 the result is always 0.
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » How to call an external C function?