Why is the ROS subscriber callback in MATLAB not triggered when messages are published from an external ROS node (not in MATLAB)?
37 views (last 30 days)
Show older comments
MathWorks Support Team
on 24 Feb 2014
Edited: MathWorks Support Team
on 28 Feb 2014
Reproduction steps:
ROS Master:
In Linux terminal,
$ roscore
Publisher:
In a new Linux terminal,
$ rostopic pub -r 2 /chatter std_msgs/String {}
Subscriber:
In MATLAB:
Node = rosmatlab.node('Test','http://localhost:11311');
Sub = Node.addSubscriber('/chatter','std_msgs/String',10);
Sub.setOnNewMessageListeners({@test_fun});
In a new file “test_fun.m”:
function test_fun(msg)
disp('Message received!')
end
With the above setup, no messages are received in MATLAB.
Accepted Answer
MathWorks Support Team
on 24 Feb 2014
This issue arises because the subscriber cannot resolve the URI of the publisher. When a ROS node advertises a topic, it provides a hostname:port combination (a URI) that other nodes use to establish contact when they want to subscribe to that topic. It is important that the hostname the publishing node provides can be used by all other nodes to contact it. The ROS client libraries use the name reported by the machine as hostname. This is the name that is returned by the Linux command “hostname”. If a machine reports a hostname that is not addressable by other machines, you need to set either the ROS_IP or ROS_HOSTNAME environment variables. (Reference: http://wiki.ros.org/ROS/NetworkSetup)
ROS_IP and ROS_HOSTNAME are optional environment variables that set the declared network address of a ROS Node or tool. They are mutually exclusive; if both are set, ROS_HOSTNAME will take precedence. Use ROS_IP if you are specifying an IP address and ROS_HOSTNAME if you are specifying a name. (Reference: http://wiki.ros.org/ROS/EnvironmentVariables)
The environment variable ROS_MASTER_URI is required when the ROS core is not running on localhost. ROS_MASTER_URI are used by ROS nodes to locate the master.
TO RESOLVE THIS ISSUE, ALWAYS SPECIFY ROS_IP BEFORE STARTING A NODE ON LINUX. Use the IP address of the network interface on which the node is communicating with the ROS network.
To ensure the correct behavior of the MATLAB ROS support package, it is recommended that you always specify ROS_IP and ROS_MASTER_URI explicitly.
For example, if the ROS core is running on a machine with IP: 192.168.198.128 and MATLAB is running on a machine with IP: 192.168.198.1, then we should set up the environment variables as follows (please replace the IP addresses below with the IP addresses of your network setup):
In MATLAB,
setenv('ROS_MASTER_URI','http://192.168.198.128:11311')
setenv('ROS_IP','192.168.198.1')
In Linux terminal:
$ export ROS_IP=192.168.198.128
$ export ROS_MASTER_URI=http://192.168.198.128:11311
$ start a roscore, publisher or subscriber
To make the above change permanent, put the above two export commands in the “.bashrc” file, as follows,
$ cd ~
$ gedit .bashrc
At the end of the file “.bashrc”, add the two export statements.
export ROS_IP=192.168.198.128
export ROS_MASTER_URI=http://192.168.198.128:11311
Restart Linux terminal.
0 Comments
More Answers (0)
See Also
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!