Arduino Mega UART -> UDP latency . Tricky one!

Hello,

I have an issue with latency for the project I'm building. Project involves mapping video on object that is moving up and down by a DMX winch. Winch is operated by a motor with an encoder. Arduino should read UART data and send it as-is via UDP. Everything works fine, except that I have a some latency and I don't know how to get rid of it.

Here is simple schematics

--Encoder UART(115200) --> [ ArduinoMega2560 ] -> UDP [Ethernet Shield] -> ethernet cable -> MacBook

Encoder UART is sending serial data (5 ASCII + delimiter) .
Ethernet Shield is V5100 I believe
Macbook is running "nc -kluvw 0 8888"

Here is the code (pretty straightforward)

void loop() {

     length1 = Serial1.readBytesUntil('-', buffer1,100);
     buffer1[length1]='\0';

     //Serial.write(outbuffer, length1+length2+2);
     sprintf(outbuffer,"%s:%s-",buffer1,buffer2);
     Udp.beginPacket(broadcast, localPort);
     Udp.write(outbuffer, length1+length2+2);
     Udp.endPacket();

     if (!reached && buffer1[1] == '4' && buffer1[2] == '8' && buffer1[3] == '1') {
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on 
        reached = true;
     }


}

Here is the video (You can see my arduino and terminal running netcat.)

I turn a led on, when encoder passes a certain position (4810).
As you can see in the video the UDP values are trailing 15 steps behind around (4795).

in the attachment you see a screenshot from the video above, when encoder reaches 4810 position.

So my question is basically how can I improve this and get rid of this lag (which I assume comes from arduino).

If I go with Arduino Duo, will it improve the speed (faster SPI?).

Is there anything else that can be done ?

I need to reduce latency as much as possible, cause currently 15 steps behind is way too much. As you can see in this video the mapping is trailing behind the object:

curiously one can feel that three is a buffer somewhere in the system, but AFAIK UDP doesn't buffer...

Thank you very much
M

It looks like a really fun project with a lot of work already done. If this is a student project great, you have test equipment available to you. For better help, you need to add a schematic, that would help. UDP is not instantaneous, it takes time for messages. Network traffic will also have an effect on transmission packet timing. you show your path as: Encoder UART(115200) --> [ ArduinoMega2560 ] -> UDP [Ethernet Shield] -> ethernet cable -> MacBook Other then the cable (a few nanoseconds does not count) everything in your path can cause a latency problem. Once you have the data you have to make a packet and send it. Determine how long this takes. You can use a scope or logic analyzer to measure this. A logic analyzer from our favorite China suppliers sells them for less then $10. How long does it take to make the data at the Encoder? What is the latency for the network? How long does it take the MAC to process the data and create the pictures. Turning on the lights on also takes time. Let us know how you do!

gilshultz:
It looks like a really fun project with a lot of work already done. If this is a student project great, you have test equipment available to you. For better help, you need to add a schematic, that would help. UDP is not instantaneous, it takes time for messages. Network traffic will also have an effect on transmission packet timing. you show your path as: Encoder UART(115200) --> [ ArduinoMega2560 ] -> UDP [Ethernet Shield] -> ethernet cable -> MacBook Other then the cable (a few nanoseconds does not count) everything in your path can cause a latency problem. Once you have the data you have to make a packet and send it. Determine how long this takes. You can use a scope or logic analyzer to measure this. A logic analyzer from our favorite China suppliers sells them for less then $10. How long does it take to make the data at the Encoder? What is the latency for the network? How long does it take the MAC to process the data and create the pictures. Turning on the lights on also takes time. Let us know how you do!

Hello thanks for your answer! this is an artistic project, so it's a future artistic installation )
Yes, indeed everything can contribute to latency. I've minimized the setup, so no I have simply arduino+shield plugged into ethernet port of mac. No switchers, no routers, no network. As you can see from the first video, even without any rendering software, but simply by running netcat, I still have the lag. I wonder if maybe instead of arduino I can use some dedicated Serial-to-Ethernet transmitters. Something similar to Moxa Inport 5210... because it seems that I can't go any faster.
Thanks for the advice for logic analyzer. Never used it.
To answer the rest of your questions:
Encoder data is generated at 150 per second and there is no latency. checked it with physical marks on the wall and 60fps camera.
Network latency is minimal, as mentioned above. Direct ethernet link.
Thanks
M

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.