0
$\begingroup$

Rosanswers logo

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)

image description

Here you can see when I use a class (I subscribe again to the "/camera/depth_registered/points" topic)

image description

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.


Originally posted by Metalzero2 on ROS Answers with karma: 293 on 2015-08-25

Post score: 2

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Subscribe_And_Publish()
{
    cout<<"initialize"<<endl;
    ros::Subscriber sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/depth_registered/points", 5, &Subscribe_And_Publish::callback, this);
} 

This is not ROS related, but a basic C++ misunderstanding.

The variable sub only exists in the local scope of the constructor of your class (Subscribe_And_Publish()). After the constructor has been run, sub will no longer exist, and hence the subscription will not exist anymore.

Make sub a member variable, initialise in the constructor (as you are doing now, but without the ros::Subscriber in front of it) and things should start working.


Edit:

your 'non-class version':

ros::Subscriber sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/depth_registered/points", 5, pointCallback);

and your 'class version':

sub = n.subscribe<pcl::PointCloud<pcl::PointXYZRGB> > ("/camera/camera_modelet_manager/camera/depth_registered/points", 5, &Subscribe_And_Publish::callback, this);

I don't know whether this is a copy/paste error, but it looks like in the first you are subscribing to /camera/depth_registered/points, and in the second to /camera/camera_modelet_manager/camera/depth_registered/points. Those topics are different, and as far as I know the latter does not exist.

Can you make sure you got this right?


Originally posted by gvdhoorn with karma: 86574 on 2015-08-26

This answer was ACCEPTED on the original site

Post score: 5


Original comments

Comment by Metalzero2 on 2015-08-26:
@gvdhoorn You are right (obviously :P ) but I also tried what you suggested. Just din't post to correct code. I just updated the code in my question. Is that what you meant? Still does not work (just tested it).

Comment by gvdhoorn on 2015-08-27:
Why did you change your title? pcl::PointCloud<pcl::PointXYZRGB is not a 'custom message'?

Also: I don't see a topic /camera/depth_registered/points in your rqt_graph screenshot. Can you verify that it exists (using rostopic list)?

Comment by Metalzero2 on 2015-08-27:
@gvdhoorn I din't change the title exactly. I was writing an other question but did not post it. When I closed the tab I guess my browser saves it somewhere and after when I tried to edit question it automatically changed the title :/. Just a small bug of answers.ros.org I guess.

Comment by Metalzero2 on 2015-08-27:
@gvdhoorn Go and see the first of the screenshot (the one where is works, where the subscriber is not in the class). There you can see my node being subscribed to the topic /camera/depth_registered/points

Comment by gvdhoorn on 2015-08-27:
That may be, but the topic should exist in the second sshot as well. Whether or not someone is subscribing to it.

Can you update your question with the complete code? So class def and main program? Afaict what you posted should work.

Comment by Metalzero2 on 2015-09-03:
@gvdhoorn Just updated with all the code. Hope you can find the problem.

Comment by Metalzero2 on 2015-09-04:
@gvdhoorn wow it works. It is a typo although I did try it correctly in my code when I ran it. I am really confused now. The only explanation is that when I tried to do it this way (before I post the question) I did some small typo again. To many hours on the computer :P.

Comment by Metalzero2 on 2015-09-04:
@gvdhoorn I will not edit my question so anybody else that sees it can know what happened. In any case, thanks a lot for you help ^^ .

Comment by gvdhoorn on 2015-09-05:
Good to hear you got things working. Just for my own understanding: was it the wrong topic that you were trying to subscribe to, or was there something else wrong?

Comment by Metalzero2 on 2015-09-05:
@gvdhoorn the correct topic was /camera/depth_registered/points . I did try it in the past but I most have done something wrong without noticed it. But at the end, after I put the correct topic (with the code in my updated question) everything worked correctly.

Comment by Heho on 2021-01-12:
Hi @gvdhoorn. Does multiple ros::Nodehandle within a topic will cause any problem? I notice that each class with subcriber or publisher must have a class member "ros::NodeHandle itsname".

If I use several different class like above, does it necessary to send the ros::NodeHandle in main to each class, which make sure that my topic only has one ros::nodehandle? Thank you!

Comment by gvdhoorn on 2021-01-12:
I'm not sure I understand what you ask.

Topics don't have NodeHandles. Multiple NodeHandles in a single node is also not a problem.

Comment by Heho on 2021-01-12:
@gvdhoorn That's what I means! Thank you for your reply.

$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.