bow_object_detector_alg_node.cpp
Go to the documentation of this file.
00001 #include "bow_object_detector_alg_node.h"
00002 
00003 BowObjectDetectorAlgNode::BowObjectDetectorAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<BowObjectDetectorAlgorithm>(),
00005   get_grasping_point_aserver_(public_node_handle_, "get_grasping_point"),
00006   EventPointCloudReady("PointCloud ready"),
00007   EventImageMaskReady("Image mask ready"),
00008   EventWrinkledMapReady("Wrinkled map ready")
00009 {
00010         //init class attributes if necessary
00011         //this->loop_rate_ = 2;//in [Hz]
00012         first_pointcloud_ = true;
00013         
00014         // [init publishers]
00015         this->image_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("image_out", 1);
00016         this->pointcloud_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("pointcloud_out", 1);
00017         
00018         // [init subscribers]
00019         this->mask_image_in_subscriber_ = this->public_node_handle_.subscribe("mask_image_in", 1, &BowObjectDetectorAlgNode::mask_image_in_callback, this);
00020         this->pointcloud_in_subscriber_ = this->public_node_handle_.subscribe("pointcloud_in", 1, &BowObjectDetectorAlgNode::pointcloud_in_callback, this);
00021         
00022         // [init services]
00023         
00024         // [init clients]
00025         pointcloud_to_descriptorset_client_ = this->public_node_handle_.serviceClient<iri_perception_msgs::PclToDescriptorSet>("pointcloud_to_descriptorset");
00026         get_vws_client_ = this->public_node_handle_.serviceClient<iri_perception_msgs::DescriptorsToVws>("get_vws");
00027         get_sift_descriptors_client_ = this->public_node_handle_.serviceClient<iri_sift::DescriptorsFromImage>("get_sift_descriptors");
00028         set_background_image_client_ = this->public_node_handle_.serviceClient<iri_perception_msgs::SetImage>("set_background_image");
00029         select_grasp_point_client_ = this->public_node_handle_.serviceClient<iri_bow_object_detector::RefineGraspPoint>("select_grasp_point");
00030         detectObjects_client_ = this->public_node_handle_.serviceClient<iri_bow_object_detector::GeoVwDetection>("detectObjects");
00031         getVwSet_client_ = this->public_node_handle_.serviceClient<iri_sift::GeoVwSetSrv>("getVwSet");
00032         
00033         // [init action servers]
00034         get_grasping_point_aserver_.registerStartCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointStartCallback, this, _1)); 
00035         get_grasping_point_aserver_.registerStopCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointStopCallback, this)); 
00036         get_grasping_point_aserver_.registerIsFinishedCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointIsFinishedCallback, this)); 
00037         get_grasping_point_aserver_.registerHasSucceedCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointHasSucceedCallback, this)); 
00038         get_grasping_point_aserver_.registerGetResultCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointGetResultCallback, this, _1)); 
00039         get_grasping_point_aserver_.registerGetFeedbackCallback(boost::bind(&BowObjectDetectorAlgNode::get_grasping_pointGetFeedbackCallback, this, _1)); 
00040         get_grasping_point_aserver_.start();
00041         
00042         // [init action clients]
00043 }
00044 
00045 BowObjectDetectorAlgNode::~BowObjectDetectorAlgNode(void)
00046 {
00047         // [free dynamic memory]
00048 }
00049 
00050 void BowObjectDetectorAlgNode::mainNodeThread(void)
00051 {
00052         pointcloud_ready_ = false;
00053         image_mask_ready_ = false;
00054         wrinkled_map_ready_ = false;
00055         
00056         // If true it grabs its own pointclouds
00057         // If false it grabs pointclouds from actions
00058         if (false) {
00059                 // subscribe pointcloud
00060                 this->pointcloud_in_subscriber_ = this->public_node_handle_.subscribe("pointcloud_in", 1, &BowObjectDetectorAlgNode::pointcloud_in_callback, this);
00061         }
00062         // else action server will do it
00063         
00064         // wait for pointcloud
00065         while (not pointcloud_ready_) {
00066                 try {
00067                         EventPointCloudReady.wait(1000000); // 1 secs
00068                 }catch(CEventTimeoutException& e){
00069                         ROS_INFO("Waiting for pointcloud...");
00070                 }catch(CException& e){
00071                         ROS_ERROR_STREAM("get_board_info -> main_thread: Exception found waiting" << e.what());
00072                         return;
00073                 }
00074         }
00075         
00076         // pointcloud -> image
00077         last_image_ = ros_pointcloud_to_ros_image(last_pointcloud_);
00078         
00079         // save pcd file with last pointcloud (for debug and new training data)
00080         save_pcd_to_disk(last_pointcloud_);
00081 
00082         // Get sift descriptors
00083         get_sift_descriptors_srv_.request.image = last_image_; 
00084         ROS_INFO("BowObjectDetectorAlgNode:: Sending New Request!"); 
00085         if (get_sift_descriptors_client_.call(get_sift_descriptors_srv_)) 
00086         { 
00087                 ROS_INFO("BowObjectDetectorAlgNode:: Service called successfully!");
00088                 last_sift_descriptors_ = get_sift_descriptors_srv_.response.descriptor_set;
00089         } 
00090         else 
00091         { 
00092                 ROS_ERROR("BowObjectDetectorAlgNode:: Failed to Call Server on topic get_sift_descriptors "); 
00093         }
00094         
00095         // Get sift visual words
00096         get_vws_srv_.request.descriptor_set = last_sift_descriptors_; 
00097         ROS_INFO("BowObjectDetectorAlgNode:: Sending New Request!"); 
00098         if (get_vws_client_.call(get_vws_srv_)) 
00099         { 
00100                 ROS_INFO("BowObjectDetectorAlgNode::  Service called successfully!");
00101                 last_sift_vw_set_ = get_vws_srv_.response.geo_vw_set;
00102         } 
00103         else 
00104         { 
00105                 ROS_ERROR("BowObjectDetectorAlgNode:: Failed to Call Server on topic get_vws "); 
00106         }
00107         
00108         
00109         // wait for mask
00110         while (not image_mask_ready_) {
00111                 try {
00112                         EventImageMaskReady.wait(1000000); // 1 secs
00113                 }catch(CEventTimeoutException& e){
00114                         ROS_INFO("Waiting for image mask...");
00115                 }catch(CException& e){
00116                         ROS_ERROR_STREAM("get_board_info -> main_thread: Exception found waiting" << e.what());
00117                         return;
00118                 }
00119         }
00120         
00121         
00122         detectGraspPoint();
00123         
00124         training();
00125         
00126         // [fill srv structure and make request to the server]
00127         
00128         // [fill msg structures]
00129         
00130         // [fill action structure and make request to the action server]
00131 
00132         // [publish messages]
00133 }
00134 
00135 /*  [subscriber callbacks] */
00136 void BowObjectDetectorAlgNode::mask_image_in_callback(const sensor_msgs::Image::ConstPtr& msg) 
00137 { 
00138         ROS_INFO("BowObjectDetectorAlgNode::mask_image_in_callback: New Message Received"); 
00139         
00140         last_image_mask_ = *msg;
00141         image_mask_ready_ = true;
00142         EventImageMaskReady.set();
00143         
00144         //use appropiate mutex to shared variables if necessary 
00145         //this->alg_.lock(); 
00146         //this->mask_image_in_mutex_.enter(); 
00147 
00148         //std::cout << msg->data << std::endl; 
00149 
00150         //unlock previously blocked shared variables 
00151         //this->alg_.unlock(); 
00152         //this->mask_image_in_mutex_.exit(); 
00153 }
00154 
00155 void BowObjectDetectorAlgNode::pointcloud_in_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00156 { 
00157         ROS_INFO("BowObjectDetectorAlgNode::pointcloud_in_callback: New Message Received"); 
00158         
00159         if (first_pointcloud_) {
00160                 first_pointcloud_ = false;
00161                 set_background_image_srv_.request.image_in = ros_pointcloud_to_ros_image(*msg); 
00162                 ROS_DEBUG("BowObjectDetectorAlgNode:: Sending New Request!"); 
00163                 if (set_background_image_client_.call(set_background_image_srv_)) 
00164                 { 
00165                         ROS_DEBUG_STREAM("BowObjectDetectorAlgNode:: Response: " << set_background_image_srv_.response.success); 
00166                 } 
00167                 else 
00168                 { 
00169                         ROS_WARN("BowObjectDetectorAlgNode:: Failed to Call Server on topic set_background_image "); 
00170                 }
00171         }
00172         else {
00173                 last_pointcloud_ = *msg;
00174                 pointcloud_ready_ = true;
00175                 solution_ready_ = false;
00176                 this->pointcloud_out_publisher_.publish(msg);
00177                 this->image_out_publisher_.publish(ros_pointcloud_to_ros_image(*msg));
00178                 EventPointCloudReady.set();
00179         }
00180         
00181         // unsubscribe
00182         this->pointcloud_in_subscriber_.shutdown();
00183 }
00184 
00185 /*  [service callbacks] */
00186 
00187 /*  [action callbacks] */
00188 void BowObjectDetectorAlgNode::get_grasping_pointStartCallback(const iri_bow_object_detector::GetGraspingPointGoalConstPtr& goal)
00189 { 
00190         alg_.lock(); 
00191         
00192         solution_ready_ = false;
00193         
00194         last_pointcloud_ = goal->pointcloud;
00195         this->pointcloud_out_publisher_.publish(goal->pointcloud);
00196         this->image_out_publisher_.publish(ros_pointcloud_to_ros_image(goal->pointcloud));
00197         
00198         pointcloud_ready_ = true;
00199         EventPointCloudReady.set();
00200         
00201         alg_.unlock(); 
00202 } 
00203 
00204 void BowObjectDetectorAlgNode::get_grasping_pointStopCallback(void) 
00205 { 
00206         alg_.lock(); 
00207         //stop action 
00208         alg_.unlock(); 
00209 } 
00210 
00211 bool BowObjectDetectorAlgNode::get_grasping_pointIsFinishedCallback(void) 
00212 { 
00213         bool ret = false; 
00214 
00215         alg_.lock(); 
00216         if (solution_ready_)
00217                 ret = true;
00218         alg_.unlock(); 
00219 
00220         return ret; 
00221 } 
00222 
00223 bool BowObjectDetectorAlgNode::get_grasping_pointHasSucceedCallback(void) 
00224 { 
00225         bool ret = false; 
00226 
00227         alg_.lock(); 
00228         if (solution_ready_)
00229                 ret = true;
00230         alg_.unlock(); 
00231 
00232         return ret; 
00233 } 
00234 
00235 void BowObjectDetectorAlgNode::get_grasping_pointGetResultCallback(iri_bow_object_detector::GetGraspingPointResultPtr& result) 
00236 { 
00237         pcl::PointCloud<pcl::PointXYZRGB> cloud;
00238         pcl::PointXYZRGB pixel;
00239         
00240         alg_.lock(); 
00241         //update result data to be sent to client 
00242         pcl::fromROSMsg (last_pointcloud_, cloud);
00243         pixel = cloud.at(select_grasp_point_srv_.response.grasp_point.x, select_grasp_point_srv_.response.grasp_point.y); 
00244         
00245         result->grasping_point.x = pixel.x;
00246         result->grasping_point.y = pixel.y;
00247         result->grasping_point.z = pixel.z;
00248         alg_.unlock(); 
00249 } 
00250 
00251 void BowObjectDetectorAlgNode::get_grasping_pointGetFeedbackCallback(iri_bow_object_detector::GetGraspingPointFeedbackPtr& feedback) 
00252 { 
00253         alg_.lock(); 
00254         //keep track of feedback 
00255         //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00256         alg_.unlock(); 
00257 }
00258 
00259 /*  [action requests] */
00260 
00261 void BowObjectDetectorAlgNode::node_config_update(Config &config, uint32_t level)
00262 {
00263         this->alg_.lock();
00264 
00265         this->alg_.unlock();
00266 }
00267 
00268 void BowObjectDetectorAlgNode::addNodeDiagnostics(void)
00269 {
00270 }
00271 
00272 /* main function */
00273 int main(int argc,char *argv[])
00274 {
00275         return algorithm_base::main<BowObjectDetectorAlgNode>(argc, argv, "bow_object_detector_alg_node");
00276 }
00277 
00278 
00279 /* Other functions */
00280 sensor_msgs::Image BowObjectDetectorAlgNode::ros_pointcloud_to_ros_image(sensor_msgs::PointCloud2 ros_cloud)
00281 {
00282         pcl::PointCloud<pcl::PointXYZRGB> pcl_cloud;
00283         pcl::fromROSMsg (ros_cloud, pcl_cloud);
00284         
00285         cv::Mat cv_image = pcl_pointcloud_to_cv_image(pcl_cloud.makeShared());
00286         
00287 //      cv_image = crop_and_resize(cv_image);
00288         
00289         return *cv_image_to_ros_image(cv_image, ros_cloud.header);
00290 }
00291 
00292 void BowObjectDetectorAlgNode::save_pcd_to_disk(sensor_msgs::PointCloud2 ros_cloud)
00293 {
00294   std::string path_out = "/tmp/tmp_cloud.pcd";
00295   pcl::PointCloud<pcl::PointXYZRGB> pcl_cloud;
00296   pcl::fromROSMsg (ros_cloud, pcl_cloud);
00297   pcl::io::savePCDFileASCII (path_out.c_str(), pcl_cloud);
00298 }
00299 
00300 cv::Mat BowObjectDetectorAlgNode::pcl_pointcloud_to_cv_image(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
00301 {
00302         pcl::PointXYZRGB pixel;
00303         cv::Mat cvimage;
00304         uint32_t rgb;
00305         uint8_t r;
00306         uint8_t g;
00307         uint8_t b;
00308 
00309         
00310         cvimage = cv::Mat::zeros(cloud->height, cloud->width, CV_8UC3);//a black image 
00311         for (int i = 0; i < cvimage.rows; i++) {
00312                 //double* Mi = cvimage.ptr<double>(i);
00313                 
00314                 for (int j = 0; j< cvimage.cols; j++) {
00315                         pixel = cloud->at(j,i);
00316                         
00317                         rgb = *reinterpret_cast<int*>(&pixel.rgb);
00318                         r = (rgb >> 16) & 0x0000ff;
00319                         g = (rgb >> 8)  & 0x0000ff;
00320                         b = (rgb)       & 0x0000ff;
00321                         //ROS_INFO_STREAM("Color: " << rgb );
00322                         if (pixel.x == pixel.x) { // if x != NaN
00323                                 //Mi[j] = rgb;
00324                                 // Change order bgr -> rgb 29/08/2012
00325                                 cvimage.at<cv::Vec3b>(i,j)[0] = b;
00326                                 cvimage.at<cv::Vec3b>(i,j)[1] = g;
00327                                 cvimage.at<cv::Vec3b>(i,j)[2] = r;
00328                         }
00329                 }
00330         }
00331         return cvimage;
00332 }
00333 
00334 cv::Mat BowObjectDetectorAlgNode::crop_and_resize(cv::Mat& image)
00335 {
00336         cv::Mat newima = image.colRange(30,image.cols - 30).rowRange(20,image.rows - 20);
00337         cv::resize(newima,newima, cv::Size(640,480));
00338         
00339         return newima;
00340 }
00341 
00342 sensor_msgs::ImagePtr BowObjectDetectorAlgNode::cv_image_to_ros_image(cv::Mat cvimage, std_msgs::Header header)
00343 {
00344         cv_bridge::CvImage out_msg;
00345         out_msg.encoding = sensor_msgs::image_encodings::BGR8;
00346         out_msg.image    = cvimage;
00347         out_msg.header.stamp = header.stamp; // This fails.
00348         return out_msg.toImageMsg();
00349 }
00350 
00351 void BowObjectDetectorAlgNode::detectGraspPoint()
00352 {
00353         detectObjects_srv_.request.geo_vw_sets.clear();
00354         detectObjects_srv_.request.geo_vw_sets.push_back(last_sift_vw_set_); 
00355         detectObjects_srv_.request.image = last_image_; 
00356         detectObjects_srv_.request.mask = last_image_mask_; 
00357         ROS_INFO("BowObjectDetectorAlgNode:: Sending New Request!"); 
00358         if (detectObjects_client_.call(detectObjects_srv_)) 
00359         {  
00360                 ROS_INFO("BowObjectDetectorAlgNode:: Service call successful!");
00361         } 
00362         else 
00363         { 
00364                 ROS_INFO("BowObjectDetectorAlgNode:: Failed to Call service detectObjects on: %s", detectObjects_client_.getService().c_str());
00365                 return;
00366         }
00367         
00368         pointcloud_to_descriptorset_srv_.request.pointcloud = last_pointcloud_; 
00369         ROS_INFO("BowObjectDetectorAlgNode:: Sending New Request!"); 
00370         if (pointcloud_to_descriptorset_client_.call(pointcloud_to_descriptorset_srv_)) 
00371         { 
00372                 ROS_INFO("BowObjectDetectorAlgNode:: Response: OK"); 
00373         } 
00374         else 
00375         { 
00376                 ROS_INFO("BowObjectDetectorAlgNode:: Failed to Call Server on topic pointcloud_to_descriptorset "); 
00377         }
00378         
00379         select_grasp_point_srv_.request.posible_solutions = detectObjects_srv_.response.posible_solutions; 
00380         select_grasp_point_srv_.request.descriptor_set = pointcloud_to_descriptorset_srv_.response.descriptor_set;
00381         select_grasp_point_srv_.request.image = last_image_; 
00382         ROS_INFO("BowObjectDetectorAlgNode:: Sending New Request!"); 
00383         if (select_grasp_point_client_.call(select_grasp_point_srv_)) 
00384         { 
00385 //      ROS_INFO("BowObjectDetectorAlgNode:: Response: %s", select_grasp_point_srv_.response.result); 
00386                 ROS_INFO("BowObjectDetectorAlgNode:: Service call successful!");
00387         } 
00388         else 
00389         { 
00390                 ROS_INFO("BowObjectDetectorAlgNode:: Failed to Call service select_grasp_point on: %s ", select_grasp_point_client_.getService().c_str());
00391                 return;
00392         }
00393         
00394         ROS_INFO_STREAM("Solution is " << select_grasp_point_srv_.response.grasp_point);
00395         
00396         solution_ready_ = true;
00397 }
00398 
00399 void BowObjectDetectorAlgNode::training()
00400 {
00401     
00402 }
00403 


iri_bow_object_detector
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 22:45:45