Access Timers Programmatically - MATLAB & Simulink (original) (raw)
Main Content
About Timer Programming Interfaces for S-Functions
Programming interfaces are available for S-functions that you can use to take advantage of efficiencies offered by absolute and elapsed timers.SimStruct
macros support simulation. TLC library functions support inlined code generation.
- To generate code for and use timers, your S-functions must register the need to use an absolute or elapsed timer by calling
ssSetNeedAbsoluteTime
orssSetNeedElapseTime
inmdlInitializeSampleTime
. - Existing S-functions that read absolute time but do not register the need to use these macros continue to operate as expected, but generate less efficient code.
Access Timers from S-Functions During Simulation
The sample time category of SimStruct
macros provide access to absolute and elapsed timers from S-functions during simulation. For these macros, the SimStruct *S
argument is a pointer to thesimstruct
of the calling S-function.
Goal | SimStruct Macro |
---|---|
Absolute Time | |
Register that an S-function requires absolute time data, and allocate an absolute time counter for the rate at which the S-function executes (if such a counter has not already been allocated). | ssSetNeedAbsoluteTime |
Check whether an S-function has registered that it requires absolute time. | ssGetNeedAbsoluteTime |
Get absolute time for a specified task. | ssGetTaskTime |
Elapsed Time | |
Register that an S-function requires elapsed time data, and allocate an elapsed time counter for the triggered subsystem in which the S-function executes (if such a counter has not already been allocated). | ssSetNeedElapseTime |
Check whether an S-function has registered that it requires elapsed time. | ssGetNeedElapseTime |
Get the value of the elapsed time counter associated with an S-function. | ssGetElapseTime |
Get the data type of the elapsed time counter associated with an S-function. | ssGetElapseTimeCounterDtype |
Get the resolution (sample time) of the elapsed time counter associated with an S-function. | ssGetElapseTimeResolution |
Get the integer value of the elapsed time counter associated with an S-Function. | ssGetElapseTimeCounter |
Use ssGetElapseTimeCounter
for blocks that require elapsed time values for fixed-point computations. If the counter size is 64 bits, the value is returned as an array of two 32-bit words, with the low-order word stored at the lower address.
To determine how to access the returned counter value, obtain the data type of the counter by calling ssGetElapseTimeCounterDtype
, as in this code:
int *y_dtype; ssGetElapseTimeCounterDtype(S, y_dtype);
switch(*y_dtype) { case SS_DOUBLE_UINT32: { uint32_T dataPtr[2]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT32: { uint32_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT16: { uint16_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_UINT8: { uint8_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; case SS_DOUBLE: { real_T dataPtr[1]; ssGetElapseTimeCounter(S, dataPtr); } break; default: ssSetErrorStatus(S, "Invalid data type for elaspe time counter"); break; }
If you want to use the actual elapsed time, access the elapsed time directly by calling the ssGetElapseTime
function. You do not need to get the counter value and then calculate the elapsed time.
double *y_elapseTime; . . . ssGetElapseTime(S, elapseTime)
Access Timers from Code Generated for Inlined S-Functions
The sample time category of TLC functions provide access to absolute and elapsed timers in code generated for inlined S-functions.