ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

subscribe to sensor_msg::PointCloud2

asked 2016-02-04 03:24:34 -0500

jhlim6 gravatar image

Hi, my ros subscriber is having trouble entering the callback. I'm using ROS_INFO to tell me if the callback is in. In the rqt_graph, it shows that the subscriber node is subscribed to the topic that the publisher is publishing. Using rostopic echo "topic", it is displaying a lots of numbers that i'm presuming that it is the pointcloud.

below is my code.

Publisher

typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;

int main(int argc, char** argv) {
 ros::init (argc, argv, "pub_pcl");
 ros::Publisher pub;
 pub = nh.advertise<sensor_msgs::PointCloud2> ("input_cloud", 1);
 pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2);
 pcl::PCDReader reader;
 reader.read (argv[1], *cloud_blob);
 sensor_msgs::PointCloud2 output;
 pcl_conversions::fromPCL(*cloud_blob, output);
 ros::Rate loop_rate(10);
 while (nh.ok())
 {
   pub.publish (output);
   ros::spinOnce ();
   loop_rate.sleep ();
 }
}

Subscriber

void cloud_callback (const sensor_msgs::PointCloud2ConstPtr& cloud_msg){
 ROS_INFO("inside callback");
}

int main (int argc, char** argv) {
 ros::init (argc, argv, "cloud_sub");
 ros::NodeHandle nh;
 ros::Rate loop_rate(10);
 ros::Subscriber sub;
 while (nh.ok()) {
   sub = nh.subscribe ("input_cloud", 1, cloud_callback);
   ros::spinOnce ();
   loop_rate.sleep ();
 }
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2016-02-04 03:40:11 -0500

updated 2016-02-04 03:43:43 -0500

The output of rostopic echo is likely to be OK. To be sure you can open Rviz and visualize the pointcloud. The subscriber declaration. sub = nh.subscribe ("input_cloud", 1, cloud_callback);

has to be done outside the while loop. Most likely this will fix your issue, as so far I do not see any other problem.

In fact, defining a rate in the subscriber does nothing helpful in this case. That is, you are executing your callbacks queue at 10 hz, but this frequency is already given by the publisher. Even if you have two publishers and only one subscriber, the 10 hz frequency will not be followed in the callback. Therefore, I would require as:

void cloud_callback (const sensor_msgs::PointCloud2ConstPtr& cloud_msg){
     ROS_INFO("inside callback");
}

int main (int argc, char** argv) {
     ros::init (argc, argv, "cloud_sub");
     ros::NodeHandle nh;
     ros::Rate loop_rate(10);
     ros::Subscriber sub;
     sub = nh.subscribe ("input_cloud", 1, cloud_callback);
     ros::spin();
 }
edit flag offensive delete link more

Comments

Hi, Its not obvious to me, but why does the ros subscriber fails in the while loop?

jhlim6 gravatar image jhlim6  ( 2016-02-10 00:20:01 -0500 )edit

@jhlim6 Because you are creating a new subscriber every iteration. AFAIK that is not impossible, it is just very bad practice. And sorry for the delay, I do not get notifications, I do not know why.

Javier V. Gómez gravatar image Javier V. Gómez  ( 2016-05-19 08:13:56 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-04 03:24:34 -0500

Seen: 8,976 times

Last updated: Feb 04 '16