Method 1 – Using realtime sampling:
Declare a queue in which sample time will be stored:

Define a task to sample the realtime when an event occurs. The idea is to add a sample to the queue every time a trigger appears. When the queue has 2 samples, their difference is calculated, and the older sample is popped from the queue:

The realtime variables have 64-bit floating point values. Floating point arithmetic can produce unexpected results if it is not used with care. To avoid potential problems with rounding-off, the sample difference in the task is multiplied by 1000 and then converted to an integer value which can later be conveniently used in other checks/calculations. In general, the difference should be multiplied by the ratio time unit/time precision – for example if a 1ns/1fs timescale is used, the difference should be multiplied by 1000000.
Every time there is a change in a signal that you want to check, make sure to call the above task (or call it in appropriate place in you code):

For every two changes of the signal the trigger_time queue will be size two and the checking can be performed then.
Method 2 – Using events:
Declare a variable to store information:

Every time there is a change in a signal you want to check, make sure an event is triggered:

In your code you need to wait for that event and start the distance counter. The next time a signal is triggered, the rst_distance_cnt is triggered again which restarts the counter.
