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

 Accepted Answer

Kaustubha Govind
Kaustubha Govind on 30 Jul 2011
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 Comments

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.
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.
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.
Set the global pointer just before calling glutIdleFunc in mdlOutputs.
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();
}
You need to declare uPtrs as global.
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();
}

Sign in to comment.

More Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Products

Tags

Community Treasure Hunt

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

Start Hunting!