ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

Subscribe and publish using a Class

asked 2015-08-25 13:25:13 -0600

Metalzero2 gravatar image

updated 2015-09-03 18:07:26 -0600

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2015-08-26 01:11:56 -0600

gvdhoorn gravatar image

updated 2015-09-04 02:21:27 -0600

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?

edit flag offensive delete link more

Comments

@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).

Metalzero2 gravatar image Metalzero2  ( 2015-08-26 18:27:05 -0600 )edit

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)?

gvdhoorn gravatar image gvdhoorn  ( 2015-08-27 01:33:39 -0600 )edit

@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.

Metalzero2 gravatar image Metalzero2  ( 2015-08-27 10:03:37 -0600 )edit

@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

Metalzero2 gravatar image Metalzero2  ( 2015-08-27 10:05:36 -0600 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2015-08-27 10:39:50 -0600 )edit

@gvdhoorn Just updated with all the code. Hope you can find the problem.

Metalzero2 gravatar image Metalzero2  ( 2015-09-03 18:08:07 -0600 )edit

@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.

Metalzero2 gravatar image Metalzero2  ( 2015-09-04 15:04:19 -0600 )edit

@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 ^^ .

Metalzero2 gravatar image Metalzero2  ( 2015-09-04 15:05:12 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-08-25 13:25:13 -0600

Seen: 19,101 times

Last updated: Sep 04 '15