How do I use a ros2 discovery server in matlab

11 views (last 30 days)
I have setup a ROS2 Fast DDS discovery server as seen at the following:
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?
close all
% Set environment variables (should be same functionality as 'export FOO=bar')
% create node
subNode = ros2node("/matlab_sub",ROS_DOMAIN_ID);
while true
% scan for topic
laserSub = ros2subscriber(subNode,"/chatter");
catch ME
while true
% read data from topic
[scanData,status,statustext] = receive(laserSub,10);

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,
  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.


Find more on Network Connection and Exploration 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!