2 * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
4 * -------------------------------------------------------------------------
5 * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
6 * -------------------------------------------------------------------------
8 * Copyright 1990-2002 The MathWorks, Inc.
9 * $Revision: 1.27.4.2 $
14 * You must specify the S_FUNCTION_NAME as the name of your S-function
15 * (i.e. replace sfuntmpl_basic with the name of your S-function).
18 #define S_FUNCTION_NAME sfuntmpl_basic
19 #define S_FUNCTION_LEVEL 2
22 * Need to include simstruc.h for the definition of the SimStruct and
23 * its associated macro definitions.
32 * You should use the following technique to report errors encountered within
35 * ssSetErrorStatus(S,"Error encountered due to ...");
38 * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
39 * It cannot be a local variable. For example the following will cause
40 * unpredictable errors:
44 * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
45 * sprintf(msg,"Error due to %s", string);
46 * ssSetErrorStatus(S,msg);
50 * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
53 /*====================*
54 * S-function methods *
55 *====================*/
57 /* Function: mdlInitializeSizes ===============================================
59 * The sizes information is used by Simulink to determine the S-function
60 * block's characteristics (number of inputs, outputs, states, etc.).
62 static void mdlInitializeSizes(SimStruct *S)
64 /* See sfuntmpl_doc.c for more details on the macros below */
66 ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
67 if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
68 /* Return if number of expected != number of actual parameters */
72 ssSetNumContStates(S, 0);
73 ssSetNumDiscStates(S, 0);
75 if (!ssSetNumInputPorts(S, 1)) return;
76 ssSetInputPortWidth(S, 0, 1);
77 ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
79 * Set direct feedthrough flag (1=yes, 0=no).
80 * A port has direct feedthrough if the input is used in either
81 * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
82 * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
84 ssSetInputPortDirectFeedThrough(S, 0, 1);
86 if (!ssSetNumOutputPorts(S, 1)) return;
87 ssSetOutputPortWidth(S, 0, 1);
89 ssSetNumSampleTimes(S, 1);
94 ssSetNumNonsampledZCs(S, 0);
96 /* Specify the sim state compliance to be same as a built-in block */
97 ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
104 /* Function: mdlInitializeSampleTimes =========================================
106 * This function is used to specify the sample time(s) for your
107 * S-function. You must register the same number of sample times as
108 * specified in ssSetNumSampleTimes.
110 static void mdlInitializeSampleTimes(SimStruct *S)
112 ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
113 ssSetOffsetTime(S, 0, 0.0);
119 #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
120 #if defined(MDL_INITIALIZE_CONDITIONS)
121 /* Function: mdlInitializeConditions ========================================
123 * In this function, you should initialize the continuous and discrete
124 * states for your S-function block. The initial states are placed
125 * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
126 * You can also perform any other initialization activities that your
127 * S-function may require. Note, this routine will be called at the
128 * start of simulation and if it is present in an enabled subsystem
129 * configured to reset states, it will be call when the enabled subsystem
130 * restarts execution to reset the states.
132 static void mdlInitializeConditions(SimStruct *S)
135 #endif /* MDL_INITIALIZE_CONDITIONS */
139 #define MDL_START /* Change to #undef to remove function */
140 #if defined(MDL_START)
141 /* Function: mdlStart =======================================================
143 * This function is called once at start of model execution. If you
144 * have states that should be initialized once, this is the place
147 static void mdlStart(SimStruct *S)
150 #endif /* MDL_START */
154 /* Function: mdlOutputs =======================================================
156 * In this function, you compute the outputs of your S-function
159 static void mdlOutputs(SimStruct *S, int_T tid)
161 const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
162 real_T *y = ssGetOutputPortSignal(S,0);
168 #define MDL_UPDATE /* Change to #undef to remove function */
169 #if defined(MDL_UPDATE)
170 /* Function: mdlUpdate ======================================================
172 * This function is called once for every major integration time step.
173 * Discrete states are typically updated here, but this function is useful
174 * for performing any tasks that should only take place once per
177 static void mdlUpdate(SimStruct *S, int_T tid)
180 #endif /* MDL_UPDATE */
184 #define MDL_DERIVATIVES /* Change to #undef to remove function */
185 #if defined(MDL_DERIVATIVES)
186 /* Function: mdlDerivatives =================================================
188 * In this function, you compute the S-function block's derivatives.
189 * The derivatives are placed in the derivative vector, ssGetdX(S).
191 static void mdlDerivatives(SimStruct *S)
194 #endif /* MDL_DERIVATIVES */
198 /* Function: mdlTerminate =====================================================
200 * In this function, you should perform any actions that are necessary
201 * at the termination of a simulation. For example, if memory was
202 * allocated in mdlStart, this is the place to free it.
204 static void mdlTerminate(SimStruct *S)
209 /*======================================================*
210 * See sfuntmpl_doc.c for the optional S-function methods *
211 *======================================================*/
213 /*=============================*
214 * Required S-function trailer *
215 *=============================*/
217 #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
218 #include "simulink.c" /* MEX-file interface mechanism */
220 #include "cg_sfun.h" /* Code generation registration function */