Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00055
00057 bool rgbdServiceCallback( RgbdAssembly::Request &request,
00058 RgbdAssembly::Response &response );
00059
00060
00061 bool assembleSensorData( ros::Duration time_out );
00062
00063
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 }
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 }