3d graphics with s-function
Mostrar comentarios más antiguos
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
Más respuestas (4)
Natalia
el 28 de Abr. de 2011
1 comentario
Kaustubha Govind
el 28 de Abr. de 2011
Find the exact line number that causes the shutdown and investigate possible issues with that.
Kaustubha Govind
el 13 de Abr. de 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 comentarios
Natalia
el 14 de Abr. de 2011
Natalia
el 14 de Abr. de 2011
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
el 14 de Abr. de 2011
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
el 15 de Abr. de 2011
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
el 16 de Abr. de 2011
Natalia
el 16 de Abr. de 2011
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
el 18 de Abr. de 2011
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
el 18 de Abr. de 2011
Natalia
el 18 de Abr. de 2011
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
el 22 de Abr. de 2011
Natalia
el 22 de Abr. de 2011
3 comentarios
Natalia
el 25 de Abr. de 2011
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
el 28 de Abr. de 2011
Artem V
el 28 de Jun. de 2018
0 votos
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!