Running a function every 250us (microseconds) on Ras Pi 2

#1

I have a project in mind where I want to grab some depth data (probably from a Kinect 360) and pass it to a Raspberry Pi, do a bit of image processing and then pump it out to a bunch of custom displays that I am designing.

For various reasons, ideally this ends up with me outputting data from the SPI port on the Pi 4000 ‘frames’ per second (don’t worry, only about 600 bits per frame, so the total datarate stays manageable at 2.4Mbps). Is there any way to attach an interrupt service routine to a timer interrupt in OF with a timer period of the order of hundreds of microseconds? Specifically, I would want to do this on the Ras Pi 2 - has anyone tried anything this mad before?

#2

I’ve had a little look deeper into this, and it seems that one way to do this would be to create a thread which wakes up, pumps out data and then sleeps. This won’t be entirely accurate as there will be an overhead associated with getting data out, but I can live with that in the short term.

However, it seems that the underlying Poco library only allows sleeps in the order of milliseconds. Am I correct in seeing this a complete roadblock, or are there other ways around?

#3

in 0.9 you there’s a new class that does exactly this, we use it to control the fps when setting it explictly. ofTimer. on an ofThread threadedFunction call:

class Scheduler: public ofThread{
public:
Scheduler(){
    timer.setPeriodicEvent(250000); // parameter is in nanoseconds
}

private:
ofTimer timer;
void threadedFunction(){
    while(isThreadRunning()){
         timer.waitNext();
         // do your thing here
    }
}
}

the timer will wake up at 250us intervals no matter how long the rest of the task takes, if the last execution took more than 250us it’ll wake up right away and execute the next. it also uses the most accurate timming functions on every OS

4 Likes
#4

Wow - thanks @arturo, great to know! I am a fair way away from actually getting down to this part, but this helps me decide on the framework and processor platform a bit.

#5

Excellent. Thank You~

#6

Excuse the newbie question but how can I add this class to my project? I need to run certain functions every 100 milliseconds but this declaration doesn’t have the structure of class files (with .cpp and .h file) and I don’t know how to make it work. Thanks for any tip!

#7

You use it like any other openframeworks class, just add it in your .h as ofTimer timer then use it from your cpp. This is usually used from a thread though since it would stop the main loop if you used it there for which you can just use ofSetFramerate which would have the same effect