Example 10 - Operating hours counter, signal timeout
Example 10a - Operating hours counter
Task
The operating time of the associated unit is to be determined from a digital control signal COMPRESSOR and displayed in hours.
Solution
CompressorOnTime_h = integrate(bool(COMPRESSOR), {storage: 'tot_compressor'}) / 3600; // output in hours
Functions used
Explanation
The integral over a constant in the period from 0 to the time t results in , the time over which the integration takes place:
If you integrate via an alternating digital switching signal with the values 0 and 1, you obtain the accumulated time for which the signal was at 1.
The storage must be specified in order to save the last calculated value after restarting the software.
Example 10b - Signal timeout
Task
For a measurement signal MEASURE, the aim is to check whether the signal is still active. For this purpose, it is assumed that the measurement signal of a physical variable always provides at least minimal small fluctuations due to the noise.
The sensor failure can also be caused by an interruption in data communication (CAN, MVB). In this case, no new data is received on the smartCORE channel.
The timeout state should be set if the measurement signal shows no activity for 10 seconds.
Solution 1
#timeless MEASURE
MEASURE_TimeOut = integrate(1, onchange(MEASURE), {upper: 100}) > 10;
Functions used
Explanation
An integral is also used here to measure the time. Unlike in example 8b, however, the reset input of the integral function is used to always start the integration at 0.
The function onchange()
is used as the event for this, which always supplies a minimal short pulse when the measurement signal MEASURE changes.
A persistent memory on the integral is not required, but the integral value is limited upwards (upper) to 100. This makes it easy to calculate the comparison for the timeout after 10 seconds.
The macro #timeless
is required, as the data communication must already be regarded as uncertain. As the math module always evaluates data at the correct time, the calculation of the integral function would be suspended (i.e. no alarm!) until a new measured value is received - or new data records are automatically generated after inputTimeoutS.
Due to the timeless mode, this channel is generally excluded from the correct time calculation. If the channel is nevertheless required for calculations, the activity monitoring(s) can be moved to a separate instance of the math module.
The macro #timeless
accepts multiple signal names, which must be separated from each other by ,
. Quotation marks should be used if the signal name contains special characters. The use of the $'...'
syntax is not permitted here, as the value of the channel is not accessed.
Solution 2
#timeless MEASURE
MEASURE_TimeOut = !pulse(onchange(MEASURE), {duration: 10, restart: true});
Functions used
Explanation
The function pulse()
is used here to generate a pulse of the specified duration duration from an event. If a new event occurs before the duration expires, the pulse is extended by the setting restart: true so that the duration is waited for again from the new event.
The function onchange()
is again used as the event for this, which always supplies a minimal short pulse when the measurement signal MEASURE changes.
As the result of the pulse()
function is to be understood here more as an activity monitor, the TimeOut results from the inversion with the !
operator.
The macro #timeless
has the same function as in solution 1.