Main Content

Access Timers Programmatically

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 or ssSetNeedElapseTime in mdlInitializeSampleTime.

  • 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 the simstruct of the calling S-function.

GoalSimStruct 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.

GoalTLC Function
Absolute Time
Get the absolute time for the task in which the S-function code executes.LibGetTaskTimeFromTID(block)
Get the absolute time of a task.LibGetTaskTime(tid)
Get the integer task time, which is the current clock tick of the task timer.LibGetClockTick(tid)
Get the resolution of the integer task time.LibGetClockTickStepSize(tid)
Get the data type of the clock tick.LibGetClockTickDataTypeId(tid)
Elapsed Time
Get the time elapsed since the last time the subsystem that contains the calling block started to execute.LibGetElapseTime(system)
Get the integer value of the elapsed time counter.LibGetElapseTimeCounter(system)
Get the data type of the elapsed time counter.LibGetElapseTimeCounterDTypeId(system)
Get the resolution of the elapsed time counter.LibGetElapseTimeResolution(system)

Related Topics