Pointer to Simulink Function

조회 수: 17 (최근 30일)
Carlos Ma
Carlos Ma 2020년 2월 21일
편집: Carlos Ma 2020년 2월 24일
Suppose I have a custom C function that accepts a function pointer as an argument,
float cFunc(float val, float (*ptrFunc)(float)) {
return ptrFunc(val);
}
and within Simulink, I defined a Simulink Function called "slxFunc" that takes a float as an input and outputs a float.
How do I obtain the pointer to slxFunc so that I could feed it to cFunc using a C-caller block?
Much appreciated, thanks.
/* edit 1: extra info */
The following C code is what I am trying to emulate,
#include <stdio.h>
/* times2 should be defined in simulink */
float times2(float val) {
return (2.0*val);
}
/* times3 should also be defined in simulink */
float times3(float val) {
return (3.0*val);
}
/* myFunc is defined in a .c file */
float myFunc(float val, float (*slxFunc)(float)) {
return (slxFunc(val));
}
int main()
{
/* printf should print 4.0 6.0 */
printf("%f\t%f\n", myFunc(2.0, times2), myFunc(2.0, times3));
return 0;
}
, where
times2
and
times3
are to be defined in the Simulink environment, and
myFunc
is implemented as a custom C function.
  댓글 수: 2
stozaki
stozaki 2020년 2월 23일
can you use stateflow?
Carlos Ma
Carlos Ma 2020년 2월 24일
Thanks for replying @stozaki.
I'd really love to get it working in stateflow too, but it returned an unresolved data error.
Is there a way I could rectify this?

댓글을 달려면 로그인하십시오.

답변 (1개)

stozaki
stozaki 2020년 2월 24일
I attahced example model, hedder and souce C file.
Please try to simulate there.
  댓글 수: 3
stozaki
stozaki 2020년 2월 24일
Blocks provided by Simulink do not have the concept of pointers.
Can you use the newly added model?
Carlos Ma
Carlos Ma 2020년 2월 24일
편집: Carlos Ma 2020년 2월 24일
Thanks for your help but again that's not what I am looking for. I have no problem calling C functions either using C Caller / Stateflow. The problem lies in passing a pointer to a Simulink function to a predefined C function, as described by the line
printf("%f\t%f\n", myFunc(2.0, times2), myFunc(2.0, times3));
If you parse this line in a generic c compiler, printf would return 4.0 and 6.0 as expected as I can pass the functions times2 and times3 into the function myFunc. The above line does not compile, however, in Stateflow with C being selected as the action language, as Simulink expects an argument for times2 and times3 but doesn't realise that I am trying to pass the functions are function pointers instead.
Thanks again if you have any ideas.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by