1. /*
  2.  * sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
  3.  *
  4.  * -------------------------------------------------------------------------
  5.  * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
  6.  * -------------------------------------------------------------------------
  7.  *
  8.  * Copyright 1990-2002 The MathWorks, Inc.
  9.  * $Revision: 1.27.4.2 $
  10.  */
  11.  
  12.  
  13. /*
  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).
  16.  */
  17.  
  18. #define S_FUNCTION_NAME fcnclass5
  19. #define S_FUNCTION_LEVEL 2
  20.  
  21.  
  22. /*
  23.  * Need to include simstruc.h for the definition of the SimStruct and
  24.  * its associated macro definitions.
  25.  */
  26. #include "simstruc.h"
  27.  
  28. #define pocet 10
  29. static double stavy[pocet];
  30.  
  31. /* Error handling
  32.  * --------------
  33.  *
  34.  * You should use the following technique to report errors encountered within
  35.  * an S-function:
  36.  *
  37.  * ssSetErrorStatus(S,"Error encountered due to ...");
  38.  * return;
  39.  *
  40.  * Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
  41.  * It cannot be a local variable. For example the following will cause
  42.  * unpredictable errors:
  43.  *
  44.  * mdlOutputs()
  45.  * {
  46.  * char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
  47.  * sprintf(msg,"Error due to %s", string);
  48.  * ssSetErrorStatus(S,msg);
  49.  * return;
  50.  * }
  51.  *
  52.  * See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
  53.  */
  54.  
  55. /*====================*
  56.  * S-function methods *
  57.  *====================*/
  58.  
  59. /* Function: mdlInitializeSizes ===============================================
  60.  * Abstract:
  61.  * The sizes information is used by Simulink to determine the S-function
  62.  * block's characteristics (number of inputs, outputs, states, etc.).
  63.  */
  64. static void mdlInitializeSizes(SimStruct *S)
  65. {
  66. /* See sfuntmpl_doc.c for more details on the macros below */
  67.  
  68. ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
  69. if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
  70. /* Return if number of expected != number of actual parameters */
  71. return;
  72. }
  73.  
  74. ssSetNumContStates(S, 0);
  75. ssSetNumDiscStates(S, 0);
  76.  
  77. if (!ssSetNumInputPorts(S, 1)) return;
  78. ssSetInputPortWidth(S, 0, 1);
  79. ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
  80. /*
  81.   * Set direct feedthrough flag (1=yes, 0=no).
  82.   * A port has direct feedthrough if the input is used in either
  83.   * the mdlOutputs or mdlGetTimeOfNextVarHit functions.
  84.   * See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
  85.   */
  86. ssSetInputPortDirectFeedThrough(S, 0, 1);
  87.  
  88. if (!ssSetNumOutputPorts(S, 1)) return;
  89. ssSetOutputPortWidth(S, 0, 1);
  90.  
  91. ssSetNumSampleTimes(S, 1);
  92. ssSetNumRWork(S, 0);
  93. ssSetNumIWork(S, 0);
  94. ssSetNumPWork(S, 0);
  95. ssSetNumModes(S, 0);
  96. ssSetNumNonsampledZCs(S, 0);
  97.  
  98. /* Specify the sim state compliance to be same as a built-in block */
  99. ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
  100.  
  101. ssSetOptions(S, 0);
  102. }
  103.  
  104.  
  105.  
  106. /* Function: mdlInitializeSampleTimes =========================================
  107.  * Abstract:
  108.  * This function is used to specify the sample time(s) for your
  109.  * S-function. You must register the same number of sample times as
  110.  * specified in ssSetNumSampleTimes.
  111.  */
  112. static void mdlInitializeSampleTimes(SimStruct *S)
  113. {
  114. double Tvz=*mxGetPr(ssGetSFcnParam(S,0));
  115. ssSetSampleTime(S, 0, Tvz);
  116. ssSetOffsetTime(S, 0, 0.0);
  117.  
  118. }
  119.  
  120.  
  121.  
  122. #define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
  123. #if defined(MDL_INITIALIZE_CONDITIONS)
  124. /* Function: mdlInitializeConditions ========================================
  125.   * Abstract:
  126.   * In this function, you should initialize the continuous and discrete
  127.   * states for your S-function block. The initial states are placed
  128.   * in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
  129.   * You can also perform any other initialization activities that your
  130.   * S-function may require. Note, this routine will be called at the
  131.   * start of simulation and if it is present in an enabled subsystem
  132.   * configured to reset states, it will be call when the enabled subsystem
  133.   * restarts execution to reset the states.
  134.   */
  135. static void mdlInitializeConditions(SimStruct *S)
  136. {
  137.  
  138. }
  139. #endif /* MDL_INITIALIZE_CONDITIONS */
  140.  
  141.  
  142.  
  143. #define MDL_START /* Change to #undef to remove function */
  144. #if defined(MDL_START)
  145. /* Function: mdlStart =======================================================
  146.   * Abstract:
  147.   * This function is called once at start of model execution. If you
  148.   * have states that should be initialized once, this is the place
  149.   * to do it.
  150.   */
  151. static void mdlStart(SimStruct *S)
  152. {
  153. int i;
  154. for(i=0;i<pocet;i++) stavy[i]=0;
  155.  
  156. }
  157. #endif /* MDL_START */
  158.  
  159.  
  160.  
  161.  
  162. /* Function: mdlOutputs =======================================================
  163.  * Abstract:
  164.  * In this function, you compute the outputs of your S-function
  165.  * block.
  166.  */
  167. static void mdlOutputs(SimStruct *S, int_T tid)
  168. {
  169. const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
  170. real_T *y = ssGetOutputPortSignal(S,0);
  171. double sum=0;
  172. int i;
  173. for(i=pocet;i>0;i--) stavy[i]=stavy[i-1];
  174. stavy[0]=u[0];
  175.  
  176. for(i=0;i<pocet;i++) sum+=stavy[i];
  177. y[0] = sum/pocet;
  178.  
  179. }
  180.  
  181.  
  182.  
  183. #define MDL_UPDATE /* Change to #undef to remove function */
  184. #if defined(MDL_UPDATE)
  185. /* Function: mdlUpdate ======================================================
  186.   * Abstract:
  187.   * This function is called once for every major integration time step.
  188.   * Discrete states are typically updated here, but this function is useful
  189.   * for performing any tasks that should only take place once per
  190.   * integration step.
  191.   */
  192. static void mdlUpdate(SimStruct *S, int_T tid)
  193. {
  194. }
  195. #endif /* MDL_UPDATE */
  196.  
  197.  
  198.  
  199. #define MDL_DERIVATIVES /* Change to #undef to remove function */
  200. #if defined(MDL_DERIVATIVES)
  201. /* Function: mdlDerivatives =================================================
  202.   * Abstract:
  203.   * In this function, you compute the S-function block's derivatives.
  204.   * The derivatives are placed in the derivative vector, ssGetdX(S).
  205.   */
  206. static void mdlDerivatives(SimStruct *S)
  207. {
  208. }
  209. #endif /* MDL_DERIVATIVES */
  210.  
  211.  
  212.  
  213. /* Function: mdlTerminate =====================================================
  214.  * Abstract:
  215.  * In this function, you should perform any actions that are necessary
  216.  * at the termination of a simulation. For example, if memory was
  217.  * allocated in mdlStart, this is the place to free it.
  218.  */
  219. static void mdlTerminate(SimStruct *S)
  220. {
  221. }
  222.  
  223.  
  224. /*======================================================*
  225.  * See sfuntmpl_doc.c for the optional S-function methods *
  226.  *======================================================*/
  227.  
  228. /*=============================*
  229.  * Required S-function trailer *
  230.  *=============================*/
  231.  
  232. #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
  233. #include "simulink.c" /* MEX-file interface mechanism */
  234. #else
  235. #include "cg_sfun.h" /* Code generation registration function */
  236. #endif
  237.