Subscribe and publish using a Class
Hello all,
I wanted to be able to subscribe to a topic and the from the callback function to publish something. After looking, I saw that generally the best way to do this is by using a class. I found this suggestion here and here.
But there was a problem. I has already successfully subscribe to a topic I wanted (with out the publisher). I copied and pasted the topic from the old subscriber to the new one in the class initialization function. For some reason it does not work. It runs with no errors but it does not subscribe correctly.
Here you can see how it is when I don't use the class (I subscribe to the "/camera/depth_registered/points" topic)
Here you can see when I use a class (I subscribe again to the "/camera/depth_registered/points" topic)
My code is as follows:
class Subscribe_And_Publish
{
private:
ros::Publisher pub;
ros::Subscriber sub;
ros::NodeHandle n;
public:
Subscribe_And_Publish()
{
sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/camera_modelet_manager/camera/depth_registered/points", 5, &Subscribe_And_Publish::callback, this);
pub = n.advertise<read_kinect_01::feature_coordinates_array> ("feature_coordinates", 500);
}
void callback(const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& cloud)
{
...
// Do some stuff
...
}
};//End of class SubscribeAndPublish
int main(int argc, char **argv)
{
ros::init(argc, argv, "front_end");
Subscribe_And_Publish SAPObject;
ros::spin();
return 0;
}
And the part of the code that does the subcription when I don't use a class is:
ros::Subscriber sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/depth_registered/points", 5, pointCallback);
As you can see, in the class I have not yet put the publishing part. For starters, I want to make the subscriber part correctly. Why do I have this problem?
Update 1:
Changed this
ros::Subscriber sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/depth_registered/points", 5, &Subscribe_And_Publish::callback, this);
to this:
sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/depth_registered/points", 5, &Subscribe_And_Publish::callback, this);
Still did not work.
Update 2: Added all my code. The code posted abode does not work. I don't get any errors but it does not subscribe to the topic. When every I so the subscription out of the class (to the same topic) it works fine.