pub.publish(...) won't work [closed]

asked 2013-07-18 09:44:22 -0600

Challen gravatar image

updated 2013-08-07 22:14:24 -0600

bit-pirate gravatar image

I am currently trying to learn how to use a laser scanner and point clouds, and I wrote code that should convert a laser scan message into a point cloud. Everything seems to work fine. My node subscribes to the /scan topic, and the laser scan data is converted to a point cloud. The problem is when I try to publish. When I make the call to pub.publisher (pub is the name of my publisher object), nothing happens. If I try to echo the /scan_cloud topic (the one I'm publishing to), nothing appears. Here is my code:

#include <ros/ros.h>
#include <sensor_msgs/PointCloud.h>
#include <laser_geometry/laser_geometry.h>
#include <tf/transform_listener.h>

laser_geometry::LaserProjection projector;
sensor_msgs::PointCloud _cloud;
bool firstMsg = 0;

void convert(const sensor_msgs::LaserScan::ConstPtr &scan) {
  sensor_msgs::PointCloud cloud;
  tf::TransformListener listener;
  projector.projectLaser(*scan, cloud);
  _cloud = cloud;
  firstMsg = 1;
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "scan_test");
  ros::NodeHandle nh;
  ros::Subscriber sub = nh.subscribe("scan", 1000, convert);
  ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud>("scan_cloud", 1000);
  ros::Rate loop_rate(10);
  while (ros::ok()) {
    std::cout << _cloud << std::endl;
    pub.publish(_cloud);
    ros::spinOnce();
    loop_rate.sleep();
  };
  return 0;
}

Thanks.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by tfoote
close date 2014-07-07 00:03:59.868022

Comments

Did you check if the laser scans are published to /scan correctly?

BennyRe gravatar image BennyRe  ( 2013-07-18 11:07:05 -0600 )edit

Have you checked via rostopic list, roswtf and rqt_graph if everything is correct?

felix k gravatar image felix k  ( 2013-07-19 02:04:13 -0600 )edit
1

I just checked, and it works: your node publishes empty point clouds on /scan_cloud (I didn't bother publishing anything on /scan). So what is your problem?

Martin Günther gravatar image Martin Günther  ( 2013-07-19 02:05:23 -0600 )edit

I guess my question is why is the cloud empty? I have checked that my scan data is coming in on the scan topic, and that the callback function is being called. So what would cause the cloud to not form completely?

Challen gravatar image Challen  ( 2013-08-07 12:35:42 -0600 )edit

`projector.projectLaser(*scan, cloud)` -> you aren't giving the cloud by value so the local var wouldn't be modified are you? Throw some printfs in there.

felix k gravatar image felix k  ( 2013-08-07 21:55:51 -0600 )edit

There's no pointer to cloud (sensor_msgs::PointCloud cloud;) So I think it is passed correctly. (void projectLaser (const sensor_msgs::LaserScan& scan_in, sensor_msgs::PointCloud& cloud_out, double range_cutoff = -1.0, int channel_options = channel_option::Default))

BennyRe gravatar image BennyRe  ( 2013-08-07 22:24:23 -0600 )edit

@Challen can you share the code where you are broadcasting the information to the topic /scan. That would be helpful to find the problem

Gudjohnson gravatar image Gudjohnson  ( 2013-08-08 02:10:46 -0600 )edit