If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Teensie RTOS (Real time operational system)

For a university project I use this real time operational system developed by Fernando Trias : https://github.com/ftrias/TeensyThreads. I use two rotary encoders one two estimate bicycle velocity and one to estimate rpm of the pedal cadence sytem. For the estimation of velocity I use the pulseIn function and then multiple this value by 2 to get the period etc. It seems that the estimation of velocity and rpm seen in the second thread ( void read_data() {) gives me false values when using the RTOS. However, the estimation is fine whenever I run the code without the (RTOS).
I am struggling with this issue a couple of days so any help is more than welcome.

I don't know how this RTOS works exactly but if it switches threads by fixed time slices without waiting for time sensitive tasks like pulseIn to accomplish, you'll naturally get wrong results. I doubt that this RTOS does context switching, I think that the micros() counter (on which the pulseIn() function depends) does just continue, even while other threads are being serviced, which will for sure go into the pants.

I think, you should rather use FTM capture to measure the pulse widths in a hardware counter without dependency from whatever task timing.

Edit: I see now that this RTOS does preemptive multitasking, thus you should make sure that the read_data() thread is not interrupted or will remain locked while the (non thread safe) pulseIn() function is active.

Thank you for your valuable help. I was thinking using FreqMeasureMulti library instead of pulseIn to estimate the frequency.Since FreqMeasureMulti library uses CPU clock cycle to estimate frequency and is not task timing related is there an issue by using this library while using the teensiethreads (RTOS)? Otherwise I will take a look at the FTM chapter of the processor manual. Is there an example using FTM capture on teensie board?

The FreqMeasureMulti library uses the internal system bus clock for the FTM timer. It - should be - thread safe, since the timer continues to count independently of the context switching but under the condition that this RTOS allows hardware interrupts to be serviced without delay.

But basically, now that you can do the pulse capture on dedicated FTM hardware in the background, perhaps the RTOS (with all its overhead) is not longer needed?

Edit: ...and the FreqMeasureMulti library is one of the best examples for FTM capture on the Teensy.

The other libs using input capture, aside from FreqMeasure & FreqMeasureMulti, are AltSoftSerial and PulsePosition. I believe tni may have posted a DMA-based example on the forum several months ago, but I don't have a link handy.

The other libs using input capture, aside from FreqMeasure & FreqMeasureMulti, are AltSoftSerial and PulsePosition. I believe tni may have posted a DMA-based example on the forum several months ago, but I don't have a link handy.