Thursday 16 February 2017

rs232 - What can I do to decrease the latency from these serial ports which are attached to a PC via a Serial to USB adapter?


I think I have accidentally discovered a need in my life for embedded systems. Which is great! And kind of scary. And I need help.


Background: I got hired to build a GUI application which takes scans from two SICK LMS-291s and integrates them with a sub-inch accuracy GPS, so you know where each scan occurred. As the naive web programmer I am, I understood that timing would be important, but didn't realize it would also be hard! If you don't know when each GPS point and each scan occurred, you can't figure out where the scans occur. Oops.


They had specified windows 7 as the platform, as well as bought a SeaLevel RS422 to USB box to hook up the sensors and GPS to, and in short order I discovered my folly. Somewhere between the sensors and my computer program, something was keeping the scans from arriving in a timely manner. The LMS spits out 75 scans per second, or at 13.32 ms/scan. My program doesn't get them in a timely manner. It gets them every 100 or so milliseconds, in groups of 7 or 8 or 10 or something. Also sometimes not enough scans show up, or they're mangled. Either this SeaPort adapter is only sending ten times a second (is that possible? I don't know how USB works) or Windows isn't checking the buffer (there must be a buffer somewhere, right?) nearly often enough.


Present Day: This leads to some inaccuracies that the client is basically okay with. I'm not, though, and since I've got a chance to do similar work for the client (integrating more sensors inputs!), I'd like to figure out how to do it right, e.g. given the accuracy of the GPS, be able to give guarantees about the precision and accuracy of the scan locations.


What does that look like? I need a UI, and to be able to check input from these three devices every 13.32 milliseconds. If I used FreeRTOS with, say, Nano-X for the GUI, run on a laptop they provide, would that sound like a sane solution? Is it possible that the RS-422 to USB adapter is causing these delays, and using Windows is actually just fine for this purpose?



Answer



The problem is almost certainly in the USB buffering related to the USB-RS422 converter. USB has variable and fairly high latency.


The easiest solution would just be a better RS422 interface, ideally something PCI/PCI-e based. That would solve the latency issues.


You may also be able to modify the USB polling rate, though this is fairly dependent on the host OS (what platform are you on?).





For what it's worth, I looked about on sealevel system's website, and it is MASSIVELY infected with marketing bullshit. They literally spend like 10 pages and multiple white-papers on saying "we use a USB hub internally, rather then doing MCU multiplexing".


Hey SeaLevel! That's what the cheap-ass $50 4-port USB-serial interface I bought on e-bay from china does too! You're not special, even if you write half a dozen vapid whitepapers trying to make it sound like you are!


Have you tried to force those things to use FTDI drivers? I'd put money on it they're just using bog-standard FTDI FT232 or similar. Can you pop the box on one of them open, and take pictures?




If you really want to spin your own hardware, for fun or educational opportunities, I'd strongly suggest you not try to do everything in the hardware. Since you need to simply time-correlate all three signals, all you really need is something that can listen on three serial lines (two RS422, one RS232 (the GPS)), time-stamps the data, and forwards it to the main computer.


Once the data is time-stamped, you're free to have all the buffer-latency you want, since you can always just look at the time-stamps.


Realistically, if you have no base in hardware, designing something with enough crunch to draw a nice GUI is quite the undertaking.


Personally, I'd probably throw a fairly chunky ARM MCU at the buffering issue, and be done with it. Despite the fact that it's an Arduino, the Arduino Due has plenty of SRAM, and is more then fast enough for what you need (and there is lots of support, which is always nice).
Alternatively, the STM32 series has similar performance, and is more intended for "advanced" users (read, there are fewer, or no examples to reference). ST makes lots of quite nice, extremely inexpensive eval boards as well.



With the Due, you do get a native USB port, for which you could roll your own CDC driver if you wanted. Some of the STM32 boards have native USB as well.


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...