mex file compiling error

I want to create mex file but the compiler give this error..
1. Error ForceSensor.c: 121 undeclared identifier `USE_DEFAULT_SIM_STATE' 2. Warning ForceSensor.c: 121 possible usage of USE_DEFAULT_SIM_STATE before definition 1 errors, 1 warnings
anybody could help

 Accepted Answer

Is this an S-function? If yes, you are probably missing:
#include "simstruc.h"

3 Comments

See also http://www.mathworks.com/matlabcentral/answers/1850-mex-error-undeclared-identifier-what-does-this-mean
Yes, it is an S-Function..I'm using basic C-mex template from Matlab 2006a which already included the "simstruc.h". The program are as follow.
#define S_FUNCTION_NAME ForceSensor
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "windows.h"
int CJr3_ReadJr3(int, int *, int);
int CJr3_WriteJr3(int addr, int *data, int nump);
void CJr3_RegisterWinDriver(void);
int CJr3_DeInitJr3(void);
int CJr3_InitJr3(unsigned long VendorID, unsigned long DeviceID, int nump);
int CJr3_MemAllocate(DWORD addr, int i);
int CJr3_download(DWORD BaseAddressHH, DWORD BaseAddressLL);
void CJr3_Wait(void);
int CJr3_GetFx(int, int); // Output raw Fx, input filter # and DSP #
int CJr3_GetFy(int, int); // Output raw Fy, input filter # and DSP #
int CJr3_GetFz(int, int); // Output raw Fz, input filter # and DSP #
int CJr3_GetMx(int, int); // Output raw Mx, input filter # and DSP #
int CJr3_GetMy(int, int); // Output raw My, input filter # and DSP #
int CJr3_GetMz(int, int); // Output raw Mz, input filter # and DSP #
void CJr3_ResetOffset(int); // Input DSP #
/* 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, 0)) return;
//ssSetInputPortWidth(S, 0, 1);
//ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
/*
* 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.
*/
//ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S, 2)) return;
ssSetOutputPortWidth(S, 0, 6);
ssSetOutputPortWidth(S, 1, 6);
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, 0);
/* Initialize force sensor board */
CJr3_RegisterWinDriver();
{
int flag,data;
flag = CJr3_InitJr3(0x1762, 0x3112, 2);
if(flag==0)
printf("Succeeded in Force Sensor Board\n");
else
printf("Error in Force Sensor Board\n");
CJr3_ReadJr3(0xfc, &data, 2);
printf("unit:%d\n",data);
}
}
/* 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
* specified in ssSetNumSampleTimes.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
#if defined(MDL_INITIALIZE_CONDITIONS)
/* Function: mdlInitializeConditions ========================================
* Abstract:
* In this function, you should initialize the continuous and discrete
* states for your S-function block. The initial states are placed
* in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
* You can also perform any other initialization activities that your
* S-function may require. Note, this routine will be called at the
* start of simulation and if it is present in an enabled subsystem
* configured to reset states, it will be call when the enabled subsystem
* restarts execution to reset the states.
*/
static void mdlInitializeConditions(SimStruct *S)
{
}
#endif /* MDL_INITIALIZE_CONDITIONS */
#define MDL_START /* Change to #undef to remove function */
#if defined(MDL_START)
/* 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
* to do it.
*/
static void mdlStart(SimStruct *S)
{
}
#endif /* MDL_START */
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
int i,addr,data,fullscale,filter;
// const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
real_T *y1 = (real_T *)ssGetOutputPortSignal(S,0);
real_T *y2 = (real_T *)ssGetOutputPortSignal(S,1);
filter=0;
for(i=0;i<6;i++){
addr = 0x90+ i + filter * 8;
CJr3_ReadJr3(addr, &data, 1);
if(data&0x8000)
data |= 0xffff0000;
addr = 0x80+ i;
CJr3_ReadJr3(addr, &fullscale, 1);
if(i<3)
y1[i] = (real_T)data /16384.0*fullscale;
else
y1[i] = (real_T)data /16384.0*fullscale/10;
}
for(i=0;i<6;i++){
addr = 0x90+ i + filter * 8;
CJr3_ReadJr3(addr, &data, 2);
if(data&0x8000)
data |= 0xffff0000;
addr = 0x80+ i;
CJr3_ReadJr3(addr, &fullscale, 2);
if(i<3)
y2[i] = (real_T)data /16384.0*fullscale;
else
y2[i] = (real_T)data /16384.0*fullscale/10;
}
}
#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
/* Function: mdlUpdate ======================================================
* Abstract:
* This function is called once for every major integration time step.
* Discrete states are typically updated here, but this function is useful
* for performing any tasks that should only take place once per
* integration step.
*/
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif /* MDL_UPDATE */
#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
/* Function: mdlDerivatives =================================================
* Abstract:
* In this function, you compute the S-function block's derivatives.
* The derivatives are placed in the derivative vector, ssGetdX(S).
*/
static void mdlDerivatives(SimStruct *S)
{
}
#endif /* MDL_DERIVATIVES */
/* Function: mdlTerminate =====================================================
* Abstract:
* In this function, you should perform any actions that are necessary
* at the termination of a simulation. For example, if memory was
* allocated in mdlStart, this is the place to free it.
*/
static void mdlTerminate(SimStruct *S)
{
CJr3_DeInitJr3();
}
/*======================================================*
* See sfuntmpl_doc.c for the optional S-function methods *
*======================================================*/
/*=============================*
* Required S-function trailer *
*=============================*/
#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
Did you verify if the link that Ken posted applies to your version of MATLAB? The error could be due to a bug.

Sign in to comment.

More Answers (0)

Asked:

on 13 Feb 2012

Edited:

dpb
on 11 Oct 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!