Publish message after filtering subscribed topic
Hi there, I want to subscribe to a PointCloud topic, filter it and publish it later on a different topic in real time. What I have so far is this:
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); //TODO: Avoid global variables. Is there another way?
void subCallback(const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& input){
pcl::copyPointCloud(*input,*cloud);
std::cerr<<"Cloud size is "<< cloud->points.size()<<std::endl;
}
int main (int argc, char** argv){
ros::init(argc, argv, "plane_extraction");
//Necessary pointclouds
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered (new pcl::PointCloud<pcl::PointXYZ>),
cloud_projected (new pcl::PointCloud<pcl::PointXYZ>),
cloud_p (new pcl::PointCloud<pcl::PointXYZ>),
cloud_f (new pcl::PointCloud<pcl::PointXYZ>);
std::vector<pcl::PointCloud<pcl::PointXYZ> > results;
//Only one Node Handle for both subscribing and publishing
ros::NodeHandle nh;
//Publishers array (one for each topic). Each topic represents a segmented plane
std::vector<ros::Publisher> publishers;
while(ros::ok()){
sub=nh.subscribe("/camera/depth/points", 5, subCallback);
/*Filters and stuff
******************
******************
******************
******************/
for(int j=0;j<i;j++){
publishers[j].publish(results[j]);
std:cerr<<"There are "<<j<<" publishers"<<std::endl;
}
ros::spinOnce();
}
return(0);
}
I don't know if the while(ros::ok()) spinOnce() is incorrect or what, but I don't find the way to do it. The message inside the callback appears randomly, and when it does, it gets stuck there, as if it never left the callback.
Thanks in advance.