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

how to pass pointcloud from the callback function in subscriber

asked 2013-02-06 21:03:42 -0500

sai gravatar image

updated 2016-10-24 09:09:45 -0500

ngrennan gravatar image

Aim-Subscribe to pointcloud from kinect and access it from main() function

I created a class and callback function as its member function.

I am able to subscribe to the pointcloud data from kinect using

 ros::Subscriber sub = nh.subscribe ("/camera/depth_registered/points", 3,&pass_cloud::callback,&cloud1);

I am the changing the member value by the callback but i am not able to see it working.

You can find my code here..Please let me know what changes can be done https://dl.dropbox.com/u/95042389/sampl.cpp

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-02-06 23:09:53 -0500

updated 2013-02-07 00:33:29 -0500

Avoid global variables. it is bad practice. here is how you could do it:

class pass_cloud
{
public:
pass_cloud (ros::NodeHandle& nh) : nh_(nh), i(0)
{
  sub = nh_.subscribe ("/camera/depth_registered/points",     3,&pass_cloud::callback,this);
} // your constructor and store the passed node handle (nh) to your class attribute nh_ 

void callback (const pcl::PointCloud& cloud) 
{
 input= cloud; // copy the variable that the callback passes in to you class variable (attribute) input
 i=i+1;
}

process()
{
//You might want to add an if statement here to only print if the input is not empty, I am not sure how but something like this if(input is not empty){
std::cout << " " << input.points[i].x << " " << input.points[i].y <<  " " << input.points[i].z << std::endl; std::cout<<"reached here"<<std::endl; 

}

void spin ()
{
 ros::Rate rate (30);
 while (ros::ok ())
 {
 ros::spinOnce ();
 rate.sleep ();
 }
}

protected: // Your class attributes
ros::NodeHandle nh_;
pcl::PointCloud& input;
cloud ros::Subscriber sub;
int i;
}; // end of class

int main(int argc, char **argv)
{

ros::init(argc, argv, "pointcloud_node");
ros::NodeHandle nh;

pass_cloud* node = 0;

node = new pass_cloud(nh);

node->spin (); 
return 0;
}

Hope this helps!

edit flag offensive delete link more

Comments

This is very nice but I have some basic questions to ask. could you give your email id

sai gravatar image sai  ( 2013-02-06 23:15:51 -0500 )edit

I am not a ROS expert as I only started learning ROS less than 2 months ago. But I will be happy to help if can. My email is khalid.ayousif@hotmail.com

K_Yousif gravatar image K_Yousif  ( 2013-02-06 23:32:35 -0500 )edit

Hi, if possible , could you see the code at this link and tell what might be wrong https://dl.dropbox.com/u/95042389/sampl.cpp

sai gravatar image sai  ( 2013-02-06 23:44:40 -0500 )edit

In your code, it is being stuck in a for loop, without updating your cloud. I do not think you need a for loop since ros::spin() is already constantly looping until you press ctrl+c, I have updated my code in my answer to your case. Copy and paste it and see if it works. let me know

K_Yousif gravatar image K_Yousif  ( 2013-02-07 00:22:08 -0500 )edit
0

answered 2013-02-06 22:21:48 -0500

updated 2013-02-06 22:47:11 -0500

If global variable doesnt work then you can use a class. Declare callback function as method of class and then u can access the pointcloud using class object.

edit flag offensive delete link more

Comments

I tried. that first but i can only access it inside the scope of call back function..any suggestion

sai gravatar image sai  ( 2013-02-06 22:23:41 -0500 )edit

I also read in another post that using global variables is not a good way

sai gravatar image sai  ( 2013-02-06 22:38:37 -0500 )edit

That is what I tried and you can find in the link provided in the question.

sai gravatar image sai  ( 2013-02-06 23:00:42 -0500 )edit

Question Tools

Stats

Asked: 2013-02-06 21:03:42 -0500

Seen: 2,322 times

Last updated: Feb 07 '13