S-function procedure calling order?
Mostrar comentarios más antiguos
I have a relativley trivial S-function I can' get to work properly.It will be a larger function but for now is a saw-tooth that ramps from -1 to +1 with slope m then reverses and ramps from +1 to -1 with slope -m.
I am using x[0] as the instantaneous saw-tooth and am using x[1] as the slope.
It is not working as I expect.
In my mdlUpdate procedure I have this code:
#define MDL_UPDATE /* Change to #undef to remove function */
#if defined(MDL_UPDATE)
/* Function: mdlUpdate ==================================================
* Abstract:
* This function is called once for every major integration time step.
* Discrete states are typically updated here, but this function is
* useful for performing any tasks that should only take place once
* per integration step.
*/
static void mdlUpdate(SimStruct *S, int_T tid)
{
real_T *dx = ssGetdX(S);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
#define SLOPE (4.0*25.0)
real_T m = SLOPE;
// generate sawtooth ramp x[0], using slope x[1]
if (x[0] > 1.0) {
x[1]= -m;
}
if (x[0] < -1.0) {
x[1]= m;
}
}
#endif /* MDL_UPDATE */
In the mdlDerivatives I have this code:
#define MDL_DERIVATIVES /* Change to #undef to remove function */
#if defined(MDL_DERIVATIVES)
/* Function: mdlDerivatives =============================================
* Abstract:
* In this function, you compute the S-function block's derivatives.
* The derivatives are placed in the derivative vector, ssGetdX(S).
*/
static void mdlDerivatives(SimStruct *S)
{
real_T *dx = ssGetdX(S);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
// generate sawtooth ramp x[0], using slope x[1]
dx[0] = x[1];
dx[1] = 0; // slope fixed
dx[2] = 0;
dx[3] = 0;
dx[4] = 0;
}
#endif /* MDL_DERIVATIVES */
The derivative of x[1] is zero because I want the slope to remain +m or -m until it gets changed in mdlUpdateIs
The mdlOutputs is this code:
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T *y = ssGetOutputPortSignal(S,0);
real_T *x = ssGetContStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
UNUSED_ARG(tid); /* not used in single tasking mode */
real_T Uw = U(0);
real_T Vw = U(1);
real_T Ww = U(2);
// compare vector components to ramp (x[0]) to generate U, V & W
y[0] = x[0]; /* ramp, slope 4/Tfast */
y[1] = x[1]; /* slope, fixed value +m or -m */
y[2] = x[2]; /* U */
y[3] = x[3]; /* V */
y[4] = x[4]; /* W *
}
I'm currently not using y[2]--y[4]
The output y[0] currently ramps from initial value of 0 to 4 at which time the simulation ends. The displayed output y[1] is the slope m=100 and doesn't change. Is the calling sequence of mdlDerivatives and mdlUpdate correct for what I'm trying to do?
1 comentario
Brian Tremaine
el 2 de Dic. de 2020
Respuestas (0)
Categorías
Más información sobre Configure C/C++ S-Function Features en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!