]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/commitdiff
Commiting the state of the art of Simulink S-Functions blocks.
authorCarlos Jenkins <carlos@jenkins.co.cr>
Fri, 24 May 2013 18:18:55 +0000 (20:18 +0200)
committerCarlos Jenkins <carlos@jenkins.co.cr>
Fri, 24 May 2013 18:18:55 +0000 (20:18 +0200)
rpp/blocks/sfunction_din.c
rpp/blocks/sfunction_lout.c
rpp/blocks/trailer.c

index fafd98e0d763fcfaa66e58cca850c0b486dc902b..bd42bdd460763ef03f213fad6beb3204daba6bc3 100644 (file)
  *     C-MEX S-function block for RPP digital input.
  *
  * References:
- *     mdlCheckParameters()       : sfunctions.pdf p. 421
- *     mdlInitializeSizes()       : sfunctions.pdf p. 441
- *     mdlInitializeSampleTimes() : sfunctions.pdf p. 436
- *     mdlSetWorkWidths()         : sfunctions.pdf p. 489
- *     mdlStart()                 : sfunctions.pdf p. 492
- *     mdlOutputs()               : sfunctions.pdf p. 447
- *     mdlTerminate()             : sfunctions.pdf p. 493
- *     mdlRTW()                   : sfunctions.pdf p. 458
+ *     common.c
  *
  *     Compile with <matlabroot>/bin/mex -v -g sfunction_din.c
  */
  * Must specify the S_FUNCTION_NAME as the name of the S-function.
  */
 #define S_FUNCTION_NAME                sfunction_din
-#define S_FUNCTION_LEVEL               2
 
 /*
  * Include common header and utilities
  */
-#include "utils.c"
+#include "common.c"
 
 #define MDL_CHECK_PARAMETERS
 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
  */
 static void mdlCheckParameters(SimStruct *S)
 {
-  /*
-   * Check the parameter 1
-   */
-  if EDIT_OK(S, 0) {
-    int_T dimsArray[2] = { 1, 1 };
-
-    /* Check the parameter attributes */
-    ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
-  }
-
-  /*
-   * Check the parameter 2 (sample time)
-   */
-  if EDIT_OK(S, 1) {
-    const double * sampleTime = NULL;
-    const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
-
-    /* Sample time must be a real scalar value or 2 element array. */
-    if (IsRealMatrix(SAMPLE_TIME) &&
-        (stArraySize == 1 || stArraySize == 2) ) {
-      sampleTime = mxGetPr(SAMPLE_TIME);
-    } else {
-      ssSetErrorStatus(S,
-                       "Invalid sample time. Sample time must be a real scalar value or an array of two real values.");
-      return;
-    }
-
-    if (sampleTime[0] < 0.0 && sampleTime[0] != -1.0) {
-      ssSetErrorStatus(S,
-                       "Invalid sample time. Period must be non-negative or -1 (for inherited).");
-      return;
-    }
+    /*
+     * Check the parameter 1
+     */
+    if EDIT_OK(S, 0) {
+        int_T dimsArray[2] = { 1, 1 };
 
-    if (stArraySize == 2 && sampleTime[0] > 0.0 &&
-        sampleTime[1] >= sampleTime[0]) {
-      ssSetErrorStatus(S,
-                       "Invalid sample time. Offset must be smaller than period.");
-      return;
+        /* Check the parameter attributes */
+        ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
     }
 
-    if (stArraySize == 2 && sampleTime[0] == -1.0 && sampleTime[1] != 0.0) {
-      ssSetErrorStatus(S,
-                       "Invalid sample time. When period is -1, offset must be 0.");
-      return;
-    }
-
-    if (stArraySize == 2 && sampleTime[0] == 0.0 &&
-        !(sampleTime[1] == 1.0)) {
-      ssSetErrorStatus(S,
-                       "Invalid sample time. When period is 0, offset must be 1.");
-      return;
+    /*
+     * Check the parameter 2 (sample time)
+     */
+    if(!checkSampleTime(S, 1)) {
+        return;
     }
-  }
 }
 
 #endif
index c05e81da2208dbcdafa2013ca5e919836506b436..cf2130a68551a297a82e641bc714fa2c6d76174f 100644 (file)
@@ -95,25 +95,6 @@ static void mdlInitializeSizes(SimStruct *S)
 }
 
 
-static void mdlInitializeSampleTimes(SimStruct *S)
-{
-    const double* sampleTime = mxGetPr(SAMPLE_TIME);
-    const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
-    ssSetSampleTime(S, 0, sampleTime[0]);
-
-    if(stArraySize == 1) {
-        ssSetOffsetTime(S, 0, (sampleTime[0] == CONTINUOUS_SAMPLE_TIME?
-        FIXED_IN_MINOR_STEP_OFFSET: 0.0));
-    } else {
-        ssSetOffsetTime(S, 0, sampleTime[1]);
-    }
-
-    #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
-    ssSetModelReferenceSampleTimeDefaultInheritance(S);
-    #endif
-}
-
-
 #ifdef MATLAB_MEX_FILE
 static void mdlCheckParameters(SimStruct *S)
 {
@@ -130,8 +111,10 @@ static void mdlCheckParameters(SimStruct *S)
         return;
     }
 }
+#endif
 
 
+#ifdef MATLAB_MEX_FILE
 static void mdlSetWorkWidths(SimStruct *S)
 {
     // Set number of run-time parameters
@@ -145,6 +128,7 @@ static void mdlSetWorkWidths(SimStruct *S)
 #endif
 
 
+#define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
 #define UNUSED_MDLOUTPUTS
 #define UNUSED_MDLTERMINATE
 #include "trailer.c"
index b03316c19dc5ac6a0a7e74711d49bfea6b94ba61..214b1bd582947ff2c1f01df9f00765be9ef65689 100644 (file)
@@ -57,6 +57,18 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 #endif
 
 
+#ifdef COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
+static void mdlInitializeSampleTimes(SimStruct *S)
+{
+    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
+    ssSetOffsetTime(S, 0, 0.0);
+    #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
+    ssSetModelReferenceSampleTimeDefaultInheritance(S);
+    #endif
+}
+#endif
+
+
 /* Function: mdlOutputs ========================================================
  * Abstract:
  *    In this function, you compute the outputs of your S-function