# ROS and Kinect data without callbacks

I'd like to get rgb and depth data from a kinect, and I found a little tutorial here: http://wiki.ros.org/cv_bridge/Tutorials/ConvertingBetweenROSImagesAndOpenCVImagesPython. It's fine, but what I'd like is to be able to get the data on demand, and not as whenever the callback is triggered, assuming I won't try to get the data faster than it can be available. I'd appreciate any help - do go easy on the ROS jargon, I'm still learning...Thanks.

You should simply use the callback method since you can have an object in your code that is always updated with the latest measurement. Then you can simply poll the measurement in that object whenever you want (say in some while loop running at a given rate). I notice you are linking to help related to using Python, whereas I am more familiar with coding ROS nodes in C++. If you were doing that then your code might look something like:

#include...

class KinectSubscriber {
...
}

int main(...) {
...
KinectSubscriber MyKinectSub;
...
while (ros::ok()) {
...
data = MyKinectSub.data;
...
}
}


Of course you don't have to access the data at each iteration of the loop, you could use some conditional statements so that the data is polled only under certain circumstances.

• Thanks @agold, I did that too but not sure why it didn't save! – Brian Lynch Dec 2 '15 at 16:55
• Thanks. Only problem is, I'm not sure how the callback gets called with "data". – Ben Dec 2 '15 at 20:28
• The callback function will be inside the class definition, with the whole point being that it is called whenever there is new data available on the topic to which you are subscribing. That means that the object (MyKinectSub in my example) will always have the most recent data in its structure -- so you don't have to think about the callback, it works in the background as needed and then you grab the data from that object when you want. – Brian Lynch Dec 2 '15 at 20:38

Until you learn more about ROS the best approach is to use a callback. You can copy the data to a global variable and use that in your main program if it helps you reason about the data flow better.

If you are trying to do this to reduce network congestion, your best bet is to change the driver parameters to reduce the framerate or resolution. For the openni_camera package the parameters are here. Other solutions exist but require either modifying the ROS drivers or not using ROS at all.

As a side note; if you don't have a specific reason to use ROS, don't use it. It will save some headaches and your program will be faster and simpler besides.

If you like to get rgb and depth or euclidean space coordinates of any point from the kinect sensor then you can try this code in ros node:

void
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
pcl::PointCloud<pcl::PointXYZRGB> output;
pcl::fromROSMsg(*input,output );
pcl::PointXYZ P[479][639];
for(int raw = 0;raw<479;raw++)
{
for(int col = 0;col<639;col++)
{
P[raw][col] = output.at(col,raw);
}
}
sensor_msgs::PointCloud2 cloud;
pcl::toROSMsg(output,cloud);
pub.publish (cloud);
}


Here p[raw][col] is the main structure which contains the rgb data and depth along with x,y coordinate data. To access the rgb data just use p[raw][col].r ... to access depth data use p[raw][col].z. Was this the answer ur looming for.