3d graphics with s-function

4 Ansichten (letzte 30 Tage)
Natalia
Natalia am 13 Apr. 2011
Beantwortet: Artem V am 28 Jun. 2018
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.

Akzeptierte Antwort

MarkB
MarkB am 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:
  1. Make a "PWork" vector, which is a list of pointers in the S-function that persist in between calls to "mdl*" functions.
  2. 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.
  3. In "mdlOutputs", access the "PWork" vector and call ".go()" "create_scene", or whatever, (I don't know much about Ogre...)
  4. In "mdlTerminate", call get the instance of "Example1" out of the "PWork" vector and use "delete" to clean up any memory that was allocated.
  1 Kommentar
Natalia
Natalia am 22 Apr. 2011
thank you very much, I will try to do that.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Natalia
Natalia am 28 Apr. 2011
I add ssSetNumPWork(S, 1);
I have a new errors:
Error in 'timo/S-Function' while executing C MEX S-function 'natash', (mdlOutputs), at time 0.0.
Unexpected Standard exception from MEX file.
What() is:OGRE EXCEPTION(6:FileNotFoundException): 'resources.cfg' file not found! in ConfigFile::load at ..\..\..\..\OgreMain\src\OgreConfigFile.cpp (line 83)
When I add resourses.cfg, I see the log-file of Ogre
11:59:52: Creating resource group General
11:59:52: Creating resource group Internal
11:59:52: Creating resource group Autodetect
11:59:52: SceneManagerFactory for type 'DefaultSceneManager' registered.
11:59:52: Registering ResourceManager for type Material
11:59:52: Registering ResourceManager for type Mesh
11:59:52: Registering ResourceManager for type Skeleton
11:59:52: MovableObjectFactory for type 'ParticleSystem' registered.
11:59:52: OverlayElementFactory for type Panel registered.
11:59:52: OverlayElementFactory for type BorderPanel registered.
11:59:52: OverlayElementFactory for type TextArea registered.
11:59:52: Registering ResourceManager for type Font
11:59:52: ArchiveFactory for archive type FileSystem registered.
11:59:52: ArchiveFactory for archive type Zip registered.
11:59:52: DDS codec registering
11:59:52: FreeImage version: 3.13.1
11:59:52: This program uses FreeImage, a free, open source image library supporting all common bitmap formats. See http://freeimage.sourceforge.net for details
11:59:52: Supported formats: bmp,ico,jpg,jif,jpeg,jpe,jng,koa,iff,lbm,mng,pbm,pbm,pcd,pcx,pgm,pgm,png,ppm,ppm,ras,tga,targa,tif,tiff,wap,wbmp,wbm,psd,cut,xbm,xpm,gif,hdr,g3,sgi,exr,j2k,j2c,jp2,pfm,pct,pict,pic,bay,bmq,cr2,crw,cs1,dc2,dcr,dng,erf,fff,hdr,k25,kdc,mdc,mos,mrw,nef,orf,pef,pxn,raf,raw,rdc,sr2,srf,arw,3fr,cine,ia,kc2,mef,nrw,qtk,rw2,sti,drf,dsc,ptx,cap,iiq,rwz
11:59:52: Registering ResourceManager for type HighLevelGpuProgram
11:59:52: Registering ResourceManager for type Compositor
11:59:52: MovableObjectFactory for type 'Entity' registered.
11:59:52: MovableObjectFactory for type 'Light' registered.
11:59:52: MovableObjectFactory for type 'BillboardSet' registered.
11:59:52: MovableObjectFactory for type 'ManualObject' registered.
11:59:52: MovableObjectFactory for type 'BillboardChain' registered.
11:59:52: MovableObjectFactory for type 'RibbonTrail' registered.
11:59:52: Loading library .\RenderSystem_Direct3D9
11:59:52: Installing plugin: D3D9 RenderSystem
11:59:52: D3D9 : Direct3D9 Rendering Subsystem created.
11:59:52: D3D9: Driver Detection Starts
11:59:52: D3D9: Driver Detection Ends
11:59:52: Plugin successfully installed
11:59:52: Loading library .\RenderSystem_GL
11:59:52: Installing plugin: GL RenderSystem
11:59:52: OpenGL Rendering Subsystem created.
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_ParticleFX
11:59:52: Installing plugin: ParticleFX
11:59:52: Particle Emitter Type 'Point' registered
11:59:52: Particle Emitter Type 'Box' registered
11:59:52: Particle Emitter Type 'Ellipsoid' registered
11:59:52: Particle Emitter Type 'Cylinder' registered
11:59:52: Particle Emitter Type 'Ring' registered
11:59:52: Particle Emitter Type 'HollowEllipsoid' registered
11:59:52: Particle Affector Type 'LinearForce' registered
11:59:52: Particle Affector Type 'ColourFader' registered
11:59:52: Particle Affector Type 'ColourFader2' registered
11:59:52: Particle Affector Type 'ColourImage' registered
11:59:52: Particle Affector Type 'ColourInterpolator' registered
11:59:52: Particle Affector Type 'Scaler' registered
11:59:52: Particle Affector Type 'Rotator' registered
11:59:52: Particle Affector Type 'DirectionRandomiser' registered
11:59:52: Particle Affector Type 'DeflectorPlane' registered
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_BSPSceneManager
11:59:52: Installing plugin: BSP Scene Manager
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_CgProgramManager
11:59:52: Installing plugin: Cg Program Manager
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_PCZSceneManager
11:59:52: Installing plugin: Portal Connected Zone Scene Manager
11:59:52: PCZone Factory Type 'ZoneType_Default' registered
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_OctreeZone
11:59:52: Installing plugin: Octree Zone Factory
11:59:52: Plugin successfully installed
11:59:52: Loading library .\Plugin_OctreeSceneManager
11:59:52: Installing plugin: Octree & Terrain Scene Manager
11:59:52: Plugin successfully installed
11:59:52: *-*-* OGRE Initialising
11:59:52: *-*-* Version 1.7.2 (Cthugha)
But Matlab is shotdown...
I try to debug with Visual Studio 9... When opens ExampleApplication.h, matlab it is closed...
  1 Kommentar
Kaustubha Govind
Kaustubha Govind am 28 Apr. 2011
Find the exact line number that causes the shutdown and investigate possible issues with that.

Melden Sie sich an, um zu kommentieren.


Kaustubha Govind
Kaustubha Govind am 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 Kommentare
Kaustubha Govind
Kaustubha Govind am 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
Natalia am 22 Apr. 2011
Kaustubha, thank you very much for your help. I will try to do that.

Melden Sie sich an, um zu kommentieren.


Natalia
Natalia am 22 Apr. 2011
I have changed a file
#include "Ogre\ExampleApplication.h";
class Example1 : public ExampleApplication
{
public:
void createScene()
{
Ogre::Entity* ent =
mSceneMgr->createEntity("MyEntity","Cube.mesh");
Ogre::SceneNode* node = mSceneMgr->createSceneNode("Node1");
mSceneMgr->getRootSceneNode()->addChild(node);
node->attachObject(ent);
node->setPosition(10,0,0);
}
};
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME natash
#include "simstruc.h"
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);
}
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
ssSetModelReferenceSampleTimeDefaultInheritance(S);
}
#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S)
{
ssGetPWork(S)[0] = (void *) new Example1;
}
#endif
static void mdlOutputs(SimStruct *S, int_T tid)
{
Example1 *c = (Example1 *) ssGetPWork(S)[0];
real_T *y = ssGetOutputPortRealSignal(S,0); // the pointers vector and use
y[0]=1;
c->go();
}
static void mdlTerminate(SimStruct *S)
{
Example1 *c = (Example1 *) ssGetPWork(S)[0];
delete c;
}
#ifdef MATLAB_MEX_FILE
#include "simulink.c"
#else
#include "cg_sfun.h"
#endifBut
I have the same error "Matlab.....must be closed". I have decided to try to use OpenGL for visualization, have followed an example from the textbook (for this purpose it is not required any additional installation). But I have the same error...(!!!!!!)
Source file of the exemple http://depositfiles.com/files/6wju89zqr I use mex -g test_vis.cpp user32.lib gdi32.lib glu32.lib opengl32.lib
This example doesn't work for all or only at me?
  3 Kommentare
Kaustubha Govind
Kaustubha Govind am 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
Natalia
Natalia am 28 Apr. 2011
if I add ssSetNumPWork(S, 1);
I have a new error:
Error in 'timo/S-Function' while executing C MEX S-function 'natash', (mdlOutputs), at time 0.0.

Melden Sie sich an, um zu kommentieren.


Artem V
Artem V am 28 Jun. 2018

Kategorien

Mehr zu Application Deployment finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by