]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/commitdiff
Improved S-Function blocks for DIN and LOUT. Not ready yet though.
authorCarlos Jenkins <carlos@jenkins.co.cr>
Fri, 24 May 2013 13:50:23 +0000 (15:50 +0200)
committerCarlos Jenkins <carlos@jenkins.co.cr>
Fri, 24 May 2013 13:50:23 +0000 (15:50 +0200)
rpp/blocks/sfunction_din.c
rpp/blocks/sfunction_lout.c
rpp/blocks/tlc_c/sfunction_din.tlc
rpp/blocks/tlc_c/sfunction_lout.tlc

index 151e1cac75b357407aea3e3cfc6b1a4f6d3b0d94..fafd98e0d763fcfaa66e58cca850c0b486dc902b 100644 (file)
  *     C-MEX S-function block for RPP digital input.
  *
  * References:
- *     ????
- */
-
-/*
- * FIXME: Adapt, then implement.
+ *     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
+ *
+ *     Compile with <matlabroot>/bin/mex -v -g sfunction_din.c
  */
 
 
 #define S_FUNCTION_LEVEL               2
 
 /*
- * Need to include simstruc.h for the definition of the SimStruct and
- * its associated macro definitions.
+ * Include common header and utilities
  */
-#include "simstruc.h"
-#define EDIT_OK(S, P_IDX) \
- (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
-#define SAMPLE_TIME                    (ssGetSFcnParam(S, 1))
-
-/*
- * Utility function prototypes.
- */
-static bool IsRealMatrix(const mxArray * const m);
+#include "utils.c"
 
 #define MDL_CHECK_PARAMETERS
 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
 
-/* Function: mdlCheckParameters ===========================================
+/* Function: mdlCheckParameters ================================================
  * Abstract:
  *    mdlCheckParameters verifies new parameter settings whenever parameter
  *    change or are re-evaluated during a simulation. When a simulation is
@@ -118,7 +114,7 @@ static void mdlCheckParameters(SimStruct *S)
 
 #endif
 
-/* Function: mdlInitializeSizes ===========================================
+/* Function: mdlInitializeSizes ================================================
  * Abstract:
  *    The sizes information is used by Simulink to determine the S-function
  *    block's characteristics (number of inputs, outputs, states, etc.).
@@ -206,7 +202,7 @@ static void mdlInitializeSizes(SimStruct *S)
                SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
 }
 
-/* Function: mdlInitializeSampleTimes =====================================
+/* 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
@@ -235,7 +231,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 #define MDL_SET_WORK_WIDTHS
 #if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
 
-/* Function: mdlSetWorkWidths =============================================
+/* Function: mdlSetWorkWidths ==================================================
  * Abstract:
  *      The optional method, mdlSetWorkWidths is called after input port
  *      width, output port width, and sample times of the S-function have
@@ -262,7 +258,7 @@ static void mdlSetWorkWidths(SimStruct *S)
 #define MDL_START
 #if defined(MDL_START)
 
-/* Function: mdlStart =====================================================
+/* 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
@@ -275,7 +271,7 @@ static void mdlStart(SimStruct *S)
 
 #endif
 
-/* Function: mdlOutputs ===================================================
+/* Function: mdlOutputs ========================================================
  * Abstract:
  *    In this function, you compute the outputs of your S-function
  *    block. Generally outputs are placed in the output vector(s),
@@ -287,7 +283,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     UNUSED_PARAMETER(tid);
 }
 
-/* Function: mdlTerminate =================================================
+/* Function: mdlTerminate ======================================================
  * Abstract:
  *    In this function, you should perform any actions that are necessary
  *    at the termination of a simulation.
@@ -300,7 +296,7 @@ static void mdlTerminate(SimStruct *S)
 #define MDL_RTW
 #if defined(MATLAB_MEX_FILE) && defined(MDL_RTW)
 
-/* Function: mdlRTW =======================================================
+/* Function: mdlRTW ============================================================
  * Abstract:
  *    This function is called when the Real-Time Workshop is generating
  *    the model.rtw file.
@@ -312,33 +308,6 @@ static void mdlRTW(SimStruct *S)
 
 #endif
 
-/* Function: IsRealMatrix =================================================
- * Abstract:
- *      Verify that the mxArray is a real (double) finite matrix
- */
-static bool IsRealMatrix(const mxArray * const m)
-{
-  if (mxIsNumeric(m) &&
-      mxIsDouble(m) &&
-      !mxIsLogical(m) &&
-      !mxIsComplex(m) &&
-      !mxIsSparse(m) &&
-      !mxIsEmpty(m) &&
-      mxGetNumberOfDimensions(m) == 2) {
-    const double * const data = mxGetPr(m);
-    const size_t numEl = mxGetNumberOfElements(m);
-    size_t i;
-    for (i = 0; i < numEl; i++) {
-      if (!mxIsFinite(data[i])) {
-        return(false);
-      }
-    }
-
-    return(true);
-  } else {
-    return(false);
-  }
-}
 
 /*
  * Required S-function trailer
index 6f2c8a486bb6d62aabefe49491c2db69c13f0044..235fa15d6143c99cac23142581e27f0a92165b84 100644 (file)
  *     C-MEX S-function block for RPP digital output.
  *
  * 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
+ *
+ *     Compile with <matlabroot>/bin/mex -v -g sfunction_lout.c
  */
 
 
 #define S_FUNCTION_LEVEL               2
 
 /*
- * Need to include simstruc.h for the definition of the SimStruct and
- * its associated macro definitions.
- */
-#include "simstruc.h"
-#define EDIT_OK(S, P_IDX) \
- (!((ssGetSimMode(S)==SS_SIMMODE_SIZES_CALL_ONLY) && mxIsEmpty(ssGetSFcnParam(S, P_IDX))))
-#define SAMPLE_TIME                    (ssGetSFcnParam(S, 1))
-
-/*
- * Utility function prototypes.
+ * Include common header and utilities
  */
-static bool IsRealMatrix(const mxArray * const m);
+#include "utils.c"
 
 #define MDL_CHECK_PARAMETERS
 #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
 
-/* Function: mdlCheckParameters ===========================================
+/* Function: mdlCheckParameters ================================================
  * Abstract:
  *    mdlCheckParameters verifies new parameter settings whenever parameter
  *    change or are re-evaluated during a simulation. When a simulation is
@@ -114,7 +114,7 @@ static void mdlCheckParameters(SimStruct *S)
 
 #endif
 
-/* Function: mdlInitializeSizes ===========================================
+/* Function: mdlInitializeSizes ================================================
  * Abstract:
  *    The sizes information is used by Simulink to determine the S-function
  *    block's characteristics (number of inputs, outputs, states, etc.).
@@ -202,7 +202,7 @@ static void mdlInitializeSizes(SimStruct *S)
                SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
 }
 
-/* Function: mdlInitializeSampleTimes =====================================
+/* 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
@@ -231,7 +231,7 @@ static void mdlInitializeSampleTimes(SimStruct *S)
 #define MDL_SET_WORK_WIDTHS
 #if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
 
-/* Function: mdlSetWorkWidths =============================================
+/* Function: mdlSetWorkWidths ==================================================
  * Abstract:
  *      The optional method, mdlSetWorkWidths is called after input port
  *      width, output port width, and sample times of the S-function have
@@ -258,7 +258,7 @@ static void mdlSetWorkWidths(SimStruct *S)
 #define MDL_START
 #if defined(MDL_START)
 
-/* Function: mdlStart =====================================================
+/* 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
@@ -271,7 +271,7 @@ static void mdlStart(SimStruct *S)
 
 #endif
 
-/* Function: mdlOutputs ===================================================
+/* Function: mdlOutputs ========================================================
  * Abstract:
  *    In this function, you compute the outputs of your S-function
  *    block. Generally outputs are placed in the output vector(s),
@@ -283,7 +283,7 @@ static void mdlOutputs(SimStruct *S, int_T tid)
     UNUSED_PARAMETER(tid);
 }
 
-/* Function: mdlTerminate =================================================
+/* Function: mdlTerminate ======================================================
  * Abstract:
  *    In this function, you should perform any actions that are necessary
  *    at the termination of a simulation.
@@ -296,7 +296,7 @@ static void mdlTerminate(SimStruct *S)
 #define MDL_RTW
 #if defined(MATLAB_MEX_FILE) && defined(MDL_RTW)
 
-/* Function: mdlRTW =======================================================
+/* Function: mdlRTW ============================================================
  * Abstract:
  *    This function is called when the Real-Time Workshop is generating
  *    the model.rtw file.
@@ -308,33 +308,6 @@ static void mdlRTW(SimStruct *S)
 
 #endif
 
-/* Function: IsRealMatrix =================================================
- * Abstract:
- *      Verify that the mxArray is a real (double) finite matrix
- */
-static bool IsRealMatrix(const mxArray * const m)
-{
-  if (mxIsNumeric(m) &&
-      mxIsDouble(m) &&
-      !mxIsLogical(m) &&
-      !mxIsComplex(m) &&
-      !mxIsSparse(m) &&
-      !mxIsEmpty(m) &&
-      mxGetNumberOfDimensions(m) == 2) {
-    const double * const data = mxGetPr(m);
-    const size_t numEl = mxGetNumberOfElements(m);
-    size_t i;
-    for (i = 0; i < numEl; i++) {
-      if (!mxIsFinite(data[i])) {
-        return(false);
-      }
-    }
-
-    return(true);
-  } else {
-    return(false);
-  }
-}
 
 /*
  * Required S-function trailer
index e29bc37cbcfb2711d5d9646285b83895fb5de2c6..6d9ea67d1c0fcae807755f266375e5ca1a9ec10b 100644 (file)
 
     %if !SLibCodeGenForSim()
         %<RppCommonStart(block, system)>
+
+        %% Get pin number and pull type
+        %assign p1_val = LibBlockParameter(p1, "", "", 0)
+        %assign p2_val = LibBlockParameter(p2, "", "", 0)
+
+        rpp_din_setup(%<p1_val>, %<p2_val>);
+
     %endif
 
 %endfunction
 %function Outputs(block, system) Output
 
     %if !SLibCodeGenForSim()
+
+        %% First executed block must update cached values
+        %if EXISTS("_RPP_DIN_UPDATE_") == 0
+            %assign _RPP_DIN_UPDATE_ = 1
+            rpp_din_update();
+        %endif
+
+        %% Get pin number and if to use variable threshold
         %assign p1_val = LibBlockParameter(p1, "", "", 0)
-        %assign u1_val = LibBlockInputSignal(0, "", "", 0)
+        %assign p3_val = LibBlockParameter(p3, "", "", 0)
+
+        %assign y1_val = LibBlockOutputSignal(0, "", "", 0)
+        %<y1_val> = rpp_din_get(%<p1_val>, %<p3_val>);
 
-        //rpp_din_get(%<p1_val>, %<u1_val>);
     %endif
 
 %endfunction
index 75f41d29eb240432a41bef1408fd41215717750d..8cd78be6db2dffa12e76590d1439eec04392f9b0 100644 (file)
 %function Outputs(block, system) Output
 
     %if !SLibCodeGenForSim()
+
+        %% Get pin number
         %assign p1_val = LibBlockParameter(p1, "", "", 0)
+
         %assign u1_val = LibBlockInputSignal(0, "", "", 0)
+        rpp_lout_set(%<p1_val>, %<u1_val>);
+
+        %% FIXME: Improve to call this function only once per step
+        %%  But... what happens if the next block want to check it's diagnostic
+        %%  value and is not the last LOUT block?
+        rpp_lout_update();
+
+        %% TODO: Get diagnostic value, if mismatch, set error output flag.
 
-        //rpp_lout_set(%<p1_val>, %<u1_val>);
     %endif
 
 %endfunction