How Do I Use Subscriber Block to Get Messages from a ROS Node Running in Linux?
Show older comments
Below is the code that generates geometry_msgs::Point messages.
#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#include <cmath>
int main(int argc, char **argv)
{
ros::init(argc, argv, "sinewaves_generator");
ros::NodeHandle n;
ros::Publisher wave_pub = n.advertise<geometry_msgs::Point>("/location", 1000);
ros::Rate loop_rate(100);
long count = 0;
while (ros::ok())
{
geometry_msgs::Point msg;
double time = (double)count / 100.;
msg.x = sin(time - M_PI / 2.);
msg.y = sin(time);
ROS_INFO("%f, %f", msg.x, msg.y);
wave_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
The subscriber model is based on this instruction: http://www.mathworks.com/help/robotics/examples/getting-started-with-ros-in-simulink.html
The roscore is running at a Ubuntu host, and the publisher is running at a Ubuntu host. The subscriber model is running on windows with network access specified to the Ubuntu host as instructed here: http://www.mathworks.com/help/robotics/ug/configure-ros-network-addresses.html
Both the publisher and the subscriber run, but the subscriber doesn't get x and y values.
Answers (1)
Sebastian Castro
on 14 Jul 2015
Edited: Sebastian Castro
on 14 Jul 2015
It should work, as geometry_msgs/Point is definitely a supported message type in Robotics System Toolbox.
First thing I would do is try ping the machine by issuing a system command from MATLAB (!) and using the IP address of the Ubuntu node.
!ping 192.168.182.138 %(For example)
Next, I would run "rosinit" in MATLAB and check that the /location topic exists:
rostopic list
If it does, and you try subscribe in MATLAB as follows, does "msg" ever become a non-empty message?
sub = rossubscriber('/location');
msg = sub.LatestMessage; %(or msg = receive(sub))
- Sebastian
2 Comments
Haoyu Zhang
on 14 Jul 2015
Sebastian Castro
on 14 Jul 2015
Yeah, the fact that you're seeing a 0x1 message means that nothing is being received on the MATLAB side.
How certain are you that your C++ node works fine? You could try do a "rostopic echo /location" in a Terminal on the Ubuntu side to see if anything is happening on the publisher side.
Then, you can enter the same command and then in MATLAB itself to see if anything is coming in.
If you see message on the Ubuntu side and not on the MATLAB/Windows side, then there might be some networking issues. Do you have a firewall or anything set up? You could also try a different port besides the default one when you do "rosinit".
- Sebastian
Categories
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!