Why does the Simulink Accelerator 6.0 (R14) produce incorrect results when using a C MEX S-function with variable step timing in a Simulink model?

1 view (last 30 days)
I am using the following callback in my S-function
mdlGetTimeOfNextVarHit
and I have set the Sample Time of my S-function as follows
static void mdlInitializeSampleTimes(SimStruct *S)
{
if(ssGetSimMode(S) == SS_SIMMODE_RTWGEN)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
else
{
ssSetSampleTime(S, 0, VARIABLE_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
}
The output of my S-function is always zero when I use the Simulink Accelerator.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This is expected behavior when the sample time is set to inherited: S-functions will only get a Variable Sample time if the sample time is set explicitly and never if the sample time is set to be inherited. When the S-function's sample time is inherited, the mdlGetTimeOfNextVarHit is not called and therefore, the S-function produces unexpected results.
Here are a few suggestions for writing variable step S-functions:
1. Set the sample time of the S-function to variable, i.e.,
ssSetSampleTime(S, 0, VARIABLE_SAMPLE_TIME);
2. Instead of using the ssGetSimMode check, check for a variable step solver using the ssIsVariableStepSolver macro, e.g.,
static void mdlInitializeSampleTimes(SimStruct *S)
{
if(ssIsVariableStepSolver(S))
{
ssSetSampleTime(S, 0, VARIABLE_SAMPLE_TIME);
}
else
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
}
ssSetOffsetTime(S, 0, 0.0);
}
The code generation that works with variable step solvers (Simulink Accelerator and RSim Target) will work with variable sample time S-functions if the S-function sets the sample time explicitly. Therefore, be sure that the sample time of your S-function is not based solely on the results of the ssGetSimMode macro, which does not query the solver being used for simulation.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!