Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem writing s-function (c/c++) inputports problem with arrays

Status
Not open for further replies.

prankmunky

Computer
Mar 29, 2006
4
I am a new user of simulink and I'm sure my problem is quite simple for someone with some experience writing s-functions. I am attempting to write an s-function in c++ that takes as input 3 double[][3] arrays(the row value is to be dynamicaly_size) and 3 integer values as input. The output I want is another double array of size 4x4. In mdloutputs all that I want to happen is a call to my c++ registration function code using the input values and the ouput to be the updated version of the 4x4 matrix that is adjusted in the registration call. The parmeters for egistration are:

Registration(double [][3], int32, double [][3], int32, double [][3], int32, double[4][4])

I do not entirely understand the use of Real_T for casting. My registration function wont recognize real_T values (I thought perhaps being cast as Real_T would automatically read the input as its true type as I declared when initializing). So my question is, how would I cast my input to make registration recognize the inputs as double[][3] and the output as a ?

Here is my code(All you probably need to look at is mdlInitializeSizes and mdlOutputs):
#define S_FUNCTION_NAME regICPMAT
#define S_FUNCTION_LEVEL 2
#define MATLAB_MEX_FILE
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
#include "registration.h"


/* Error handling
* --------------
*
* You should use the following technique to report errors
encountered within
* an S-function:
*
* ssSetErrorStatus(S,"Error encountered due to ...");
* return;
*
* Note that the 2nd argument to ssSetErrorStatus must be persistent
memory.
* It cannot be a local variable. For example the following will
cause
* unpredictable errors:
*
* mdlOutputs()
* {
* char msg[256]; {ILLEGAL: to fix use "static char
msg[256];"}
* sprintf(msg,"Error due to %s", string);
* ssSetErrorStatus(S,msg);
* return;
* }
*
* See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
*/

/*====================*
* S-function methods *
*====================*/

/* Function: mdlInitializeSizes
===============================================
* Abstract:
* The sizes information is used by Simulink to determine the
S-function
* block's characteristics (number of inputs, outputs, states,
etc.).
*/
static void mdlInitializeSizes(SimStruct *S)
{
/* See sfuntmpl_doc.c for more details on the macros below */

ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual
parameters */
return;
}

ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);

if (!ssSetNumInputPorts(S, 6)) return;
ssSetInputPortMatrixDimensions(S, 0, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 1, SS_INT32);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortMatrixDimensions(S, 2, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 3, SS_INT32);
ssSetInputPortWidth(S, 3, 1);
ssSetInputPortMatrixDimensions(S, 4, DYNAMICALLY_SIZED, 3);
ssSetInputPortDataType(S, 5, SS_INT32);
ssSetInputPortWidth(S, 5, 1);
ssSetInputPortMatrixDimensions(S, 6, 4, 4);

ssSetInputPortRequiredContiguous(S, 0, true); /*direct input
signal access*/
ssSetInputPortRequiredContiguous(S, 1, true);
ssSetInputPortRequiredContiguous(S, 2, true);
ssSetInputPortRequiredContiguous(S, 3, true);
ssSetInputPortRequiredContiguous(S, 4, true);
ssSetInputPortRequiredContiguous(S, 5, true);
ssSetInputPortRequiredContiguous(S, 6, true);

/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
*/
//i think this should be zero for all since they do not directly c
ssSetInputPortDirectFeedThrough(S, 0, 0);
ssSetInputPortDirectFeedThrough(S, 1, 0);
ssSetInputPortDirectFeedThrough(S, 2, 0);
ssSetInputPortDirectFeedThrough(S, 3, 0);
ssSetInputPortDirectFeedThrough(S, 4, 0);
ssSetInputPortDirectFeedThrough(S, 5, 0);

if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortMatrixDimensions(S, 0, 4, 4);

ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);

ssSetOptions(S, 0);
}

static void mdlInitializeSampleTimes(SimStruct *S)
{
//what kind of sample time should be used
//ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);

}

#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove
function */
#if defined(MDL_INITIALIZE_CONDITIONS)

static void mdlInitializeConditions(SimStruct *S)
{
}
#endif /* MDL_INITIALIZE_CONDITIONS */

#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)

static void mdlStart(SimStruct *S)
{
}
#endif /* MDL_START */

static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *in1 = (real_T*) ssGetInputPortSignal(S,0);
real_T *in2 = (real_T*) ssGetInputPortSignal(S,1);
real_T *in3 = (real_T*) ssGetInputPortSignal(S,2);
real_T *in4 = (real_T*) ssGetInputPortSignal(S,3);
real_T *in5 = (real_T*) ssGetInputPortSignal(S,4);
real_T *in6 = (real_T*) ssGetInputPortSignal(S,5);

double matrix[4][4];

registration(in1,in2,in3,in4,in5,in6,matrix);

real_T *y = ssGetOutputPortSignal(S,0); //this is fine
y[0] = matrix;
}

#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)

static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif /* MDL_UPDATE */

#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)

static void mdlDerivatives(SimStruct *S)
{
}
#endif /* MDL_DERIVATIVES */

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor