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

Revision history [back]

click to hide/show revision 1
initial version
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) and things should start working.

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) now, but without the ros::Subscriber in front of it) and things should start working.

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?