I am trying to read can ids as fast as possible but the required instance().loop() call at the top of loop() is slowing me down to 1ms
The FreeRTOS task scheduler only calls loop() 1000 times per second at most; I don't think it's the edge instance loop that is causing the delay.
You need a smaller loop within loop() that processes all of the outstanding messages in the CAN event buffer before returning. It would be good to have an upper bound of how many times you loop processing messages before returning from loop().
The same 1ms delay occur when calling a publish is that correct?
The FreeRTOS task scheduler uses a 1 millisecond tick. If any thread exceeds its time slice, it's pre-empted. If a thread yields execution, such as by calling delay(1)
, then the thread will not be called again until higher priority threads have been given CPU time.
Generally speaking, the loop thread cannot be guaranteed to ever be called more than once per millisecond for this reason.
It's likely that publish yields thread execution, which would explain the 1 ms delay.
For timing-sensitive code you should put it in a separate worker thread. It will still have a 1 ms cadence, but you don't have to worry about other things slowing it down. However, if it's the CAN controller be sure to use SPI transactions to prevent another thread from accessing the controller chip at the same time as the worker thread.