5

I'm using a Python script via (TkInter) on the Raspberry Pi to capture mouse movement and events. My goal is to send this data, in real time, to another connected Arduino device(Leonardo).

My first decision is how to connect-send this data. My understanding is that there are 3 main options:

  1. direct via USB
  2. I2C
  3. Serial GPIO

Since both USB ports are being used I was opting for the 3rd method, serial GPIO. However, I am concerned with latency issues since one Python module RPi.GPIO mentions:

this module is unsuitable for real-time or timing critical applications

For serial GPIO there is also WiringPI which I'm guessing might have less latency issues since it is written in C and has a Python wrapper.

Can any Raspberry Pi / Python experts chime in? Would I be better of going with a USB solution to minimize latency?

migrated from arduino.stackexchange.com Jul 14 '14 at 19:52

This question came from our site for developers of open-source hardware and software that is compatible with Arduino.

  • How do you define "real-time"? How much latency can you accept? – Craig Jul 14 '14 at 20:03
  • If you are capturing manual input there is no need to worry about latency as human response times are ~200mS – Milliways Jul 15 '14 at 0:09
  • This has not much to do with real-time nor with latency, because you use well defined interfaces. Easiest way to connect would be the USB Serial line that apears as /dev/ttyACM0 or /dev/ttyUSB0 on RaspberryPI. See link. – user19038 Jul 15 '14 at 14:56
  • USB itself has surprisingly high latency - it is good for moving a lot of data quickly, but comparatively slow to get started. See for very short messages, high baudrate serial or SPI wins. – Chris Stratton Feb 10 '15 at 3:54
4

If you need low latency communication, know that interpreted code is always going to be relatively slow. Code compiled into CPU-native instructions, as C and C++ almost always are, will run much faster, 50:1 or more in some cases. For extreme cases, once your application works, re-visit the critical code and see if it can be improved by hand-writing it, or a critical portion of it, in assembly language. Interrupt service routines are often hand-optimized this way if they will called often enough or have enough processing to do that they would affect their own or the system's latency.

  • 1
    I think the biggest problem is other programs running in parallel. Writing in assembly doesn't fix this. – Gerben Jul 15 '14 at 18:08
  • 1
    Low latency code needs to run at a higher priority than its competition (as well as being fast). That usually means either running as an ISR, or as an exec level function initiated by an ISR (on the receive end) or by an API call or equivalent (on the transmit end). – JRobert Jul 15 '14 at 19:10
  • guys lets be realistic here, OP did not specify ANY constraints to his communications and "latency" problems. We also don't know the VOLUME of the data he is pushing. JRobert argues that OP should not use python, but it might be best for his application. @Gerben: you are correct, but only to a point. 1 Ghz is very fast compute for a modest range of number crunching problems. Don't assume that a single core is the bottle neck for him – portforwardpodcast Jan 28 '16 at 7:44
  • OP did, in fact, specify a concern with latency issues. – JRobert Jan 28 '16 at 18:03
3

Between a Raspberry Pi and an Arduino, using UART is the best option. I know because this is my 3rd project doing so. (And also USB is impossible) You should run the UART at 1000000 baud. Default UART configuration is 9 bits per character. This leaves a latency of 1/(1000000/9) or 0.000009 aka 9 microseconds. If you are sending mouse commands I would guess that it would take 2 bytes per update so that's 18 microseconds per update. For comparison the absolute best latency of Bluetooth 625 microseconds according to Wikipedia.

So just use UART and be happy. Also you need to use a level shifter or just a plain resistor divider on the TX pin of the Arduino going to the RX of the Raspberry Pi. This is because Arduino is 5 V and Raspberry Pi is 3.3 V. Post a comment if you need help with this.

Finally: using Python on the Pi is fine. You won't add any noticeable latency to your system with Python over C (assuming you are just doing straight data transfer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.