objprocessing.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 SoftBank Robotics Europe
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015 */
00016 
00017 #include <geometry_msgs/PoseStamped.h>
00018 
00019 #include <moveit_msgs/CollisionObject.h>
00020 
00021 #include "romeo_moveit_actions/objprocessing.hpp"
00022 #include "romeo_moveit_actions/toolsForAction.hpp"
00023 #include "romeo_moveit_actions/toolsForObject.hpp"
00024 
00025 namespace moveit_simple_actions
00026 {
00027 
00028 ObjProcessor::ObjProcessor(ros::NodeHandle *nh,
00029                            Evaluation *evaluation):
00030   nh_(nh),
00031   evaluation_(evaluation),
00032   OBJECT_RECOGNITION_ACTION("/recognize_objects"),
00033   target_frame_("odom"),
00034   object_topic_("/recognized_object_array"),
00035   found_srv_obj_info_(false)
00036 {
00037   std::string mesh_srv_name("get_object_info");
00038   found_srv_obj_info_ = true;
00039   found_obj_reco_client_ = false;
00040 
00041   ros::Time start_time = ros::Time::now();
00042   while (!ros::service::waitForService(mesh_srv_name, ros::Duration(2.0)))
00043   {
00044     ROS_INFO("Waiting for %s service to come up", mesh_srv_name.c_str());
00045     if (!nh_->ok() || ros::Time::now() - start_time >= ros::Duration(5.0))
00046     {
00047       found_srv_obj_info_ = false;
00048       break;
00049     }
00050   }
00051 
00052   if (found_srv_obj_info_)
00053   {
00054     get_model_mesh_srv_ = nh_->serviceClient<GetObjInfo>
00055       (mesh_srv_name, false);
00056   }
00057 
00058   object_sub_ = nh_->subscribe<RecognizedObjArray>(object_topic_,
00059                                                    1,
00060                                                    &ObjProcessor::getRecognizedObjects,
00061                                                    this);
00062 
00063   msg_obj_poses_.header.frame_id = target_frame_;
00064 
00065   pub_obj_poses_ = nh_->advertise<geometry_msgs::PoseArray>("/obj_poses", 1);
00066 }
00067 
00068 bool ObjProcessor::triggerObjectDetection()
00069 {
00070   if(!obj_reco_client_)
00071   {
00072     obj_reco_client_.reset(new ObjRecoActionClient(OBJECT_RECOGNITION_ACTION, false));
00073   }
00074   if (!found_obj_reco_client_)
00075   {
00076     try
00077     {
00078       ROS_DEBUG("Waiting for the Object recognition client");
00079       waitForAction(obj_reco_client_,
00080                     *nh_,
00081                     ros::Duration(5, 0),
00082                     OBJECT_RECOGNITION_ACTION);
00083       ROS_DEBUG("Object recognition client is ready");
00084       found_obj_reco_client_ = true;
00085     }
00086     catch(std::runtime_error &ex)
00087     {
00088       ROS_ERROR("Object recognition action: %s", ex.what());
00089       return false;
00090     }
00091   }
00092 
00093   if (found_obj_reco_client_)
00094   {
00095     object_recognition_msgs::ObjectRecognitionGoal goal;
00096     obj_reco_client_->sendGoal(goal);
00097     if (!obj_reco_client_->waitForResult())
00098     {
00099       ROS_DEBUG_STREAM("Object recognition client returned early");
00100       return false;
00101     }
00102     if (obj_reco_client_->getState() != actionlib::SimpleClientGoalState::SUCCEEDED)
00103     {
00104       ROS_DEBUG_STREAM("Fail: "
00105                        << obj_reco_client_->getState().toString() << ":"
00106                        << obj_reco_client_->getState().getText());
00107       return true;
00108     }
00109   }
00110 //ROS_INFO_STREAM("triggerObjectDetection finished");
00111   return false;
00112 }
00113 
00114 bool ObjProcessor::getMeshFromDB(GetObjInfo &obj_info)
00115 {
00116   if (!found_srv_obj_info_)
00117     return false;
00118 
00119   if ( !get_model_mesh_srv_.call(obj_info) )
00120   {
00121     ROS_ERROR("Get model mesh service service call failed altogether");
00122     return false;
00123   }
00124   return true;
00125 }
00126 
00127 void ObjProcessor::getRecognizedObjects(const RecognizedObjArray::ConstPtr& msg)
00128 {
00129   if (msg->objects.empty())
00130     return;
00131 
00132   std::vector<moveit_msgs::CollisionObject> coll_objects;
00133 
00134   float block_size = 0.03f;
00135   try
00136   {
00137     int obj_id = 0;
00138     RecognizedObjArray::_objects_type::const_iterator it;
00139     for (it = msg->objects.begin(); it != msg->objects.end(); ++it)
00140     {
00141       geometry_msgs::PoseStamped msg_obj_cam_, msg_obj_pose;
00142 
00143       msg_obj_cam_.header = msg->header;
00144       msg_obj_cam_.header.stamp = ros::Time(0);
00145       msg_obj_cam_.pose = it->pose.pose.pose;
00146       listener_.transformPose(target_frame_, msg_obj_cam_, msg_obj_pose);
00147 
00148       if (!evaluation_->inWorkSpace(msg_obj_pose.pose, 0, 0, 1))
00149         continue;
00150 
00151       ROS_INFO_STREAM("xyz: " << msg_obj_pose.pose.position.x << " "
00152                       << msg_obj_pose.pose.position.y << " "
00153                       << msg_obj_pose.pose.position.z);
00154 
00155       GetObjInfo obj_info;
00156       obj_info.request.type = it->type;
00157 
00158       //check if exists
00159       std::stringstream obj_id_str;
00160       obj_id_str << obj_id << "_" << it->type.key;
00161 
00162       int idx = findObj(&blocks_, obj_id_str.str());
00163       //msg_obj_pose.pose.position.z -= 0.01;
00164       if (idx == -1) //if not found, create a new one
00165       {
00166         if (getMeshFromDB(obj_info))
00167         {
00168           blocks_.push_back(MetaBlock(obj_id_str.str(),
00169                                       msg_obj_pose.pose,
00170                                       obj_info.response.information.ground_truth_mesh,
00171                                       it->type,
00172                                       msg->header.stamp,
00173                                       target_frame_));
00174           msg_obj_poses_.poses.push_back(msg_obj_pose.pose);
00175         }
00176         else
00177         {
00178           blocks_.push_back(MetaBlock(obj_id_str.str(),
00179                                       msg_obj_pose.pose,
00180                                       sprimitive::CYLINDER,
00181                                       block_size,
00182                                       block_size*3.0f,
00183                                       0.0f,
00184                                       msg->header.stamp,
00185                                       target_frame_));
00186           msg_obj_poses_.poses.push_back(msg_obj_pose.pose);
00187         }
00188 
00189         coll_objects.push_back(blocks_.back().collObj_);
00190         //ROS_INFO_STREAM("adding an object " << obj_id_str.str());
00191       }
00192       else if ((idx >= 0) && (idx < blocks_.size()))
00193       {
00194         blocks_.at(idx).updatePose(msg_obj_pose.pose);
00195         if (idx >= msg_obj_poses_.poses.size())
00196           msg_obj_poses_.poses.resize(idx+1);
00197         msg_obj_poses_.poses[idx] = msg_obj_pose.pose;
00198 
00199         coll_objects.push_back(blocks_.at(idx).collObj_);
00200         //ROS_INFO_STREAM("updating the object " << obj_id_str.str());
00201       }
00202       ++obj_id;
00203     }
00204   }
00205   catch (tf::TransformException ex)
00206   {
00207     ROS_ERROR("%s",ex.what());
00208     ros::Duration(1.0).sleep();
00209   }
00210 
00211   if (blocks_.size() == 0)
00212     return;
00213 
00214   // publish all objects as collision blocks
00215   if (coll_objects.size() > 0)
00216     current_scene_.addCollisionObjects(coll_objects);
00217   //publishAllCollObj(blocks_);
00218 
00219   ROS_INFO_STREAM("The detected object: "
00220                   << blocks_.front().pose_.position.x << " "
00221                   << blocks_.front().pose_.position.y << " "
00222                   << blocks_.front().pose_.position.z
00223                   << " out of " << coll_objects.size() );
00224 
00225   //ROS_INFO_STREAM("blocks_.size() " << blocks_.size() << " " << msg_obj_poses_.poses.size());
00226 }
00227 
00228 void ObjProcessor::publishAllCollObj(std::vector<MetaBlock> *blocks)
00229 {
00230   if (blocks->empty())
00231     return;
00232 
00233   std::vector<moveit_msgs::CollisionObject> coll_objects;
00234 
00235   std::vector<MetaBlock>::iterator block=blocks->begin();
00236   for (; block != blocks->end(); ++block)
00237     coll_objects.push_back(block->collObj_);
00238 
00239   if (coll_objects.size() > 0)
00240     current_scene_.addCollisionObjects(coll_objects);
00241 }
00242 
00243 MetaBlock * ObjProcessor::getBlock(const int &id)
00244 {
00245   MetaBlock * block;
00246   if (id < blocks_.size())
00247     block = &blocks_[id];
00248   return block;
00249 }
00250 
00251 void ObjProcessor::addBlock(const MetaBlock &block)
00252 {
00253   blocks_.push_back(block);
00254 
00255   blocks_.back().publishBlock(&current_scene_);
00256 }
00257 
00258 //clean the object list based on the timestamp
00259 void ObjProcessor::cleanObjects(const bool list_erase)
00260 {
00261   std::vector<std::string> objects_list = getObjectsOldList(&blocks_);
00262   current_scene_.removeCollisionObjects(objects_list);
00263 
00264   //remove from the memory
00265   if (list_erase)
00266   {
00267     blocks_.clear();
00268     msg_obj_poses_.poses.clear(); //TOCHECK
00269     pub_obj_poses_.publish(msg_obj_poses_);
00270   }
00271 }
00272 }


romeo_moveit_actions
Author(s):
autogenerated on Thu Jun 6 2019 21:57:24