The Simulink documentation for ssSetTNext says that:
"A discrete S-function with a variable sample time should use this macro in mdlGetTimeOfNextVarHit to specify the time of the next sample hit".
Now, I'd like to compute the next sample hit for the S-function after the execution of mdlOutputs. However, according to Simulink Engine Interaction with C S-Functions, mdlGetTimeOfNextVarHit is executed before mdlOutputs (see the picture describing the Simulation Loop).
On the other hand, the documentation for mdlUpdate (unfortunately, I'm not allowed to post the link) says that:
"The method can also perform any other tasks that the S-function needs to perform at each major time step".
Since mdlUpdate is executed after mdlOutputs, I was wondering if it's safe to ignore the method mdlGetTimeOfNextVarHit and put all the logics defining the next sample hit (and also use ssSetTNext) inside mdlUpdate.
Thank you in advance.
--Matteo
In short, yes.
It is possible to set the variable-rate task's next hit time outside of the
mdlGetTimeOfNextVarHit()method. The next sample instant simply must be set before the end of the present simulation loop: it may therefore be set either in themdlOutputs()ormdlUpdate()methods.Remember that C-MEX S-Functions configured to use a variable timestep task still require:
MDL_GET_TIME_OF_NEXT_VAR_HITbe defined, andmdlGetTimeOfNextVarHit().This means that if you wish to set your new sample instant elsewhere, you still need a (potentially empty) definition for
mdlGetTimeOfNextVarHit().Gotcha's:
If
MDL_GET_TIME_OF_NEXT_VAR_HITis not defined for a C-MEX S-Function configured to use a variable timestep task and there is no definition formdlGetTimeOfNextVarHit(); compilation will succeed, but upon invocation the following error diagnostic will be reported, and the simulation will terminate:Lastly, if
MDL_GET_TIME_OF_NEXT_VAR_HITis not defined but there is a definition ofmdlGetTimeOfNextVarHit(), compilation will fail.