Why is the ROS subscriber callback in MATLAB not triggered when messages are published from an external ROS node (not in MATLAB)?
조회 수: 35 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2014년 2월 24일
편집: MathWorks Support Team
2014년 2월 28일
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.
채택된 답변
MathWorks Support Team
2014년 2월 24일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Publishers and Subscribers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!