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
00031
00032
00033
00034
00035
00036
00037 #include <ros/ros.h>
00038
00039 #include <string>
00040 #include <vector>
00041
00042 #include "tabletop_object_detector/TabletopDetection.h"
00043 #include "tabletop_object_detector/TabletopSegmentation.h"
00044 #include "tabletop_object_detector/TabletopObjectRecognition.h"
00045
00046 namespace tabletop_object_detector {
00047
00048 class TabletopCompleteNode
00049 {
00050 private:
00051 ros::NodeHandle nh_;
00052 ros::NodeHandle priv_nh_;
00053
00054 ros::ServiceClient seg_srv_;
00055 ros::ServiceClient rec_srv_;
00056 ros::ServiceServer complete_srv_;
00057
00059 bool perform_fit_merge_;
00060
00061 bool serviceCallback(TabletopDetection::Request &request, TabletopDetection::Response &response);
00062
00063 public:
00064 TabletopCompleteNode();
00065 };
00066
00067 TabletopCompleteNode::TabletopCompleteNode() : nh_(""), priv_nh_("~")
00068 {
00069 std::string service_name;
00070
00071 priv_nh_.param<std::string>("segmentation_srv", service_name, "/tabletop_segmentation");
00072 while ( !ros::service::waitForService(service_name, ros::Duration(2.0)) && nh_.ok() )
00073 {
00074 ROS_INFO("Waiting for %s service to come up", service_name.c_str());
00075 }
00076 if (!nh_.ok()) exit(0);
00077 seg_srv_ = nh_.serviceClient<TabletopSegmentation>(service_name, true);
00078
00079 priv_nh_.param<std::string>("recognition_srv", service_name, "/tabletop_object_recognition");
00080 while ( !ros::service::waitForService(service_name, ros::Duration(2.0)) && nh_.ok() )
00081 {
00082 ROS_INFO("Waiting for %s service to come up", service_name.c_str());
00083 }
00084 if (!nh_.ok()) exit(0);
00085 rec_srv_ = nh_.serviceClient<TabletopObjectRecognition>(service_name, true);
00086
00087 complete_srv_ = nh_.advertiseService("object_detection", &TabletopCompleteNode::serviceCallback, this);
00088 ROS_INFO("Tabletop complete node ready");
00089
00090 priv_nh_.param<bool>("perform_fit_merge", perform_fit_merge_, true);
00091 }
00092
00093 bool TabletopCompleteNode::serviceCallback(TabletopDetection::Request &request, TabletopDetection::Response &response)
00094 {
00095 tabletop_object_detector::TabletopSegmentation segmentation_srv;
00096 if (!seg_srv_.call(segmentation_srv))
00097 {
00098 ROS_ERROR("Call to segmentation service failed");
00099 response.detection.result = response.detection.OTHER_ERROR;
00100 return true;
00101 }
00102 response.detection.result = segmentation_srv.response.result;
00103 if (segmentation_srv.response.result != segmentation_srv.response.SUCCESS)
00104 {
00105 ROS_ERROR("Segmentation service returned error %d", segmentation_srv.response.result);
00106 return true;
00107 }
00108 ROS_INFO("Segmentation service succeeded. Detected %d clusters", (int)segmentation_srv.response.clusters.size());
00109 response.detection.table = segmentation_srv.response.table;
00110 response.detection.clusters = segmentation_srv.response.clusters;
00111 if (segmentation_srv.response.clusters.empty() || !request.return_models) return true;
00112
00113 tabletop_object_detector::TabletopObjectRecognition recognition_srv;
00114 recognition_srv.request.table = segmentation_srv.response.table;
00115 recognition_srv.request.clusters = segmentation_srv.response.clusters;
00116 recognition_srv.request.num_models = request.num_models;
00117 recognition_srv.request.perform_fit_merge = perform_fit_merge_;
00118 if (!rec_srv_.call(recognition_srv))
00119 {
00120 ROS_ERROR("Call to recognition service failed");
00121 response.detection.result = response.detection.OTHER_ERROR;
00122 return true;
00123 }
00124 response.detection.models = recognition_srv.response.models;
00125 response.detection.cluster_model_indices = recognition_srv.response.cluster_model_indices;
00126 return true;
00127 }
00128
00129 }
00130
00131 int main(int argc, char **argv)
00132 {
00133 ros::init(argc, argv, "tabletop_node");
00134 ros::NodeHandle nh;
00135 tabletop_object_detector::TabletopCompleteNode node;
00136 ros::spin();
00137 return true;
00138 }