araffin / arduino-robust-serial

A simple and robust serial communication protocol. It was designed for Arduino but can be used for other purposes (e.g. bluetooth, sockets). Implementation in C Arduino, C++, Python and Rust.
https://medium.com/@araffin/simple-and-robust-computer-arduino-serial-communication-f91b95596788
MIT License
135 stars 28 forks source link

Serial latency? #6

Open Armandpl opened 1 year ago

Armandpl commented 1 year ago

Hello,

I'm curious to know what kind of latency you were getting and with which arduino?

I'm using a similar technique to try and communicate with my rotary inverted pendulum but I'm running into latency issues. It's taking a long time to send the command and get back an answer from the arduino. To be clear I'm not using this specific library but my code is really close.

Using an arduino nano it takes me 15.5 ms to send and then receive 8 bytes from pyserial. Using a nano every it is much faster and takes only 1.5 ms. I'm trying to investigate what's affecting the speed and to figure out if I've made a mistake somewhere.

Cheers

araffin commented 1 year ago

Hello, will try to answer a bit longer by this weekend. In the past I used arduino mega mostly and I remember having ~1-10ms latency (but I'm not sure we checked it closely back then, that was more than 5 years ago ^^").

Using an arduino nano it takes me 15.5 ms to send and then receive 8 bytes from pyserial

so the problem is python? if so, you might consider going to c++/rust or try to find faster implementation of Serial.read(). (the latency is from the python side, right?)

Armandpl commented 1 year ago

so the problem is python?

Looks like it's not as the latency varies depending on the arduino. e.g using the arduino nano every and the same code I get 1.5 ms instead of 15.5 ms.

try to find faster implementation of Serial.read() On the arduino side? How would I go about finding this? any keyword I should search for?

araffin commented 1 year ago

You could also debug by mocking the arduino (maybe take a look at python serial tests: https://github.com/pyserial/pyserial/blob/master/test/handlers/protocol_test.py): https://github.com/balp/arduino-mock

Did you had a look at the low_latency_mode option?

See https://github.com/pyserial/pyserial/pull/290

Armandpl commented 1 year ago

thanks very much I'm going to try that :)

NaiveInvestigator commented 2 weeks ago

@Armandpl what was the fix you later on came to settle into? Did 'low_latency_mode' fix the issue for you or something else?

Armandpl commented 2 weeks ago

@NaiveInvestigator old arduinos e.g the nano have a separate usb serial modem while newer versions such as the nano every have the modem included in the main chip. I don't know why but those are faster. The fix was switching to one of the newer chips, I choose a xiao samd21.