table_extraction_node.cpp
Go to the documentation of this file.
00001 
00064 //##################
00065 //#### includes ####
00066 #include <algorithm>
00067 
00068 // ROS includes
00069 #include <ros/ros.h>
00070 #include <rosbag/bag.h>
00071 #include <dynamic_reconfigure/server.h>
00072 
00073 // ros message includes
00074 #include <sensor_msgs/PointCloud.h>
00075 #include <visualization_msgs/Marker.h>
00076 #include <visualization_msgs/MarkerArray.h>
00077 
00078 // PCL includes
00079 #include <pcl/point_cloud.h>
00080 #include <pcl/point_types.h>
00081 #include <pcl/ros/conversions.h>
00082 #include <pcl/io/pcd_io.h>
00083 
00084 //internal includes
00085 #include <cob_3d_mapping_msgs/ShapeArray.h>
00086 #include <cob_3d_mapping_msgs/GetGeometryMap.h>
00087 #include <cob_3d_mapping_msgs/GetTables.h>
00088 #include <cob_3d_mapping_semantics/table_extraction.h>
00089 #include <cob_3d_mapping_semantics/table_extraction_nodeConfig.h>
00090 #include <cob_3d_mapping_common/ros_msg_conversions.h>
00091 
00092 using namespace cob_3d_mapping;
00093 
00094 class TableExtractionNode
00095 {
00096 public:
00097 
00098   // Constructor
00099   TableExtractionNode () :
00100     target_frame_id_ ("/map")
00101   {
00102     config_server_.setCallback(boost::bind(&TableExtractionNode::dynReconfCallback, this, _1, _2));
00103 
00104     sa_sub_ = n_.subscribe ("shape_array", 10, &TableExtractionNode::callbackShapeArray, this);
00105     sa_pub_ = n_.advertise<cob_3d_mapping_msgs::ShapeArray> ("shape_array_pub", 1);
00106 
00107     get_tables_server_ = n_.advertiseService ("get_tables", &TableExtractionNode::getTablesService, this);
00108   }
00109 
00110   // Destructor
00111   ~TableExtractionNode ()
00112   {
00114   }
00115 
00126   void
00127   dynReconfCallback(cob_3d_mapping_semantics::table_extraction_nodeConfig &config, uint32_t level)
00128   {
00129     ROS_INFO("[table_extraction]: received new parameters");
00130     target_frame_id_ = config.target_frame_id;
00131     te_.setNormalBounds (config.tilt_angle);
00132     te_.setHeightMin (config.height_min);
00133     te_.setHeightMax (config.height_max);
00134     te_.setAreaMin (config.area_min);
00135     te_.setAreaMax (config.area_max);
00136   }
00137 
00146   void
00147   callbackShapeArray (const cob_3d_mapping_msgs::ShapeArray::ConstPtr sa_ptr)
00148   {
00149     if(sa_ptr->header.frame_id != target_frame_id_)
00150     {
00151       ROS_ERROR("Frame IDs do not match, aborting...");
00152       return;
00153     }
00154     cob_3d_mapping_msgs::ShapeArray tables;
00155     tables.header = sa_ptr->header;
00156     tables.header.frame_id = target_frame_id_ ;
00157     processMap(*sa_ptr, tables);
00158     sa_pub_.publish (tables);
00159     ROS_INFO("Found %u tables", (unsigned int)tables.shapes.size());
00160   }
00161 
00162 
00171   bool
00172   getTablesService (cob_3d_mapping_msgs::GetTablesRequest &req,
00173                     cob_3d_mapping_msgs::GetTablesResponse &res)
00174   {
00175     ROS_INFO("table detection started...");
00176 
00177     cob_3d_mapping_msgs::ShapeArray sa;
00178     if (getMapService (sa))
00179     {
00180         if(sa.header.frame_id != target_frame_id_)
00181         {
00182                 ROS_ERROR("Frame IDs do not match, aborting...");
00183                 return false;
00184         }
00185       processMap(sa, res.tables);
00186       res.tables.header = sa.header;
00187       ROS_INFO("Found %u tables", res.tables.shapes.size());
00188       return true;
00189     }
00190     else
00191       return false;
00192   }
00193 
00199   bool
00200   getMapService (cob_3d_mapping_msgs::ShapeArray& sa)
00201   {
00202     ROS_INFO("Waiting for service server to start.");
00203     ros::service::waitForService ("get_geometry_map", 10); //will wait for infinite time
00204 
00205     ROS_INFO("Server started, polling map.");
00206 
00207     //build message
00208     cob_3d_mapping_msgs::GetGeometryMapRequest req;
00209     cob_3d_mapping_msgs::GetGeometryMapResponse res;
00210 
00211     if (ros::service::call ("get_geometry_map", req, res))
00212     {
00213       ROS_INFO("Service call finished.");
00214     }
00215     else
00216     {
00217       ROS_INFO("Service call failed.");
00218       return 0;
00219     }
00220     sa = res.map;
00221     return true;
00222   }
00223 
00232   void
00233   processMap(const cob_3d_mapping_msgs::ShapeArray& sa, cob_3d_mapping_msgs::ShapeArray& tables)
00234   {
00235     for (unsigned int i = 0; i < sa.shapes.size (); i++)
00236     {
00237       Polygon::Ptr poly_ptr (new Polygon());
00238       fromROSMsg(sa.shapes[i], *poly_ptr);
00239       te_.setInputPolygon(poly_ptr);
00240       if (te_.isTable())
00241       {
00242         poly_ptr->color_[0] = 1;
00243         poly_ptr->color_[1] = 0;
00244         poly_ptr->color_[2] = 0;
00245         poly_ptr->color_[3] = 1;
00246         cob_3d_mapping_msgs::Shape s;
00247         s.header = sa.header;
00248         s.header.frame_id = target_frame_id_;
00249         toROSMsg(*poly_ptr,s);
00250         ROS_INFO("getTablesService: Polygon[%d] converted to shape",i);
00251         tables.shapes.push_back (s);
00252       }
00253     }
00254   }
00255 
00256   ros::NodeHandle n_;
00257 
00258 protected:
00259   ros::Subscriber sa_sub_;
00260   ros::Publisher sa_pub_;
00261   ros::ServiceServer get_tables_server_;
00262 
00266   dynamic_reconfigure::Server<cob_3d_mapping_semantics::table_extraction_nodeConfig> config_server_;
00267 
00268   TableExtraction te_;
00269 
00270   std::string target_frame_id_;
00271 };
00272 
00273 int
00274 main (int argc, char** argv)
00275 {
00276   ros::init (argc, argv, "table_extraction_node");
00277 
00278   TableExtractionNode sem_exn_node;
00279   ros::spin ();
00280 }


cob_3d_mapping_semantics
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:34