rgbd_assembler_kinect_node.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <ros/ros.h>
00031 
00032 #include <sensor_msgs/Image.h>
00033 #include <sensor_msgs/CameraInfo.h>
00034 #include <stereo_msgs/DisparityImage.h>
00035 #include <sensor_msgs/PointCloud2.h>
00036 
00037 #include "rgbd_assembler/msg_saver.h"
00038 #include "rgbd_assembler/RgbdAssembly.h"
00039 
00040 namespace rgbd_assembler
00041 {
00042 
00043 class RgdbAssemblerKinect
00044 {
00045 
00046 private:
00048   ros::NodeHandle root_nh_;
00050   ros::NodeHandle priv_nh_;
00051 
00052   ros::ServiceServer rgbd_srv_;
00053 
00054   //------------------ Callbacks -------------------
00055 
00057   bool rgbdServiceCallback( RgbdAssembly::Request &request,
00058       RgbdAssembly::Response &response );
00059 
00060   //------------------ Individual processing steps -------
00061   bool assembleSensorData( ros::Duration time_out );
00062 
00063   // ------------------- The Data --------------
00064 
00065   sensor_msgs::Image image_;
00066   stereo_msgs::DisparityImage disparity_image_;
00067   sensor_msgs::CameraInfo cam_info_;
00068   sensor_msgs::PointCloud2 point_cloud_;
00069 
00070   std::string image_topic_;
00071   std::string disparity_image_topic_;
00072   std::string camera_info_topic_;
00073   std::string point_cloud_topic_;
00074 
00075   bool use_disparity_image_;
00076 
00077 public:
00078 
00079   RgdbAssemblerKinect() :
00080     root_nh_(""), priv_nh_("~")
00081   {
00082     rgbd_srv_ = root_nh_.advertiseService(
00083         root_nh_.resolveName("/rgbd_assembly"),
00084         &RgdbAssemblerKinect::rgbdServiceCallback, this);
00085 
00086     priv_nh_.param<std::string>("image_topic", image_topic_, "/head_mount_kinect/depth_registered/points_image");
00087     priv_nh_.param<std::string>("disparity_image_topic", disparity_image_topic_, "/head_mount_kinect/depth_registered/disparity");
00088     priv_nh_.param<std::string>("camera_info_topic", camera_info_topic_, "/head_mount_kinect/depth_registered/camera_info");
00089     priv_nh_.param<std::string>("point_cloud_topic", point_cloud_topic_, "/head_mount_kinect/depth_registered/points");
00090     priv_nh_.param<bool>("use_disparity_image", use_disparity_image_, true);
00091 
00092     ROS_INFO("RGB-D Kinect Assembler node started");
00093   }
00094 
00095   ~RgdbAssemblerKinect()
00096   {
00097   }
00098 
00099 };
00100 
00101 bool RgdbAssemblerKinect::rgbdServiceCallback( RgbdAssembly::Request &request,
00102     RgbdAssembly::Response &response )
00103 {
00104   if (!assembleSensorData(ros::Duration(15.0)))
00105     return false;
00106   ROS_INFO("Assembled Sensor Data");
00107 
00108   if (!priv_nh_.ok())
00109     return false;
00110 
00111   ROS_INFO("RGB-D Kinectassembly has succeeded;");
00112   response.point_cloud = point_cloud_;
00113   response.image = image_;
00114   if(use_disparity_image_) response.disparity_image = disparity_image_;
00115   response.camera_info = cam_info_;
00116   response.result = response.SUCCESS;
00117 
00118   return true;
00119 }
00120 
00121 bool RgdbAssemblerKinect::assembleSensorData( ros::Duration time_out )
00122 {
00123   ROS_INFO("RGB-D Assembly: waiting for messages...");
00124 
00125   MsgSaver<sensor_msgs::Image> recent_image(image_topic_);
00126   MsgSaver<stereo_msgs::DisparityImage> recent_disparity_image(disparity_image_topic_);
00127   MsgSaver<sensor_msgs::CameraInfo> recent_camera_info(camera_info_topic_);
00128   MsgSaver<sensor_msgs::PointCloud2> recent_point_cloud(point_cloud_topic_);
00129   ros::Time start_time = ros::Time::now();
00130 
00131   while (((use_disparity_image_ && !recent_disparity_image.hasMsg()) || !recent_image.hasMsg()
00132       || !recent_camera_info.hasMsg() || !recent_point_cloud.hasMsg())
00133       && priv_nh_.ok())
00134   {
00135     ros::spinOnce();
00136 
00137     ros::Time current_time = ros::Time::now();
00138     if (time_out >= ros::Duration(0) && current_time - start_time >= time_out)
00139     {
00140       ROS_ERROR("Timed out while waiting for sensor data.");
00141       return false;
00142     }
00143     ros::Duration(1.0).sleep();
00144   }
00145   if (!priv_nh_.ok())
00146     return false;
00147 
00148   image_ = *recent_image.getMsg();
00149   if(use_disparity_image_) disparity_image_ = *recent_disparity_image.getMsg();
00150   cam_info_ = *recent_camera_info.getMsg();
00151   point_cloud_ = *recent_point_cloud.getMsg();
00152 
00153   image_.header = disparity_image_.header;
00154 
00155   if ( use_disparity_image_ && (image_.width != disparity_image_.image.width || image_.height != disparity_image_.image.height) )
00156   {
00157     ROS_ERROR( "Size mismatch between image and disparity image!");
00158     return false;
00159   }
00160   
00161   if ( image_.width != point_cloud_.width || image_.height != point_cloud_.height )
00162   {
00163     ROS_ERROR( "Size mismatch between image and point cloud!");
00164     return false;
00165   }
00166   
00167   return true;
00168 }
00169 
00170 } // namespace rgbd_assembler
00171 
00172 int main( int argc, char **argv )
00173 {
00174   ros::init(argc, argv, "rgbd_assembler_kinect_node");
00175   rgbd_assembler::RgdbAssemblerKinect node;
00176   ros::spin();
00177   return 0;
00178 }


rgbd_assembler
Author(s): Jeannette Bohg
autogenerated on Fri Jan 3 2014 12:02:05