Writing S-Functions | ![]() ![]() |
Specify the sample rates at which this S-function operates.
Syntax
void mdInitializeSampleTimes(SimStruct *S)
Arguments
S
Simstruct representing an S-function block.
Description
This method should specify the sample time and offset time for each sample rate at which this S-function operates via the following paired macros
ssSetSampleTime(S, sampleTimeIndex, sample_time) ssSetOffsetTime(S, offsetTimeIndex, offset_time)
where sampleTimeIndex
runs from 0
to one less than the number of sample times specified in mdlInitializeSizes via ssSetNumSampleTimes.
If the S-function operates at one or more sample rates, this method can specify any of the following sample time and offset values for a given sample time:
[CONTINUOUS_SAMPLE_TIME, 0.0
][CONTINUOUS_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET]
[discrete_sample_period, offset]
[VARIABLE_SAMPLE_TIME, 0.0]
The upper case values are macros defined in simstruc.h
.
If the S-function operates at one rate, this method can alternatively set the sample time to one of the following sample/offset time pairs.
[INHERITED_SAMPLE_TIME, 0.0]
INHERITED_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET]
If the number of sample times is 0, Simulink assumes that the S-function inherits its sample time from the block to which it is connected, i.e., that the sample time is
[INHERITED_SAMPLE_TIME, 0.0]
This method can therefore return without doing anything.
Use the following guidelines when specifying sample times.
[CONTINUOUS_SAMPLE_TIME, 0.0]
[CONTINUOUS_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET]
[discrete_sample_period, offset
]
where
discrete_sample_period > 0.0
and
0.0 <= offset < discrete_sample_period
[VARIABLE_SAMPLE_TIME, 0.0]
Simulink invokes mdlGetTimeOfNextVarHit function to get the time of the next sample hit for the variable step discrete task.
Note that VARIABLE_SAMPLE_TIME
requires a variable step solver.
[INHERITED_SAMPLE_TIME, 0.0]
SS_DISALLOW_CONSTANT_SAMPLE_TIME
simulation option in mdlInitializeSizesmdlSetWorkWidth
s:After propagating sample times throughout the block diagram, Simulink assigns the sample time
[INHERITED_SAMPLE_TIME, INHERITED_SAMPLE_TIME]
to discrete blocks residing in triggered subsystems.
If this function has no intrinsic sample time, it should set its sample time to inherited according to the following guidelines:
[INHERITED_SAMPLE_TIME, 0.0]
A function that changes as its input changes, but doesn't change during minor integration steps (i.e., held during minor steps) should set its sample time to
[INHERITED_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET]
The S-function should use the ssIsSampleHit or ssIsContinuousTask macros to check for a sample hit during execution (in mdlOutputs or mdlUpdate). For example, if the block's first sample time is continuous, the function can use the following code-fragment to check for a sample hit.
if (ssIsContinuousTask(S,tid)) { }
If the function wanted to determine if the third (discrete) task has a hit, it could use the following code-fragment.
if (ssIsSampleHit(S,2,tid) { }
Languages
See Also
mdlSetInputPortSampleTime, mdlSetOutputPortSampleTime
![]() | mdlInitializeConditions | mdlInitializeSizes | ![]() |