How to Read Multiple Sensors on the same teensy?

mstreet

New member
I am trying to read multiply sensors on my teensy 4.1 using Teensyduino. Each sensor is on its own SPI port. The problem I am running into is that each sensor has its own rate that it runs at. For example I have one sensor that runs at a period of 5 ms and takes 250 us to read the data. I have another at 2000 hz and takes 150 us to read the data. And finally a third at 4000 hz that takes 20 us to read the data. I'd like to have an interrupt for each sensor and then run the data reading on some sort of prioritized thread but arduino/teensyduino doesn't seem to have that capability. I have looked at tons of third party schedulers, "threading" libraries but none of them allow me to interrupt the slower 5ms sensor that takes 250 us to read. It would make sense to be able to let that 5ms sensor begin reading the data and interrupt it to get the 4000 hz data and then continue reading the data. Has anyone done this using Teensyduino? Any recommendations?



Interrupt 1 ------+--------+---------+-------
ReadSensor ------- +++-----+++------+++-
Interrupt 2 --+--+--+--+--+--+--+--+--+--
ReadSensor---+--+--+--+--+--+--+--+--+-
 
Look at TeensyThread perhaps?

With the devices being on separate SPI buses it isn't guaranteed to fail - but it may not work reliably having the processor pulled away and interrupted during an active read.

Setting up three threads each getting their own sensor data and doing thread.yield when done perhaps should reserve as much time for any other threads to do 'work' on the data as it accumuolates.

Similar could be done MANUALLY with IntervalTimer interrupts where each gets an appropriate level of priority where one would interrupt the lower ones.
 
Back
Top