]> rtime.felk.cvut.cz Git - jenkicar/testing.git/commitdiff
Lookup table version previous to S-Function implementation.
authorCarlos Jenkins <carlos@work.(none)>
Thu, 21 Feb 2013 15:14:23 +0000 (16:14 +0100)
committerCarlos Jenkins <carlos@work.(none)>
Thu, 21 Feb 2013 15:14:23 +0000 (16:14 +0100)
Motor controller/motor_controller.slx [new file with mode: 0644]
Motor controller/rotation_dec.c [new file with mode: 0644]
Motor controller/test1.slx [new file with mode: 0644]
Motor controller/timestwo.c [new file with mode: 0644]

diff --git a/Motor controller/motor_controller.slx b/Motor controller/motor_controller.slx
new file mode 100644 (file)
index 0000000..0ae6882
Binary files /dev/null and b/Motor controller/motor_controller.slx differ
diff --git a/Motor controller/rotation_dec.c b/Motor controller/rotation_dec.c
new file mode 100644 (file)
index 0000000..06dc0ab
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2013 Czech Technical University in Prague
+ *
+ * Authors:
+ *      - Carlos Jenkins <carlos@jenkins.co.cr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * File : rotation_dec.c
+ * Abstract:
+ *       Simple lookup-table / mapping function between a sensors decoded
+ *       integer and rotation sense, error or no rotation. MEX S-function
+ *       version of the following table:
+ *
+ *         I    A  Ap B  Bp |  O   Err
+ *         ----------------------------
+ *         00)  0  0  0  0  |   0   f
+ *         01)  0  0  0  1  |  -1   f
+ *         02)  0  0  1  0  |  +1   f
+ *         03)  0  0  1  1  |   0   f
+ *         04)  0  1  0  0  |  +1   f
+ *         05)  0  1  0  1  |  -2   t
+ *         06)  0  1  1  0  |  -2   t
+ *         07)  0  1  1  1  |  -1   f
+ *         08)  1  0  0  0  |  -1   f
+ *         09)  1  0  0  1  |  -2   t
+ *         10)  1  0  1  0  |  -2   t
+ *         11)  1  0  1  1  |  +1   f
+ *         12)  1  1  0  0  |   0   f
+ *         13)  1  1  0  1  |  +1   f
+ *         14)  1  1  1  0  |  -1   f
+ *         15)  1  1  1  1  |   0   f
+ *
+ */
+
+#define S_FUNCTION_NAME  rotation_dec
+#define S_FUNCTION_LEVEL 2
+
+#include "simstruc.h"
+
+static real_T DEC_TABLE[16] = {
+        0, -1, +1, 0, +1, -2, -2, -1, -1, -2, -2, +1, 0, +1, -1, 0
+    };
+
+/* Function: mdlInitializeSizes ===============================================
+ * Abstract:
+ *   Setup sizes of the various vectors.
+ */
+static void mdlInitializeSizes(SimStruct *S)
+{
+    /* Configure parameters */
+    ssSetNumSFcnParams(S, 0);
+    if(ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
+        return;
+    }
+
+    /* Configure input ports */
+    if(!ssSetNumInputPorts(S, 1)) {
+        return;
+    }
+    ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
+    ssSetInputPortDirectFeedThrough(S, 0, 1);
+
+    /* Configure output ports */
+    if(!ssSetNumOutputPorts(S, 2)) {
+        return;
+    }
+    ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED); /* Output     */
+    ssSetOutputPortWidth(S, 1, 1);                 /* Error flag */
+    ssSetOutputPortDataType(S, 1, SS_BOOLEAN);
+
+    /* Configure sample time */
+    ssSetNumSampleTimes(S, 1);
+
+    /* FIXME: Check the following lines, not sure what they does. */
+    /* specify the sim state compliance to be same as a built-in block */
+    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
+
+    /* Take care when specifying exception free code - see sfuntmpl_doc.c */
+    ssSetOptions(S,
+                 SS_OPTION_WORKS_WITH_CODE_REUSE |
+                 /*SS_OPTION_EXCEPTION_FREE_CODE |*/
+                 SS_OPTION_USE_TLC_WITH_ACCELERATOR);
+}
+
+
+/* Function: mdlInitializeSampleTimes =========================================
+ * Abstract:
+ *    Specifiy that we inherit our sample time from the driving block.
+ */
+static void mdlInitializeSampleTimes(SimStruct *S)
+{
+    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
+    ssSetOffsetTime(S, 0, 0.0);
+    ssSetModelReferenceSampleTimeDefaultInheritance(S);
+}
+
+/* Function: mdlOutputs =======================================================
+ * Abstract:
+ *    o = table[i]
+ */
+static void mdlOutputs(SimStruct *S, int_T tid)
+{
+    /* IO ports pointers */
+    InputRealPtrsType inptr  = ssGetInputPortRealSignalPtrs(S, 0);
+    real_T*           outptr = ssGetOutputPortRealSignal(S, 0);
+    boolean_T*        errptr = (boolean_T*) ssGetOutputPortSignal(S, 1);
+
+    /* Process variables */
+    boolean_T err = false;
+    real_T input;
+    real_T value;
+
+    int_T i;
+    for(i = 0; i < ssGetOutputPortWidth(S, 0); i++) {
+
+        input = *inptr[i];
+
+        /* Check decoder out of bounds */
+        if((input < 0.0) || (input >= 16.0)) {
+            input = 0.0;
+            err = true;
+        }
+
+        /* Check decoder valid entry */
+        value = DEC_TABLE[(int) input];
+        if(value == -2) {
+            err = true;
+        }
+
+        *outptr++ = value;
+    }
+
+    /* Set or clear error flag */
+    errptr[0] = err;
+}
+
+
+/* Function: mdlTerminate =====================================================
+ * Abstract:
+ *    No termination needed, but we are required to have this routine.
+ */
+static void mdlTerminate(SimStruct *S)
+{
+}
+
+
+
+#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
+
diff --git a/Motor controller/test1.slx b/Motor controller/test1.slx
new file mode 100644 (file)
index 0000000..206f681
Binary files /dev/null and b/Motor controller/test1.slx differ
diff --git a/Motor controller/timestwo.c b/Motor controller/timestwo.c
new file mode 100644 (file)
index 0000000..1555e83
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * File : timestwo.c
+ * Abstract:
+ *       An example C-file S-function for multiplying an input by 2,
+ *       y  = 2*u
+ *
+ * Real-Time Workshop note:
+ *   This file can be used as is (noninlined) with the Real-Time Workshop
+ *   C rapid prototyping targets, or it can be inlined using the Target 
+ *   Language Compiler technology and used with any target. See 
+ *     matlabroot/toolbox/simulink/blocks/tlc_c/timestwo.tlc   
+ *   the TLC code to inline the S-function.
+ *
+ * See simulink/src/sfuntmpl_doc.c
+ *
+ * Copyright 1990-2009 The MathWorks, Inc.
+ * $Revision: 1.1.6.2 $
+ */
+
+
+#define S_FUNCTION_NAME  timestwo
+#define S_FUNCTION_LEVEL 2
+
+#include "simstruc.h"
+
+/*================*
+ * Build checking *
+ *================*/
+
+
+/* Function: mdlInitializeSizes ===============================================
+ * Abstract:
+ *   Setup sizes of the various vectors.
+ */
+static void mdlInitializeSizes(SimStruct *S)
+{
+    ssSetNumSFcnParams(S, 0);
+    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
+        return; /* Parameter mismatch will be reported by Simulink */
+    }
+
+    if (!ssSetNumInputPorts(S, 1)) return;
+    ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
+    ssSetInputPortDirectFeedThrough(S, 0, 1);
+
+    if (!ssSetNumOutputPorts(S,1)) return;
+    ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
+
+    ssSetNumSampleTimes(S, 1);
+
+    /* specify the sim state compliance to be same as a built-in block */
+    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
+
+    /* Take care when specifying exception free code - see sfuntmpl_doc.c */
+    ssSetOptions(S,
+                 SS_OPTION_WORKS_WITH_CODE_REUSE |
+                 SS_OPTION_EXCEPTION_FREE_CODE |
+                 SS_OPTION_USE_TLC_WITH_ACCELERATOR);
+}
+
+
+/* Function: mdlInitializeSampleTimes =========================================
+ * Abstract:
+ *    Specifiy that we inherit our sample time from the driving block.
+ */
+static void mdlInitializeSampleTimes(SimStruct *S)
+{
+    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
+    ssSetOffsetTime(S, 0, 0.0);
+    ssSetModelReferenceSampleTimeDefaultInheritance(S); 
+}
+
+/* Function: mdlOutputs =======================================================
+ * Abstract:
+ *    y = 2*u
+ */
+static void mdlOutputs(SimStruct *S, int_T tid)
+{
+    int_T             i;
+    InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
+    real_T            *y    = ssGetOutputPortRealSignal(S,0);
+    int_T             width = ssGetOutputPortWidth(S,0);
+
+    for (i=0; i<width; i++) {
+        /*
+         * This example does not implement complex signal handling.
+         * To find out see an example about how to handle complex signal in 
+         * S-function, see sdotproduct.c for details.
+         */
+        *y++ = 2.0 *(*uPtrs[i]); 
+    }
+}
+
+
+/* Function: mdlTerminate =====================================================
+ * Abstract:
+ *    No termination needed, but we are required to have this routine.
+ */
+static void mdlTerminate(SimStruct *S)
+{
+}
+
+
+
+#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