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.
采纳的回答
更多回答(4 个)
Natalia
2011-4-28
1 个评论
Kaustubha Govind
2011-4-28
Find the exact line number that causes the shutdown and investigate possible issues with that.
Kaustubha Govind
2011-4-13
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 个评论
Natalia
2011-4-14
Natalia
2011-4-14
Kaustubha Govind
2011-4-14
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
2011-4-14
Kaustubha Govind
2011-4-14
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
2011-4-15
Kaustubha Govind
2011-4-15
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
2011-4-16
Natalia
2011-4-16
Kaustubha Govind
2011-4-17
You could just add the Ogre folder to the system path, and MATLAB will look for the DLLs in that folder.
Natalia
2011-4-18
Kaustubha Govind
2011-4-18
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
2011-4-18
Natalia
2011-4-18
Kaustubha Govind
2011-4-19
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
2011-4-22
Natalia
2011-4-22
3 个评论
Natalia
2011-4-25
Kaustubha Govind
2011-4-25
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
2011-4-28
Artem V
2018-6-28
0 个投票
类别
在 帮助中心 和 File Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!