Generate Code for Elapsed Time Counter
This example shows a model from which you can generate code for an elapsed time counter. The code generator allocates and maintains an elapsed time counter for a triggered subsystem or unconditionally executed subsystem within a triggered subsystem that contains a block that depends on elapsed time. An example of a block that requires elapsed time is the Discrete-Time Integrator block.
Open Example Model
Open the example model ElapsedTimeCounter
. The model includes a triggered subsystem, Amplifier.
The model is configured to use a data code interface.
open_system("ElapsedTimeCounter.slx");
Open the Amplifier
subsystem. The subsystem includes a Discrete-Time Integrator block, which uses elapsed time.
Generate and Inspect Code
Generate code for the model.
Declarations for Fixed-Step Size Timer
In header file ElapsedTimeCounter.h
, within the real-time model data structure tag_RTM_ElapsedTimeCounter_T
, the code generator allocates storage for timer information in a substructure. Within that substructure, the code generator uses 64 bits (fields clockTick0
and ClockTickH0
) to store and update the time counter for the model fixed-step size (fundamental sample time).
struct tag_RTM_ElapsedTimeCounter_T { const char_T *errorStatus; RTWLogInfo *rtwLogInfo; struct { time_T taskTime0; uint32_T clockTick0; uint32_T clockTickH0; time_T stepSize0; time_T tFinal; boolean_T stopRequestedFlag; } Timing; };
Declarations for Elapsed and Previous Time Data
Header file ElapsedTimeCounter.h
also declares variables for elapsed and previous time data. For subsystem Amplifier
, the code generator allocates storage for the elapsed time and previous time values (fields Amplifier_ELAPS_T[2]
and AMPLIFIER_PREV_T[2]
) in the model block state structure DW_ElapsedTimeCounter_T
.
typedef struct { real_T DiscreteTimeIntegrator_DSTATE;/* '<S1>/Discrete-Time Integrator' */ int32_T clockTickCounter; /* '<Root>/Pulse Generator' */ uint32_T Amplifier_ELAPS_T[2]; /* '<Root>/Amplifier' */ uint32_T Amplifier_PREV_T[2]; /* '<Root>/Amplifier' */ } DW_ElapsedTimeCounter_T;
Elapsed-Time Computation
In file ElapasedTimeCounter.c
, entry-point function ElapasedTimeCounter_step
contains the code that computes the elapsed time. The function maintains the elapsed time as a state of the triggered subsystem.
void ElapsedTimeCounter_step(void) { . . . zcEvent = rt_ZCFcn(RISING_ZERO_CROSSING, &ElapsedTimeCounter_PrevZCX.Amplifier_Trig_ZCE, ((real_T)rtb_PulseGenerator)); if (zcEvent != NO_ZCEVENT) { Amplifier_ELAPS_T_tmp_tmp = ElapsedTimeCounter_M->Timing.clockTick0; ElapsedTimeCounter_DW.Amplifier_ELAPS_T[0] = Amplifier_ELAPS_T_tmp_tmp - ElapsedTimeCounter_DW.Amplifier_PREV_T[0]; elapseT_H_tmp = ElapsedTimeCounter_M->Timing.clockTickH0; elapseT_H = elapseT_H_tmp - ElapsedTimeCounter_DW.Amplifier_PREV_T[1]; if (ElapsedTimeCounter_DW.Amplifier_PREV_T[0] > Amplifier_ELAPS_T_tmp_tmp) { elapseT_H--; } ElapsedTimeCounter_DW.Amplifier_ELAPS_T[1] = elapseT_H; ElapsedTimeCounter_DW.Amplifier_PREV_T[0] = Amplifier_ELAPS_T_tmp_tmp; ElapsedTimeCounter_DW.Amplifier_PREV_T[1] = elapseT_H_tmp; . . .
Discrete-Time Integrator Use of Elapsed Time
After entry-point function ElapasedTimeCounter_step
computes the elapsed time, the function uses the computed value to perform the output and update computations for the Discrete-Time Integrator block.
. . . OUTPUT = ElapsedTimeCounter_DW.DiscreteTimeIntegrator_DSTATE; ElapsedTimeCounter_DW.DiscreteTimeIntegrator_DSTATE += 0.3 * (real_T) ElapsedTimeCounter_DW.Amplifier_ELAPS_T[0] * 1.5; } . . .