3d graphics with s-function

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.

 Respuesta aceptada

Mark
Mark el 21 de Abr. de 2011

0 votos

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 comentario

Natalia
Natalia el 22 de Abr. de 2011
thank you very much, I will try to do that.

Iniciar sesión para comentar.

Más respuestas (4)

Natalia
Natalia el 28 de Abr. de 2011

1 voto

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 comentario

Kaustubha Govind
Kaustubha Govind el 28 de Abr. de 2011
Find the exact line number that causes the shutdown and investigate possible issues with that.

Iniciar sesión para comentar.

Kaustubha Govind
Kaustubha Govind el 13 de Abr. de 2011

0 votos

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 comentarios

Natalia
Natalia el 14 de Abr. de 2011
I moved
Example1 app;
app.go();
to the mdlOutputs.
It is compiled successfully.
When I use s-function (in mdl), there is a following error:
Invalid MEX-file 'C:\Program Files\MATLAB\R2010b\work\experiment\natash.mexw32'...
Natalia
Natalia el 14 de Abr. de 2011
Cod of the s-function:
#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);
}
};
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]);
}
Example1 app;
app.go();
}
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
Kaustubha Govind
Kaustubha Govind el 14 de Abr. de 2011
It seems like your application is looking for certain DLLs that are not on the system path. You can use Dependency Walker to determine which DLLs are missing and add their location to the system PATH environment variable. See http://www.mathworks.com/support/solutions/en/data/1-2RQL4L/index.html for information about Dependency Walker. (IMPORTANT: Since S-functions cannot be run from the MATLAB prompt like regular MEX-files, follow the steps under "Alternatively: " and run the Simulink model in Step#2).
Natalia
Natalia el 14 de Abr. de 2011
I downloaded and installed Dependency Walker.
Then I follow the steps under "Alternatively:"
Alternatively:
1. Click File > Open and select $MATLAB\bin\win32\matlab.exe to launch MATLAB
2. In MATLAB, I open and run my mdl file.
3. Switch to Dependency Walker and, if necessary, click Window > MATLAB.EXE to select MATLAB history.
4. Click File > Save As to save the file as a .DWI-file.
My dwi file:
http://depositfiles.com/files/rjl75oshg
I don't see information, which I need.
Kaustubha Govind
Kaustubha Govind el 14 de Abr. de 2011
Did you run the s-function by executing the Simulink model? Alternatively, you can look for all DLLs in the OgreSDK package, and ensure that their locations are on the system path.
Natalia
Natalia el 15 de Abr. de 2011
Yes, I have started s-function, carrying out model Sinulink. My package OgreSDK already is on a system way (Variable environments). Or I not so have understood something?
Kaustubha Govind
Kaustubha Govind el 15 de Abr. de 2011
Specifically, do you have the locations of all DLLs on the PATH environment variable? Also, you mentioned earlier that you compiled a similar application in Visual Studio - are you able to run that executable from the system command prompt (or by double-clicking on the executable)? If yes, I wonder if the PATH variable read by MATLAB is different from the actual value.
Try
>> !echo %PATH%
in MATLAB, and then from system command window:
> echo %PATH%
Do they both return the same paths?
Natalia
Natalia el 16 de Abr. de 2011
I have an enviroment variable: %OGRE_HOME %\bin\debug.
echo %PATH% in Matlab:
C:\Program Files\MATLAB\R2010b\bin\win32;
C:\MATLAB701\bin\win32;
C:\Program Files\PC Connectivity Solution\;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
c:\Program Files\Microsoft SQL Server\90\Tools\binn\;
C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;
C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;
C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;
C:\Program Files\Common Files\Autodesk Shared\;
C:\Program Files\backburner 2\;
C:\Program Files\QuickTime\QTSystem\;
C:\MATLAB701\bin\win32;
C:\Program Files\MATLAB\R2010b\runtime\win32;
C:\Program Files\MATLAB\R2010b\bin;
C:\Program Files\Autodesk\Backburner\
in Windows:
http://i036.radikal.ru/1104/b8/d54ff6257087.jpg
They return the same paths...
When I build exe in Visual Studio, Visual Studio creat the exe in OgreSDk\bin\debug. There are many dll files in that folder. Without them it doesn't work. And it does not work If I transfer all it to other folder...
Natalia
Natalia el 16 de Abr. de 2011
So this folder looks:
http://s016.radikal.ru/i336/1104/61/c91b13ac74b7.jpg
Probably, I should establish Ogre in folder Matlab? Ogre is a self-unpacked archive with libraries...
Kaustubha Govind
Kaustubha Govind el 17 de Abr. de 2011
You could just add the Ogre folder to the system path, and MATLAB will look for the DLLs in that folder.
Natalia
Natalia el 18 de Abr. de 2011
I added Ogre in the system path:
>> !echo %PATH%
C:\Program Files\MATLAB\R2010b\bin\win32;C:\MATLAB701\bin\win32;C:\Program Files\PC Connectivity Solution\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\Program Files\Common Files\Autodesk Shared\;C:\Program Files\backburner 2\;C:\Program Files\QuickTime\QTSystem\;C:\MATLAB701\bin\win32;C:\Program Files\MATLAB\R2010b\runtime\win32;C:\Program Files\MATLAB\R2010b\bin;C:\Program Files\Autodesk\Backburner\;C:\OgreSDK_vc9_v1-7-2;C:\OgreSDK_vc9_v1-7-2\bin\debug;C:\OgreSDK_vc9_v1-7-2\bin\release;C:\OgreSDK_vc9_v1-7-2\boost_1_44\lib;C:\OgreSDK_vc9_v1-7-2\lib\debug;C:\OgreSDK_vc9_v1-7-2\lib\debug\opt;
Now, when I try to run mdl, I have an error (Matlab has encountered an internal problem and need to close)...
And if I open mex-file at the Dependency Walker, I have the folowing dwi:
http://depositfiles.com/files/ij1yzpvug
there are not ogre dll's...
Kaustubha Govind
Kaustubha Govind el 18 de Abr. de 2011
You should now debug the S-function and see what call is causing the crash: http://www.mathworks.com/help/toolbox/simulink/sfg/bq2rjeu-1.html
Natalia
Natalia el 18 de Abr. de 2011
Hmm... I such yet didn't meet... During debugging the file iosfwd opens then there is a closing matlab...
Natalia
Natalia el 18 de Abr. de 2011
Maybe, this occurs when the threading library used by the mex file is conflicting with the threading library used by MATLAB...
Kaustubha Govind
Kaustubha Govind el 19 de Abr. de 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 el 22 de Abr. de 2011
Kaustubha, thank you very much for your help. I will try to do that.

Iniciar sesión para comentar.

Natalia
Natalia el 22 de Abr. de 2011

0 votos

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 comentarios

Natalia
Natalia el 25 de Abr. de 2011
I thank for any answer.
Kaustubha Govind
Kaustubha Govind el 25 de Abr. de 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 el 28 de Abr. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 13 de Abr. de 2011

Respondida:

el 28 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by