sf_posreg.cc

00001 #include <math.h>
00002 #include <trgen.h>
00003 #include <balet.h>
00004 
00005 #ifdef __cplusplus
00006 extern "C" { // use the C fcn-call standard for all functions  
00007 #endif       // defined within this scope                     
00008 
00009 #define S_FUNCTION_LEVEL 2
00010 #define S_FUNCTION_NAME  sf_posreg
00011 
00012 /*
00013  * Need to include simstruc.h for the definition of the SimStruct and
00014  * its associated macro definitions.
00015  */
00016 #include "simstruc.h"
00017 
00018 
00019 
00020 #define INPUT_REF_POS 0
00021 #define INPUT_EST_POS 1
00022 
00023 #define DP_K_IDX 0
00024 #define K_PARAM(S) (ssGetSFcnParam(S,DP_K_IDX))
00025 
00026 /*====================*
00027  * S-function methods *
00028  *====================*/
00029 
00030 #define MDL_CHECK_PARAMETERS
00031 #if defined(MDL_CHECK_PARAMETERS)  && defined(MATLAB_MEX_FILE)
00032 /*
00033  * Check to make sure that each parameter is 1-d and positive
00034  */
00035 static void mdlCheckParameters(SimStruct *S)
00036 {
00037     const mxArray *k = K_PARAM(S);
00038 
00039     if (mxGetM(k) * mxGetN(k) != 3) {
00040         ssSetErrorStatus(S, "Parameter must bu 3 element vector");
00041         return;
00042     }
00043 }
00044 #endif
00045 
00046 
00047 /* Function: mdlInitializeSizes ===============================================
00048  * Abstract:
00049  *    The sizes information is used by Simulink to determine the S-function
00050  *    block's characteristics (number of inputs, outputs, states, etc.).
00051  */
00052 static void mdlInitializeSizes(SimStruct *S)
00053 {
00054     /* See sfuntmpl.doc for more details on the macros below */
00055     ssSetNumSFcnParams(S, 1);  /* Number of expected parameters */
00056 
00057 #if defined(MATLAB_MEX_FILE)
00058     if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
00059         mdlCheckParameters(S);
00060         if (ssGetErrorStatus(S) != NULL) {
00061             return;
00062         }
00063     } else {
00064         return; /* Parameter mismatch will be reported by Simulink */
00065     }
00066 #endif
00067 
00068     ssSetSFcnParamTunable(S, DP_K_IDX, 1);
00069 
00070     ssSetNumContStates(S, 0);
00071     ssSetNumDiscStates(S, 0);
00072 
00073     if (!ssSetNumInputPorts(S, 2)) return;
00074 
00075     ssSetInputPortWidth(S, INPUT_REF_POS, 5);
00076     ssSetInputPortDirectFeedThrough(S, INPUT_REF_POS, 1);
00077     ssSetInputPortRequiredContiguous(S, INPUT_REF_POS, 1);
00078 
00079     ssSetInputPortWidth(S, INPUT_EST_POS, 3);
00080     ssSetInputPortDirectFeedThrough(S, INPUT_EST_POS, 1);
00081     ssSetInputPortRequiredContiguous(S, INPUT_EST_POS, 1);
00082     
00083     if (!ssSetNumOutputPorts(S, 1)) return;
00084     ssSetOutputPortWidth(S, 0, 2);
00085 
00086 
00087     ssSetNumSampleTimes(S, 1);
00088     ssSetNumRWork(S, 0);
00089     ssSetNumIWork(S, 0);
00090     ssSetNumPWork(S, 0); // reserve element in the pointers vector
00091     ssSetNumModes(S, 0); // to store a C++ object
00092     ssSetNumNonsampledZCs(S, 0);
00093 
00094     ssSetOptions(S, 0);
00095 }
00096 
00097 
00098 
00099 /* Function: mdlInitializeSampleTimes =========================================
00100  * Abstract:
00101  *    This function is used to specify the sample time(s) for your
00102  *    S-function. You must register the same number of sample times as
00103  *    specified in ssSetNumSampleTimes.
00104  */
00105 static void mdlInitializeSampleTimes(SimStruct *S)
00106 {
00107     ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
00108     ssSetOffsetTime(S, 0, 0.0);
00109     ssSetModelReferenceSampleTimeDefaultInheritance(S);
00110 }
00111 
00112 //#define MDL_START  /* Change to #undef to remove function */
00113 #if defined(MDL_START) 
00114   /* Function: mdlStart =======================================================
00115    * Abstract:
00116    *    This function is called once at start of model execution. If you
00117    *    have states that should be initialized once, this is the place
00118    *    to do it.
00119    */
00120   static void mdlStart(SimStruct *S)
00121   {
00122   }
00123 #endif /*  MDL_START */
00124 
00125 /* Function: mdlOutputs =======================================================
00126  * Abstract:
00127  *    In this function, you compute the outputs of your S-function
00128  *    block. Generally outputs are placed in the output vector, ssGetY(S).
00129  */
00130 static void mdlOutputs(SimStruct *S, int_T tid)
00131 {
00132     const real_T  *y = ssGetInputPortRealSignal(S,INPUT_EST_POS); 
00133     const real_T  *r = ssGetInputPortRealSignal(S,INPUT_REF_POS); 
00134     double *u = ssGetOutputPortRealSignal(S,0);
00135     const double *k = mxGetPr(K_PARAM(S));
00136     Pos rp;
00137     rp.x = r[0];
00138     rp.y = r[1];
00139     rp.phi = r[2];
00140     rp.v = r[3];
00141     rp.omega = r[4];
00142 
00143     balet(rp, y, u, k);
00144 
00145     UNUSED_ARG(tid);
00146 }                                                
00147 
00148 /* Function: mdlTerminate =====================================================
00149  * Abstract:
00150  *    In this function, you should perform any actions that are necessary
00151  *    at the termination of a simulation.  For example, if memory was
00152  *    allocated in mdlStart, this is the place to free it.
00153  */
00154 static void mdlTerminate(SimStruct *S)
00155 {
00156 }                                              // function
00157 /*======================================================*
00158  * See sfuntmpl.doc for the optional S-function methods *
00159  *======================================================*/
00160 
00161 /*=============================*
00162  * Required S-function trailer *
00163  *=============================*/
00164 
00165 #ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
00166 #include "simulink.c"      /* MEX-file interface mechanism */
00167 #else
00168 #include "cg_sfun.h"       /* Code generation registration function */
00169 #endif
00170 
00171 #ifdef __cplusplus
00172 } // end of extern "C" scope
00173 #endif

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3