- Make a "PWork" vector, which is a list of pointers in the S-function that persist in between calls to "mdl*" functions.
- In "mdlStart" use "new" to dynamcially create an instance of "Example1" and use the "PWork" vector as a handle so that you can access the instance of "Example1" in other "mdl" functions.
- In "mdlOutputs", access the "PWork" vector and call ".go()" "create_scene", or whatever, (I don't know much about Ogre...)
- In "mdlTerminate", call get the instance of "Example1" out of the "PWork" vector and use "delete" to clean up any memory that was allocated.
3d graphics with s-function
    9 views (last 30 days)
  
       Show older comments
    
Hello! Help me please! I try to use 3d graphics (ogre3d) to visualize calculations in s-function. I experiment with standard s-function timestwo. I have added in it application Win32 which simply displays a window with simple object (It doesn't depend on calculations of s-function). Code of s-function natash.cpp:
#define S_FUNCTION_NAME  natash
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "Ogre\ExampleApplication.h";
class Example1 : public ExampleApplication
{
public:
void createScene()
{
  Ogre::Entity* ent =
mSceneMgr->createEntity("MyEntity","tiphak..mesh");
Ogre::SceneNode* node = mSceneMgr->createSceneNode("Node1");
mSceneMgr->getRootSceneNode()->addChild(node);
node->attachObject(ent);
node->setPosition(10,0,0);
Ogre::Entity* ent2 = mSceneMgr->createEntity("MyEntity2","Sinbad.mesh");
Ogre::SceneNode* node2 = mSceneMgr->createSceneNode("Node2");
node->addChild(node2);
node2->setPosition(0,10,20);
node2->attachObject(ent2);
}
};
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
{
Example1 app;
app.go();
return 0;
}
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);
            ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
            ssSetOptions(S,
                   SS_OPTION_WORKS_WITH_CODE_REUSE |
                   SS_OPTION_EXCEPTION_FREE_CODE |
                   SS_OPTION_USE_TLC_WITH_ACCELERATOR);
  }
    static void mdlInitializeSampleTimes(SimStruct *S)
  {
      ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
      ssSetOffsetTime(S, 0, 0.0);
      ssSetModelReferenceSampleTimeDefaultInheritance(S); 
  }
    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++) {
                    *y++ = 2.0 *(*uPtrs[i]); 
      }
  }
    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
I do mex with it (with include files and liblaries):
mex -IC:\OgreSDK_vc9_v1-7-2\include -IC:\OgreSDK_vc9_v1-7-2\boost_1_44 -LC:\OgreSDK_vc9_v1-7-2\boost_1_44\lib -LC:\OgreSDK_vc9_v1-7-2\lib -LC:\OgreSDK_vc9_v1-7-2\lib\debug natash.cpp -lOgreMain_d -lOIS_d -llibboost_thread-vc90-mt-1_44 -llibboost_date_time-vc90-mt-1_44
It is compiled successfully. When I use s-function, it increases a signal in 2 times, but doesn't deduce any graphic window. I don't understand why.
I thank for any answer.
0 Comments
Accepted Answer
  Mark
    
 on 21 Apr 2011
        For testing, I would recommend Kaustubha's approach for creating an instance of "Example1" and then trying to use it. For the bigger project, I would recommend that you:
More Answers (4)
  Natalia
 on 28 Apr 2011
        1 Comment
  Kaustubha Govind
    
      
 on 28 Apr 2011
				Find the exact line number that causes the shutdown and investigate possible issues with that.
  Kaustubha Govind
    
      
 on 13 Apr 2011
        WinMain is not executed by the S-function - in order for the application to run, you must execute the code:
Example1 app;
app.go();
from one of the S-function methods (I recommend mdlOutputs).
16 Comments
  Kaustubha Govind
    
      
 on 19 Apr 2011
				Please see this solution for use of multithreading in MEX-functions: http://www.mathworks.com/support/solutions/en/data/1-V3B5T/?solution=1-V3B5T
  Natalia
 on 22 Apr 2011
        3 Comments
  Kaustubha Govind
    
      
 on 25 Apr 2011
				In mdlInitializeSizes, you need to add:
ssSetNumPWork(S, 1);
Also, here are some tips to debug S-functions: http://www.mathworks.com/help/toolbox/simulink/sfg/bq2rjeu-1.html
See Also
Categories
				Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


