MATLAB Answers

Why is the ROS subscriber callback in MATLAB not triggered when messages are published from an external ROS node (not in MATLAB)?

146 views (last 30 days)
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
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.

  1 Comment

olaf just
olaf just on 10 Mar 2017
Thank you very much. This solves my problem with the youbot and rosserial after a whole week of trying. The export ROS_IP=<IP of my MatlabPC> was missing in the .bashrc. Now I am able to get ADC-Data from my Arduino and use them in Matlab.

Sign in to comment.

More Answers (2)

Vincent Talon
Vincent Talon on 2 Jul 2017
Edited: Walter Roberson on 5 Jul 2017
I use Matlab 2017a with Windows10, I remove all firewall
on my turtelbot the .batchrc have :
export ROS_IP=192.168.1.28
export ROS_MASTER_URI=http://192.168.1.28:11311
In Matlab, I add :
setenv('ROS_IP','192.168.1.41')
setenv('ROS_MASTER_URI','http://192.168.1.28:11311')
I'm well able to read all turtlebot topic with echo on Bash terminal on Windows10
I'm well able to order to trutrlebot to move (with matlab)
velocity = -0.1; % meters per second
robot = rospublisher('/mobile_base/commands/velocity')
velmsg = rosmessage(robot)
velmsg.Linear.X = velocity;
for i=1:50000
send(robot,velmsg);
end
But when I try to creat a subscriber
odom = rossubscriber('/odom')
odomdata = receive(odom,3);
With 2nd line, I have all time the same error message
Error using robotics.ros.Subscriber/receive (line 325)
The function did not receive any data and timed out.
to synthesis
  1. I'm able to display all rostopic --> rostopic list
  2. I'm able to send order of moving to turtlebot with Matlab
  3. I'm not able to ready any ROS topic
What I can test ?
thank you in advance
Vincent

  2 Comments

George Smith
George Smith on 5 Jul 2017
I am in the same situation as Vincent Talon, exactly the same problem. From what I've noticed, is that I can not receive data from the roscore to MatLab, although could list all the topics, and both machines could ping each other, and I could send commands from Matlab to the roscore.
I tried using a different linux machine, and it works just fine. So the problem is with windows networking somehow. Could not figure out the problem yet, maybe this helps someone.
Chang Liu
Chang Liu on 14 Jul 2017
Refer to the link given by @Stefan below. It does work! I just tested it.
The step 1 and 2 given in the link are important.

Sign in to comment.


Stefan
Stefan on 13 Jun 2017
Edited: Stefan on 13 Jun 2017
The interested reader might also want to check the similar topic on:
https://nl.mathworks.com/matlabcentral/answers/196911-use-matlab-robotics-system-toolbox-to-receive-ros-message

  0 Comments

Sign in to comment.

Products


Release

No release entered yet.


Translated by