target_object_recognizer.cpp
Go to the documentation of this file.
00001 #include <target_object_recognizer.h>
00002 
00003 TargetObject::TargetObject(geometry_msgs::PoseStamped transed_pose,
00004                            jsk_recognition_msgs::BoundingBox box)
00005   : box_(box), counter_(0)
00006 {
00007   box_.header = transed_pose.header;
00008   box_.pose = transed_pose.pose;
00009 }
00010 
00011 TargetObject::~TargetObject()
00012 {
00013 }
00014 
00015 geometry_msgs::Pose TargetObject::getPose()
00016 {
00017   return box_.pose;
00018 }
00019 
00020 jsk_recognition_msgs::BoundingBox TargetObject::getBox()
00021 {
00022   box_.header.frame_id = "map";
00023   return box_;
00024 }
00025 
00026 void TargetObject::addTargetObject(geometry_msgs::PoseStamped transed_pose,
00027                                    jsk_recognition_msgs::BoundingBox new_box)
00028 {
00029   jsk_recognition_msgs::BoundingBox box_buf; geometry_msgs::Pose buf;
00030   box_buf.pose.position.x = box_.pose.position.x + transed_pose.pose.position.x;
00031   box_buf.pose.position.y = box_.pose.position.y + transed_pose.pose.position.y;
00032   box_buf.pose.orientation.x = box_.pose.orientation.x + transed_pose.pose.orientation.x;
00033   box_buf.pose.orientation.y = box_.pose.orientation.y + transed_pose.pose.orientation.y;
00034   box_buf.pose.orientation.z = box_.pose.orientation.z + transed_pose.pose.orientation.z;
00035   box_buf.pose.orientation.w = box_.pose.orientation.w + transed_pose.pose.orientation.w;
00036   box_buf.dimensions.x = box_.dimensions.x + new_box.dimensions.x;
00037   box_buf.dimensions.y = box_.dimensions.y + new_box.dimensions.y;
00038   box_buf.dimensions.z = box_.dimensions.z + new_box.dimensions.z;
00039   box_.pose.position.x = box_buf.pose.position.x / 2.0;
00040   box_.pose.position.y = box_buf.pose.position.y / 2.0;
00041   box_.pose.orientation.x = box_buf.pose.orientation.x / 2.0;
00042   box_.pose.orientation.y = box_buf.pose.orientation.y / 2.0;
00043   box_.pose.orientation.z = box_buf.pose.orientation.z / 2.0;
00044   box_.pose.orientation.w = box_buf.pose.orientation.w / 2.0;
00045   box_.dimensions.x = box_buf.dimensions.x / 2.0;
00046   box_.dimensions.y = box_buf.dimensions.y / 2.0;
00047   box_.dimensions.z = box_buf.dimensions.z / 2.0;
00048   // box_.pose = transed_pose.pose;
00049   // box_.header = transed_pose.header;
00050   // box_.dimensions = new_box.dimensions;
00051   //  pose_ = new_pose;
00052 }
00053 
00054 int TargetObject::getCounter()
00055 {
00056   return counter_;
00057 }
00058 
00059 void TargetObject::addCounter()
00060 {
00061   counter_++;
00062 }
00063 
00064 TargetObjectRecognizer::TargetObjectRecognizer(ros::NodeHandle nh)
00065   : nh_(nh),
00066     rate_(10)
00067 {
00068   detected_sub_ = nh_.subscribe("/clustering_result", 1, &TargetObjectRecognizer::detectedCallback, this);
00069   recognized_pub_ = nh_.advertise<jsk_recognition_msgs::BoundingBoxArray>("/recognized_result", 1);
00070   robotpose_sub_ = nh_.subscribe("/amcl_pose", 1, &TargetObjectRecognizer::robotPoseCallback, this);
00071 }
00072 
00073 TargetObjectRecognizer::~TargetObjectRecognizer()
00074 {
00075 }
00076 
00077 void TargetObjectRecognizer::detectedCallback(const jsk_recognition_msgs::BoundingBoxArray::ConstPtr &detected_objects)
00078 {
00079   {
00080     boost::mutex::scoped_lock(robot_pose_mutex_);
00081     for (size_t i = 0; i < detected_objects->boxes.size(); ++i) {
00082       bool is_stored(false);
00083       geometry_msgs::PoseStamped in_pose;
00084       in_pose.pose = detected_objects->boxes[i].pose;
00085       in_pose.header = detected_objects->boxes[i].header;
00086       geometry_msgs::PoseStamped out_pose;
00087       try {
00088         tf_.transformPose("map", in_pose, out_pose);
00089       } catch (tf::TransformException &ex) {
00090         ROS_ERROR("%s",ex.what());
00091       }
00092       for (size_t j = 0; j < target_object_candidates_.size(); ++j) {
00093         double dist = this->calcDistance(target_object_candidates_[j].getPose(),
00094                                          out_pose.pose);
00095         if (dist < 1.0) {
00096           target_object_candidates_[j].addCounter();
00097           target_object_candidates_[j].addTargetObject(out_pose,
00098                                                        detected_objects->boxes[i]);
00099           is_stored = true;
00100         }
00101       }
00102       if (! is_stored) {
00103         target_object_candidates_.push_back(TargetObject(out_pose,
00104                                                          detected_objects->boxes[i]));
00105       }
00106     }
00107     jsk_recognition_msgs::BoundingBoxArray box_array;
00108     for (size_t i = 0; i < target_object_candidates_.size(); ++i) {
00109       if(target_object_candidates_[i].getCounter() > 5){
00110         box_array.boxes.push_back(target_object_candidates_[i].getBox());
00111       }
00112     }
00113     ROS_INFO_STREAM("target object candidates : " << target_object_candidates_.size());
00114     box_array.header.stamp = ros::Time::now();
00115     box_array.header.frame_id = "map";
00116     recognized_pub_.publish(box_array);
00117   }
00118 }
00119 
00120 void TargetObjectRecognizer::robotPoseCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& amcl_pose)
00121 {
00122   //robotの位置をサブスクライブして、バッファにいれる(要mutex)
00123   {
00124     boost::mutex::scoped_lock(robot_pose_mutex_);
00125     latest_robot_pose_ = *amcl_pose;
00126     // 最新のロボットの位置と比較してn[m]離れててかつcounterがしきい値以下ならその候補は消す
00127     for (std::vector<TargetObject>::iterator it = target_object_candidates_.begin(); it != target_object_candidates_.end(); ) {
00128       double dist_from_robot = this->calcDistance(it->getPose(), latest_robot_pose_.pose.pose);
00129       if(dist_from_robot >= 5.0 && it->getCounter() < 3)
00130       {
00131         it = target_object_candidates_.erase(it);
00132         continue;
00133       }
00134       it++;
00135     }
00136   }
00137 
00138 }
00139 
00140 void TargetObjectRecognizer::run()
00141 {
00142   while(nh_.ok()){
00143     ros::spinOnce();
00144     rate_.sleep();
00145   }
00146 }
00147 
00148 double TargetObjectRecognizer::calcDistance(geometry_msgs::Pose pose_1, geometry_msgs::Pose pose_2)
00149 {
00150   double dist = sqrt(pow(pose_1.position.x - pose_2.position.x, 2)
00151                      + pow(pose_1.position.y - pose_2.position.y, 2));
00152   return dist;
00153 }
00154 


target_obejct_detector
Author(s): CIR-KIT
autogenerated on Thu Jun 6 2019 20:19:57