Clear Filters
Clear Filters

How do I use a ros2 discovery server in matlab

10 views (last 30 days)
I have setup a ROS2 Fast DDS discovery server as seen at the following: https://docs.ros.org/en/foxy/Tutorials/Advanced/Discovery-Server/Discovery-Server.html
This works great using the ros2-foxy CLI when I do export ROS_DISCOVERY_SERVER=<server_location>, and then run a listener node.
However, in MATLAB, using setenv("ROS_DISCOVERY_SERVER",<server_location>) does not allow me to use that discovery server. It seems to have no effect at all. I have also made sure to set the RMW implementation to rmw_fastrtps_cpp.
Is this feature just not supported? Is it a bug? Or am I simply doing something wrong?
Thanks!
close all
clear
clc
% Set environment variables (should be same functionality as 'export FOO=bar')
ROS_DOMAIN_ID = 0;
setenv("ROS_DOMAIN_ID",num2str(ROS_DOMAIN_ID))
setenv("RMW_IMPLEMENTATION","rmw_fastrtps_dynamic_cpp")
setenv("ROS_DISCOVERY_SERVER","192.168.1.128:11811")
% create node
subNode = ros2node("/matlab_sub",ROS_DOMAIN_ID);
while true
pause(.2)
try
% scan for topic
laserSub = ros2subscriber(subNode,"/chatter");
break
catch ME
disp(ME.message)
end
end
while true
% read data from topic
[scanData,status,statustext] = receive(laserSub,10);
scanData.data
pause(.2)
end

Answers (1)

Josh Chen
Josh Chen on 6 Sep 2022
Hi Samuel,
I just tried this on my end and it works fine as shown below:
sub3 was created with a node when "ROS_DISCOVERY_SERVER" was set to empty, while sub2 was with a node when "ROS_DISCOVERY_SERVER" was set to the same address as the virtual machine IP. As shown on the screenshot, sub3 did not receive anything, while sub2 was able to continuously receive messages from the publisher on remote device.
Couple things to check are:
  • Which version of MATLAB are you currently use? I did the above experiment with MATLAB R2022a, which supports ROS 2 Foxy.
  • Make sure you set the correct IP address for the environment variable "ROS_DISCOVERY_SERVER". This can only be the ipaddress of the remote machine. If you set it to something else, it may work for local network inside the remote machine (e.g. publisher and subscirber all start in the same remote machine), but it will not work elsewhere.
  • It is always good practice to provide subscriber type while creating subscriber, and use "sub.LatestMessage" to check during prototype to avoid network issues.
Hope this helps,
Josh
  1 Comment
Samuel Kemp
Samuel Kemp on 29 Sep 2022
Hello, thanks for the reply.
It is still not working. I upgraded to matlab 2022b. I ensured the ip address and ports were correct. I have verified I can receive the message on another machine running linux and using the ros2 topic echo command. Are there any debugging steps I can take?

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!