s-function and input data

Hello! I have a little s-function. I want to use input data in void spinDisplay(void). But this function must be void. How can I do that? Thank for any answer. Natalia
#define S_FUNCTION_NAME timestwo
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include <GL/glut.h>
#include <stdlib.h>
static GLfloat spin = 0.0;
static GLfloat k = 0.0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
void spinDisplay(void)
{
spin = k;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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);
/* specify the sim state compliance to be same as a built-in block */
ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
/* Take care when specifying exception free code - see sfuntmpl_doc.c */
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)
{
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
}
}
#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 2011년 7월 30일

1 개 추천

To access the S-function input data, you need to have access to the Simstruct pointer (the S argument in all S-function callback methods), which needs to be passed in.
At best, you can have a global variable to cache the pointer to the input signal that you could update right before calling spinDisplay(). Note that global data is shared if you have multiple instances of the S-function in your model, so you must only use it to cache the pointer just before calling your function.
I'm not entirely confident that this is the best solution, but there's an idea for you to try.

댓글 수: 7

Natalia
Natalia 2011년 7월 31일
Thank you for your answer, Kaustubha. As I have understood, I have to make something like it...
int_T i = 0;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
void spinDisplay(void)
{
spin = *uPtrs[i];
i=i+1;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
But there is an error
error C2065: 'S' : undeclared identifier
How to declare S?
Natalia.
Kaustubha Govind
Kaustubha Govind 2011년 7월 31일
Where is spinDisplay called from? I assumed that it was called from one of the functions that have access to 'S'. If yes, you can then assign the input pointer to the global variable in that function right before calling spinDisplay.
Natalia
Natalia 2011년 7월 31일
spinDisplay is called from mdlOutputs and mdlOutputs have access to S.
But spinDisplay is parameter of other function glutIdleFunc. Therefore spinDisplay should be void.
static void mdlOutputs(SimStruct *S, int_T tid)
{ ...
init ();
...
glutIdleFunc(spinDisplay);
...
}
}
Therefore I can't understand how to take the input data from spinDisplay.
Is it possible to make it?
Thank you.
Kaustubha Govind
Kaustubha Govind 2011년 8월 1일
Set the global pointer just before calling glutIdleFunc in mdlOutputs.
Natalia
Natalia 2011년 8월 1일
Hm... I do that... But I have an error: error C2065: 'uPtrs' : undeclared identifier
...
void spinDisplay()
{
spin = spin + 10*(*uPtrs[i]);
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
...
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
int_T width = ssGetOutputPortWidth(S,0);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
}
Kaustubha Govind
Kaustubha Govind 2011년 8월 2일
You need to declare uPtrs as global.
Natalia
Natalia 2011년 8월 4일
I think that I incorrectly declare uPtrs. I don't understand how to do that correctly.
I declare uPtrs before calling all functions, but Matlab is shutdown...
...
InputRealPtrsType uPtrs = 0;
void spinDisplay()
{
spin = spin + 10*(*uPtrs[i]);
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortRealSignal(S,0);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
int_T width = ssGetOutputPortWidth(S,0);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
}

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by