Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simulink: Strange behaviour using S-Function with C MEX files

17 views
Skip to first unread message

Gabriele Scaroni

unread,
Nov 12, 2006, 4:07:21 AM11/12/06
to
Hi all,
i'm using Matlab 6.5 R13 and Simulink.
I'm trying to create a Simulink "Real-Time Windows Target" model and
i've some difficulties because i need to use my own functions (and
MatlabFcn isn't supported in RealTime mode): so i've written a test
function in C, compiled it with "mex -x" and used in a S-Function
block.
In this test model i've placed a sin block as input of my s-function
block and then used a scope to verify the output of the s-function. The

function i've written in C doesn't change the input value (so i can see

it in the scope) and try to send fake data (because i only need to test

that it works) to a dde server (that works fine). The problem is that
if i put some commands in the "mdlInitializeSizes" or "mdlTerminate"
methods all is ok... but commands in the "mdlOutputs" method aren't
executed and i can't understand why.
As you can see in the source code i send "0.0" in the
mdlInitializeSizes, "45.0" in the mdlOutputs and "90.0" in the
mdlTerminate, but only 0.0 and 90.0 are executed (and also for the text

is the same: "output" is never written).
I have to say that there are no errors when i run the model in
real-time, but i can't understand why the mdlOutputs method doesn't
execute the code i write in it, but if i multiply the output per 2
(like in the matlab timestwo example) i can see that the output is the
double of the input. Also if i write some text with printf function i
can see the text only in the mdlInitializeSizes and mdlTerminate
methods, but not the one in the mdlOutputs, where i write the command

printf("Output");


Thanks in advance,
Gabriele


Here is the source code i've written


=====================================================
=====================================================


#define S_FUNCTION_NAME comunicazione
#define S_FUNCTION_LEVEL 2


#include "simstruc.h"


/*================*
* Build checking *
*================*/


/* Function: mdlInitializeSizes
===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
printf("Initialize");


mexEvalString("valore_can = ddeinit('DdemlSvr', 'MyTopic');");
mexEvalString("valori = [0.0 0.0 0.0 0.0];");
mexEvalString("ddepoke(valore_can, 'MyItem', valori);");


ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}


if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);


if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);


ssSetNumSampleTimes(S, 1);


/* Take care when specifying exception free code - see
sfuntmpl_doc.c */
ssSetOptions(S,
SS_OPTION_WORKS_WITH_CODE_REUSE |
SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR);

}


/* Function: mdlInitializeSampleTimes
=========================================
* Abstract:
* Specifiy that we inherit our sample time from the driving
block.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);


}


/* Function: mdlOutputs
=======================================================
* Abstract:
* y = 1*u
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);

printf("Output");
mexEvalString("valori = [45.0 45.0 45.0 45.0];");
mexEvalString("ddepoke(valore_can, 'MyItem', valori);");


for (i=0; i<width; i++) {
/*
* This example does not implement complex signal handling.
* To find out see an example about how to handle complex
signal in
* S-function, see sdotproduct.c for details.
*/
*y++ = 1.0 *(*uPtrs[i]);
}

}


/* Function: mdlTerminate
=====================================================
* Abstract:
* No termination needed, but we are required to have this
routine.
*/
static void mdlTerminate(SimStruct *S)
{
printf("The End");
mexEvalString("valori = [90.0 90.0 90.0 90.0];");
mexEvalString("ddepoke(valore_can, 'MyItem', valori);");

mexEvalString("ddeterm(valore_can);");

}


#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a
MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function
*/
#endif

0 new messages