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.
Best Answer