$search
00001 /**************************************************************** 00002 * 00003 * Copyright (c) 2010 00004 * 00005 * Fraunhofer Institute for Manufacturing Engineering 00006 * and Automation (IPA) 00007 * 00008 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00009 * 00010 * Project name: care-o-bot 00011 * ROS stack name: cob_environment_perception_intern 00012 * ROS package name: cob_3d_mapping_semantics 00013 * Description: 00014 * 00015 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00016 * 00017 * Author: Waqas Tanveer email:Waqas.Tanveer@ipa.fraunhofer.de 00018 * Supervised by: Georg Arbeiter, email:georg.arbeiter@ipa.fhg.de 00019 * 00020 * Date of creation: 01/2012 00021 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 * 00023 * Redistribution and use in source and binary forms, with or without 00024 * modification, are permitted provided that the following conditions are met: 00025 * 00026 * * Redistributions of source code must retain the above copyright 00027 * notice, this list of conditions and the following disclaimer. 00028 * * Redistributions in binary form must reproduce the above copyright 00029 * notice, this list of conditions and the following disclaimer in the 00030 * documentation and/or other materials provided with the distribution. 00031 * * Neither the name of the Fraunhofer Institute for Manufacturing 00032 * Engineering and Automation (IPA) nor the names of its 00033 * contributors may be used to endorse or promote products derived from 00034 * this software without specific prior written permission. 00035 * 00036 * This program is free software: you can redistribute it and/or modify 00037 * it under the terms of the GNU Lesser General Public License LGPL as 00038 * published by the Free Software Foundation, either version 3 of the 00039 * License, or (at your option) any later version. 00040 * 00041 * This program is distributed in the hope that it will be useful, 00042 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00043 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00044 * GNU Lesser General Public License LGPL for more details. 00045 * 00046 * You should have received a copy of the GNU Lesser General Public 00047 * License LGPL along with this program. 00048 * If not, see <http://www.gnu.org/licenses/>. 00049 * 00050 ****************************************************************/ 00051 //################## 00052 //#### includes #### 00053 00054 //standard includes 00055 #include <iostream> 00056 #include <fstream> 00057 #include <sstream> 00058 00059 //ros includes 00060 #include "ros/ros.h" 00061 #include <rosbag/bag.h> 00062 00063 // ROS message includes 00064 #include <cob_3d_mapping_msgs/GetObjectsOfClass.h> 00065 #include <cob_3d_mapping_msgs/ShapeArray.h> 00066 00067 // PCL includes 00068 #include <pcl/io/pcd_io.h> 00069 #include <pcl/point_types.h> 00070 00071 class TablesClient 00072 { 00073 public: 00074 // Constructor 00075 TablesClient () 00076 00077 { 00078 get_tables_client_ = nh.serviceClient<cob_3d_mapping_msgs::GetObjectsOfClass> ("get_objects_of_class"); 00079 pc2_pub_ = nh.advertise<sensor_msgs::PointCloud2> ("/get_tables_client/point_cloud2", 100); 00080 ros::param::param ("~file_path", file_path_, std::string ("/home/goa-wq/tmp/")); 00081 ros::param::param ("~save_txt_file", save_txt_file_, true); 00082 ros::param::param ("~save_pcd_file", save_pcd_file_, true); 00083 ros::param::param ("~save_bag_file", save_bag_file_, true); 00084 00085 std::cout << "ROS_PARAM: file_path = " << file_path_ << std::endl; 00086 ROS_INFO("ROS_PARAM: save_txt_file = %d",save_txt_file_); 00087 ROS_INFO("ROS_PARAM: save_pcd_file = %d",save_pcd_file_); 00088 ROS_INFO("ROS_PARAM: save_bag_file = %d",save_bag_file_); 00089 } 00090 00091 // Destructor 00092 ~TablesClient () 00093 { 00095 } 00096 00102 void 00103 callService () 00104 { 00105 ROS_INFO("waiting for server to start"); 00106 ros::service::waitForService ("get_objects_of_class"); 00107 //build message 00108 cob_3d_mapping_msgs::GetObjectsOfClassRequest req; 00109 cob_3d_mapping_msgs::GetObjectsOfClassResponse res; 00110 get_tables_client_.call (req, res); 00111 ROS_INFO("Service call finished"); 00112 saveTables (res); 00113 } 00114 00122 void 00123 saveTables (cob_3d_mapping_msgs::GetObjectsOfClass::Response &res) 00124 { 00125 ROS_INFO("Table objects received : %d",res.objects.shapes.size ()); 00126 if (save_txt_file_ || save_pcd_file_) 00127 { 00128 for (unsigned int i = 0; i < res.objects.shapes.size (); i++) 00129 { 00130 if (save_txt_file_ == true && res.objects.shapes.size() > 0) 00131 { 00132 std::ofstream object_file; 00133 std::stringstream ss; 00134 ROS_INFO("Saving Table object: table_ %d",i); 00135 ss << file_path_ << "table_" << i << ".txt"; 00136 object_file.open (ss.str ().c_str ()); 00137 object_file << "#Table Points\n"; 00138 object_file << res.objects.shapes[i] << "\n"; 00139 object_file.close (); 00140 ROS_INFO("Text file saved"); 00141 } 00142 if (save_pcd_file_ == true && res.objects.shapes.size() > 0) 00143 { 00144 for (unsigned int j = 0; j < res.objects.shapes[i].points.size (); j++) 00145 { 00146 ROS_INFO("Saving Table object: table_%d_%d",i,j); 00147 std::stringstream ss1; 00148 ss1 << file_path_ << "table_" << i << "_" << j << ".pcd"; 00149 pcl::PointCloud<pcl::PointXYZ> p; 00150 pcl::fromROSMsg (res.objects.shapes[i].points[j], p); 00151 pcl::io::savePCDFile (ss1.str (), p, false); 00152 ROS_INFO("PCD file saved"); 00153 } 00154 } 00155 00156 } 00157 } 00158 00159 if (save_bag_file_ == true && res.objects.shapes.size() > 0) 00160 { 00161 cob_3d_mapping_msgs::ShapeArray sa;// = res.objects.shapes; 00162 sa.header.stamp = ros::Time (1.0); 00163 00164 sa.header.frame_id = "/map"; 00165 for (unsigned int i = 0; i < res.objects.shapes.size (); i++) 00166 { 00167 sa.shapes.push_back (res.objects.shapes[i]); 00168 /* 00169 for (unsigned int j = 0; j < res.objects.shapes[i].points.size (); j++) 00170 { 00171 sensor_msgs::PointCloud2 pc2; 00172 pc2 = res.objects.shapes[i].points[j]; 00173 pc2.header.frame_id = "/map"; 00174 pc2_pub_.publish(pc2); 00175 ros::Duration(10).sleep(); 00176 } 00177 */ 00178 } 00179 00180 rosbag::Bag bag; 00181 std::stringstream ss1; 00182 ss1 << file_path_ << "table.bag"; 00183 bag.open (ss1.str().c_str(), rosbag::bagmode::Write); 00184 //bag.write("shape_array",ros::Time::now(),sa); 00185 bag.write ("/get_tables_client/shape_array", sa.header.stamp, sa); 00186 bag.close (); 00187 ROS_INFO("BAG file saved"); 00188 //std::cout << sa.header.stamp << std::endl; 00189 } 00190 else 00191 ROS_INFO("No table object : BAG file not saved"); 00192 00193 } 00194 00195 ros::NodeHandle nh; 00196 00197 protected: 00198 ros::ServiceClient get_tables_client_; 00199 ros::Publisher pc2_pub_; 00200 00201 std::string file_path_; 00202 bool save_txt_file_; 00203 bool save_pcd_file_; 00204 bool save_bag_file_; 00205 00206 }; 00207 00208 int 00209 main (int argc, char **argv) 00210 { 00211 ros::init (argc, argv, "client"); 00212 00213 TablesClient client; 00214 client.callService (); 00215 00216 //ros::spin (); 00217 return 0; 00218 } 00219