Realtime Streaming with C mex s-Function

Post Reply
jaykay
Posts: 5
Joined: Tue Oct 28, 2014 6:39 am

Realtime Streaming with C mex s-Function

Post by jaykay »

Hello Everybody,

i am trying to creat a block diagram in Simulink. Then i want to build a Realtime Windows Target with the Simulink Coder. For this i created a C mex s-Function which receives data from Motive and returns Location and Orientation. I used the mex-Compiler to compile it and insert it in a s-Function block. When i want to run Simulation, Matlab breaks down because of an fatal execusion in mdlOutputs.

I cant't find the mistake. Any suggestions?

Code: Select all

#define S_FUNCTION_NAME  sfun_c_test
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"


#ifdef WIN32
#  pragma warning( disable : 4996 )
#  define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif

#include <cstring> // For memset.
#include <windows.h>
#include <winsock.h>
#include "resource.h"
#include <tchar.h>

// NatNet SDK 2.7
#include "NatNetTypes.h"    // NatNet data & Rigid Body DataDiscription
#include "NatNetClient.h"   // Initialize Client
#include "natutils.h"       // Convert quaternion to Euler angles

#pragma comment(lib, "NatNetLib")

NatNetClient* theClient = new NatNetClient;

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

static void mdlInitializeSizes(SimStruct *S)
{
    int_T nInputPorts  = 0;  /* number of input ports  */
    int_T nOutputPorts = 1;  /* number of output ports */
    int_T needsInput   = 0;  /* direct feed through    */
    
    int_T inputPortIdx  = 0;
    int_T outputPortIdx = 0;
    
    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);
    
    // Output
    if (!ssSetNumOutputPorts(S, nOutputPorts)) return;
    if(!ssSetOutputPortDimensionInfo(S,outputPortIdx,DYNAMIC_DIMENSION)) return;
    ssSetOutputPortWidth(S, 0, 1);

    // Sample Time
    ssSetNumSampleTimes(S, 1);
    // Work vectors
    ssSetNumRWork(         S, 0);   /* real work vector elements   */
    ssSetNumIWork(         S, 0);   /* integer work vector elements*/
    ssSetNumPWork(         S, 0);   /* pointer work vector elements*/
    ssSetNumModes(         S, 0);   /* mode work vector elements   */
    ssSetNumNonsampledZCs( S, 0);   /* nonsampled zero crossings   */

    ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
    ssSetOptions(S, 0);
}   /* end mdlInitializeSizes */


static void mdlInitializeSampleTimes(SimStruct *S)
{
    double sampleTime = 0.01;
    ssSetSampleTime(S, 0, sampleTime);
    ssSetOffsetTime(S, 0, 0.0);
}   /* end mdlInitializeSampleTimes */


#define MDL_START  
#if defined(MDL_START) 
  static void mdlStart(SimStruct *S)
  {
    // NatNet server IP address.
    char szIPAddress[30] = {127,0,0,1};
    char szServerIPAddress[30] = {127,0,0,1};

    theClient->Initialize(szIPAddress, szServerIPAddress);
  }
#endif /*  MDL_START */

 
static void mdlOutputs(SimStruct *S, int_T tid)
{
    real_T       *y = ssGetOutputPortRealSignal(S,0);
    sFrameOfMocapData* pData = theClient->GetLastFrameOfData();
    y[0] = pData->iFrame;
}

static void mdlTerminate(SimStruct *S)
{
    theClient->Uninitialize();
}

/*=============================*
 * 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
jaykay
Posts: 5
Joined: Tue Oct 28, 2014 6:39 am

Re: Realtime Streaming with C mex s-Function

Post by jaykay »

If comment the lines:

sFrameOfMocapData* pData = theClient->GetLastFrameOfData();
y[0] = pData->iFrame;

and replace them with y[0] = 42 i don't get an error.
It seems that NatNatClient causes the Matlab crash. Maybe it's a memory allocation error.
Is there anybody who can help me?
Post Reply