Multiple TCP connections using rosserial_server with WiFi Arduino
Dear all,
I am currently running an Arduino Uno* with an ATWINC1500 WiFi breakout from Adafruit and can successfully publish topics over WiFi to a master using rosrun rosserial_python serial_node.py tcp
. This has been achieved using code modified from this previous ROS answer supplied by @ahendrix.
The trouble I run into is running two of these devices at the same time. They both have unique IP addresses and are connected to the network, running differently named topics, and individually can be successfully connected and publish topics.
The first device can be connected using rosrun rosserial_python serial_node.py tcp
. Simply starting a second device does not lead to be being connected. If I run a second serial_node.py with a different node name to avoid conflicts (by using rosrun rosserial_python serial_node.py tcp __name:=OtherNodeName
), I get an error "socket.error: [Errno 98] Address already in use"
.
As the socket can't be given as an argument to serial_node.py, I tried using rosrun rosserial_server socket_node
as rosserial_server in theory can handle multiple connections. Using default port 11411, I find the node displays:
Listening for rosserial TCP connection on port 11411
but never finds any devices, unlike rosserial_python serial_node.py which was successful.
Am I missing something for rosserial_server to be able to identify and connect to these devices?
Is there another way of connecting multiple TCP devices such as these arduinos with wifi simultaneously?
Thank you for your time,
Andy
UPDATE:
Having followed some very good advice from gvdhoorn, I investigated the packet traffic using Wireshark. Turns out for rosserial_server socket_node
the connection between the arduino and the master must not complete (there are a few messages back and forth, but finishes before the "normal" regular published traffic commences as seen when using rosserial_python serial_node.py tcp
).
By adding a short delay after nh.advertise()
, the master and the arduino are able to make a connection (though socket_node reports some errors to begin with), and start publishing. This also allows for multiple arduinos to be connected! However, the connection then drops out after a minute or so from what I observe as TCP out-of-order issues.
I thought this might be because of the delay(1000)
in the main loop, so I replaced this instead for using millis()
and letting nh.spinonce()
not get held up. This didn't improve the situation, it in fact might have made the drop out occur earlier, but I haven't done any actual timings to confirm that.
There are occurrences of TCP out-of-order when using serial_node.py
, but they seem to resolve themselves very quickly and not terminate the connection between the arduino and the master when using identical code. I left this running for a few hours with no termination of connection.
Better questions:
What is it about socket_node
that leads to traffic being treated differently compared to serial_node.py
and lead to the connection being terminated?
What could be done to improve the stability of TCP connections when using rosserial_server socket_node ...
I'm not a
rosserial
expert, but just to get a feeling for what is going on at the network level, I'd run Wireshark while trying to get things to connect. That should at least tell you whether your clients are trying to connect, how, where and in what way. It should also show server activity.Note that using TCP/IP over a wireless link is not a very good choice for anything interactive. Whenever I used
rosserial
over wireless I've used the UDP variant. Trouble is though that it currently lacks some of the functionality that the TCP implementation does have.I've investigated using Wireshark. For
rosserial_python serial_node.py
it appears to run nicely, however forrosserial_server socket_node
there are some errors being thrown around. By addingdelay(100);
just afternh.advertise();
the arduinos manage to connect! But dropout randomly later.The
"socket.error: [Errno 98] Address already in use"
suggest that both copies ofserial_node.py
are trying to listen on the same port number. I'm not sure if a single node is designed to handle multiple clients or not, you might want to look for documentation about this.If a single node is supposed to handle multiple clients, then this behavior is a bug. Otherwise you may want to try running the second node and its Arduino on a different port number.
(Failing to handle multiple clients is still probably a bug for most software that has a listening socket, but if the docs don't say it's supported you may have a hard time getting this "bug" fixed unless you're willing to submit the fix yourself)
Hello dear community. I need your help. I am facing problems with rosserial_server socket_node. can you please take a look to my post? Thank you so much RosSerial_Server