5.2 Sensor/ADC processing

You can use one thread to convert the values in circular buffers into the final values for error correction and mapping. This kind of processing includes deglitching, averaging and possibly step detection. In addition, it is best to convert the ADC values to distance units using interpolation and a look-up table.

This kind of computation can potentially take a bit of time, which makes it unsuitable in an ISR. Moving this logic into a thread makes it possible the perform the computation without affecting much of the rest of the system.

The triggering of the computation can be a rtkSemaphoreVNoCS from the ADC ISR after a certain number of samples are collected in the circular buffer. Because the computation can potentially take a bit of time, I suggest copying a buffer to be processed into a local buffer in a critical section. Then, leave the critical section (that locks interrupt) and perform the length computation.

Note that the lengthy computation does not need to be in a critical section if the length variable used by the rest of the program is double buffered. This means that the computation can use a local length value as the rest of the program uses a global variable that represents the previously calculated value. This way, only the transfer from the internal (local) length to the global variable needs to be in a critical section.

How often should this thread convert ADC values into lengths? Since the sensor has a lag of 40ms, it doesn’t make sense to make this thread go any faster than every 40ms.