Receive Data ROS2
Show older comments
We are trying to connect a Linux based machine (using Ubuntu 18.04 with ROS2 Dashing) to Matlab running on a Windows 10 PC, using ROS2 DDS services. They both are connected on the same network and their subnets are the same (PC is 10.221.114.24 and the device is 10.221.114.161). I set the domain ID of both to 5. When I create a node in either of them, I can detect it in another (using ros2 node list). Likewise, I can do the same for topics (using ros2 topic list). I have a basic publisher and subscriber (std_msgs string), which work perfectly fine, when run in the Linux device. When I run the publisher on the Linux machine; although Matlab in my PC can detect both the publisher’s node and the topic, the Matlab "receive" function always timeout and does not receive any messages. I tried both “reliable” and “besteffort” reliability settings, in the Matlab “ros2subscriber” function, with no luck. I also have turned all firewalls off. Moreover, I have tried to follow all guidelines given in this page, with no luck.
I tested this on both Matlab 2020a and 2021b with no luck.
Here is the Matlab code:
clear all
clc
setenv("ROS_DOMAIN_ID","5");
ros2 topic list
topicList=ros2("topic","list");
if(sum(contains(topicList, "test_publisher_topic")))
SubscriberNode=ros2node("/matlab_subscriber");
pause(2);
msgSub=ros2subscriber(SubscriberNode, "/test_publisher_topic" ,"Reliability", "besteffort");
while(1)
Message=receive(msgSub, 5);
disp(Message)
end
else
disp('hossein_topic not present!')
end
ERROR Code:
Error using ros2subscriber/receive (line 458)
Subscriber did not receive any messages and timed out.
Any idea?
Best
Hossein
9 Comments
Cam Salzberger
on 9 May 2022
Hello Hossein,
I'd like to suggest a couple of tweaks to the test code to make it a little more consistent.
- Could use a pause after the first topic list call to allow a newly-created introspection node time to get metacommunication on the network. I know that's not the issue here, but it's good practice.
- Minor improvement to the conditional on checking if the topic is listed
- Use reliable to avoid potentially dealing with dropped messages
- Use the subscriber callback rather than receive to avoid blocking and allow the script to work no matter if MATLAB subscriber or Ubuntu publisher was created first
- Specify the message type for the same reason
clear
topic = "/test_publisher_topic";
type = "std_msgs/String";
domain = 5;
setenv("ROS_DOMAIN_ID", num2str(domain));
ros2("topic", "list"); % Dummy call to initialize the introspection node
pause(3)
topicList = ros2("topic", "list");
if ~ismember(topic, topicList)
error(sprintf("Topic %s not present on domain %d", topic, domain))
end
node = ros2node("/matlab_subscriber");
if node.ID ~= domain
error(sprintf("Node is on domain %d while environment specifies %d", node.ID, domain))
end
% Using default QoS settings, but being explicit
sub = ros2subscriber(node, topic, type, @disp, ...
"Reliability", "reliable", "Durability", "volatile");
% Incoming messages should now display
Then in Ubuntu, we want to make sure that the QoS settings are compatible. So we can run something like this:
source /path/to/ros/setup.bash
export ROS_DOMAIN_ID=5
ros2 topic pub --rate 1 --qos-reliability reliable --qos-durability volatile /test_publisher_topic std_msgs/String "{data: hello}"
Can you give it a try with the Ubuntu and MATLAB commands, and see if the result is different?
-Cam
Hossein Moghimi
on 9 May 2022
Cam Salzberger
on 9 May 2022
Edited: Cam Salzberger
on 9 May 2022
What error? It shouldn't give you the "receive" error, since it's not calling that any more.
Is the error in MATLAB or on Ubuntu?
Hossein Moghimi
on 9 May 2022
Cam Salzberger
on 9 May 2022
If you are getting this error:
Error using ros2subscriber/receive (line 458)
Then there is something wrong with how you are running the code. Nowhere in my code does it call "receive". So if the "receive" method is still being triggered, then some other code is running it.
Clear everything from your workspace, make sure to run my code in isolation (nothing else in the file), and if you still get that error we need to look at shadowed files and startup scripts.
Hossein Moghimi
on 9 May 2022
Cam Salzberger
on 9 May 2022
Well, there was a reason that I went for display callback rather than receive. It could take several seconds for the initial connection to be made, so a timeout on the very first messages isn't necessarily an indicator of an issue. But if the callback wasn't being triggered, then yeah, that's a problem.
Did the standalone router do the trick?
You can also run the Ubuntu and MATLAB code (remove the "receive" section so no error occurs and the subscriber stays active), then then run this in a separate window on Ubuntu:
ros2 topic info /test_publisher_topic
ros2 node list
ros2 node info /matlab_subscriber
I'm curious if Ubuntu is able to see the MATLAB node and subscriber.
Hossein Moghimi
on 10 May 2022
Hossein Moghimi
on 11 May 2022
Answers (1)
Cam Salzberger
on 11 May 2022
Moved: Remo Pillat
on 26 Jan 2023
0 votes
Sorry, this is a pretty new issue to me. I think I've seen one-directional communication, but not the inability to see the subscribers in MATLAB. And usually this issue is with ROS 1 not ROS 2.
Both R2020a and R2021b use Dashing on their back-end as well, so version incompatibility should not be the issue. If you upgrade to R2022a, it uses Foxy, so you may get an issue at that point fi your Ubuntu is still using Dashing.
If, after making your XML file, you are still able to see the MATLAB nodes in "ros2 node list" and are able to receive messages from MATLAB publishers on Ubuntu, then the XML file is working correctly. The environment variables you set, however, I believe only apply to Foxy (and maybe Eloquent), but not Dashing.
The one thing I think should be tried next is a bit intensive. If you are able to install ROS 2 Dashing on your Windows machine, we can isolate whether the issue is with MATLAB's ROS 2 implementation, or with the general ROS 2 communication between Windows and Ubuntu.
-Cam
Categories
Find more on Publishers and Subscribers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!