pub.publish(...) won't work [closed]
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.
Did you check if the laser scans are published to /scan correctly?
Have you checked via
rostopic list
,roswtf
andrqt_graph
if everything is correct?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?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?
`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.
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))
@Challen can you share the code where you are broadcasting the information to the topic /scan. That would be helpful to find the problem