A simple RS485-UART converter for NXT (EV3 sensor compatible)

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: imaqine, 99jonathan, roger

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sat Apr 11, 2015 5:57 pm

Hi all,

For a long time, I searched for a solution to connect several UART devices, like EV3 UART senors, to my NXT brick. As many of you know the RS485 protocol can easily be converted to UART just by using a RS485 transceiver. But since RS485 is only half-duplex,compared to full-duplex UART, a direction signal is necessary.
Yesterday I finally had an idea how to realize this signal without a huge amount of hardware: Use the ability of NXT to switch on and off 9V on the sensors analog lines! Just connect the analog line via a resistor divider and a schmitt-trigger to the direction pin of the transceiver.

Within two hours I got a working circuit. Thanks to the lejos firmware, the first working demo was quickly up and running. And to further push the boundaries, I even developed a proof-of-concept program to access the EV3 color sensor with NXT (which, in fact, was the really hard part because the EV3 UART protocol is utterly over-engineered).

Using the cheap ESP8266 WiFi-UART module, its also possible to connect our NXTs to the internet for a few bucks.

Github for code and schematic https://github.com/StefansProjects/EV3UARTForNXT
Blogger for a complete article http://stefanshacks.blogspot.de/2015/04 ... -uart.html
Last edited by BlackSuit on Sat Apr 11, 2015 6:42 pm, edited 1 time in total.
http://stefanshacks.blogspot.de/

skoehler
leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby skoehler » Sat Apr 11, 2015 6:09 pm

Does the Wifi module support RS232 flow control? I mean, the wifi module must not attempt to send data to the NXT when the NXT is trying to send data to the wifi module. Otherwise, the data sent by the wifi module is lost and things are out of sync. Usually, proper RS232 devices have some lines to indicate that the other side is not ready to receive (this is called flow control).

So, beside using the analog line for indicating direction, did you also try to workout the flow control? Your schemetics don't seem to involve any RS232 lines other than rx/tx. That won't work, unless the protocol spoken by the Wifi module basically follows the master/slave principle, in which the slave only responds to whatever the master says. The slave never talks when not being told to by the master. Did you check the protocol of the wifi module whether it has these properties?

P.S.: the github URL doesn't work

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sat Apr 11, 2015 6:53 pm

skoehler wrote:Does the Wifi module support RS232 flow control? I mean, the wifi module must not attempt to send data to the NXT when the NXT is trying to send data to the wifi module. Otherwise, the data sent by the wifi module is lost and things are out of sync. Usually, proper RS232 devices have some lines to indicate that the other side is not ready to receive (this is called flow control).


I did some first tests with the ESP8266 modules. WIth stock firmware (the firmware is open source) the WiFi module does not support flow control. Of course one could change the firmware to directly control a RS485 transceiver, then my hack is not necessary. Maybe I even do that on a free weekend. But like in my EV3 UART sensor implementation shown above, it should work because it is pretty well defined by the software protocol when the WiFi module or its host are expected to send data. Even when there is a accidental collision, it won't kill the hardware (I tried that many times - accidentally). The setup with the EV3 color sensor did not even care.


Fixed github URL.
http://stefanshacks.blogspot.de/

skoehler
leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby skoehler » Sat Apr 11, 2015 7:00 pm

Sure, it won't kill the hardware, but it won't work reliably. Data of TCP-connections will be corrupted - or even the length of the data may changes, etc.

Pretty bad if you ask me.

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sat Apr 11, 2015 7:05 pm

skoehler wrote:Sure, it won't kill the hardware, but it won't work reliably.


With all half-duplex hardware comm systems you need either a way to detect collisions or write a software protocol capable of avoiding them. In my experience its all about knowing your timing windows to write a reliable software protocol. Especially at high baudrates the bus is idle most of the time anyway so your timing windows are big enough.
http://stefanshacks.blogspot.de/

skoehler
leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby skoehler » Sat Apr 11, 2015 7:13 pm

TCP is entirely about guaranteeing two reliable data streams (one in each direction). You will not find many protocols on top of TCP that tolerate data corruption. Your approach is therefore highly dubious.

Furthermore, most likely, the protocol between the wifi adapter and the NXT does not tolerate data curruption. Again, this renders your approach dubious as the state within the wifi module and NXT driver will diverge over time.

We currently have 2 "Wifi sensors" for the NXT on the market. One of them has a protocol that _almost_ follows the master/slave principle (the author insisted to have at least one thing in the protocol that would threaten data integrity, because otherwise it would work too reliably, I guess) and the other one just used an rs232 wifi module and was totally ignorant of the issues that it would cause.

If the firmware of that module can be changed, that would be great.

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sat Apr 11, 2015 7:24 pm

skoehler wrote:TCP is about guaranteeing reliability (data is assumed to be preserved). You will not find many protocols on top of TCP that tolerate data corruption. Your approach is therefore highly dubious.

Furthermore, most likely, the protocol between the wifi adapter and the NXT does not tolerate data curruption. Again, this renders your approach dubious as the state within the wifi module and NXT driver will diverge over time.


The stock firmware uses an AT like protocol. The brick can ask the current state of the module anytime and sync. But you know, IMHO this specific discussion is somewhat useless. All in all my project has proven to work with the EV3 color sensor, and will most probably work with the EV3 IR sensor. I have not done any work on connecting the ESP8266 with the NXT. Maybe in the future, I have a spare weekend and will do so. Maybe I use stock firmware, maybe an alternate firmware (there is nice project with Lua on the ESP ongoing), maybe it will work out of the box, maybe its unreliable. I am just saying I think its possible.
Another approach I am thinking of is a simple UART-bluetooth bridge. So you can connect (multiple?) EV3 sensors and the ESP8266 via bluetooth to your NXT brick.
http://stefanshacks.blogspot.de/

skoehler
leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby skoehler » Sat Apr 11, 2015 7:33 pm

The protocol used by the EV3 UART sensors requires a full duplex connection as well. I agree that the protocol is simple to enough to have a simple retry logic in case the "change sensor mode"-message gets currupted. But then again, some Lego sensors (I think the color sensor in RGB mode) are not tested well enough and thus generate invalid checksums. So we have to disable checksum checking, actually. So there's not way of discarding corrupted data messages anymore.

However, Mindsensors EV3 Sensors Multiplexer might provide the more stable experience (it also works with the NXT).

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sat Apr 11, 2015 7:43 pm

skoehler wrote:The protocol used by the EV3 UART sensors requires a full duplex connection as well. I agree that the protocol is simple to enough to have a simple retry logic in case the "change sensor mode"-message gets currupted. But then again, some Lego sensors (I think the color sensor in RGB mode) are not tested well enough and thus generate invalid checksums. So we have to disable checksum checking, actually. So there's not way of discarding corrupted data messages anymore.

However, Mindsensors EV3 Sensors Multiplexer might provide the more stable experience (it also works with the NXT).


Since the hardware on the sensor side is full duplex, the mode switch on a SELECT command will happen. So the only problem that might happen is one corrupted data packet from the sensor to the NXT. Thanks to the checksums (which are definitively a good idea), this will be detected for the IR sensor and all modes of the color sensor but one (which is buggy). But you have to consider: UART is not made for noisy environments, so electric noise (from a motor (cable) nearby) might disturb the communication (RS485 is actually far more suitable in such situations) and flip a bit, too. So the only method to get reliable data in the broken mode of the color sensor is to evaluate multiple measurements and discard the ones far off.
http://stefanshacks.blogspot.de/

skoehler
leJOS Team Member
Posts: 1550
Joined: Thu Oct 30, 2008 4:54 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby skoehler » Sat Apr 11, 2015 8:51 pm

The host is required to send a NACK every 100ms to the sensor [1]. So definitely, more than one data packet can be corrupted.

[1] https://sourceforge.net/p/lejos/wiki/UA ... 0Protocol/

BlackSuit
New User
Posts: 14
Joined: Thu Dec 31, 2009 1:25 pm

Re: A simple RS485-UART converter for NXT (EV3 sensor compatible)

Postby BlackSuit » Sun Apr 12, 2015 6:42 am

skoehler wrote:The host is required to send a NACK every 100ms to the sensor [1]. So definitely, more than one data packet can be corrupted.

[1] https://sourceforge.net/p/lejos/wiki/UA ... 0Protocol/


Even if host gets a corrupted data packet, then its detected by its wrong checksum. And NACK is usually immediately followed by another data packet from the sensor, so the loss in information is acceptable. Currently ckecksums are not implemented, but the program is just a proof of concept now and it I only get corrupted packets infrequently.
I have a complete implementation of the EV3 UART protocol developed for RPI, including parsing all the header information, checking each and every message byte and checksum. Porting it to leJOS is definitely possible and would exclude any wrong information arriving at the user.
http://stefanshacks.blogspot.de/


Return to “NXJ Hardware”

Who is online

Users browsing this forum: No registered users and 1 guest