Writing S-Functions | ![]() ![]() |
Source File Format
The format of the C++ source for an S-function is nearly identical to that of the source for an S-function written in C. The main difference is that you must tell the C++ compiler to use C calling conventions when compiling the callback methods. This is necessary because the Simulink simulation engine assumes that callback methods obey C calling conventions.
To tell the compiler to use C calling conventions when compiling the callback methods, wrap the C++ source for the S-function callback methods in an extern "C"
statement. The C++ version of the sfun_counter
S-function example (matlabroot
/simulink/src/sfun_counter_cpp.cpp
) illustrates usage of the extern "C"
directive to ensure that the compiler generates Simulink-compatible callback methods:
/* File : sfun_counter_cpp.cpp * Abstract: * * Example of an C++ S-function which stores an C++ object in * the pointers vector PWork. * * Copyright 1990-2000 The MathWorks, Inc. * */ #include "iostream.h" class counter { double x; public: counter() { x = 0.0; } double output(void) { x = x + 1.0; return x; } }; #ifdef __cplusplus extern "C" { // use the C fcn-call standard for all functions #endif // defined within this scope #define S_FUNCTION_LEVEL 2 #define S_FUNCTION_NAME sfun_counter_cpp /* * Need to include simstruc.h for the definition of the SimStruct and * its associated macro definitions. */ #include "simstruc.h" /*====================* * S-function methods * *====================*/ /* Function: mdlInitializeSizes =============================================== * Abstract: * The sizes information is used by Simulink to determine the S-function * block's characteristics (number of inputs, outputs, states, etc.). */ static void mdlInitializeSizes(SimStruct *S) { /* See sfuntmpl_doc.c for more details on the macros below */ ssSetNumSFcnParams(S, 1); /* Number of expected parameters */ if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { /* Return if number of expected != number of actual parameters */ return; } ssSetNumContStates(S, 0); ssSetNumDiscStates(S, 0); if (!ssSetNumInputPorts(S, 0)) return; if (!ssSetNumOutputPorts(S, 1)) return; ssSetOutputPortWidth(S, 0, 1); ssSetNumSampleTimes(S, 1); ssSetNumRWork(S, 0); ssSetNumIWork(S, 0); ssSetNumPWork(S, 1); // reserve element in the pointers vector ssSetNumModes(S, 0); // to store a C++ object ssSetNumNonsampledZCs(S, 0); ssSetOptions(S, 0); } /* Function: mdlInitializeSampleTimes ========================================= * Abstract: * This function is used to specify the sample time(s) for your * S-function. You must register the same number of sample times as * specified in ssSetNumSampleTimes. */ static void mdlInitializeSampleTimes(SimStruct *S) { ssSetSampleTime(S, 0, mxGetScalar(ssGetSFcnParam(S, 0))); ssSetOffsetTime(S, 0, 0.0); } #define MDL_START /* Change to #undef to remove function */ #if defined(MDL_START) /* Function: mdlStart ======================================================= * Abstract: * This function is called once at start of model execution. If you * have states that should be initialized once, this is the place * to do it. */ static void mdlStart(SimStruct *S) { ssGetPWork(S)[0] = (void *) new counter; // store new C++ object in the } // pointers vector #endif /* MDL_START */ /* Function: mdlOutputs ======================================================= * Abstract: * In this function, you compute the outputs of your S-function * block. Generally outputs are placed in the output vector, ssGetY(S). */ static void mdlOutputs(SimStruct *S, int_T tid) { counter *c = (counter *) ssGetPWork(S)[0]; // retrieve C++ object from real_T *y = ssGetOutputPortRealSignal(S,0); // the pointers vector and use y[0] = c->output(); // member functions of the } // object /* Function: mdlTerminate ===================================================== * Abstract: * In this function, you should perform any actions that are necessary * at the termination of a simulation. For example, if memory was * allocated in mdlStart, this is the place to free it. */ static void mdlTerminate(SimStruct *S) { counter *c = (counter *) ssGetPWork(S)[0]; // retrieve and destroy C++ delete c; // object in the termination } // function /*======================================================* * See sfuntmpl_doc.c for the optional S-function methods * *======================================================*/ /*=============================* * Required S-function trailer * *=============================*/ #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 #ifdef __cplusplus } // end of extern "C" scope #endif
![]() | Creating C++ S-Functions | Making C++ Objects Persistent | ![]() |