Description of the problem
When working with signals, we would often like to determine the time duration, either time duration between events or the duration of events. This can very easily be done with the Pulsewidth and Stopwatch functions; however, it can, at first, be difficult to determine which command should be used for a given scenario.
This solution will go over the key differences between Pulsewidth and Stopwatch in an actual example, thus making it easier to differentiate between the two options.
Description of the solution
The Pulsewidth and Stopwatch commands can be found in the Math module, Formula setup, Measure math functions:
In the F1 manual, the Pulsewidth function is defined as:
PULSEWIDTH(cond [, rearm ])
Measures time [s] between two cond edges (cond jumps from 0 to 1), rearm edge optional
The Stopwatch function is defined as:
STOPWATCH(cond1, cond2 ),
Measures time [s] between cond1 and cond2 edges (cond jumps from 0 to 1)
For the purpose of this demonstration, we will create two User input signals: Signal1 (red color) and Signal2 (yellow color):
We will then switch their values between 0 and 1 at random, creating a pattern, as seen in the image below:
We can now continue by adding Pulsewidth and Stopwatch channels. To make differentiating easier, we will start off by creating a Stopwatch channel, thus highlighting the function's functionality.
The Stopwatch functionality
The Stopwatch will calculate the time between two conditions, Cond1 and Cond2. For a condition to be true, its digital value needs to change from false (0) to true (1).
We define the function
stopwatch('Signal1'=1,'Signal2'=1)
So, the stopwatch will start measuring time the moment the first condition is true (signal1 = 1) and will stop measuring time the moment the second condition is true (Signal2 = 1). The formula will then reset- we will begin to look for the moment the first condition turns from False (0) to True (1). After the first condition turns to 1, we will start looking for the sample, where the second condition turns from 0 to 1:
With the word ''moment'' we mean sample.
If we run the file, we get to see the result of this calculation:
The Pulsewidth functionality
The Pulsewidth will measure the time from the moment the condition (Cond1) changes from False to True (0 to 1) and will keep tracking time until the condition is no longer true (jumps from 1 to 0). If we enter two conditions, we actually define the moment at which the formula is ready to hold another value, when Cond1 is fulfilled again.
Let's start off without the rearm function:
pulsewidth('Signal1'=1)
We will measure the time from the first moment (sample) Signal1 jumps from 0 to 1, to the very next moment Signal1 jumps from 0 to 1.
The cycle will reset (the function will start to measure the new time) the moment (sample) the value of the previous calculation ends:
The same happens if we modify the formula:
pulsewidth('Signal2'=1)
We will measure the time from the first moment Signal2 jumps from 0 to 1, to the very next moment Signal2 jumps from 0 to 1.
If we use the exact formula from the Stopwatch, but change the Stopwatch to Pulsewidth, we are actually working with a rearm condition:
pulsewidth('Signal1'=1,'Signal2'=1)
In this case, we will start to measure time the moment Signal1 jumps from 0 to 1, until the next moment the signal Signal1 jumps back from 0 to 1. However, we have a rearm condition: Signal2=1. This means that the formula will start holding a new value once this condition is true. In other words:
We will start measuring time the moment we find the first sample where Signal1 goes from 0 to 1.
We will then keep counting until Signal2 jumps from 0 to 1, and will keep counting until we reach a sample (after Signal2 has jumped from 0 to 1) where Signal1 has again jumped from 0 to 1.
We will start tracking time from that moment but will again wait until signal2 jumps from 0 to 1, then find the very first sample at which Signal1=1 after Signal2=1.
If we run the measurement:
We can now make one more equation:
pulsewidth('Signal1'=1 and 'Signal2'=1)
Now, the software will find the first point where both Signal1 and Signal2 are equal to 1, and will stop counting the next time both signals are equal to 1. At this point, the formula will reset, and we will start counting from scratch.
On a video:
To sum up:
The Stopwatch function will calculate the time between two conditions, Cond1 and Cond2. For a condition to be true, its digital value needs to change from false (0) to true (1).
The Pulsewidth function will measure the time from the moment Cond1 changes from False to True (0 to 1) and will keep tracking time until it is no longer true (the condition jumps from 1 to 0). If we enter two conditions, we define the moment at which the formula is ready to hold another value once Cond1 is fulfilled again.
Additional information
Some information can additionally be found in our F1 manual, Operators and mathematical functions.
Another practical example of the Pulsewidth can be found in our Example of measure functions F1 manual, and an example of a Stopwatch can be found in our Example of stopwatch F1 manual.
The file for this FAQ can be found on our Google Drive.