Hacker News new | ask | show | jobs
Ask HN: What are the language/OS choices for low-latency
8 points by majidfn 4474 days ago
Hello,

For purpose of an academic research, I need your inputs on implementing the following software so I can give better recommendations to a friend.

The software will display images in frequencies ( Images are flickering between the image itself and a black screen). When an image is shown a trigger will be sent to an external device, and another trigger will be sent when the image is no longer visible (black screen appears).

The frequency of images is about 50Hz, meaning that for 20ms an image is shown, and for 20ms the black screen is shown.

The synchronization between the code, the visual output of the images and trigger sent to device is very important, it should be as close as possible to real-time. (<5ms delay).

(For the moment we are not bringing the latency of the external device in to account)

As this is a research project, choices of the tech would highly impact the results.

So far the options for the language are:

- C

- C#

- Matlab (and external toolboxes or Matlab’s realtime workshop)

- Python (and external libraries)

1 - Considering the performance, development time, availability of external libraries, etc. which of the above languages you choose to implement the system, and why?

(Python and Matlab are preferable because ease of use and faster development, but doubtful about the required low latency)

2 - What would be your pick for the OS for such software? Which allows the software to have a high priority execution thread. An OS with low latency. (Probably a RTOS, Real Time Operating System, is not needed)

Any opinion is much appreciated.

2 comments

You don't need anything fancy here. Coming from the game industry where frame updates are typically targeted at 60 Hz or 16.66 ms per frame. Even mobile games often target 60Hz. During the 16ms a game typically has to do a lot of stuff such as ai, rendering, user input. So in comparison your needs are very light. You can probably develop what you need in any number of languages and OS. I would go with what is you are comfortable with. Take a look at SDL2, you could probably put together something in one day. The only thing unusual you mentioned is the external triggering mechanism. Whatever this is you will need some kind of device API/interfacing code, so this may decide which OS you will need to use. SDL2 is cross platform so you should be good to go there.
Maybe you don't need to do it in software? If you get LCD with LED backlight, you can turn it on and off from arduino or even external device itself. It would have nanoseconds of latency (it depends on length of cables now). If you need software solution I would propose C/C++ and linux, if you use library which handles high resolution timers, it's rather easy to achieve sub 1ms latency.
You should also know that at such frequencies, displaying hardware matters. You can have distortions from refreshing frequency. If you show and hide image in software at 50hz and your display refreshes at 60hz, you eaither have tearing (one part of screen is black, other still has image) or "frame drops" when you use v-blanking synchronization (http://en.wikipedia.org/wiki/Vertical_blanking_interval). Such artifacts ARE visible in case of image/noimage transitions and can alter results of experiment. Your friend should use some 200hz tv for tests. For measuring on/off, he could also mask some part of screen, display a white rectangle there and measure on/off with a photodiode (photoresistor may be too slow) under this mask. Of course just turning LED backlight on and off won't have such problems if displayed image is still static.
Maybe you don't need to do it in software?

That's my thought. An optical sensor that detects changes to the screen from the viewer's perspective would provide an accurate signal and decouple the instruction timing. from hardware response.

Measure the results not the inputs.

Thanks for your thoughts. The issue is user should be able to interact with the system. For example after viewing the flickering image for couple of seconds, user should make a choice and click/touch.
Then combined solution, arduino can be connected to computer and when someone clicks, arduino stops blinking image and notifies program, which does what it needs. Then when program is ready to blink another image, it shows blank screen, notifies arduino to start blinking again and then shows image. Cycle repeats. If you need better responses, you should provide more data about what is required, what you need to measure.