ScavTaskWhiteBoard.cpp
Go to the documentation of this file.
00001 
00002 #include "geometry_msgs/PoseStamped.h"
00003 #include "std_msgs/String.h"
00004 
00005 #include <pcl_ros/point_cloud.h>
00006 #include <pcl/point_types.h>
00007 #include <opencv2/highgui/highgui.hpp>
00008 #include <cv_bridge/cv_bridge.h>
00009 #include <image_transport/image_transport.h>
00010 #include <sensor_msgs/image_encodings.h>
00011 
00012 #include "ScavTaskWhiteBoard.h"
00013 
00014 typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloud;
00015 
00016 sensor_msgs::ImageConstPtr wb_image; 
00017 std::string wb_path_to_image; 
00018 
00019 ScavTaskWhiteBoard::ScavTaskWhiteBoard(ros::NodeHandle *nh, std::string dir) {
00020     this->nh = nh; 
00021     directory = dir;
00022     task_description = "find a person standing near a whiteboard"; 
00023     task_name = "White board"; 
00024     certificate = ""; 
00025 
00026     task_completed = false; 
00027 }
00028 
00029 
00030 /*---------------------------------------------------------------
00031         a           b           1, board near conference room
00032            m                    2, board near 400/500 doors
00033         c           d       
00034     M of coordinates(x,y) is inside the rectangle iff, 
00035     (0 < AM dot AB < AB dot AB) AND (0 < AM dot AC < AC dot AC) 
00036 -----------------------------------------------------------------*/
00037 
00038 bool ScavTaskWhiteBoard::inRectangle(Pose m, Pose a, Pose b, Pose c) 
00039 {
00040     float am_ab = (m.x - a.x) * (b.x - a.x) + (m.y - a.y) * (b.y - a.y);
00041     float ab_ab = (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y);
00042     float am_ac = (m.x - a.x) * (c.x - a.x) + (m.y - a.y) * (c.y - a.y);
00043     float ac_ac = (c.x - a.x) * (c.x - a.x) + (c.y - a.y) * (c.y - a.y);
00044 
00045     return (0 < am_ab and am_ab < ab_ab) and (0 < am_ac and am_ac < ac_ac); 
00046 }
00047 
00048 void ScavTaskWhiteBoard::callback_human_detected(const geometry_msgs::PoseStamped::ConstPtr& msg)
00049 {
00050     Pose a1 = Pose(-30.88, 0.06);
00051     Pose b1 = Pose(-29.48, 0.06);
00052     Pose c1 = Pose(-30.89, -3.16);
00053 
00054     Pose a2 = Pose(-8.25, -5.99);
00055     Pose b2 = Pose(-6.83, -6.05);
00056     Pose c2 = Pose(-8.21, -11.26);
00057 
00058     Pose pose = Pose(msg->pose.position.x, msg->pose.position.y); 
00059 
00060     if (inRectangle(pose, a1, b1, c1) or inRectangle(pose, a2, b2, c2)) {
00061 
00062         ROS_INFO("People detected near a white board, saving picture...");
00063 
00064         cv_bridge::CvImageConstPtr cv_ptr;
00065         cv_ptr = cv_bridge::toCvShare(wb_image, sensor_msgs::image_encodings::BGR8);
00066 
00067         boost::posix_time::ptime curr_time = boost::posix_time::second_clock::local_time(); 
00068         // std::string time_str = boost::posix_time::to_simple_string(curr_time);
00069         std::string time_str = boost::posix_time::to_iso_extended_string(curr_time);
00070 
00071         if (false == boost::filesystem::is_directory(directory)) {
00072             boost::filesystem::path tmp_path(directory);
00073             boost::filesystem::create_directory(tmp_path);
00074         }
00075  
00076         wb_path_to_image = directory + "white_board_" + time_str + ".PNG"; 
00077         cv::imwrite(wb_path_to_image, cv_ptr->image);
00078         search_planner->setTargetDetection(true); // change status to terminate the motion thread
00079 
00080         ROS_INFO_STREAM("Finished saving picture: " << wb_path_to_image);
00081 
00082         task_completed = true; 
00083 
00084     }
00085 }
00086 
00087 void ScavTaskWhiteBoard::callback_image(const sensor_msgs::ImageConstPtr& msg) {
00088     wb_image = msg;
00089 }
00090 
00091 void ScavTaskWhiteBoard::motionThread() {
00092 
00093     std::string path_to_yaml; 
00094 
00095     if (false == nh->hasParam("path_to_search_points"))
00096         ROS_ERROR("path to yaml file of search points not set"); 
00097     ros::param::get("path_to_search_points", path_to_yaml); 
00098 
00099     search_planner = new SearchPlanner(nh, path_to_yaml, tolerance);           
00100 
00101     int next_goal_index;   
00102     ros::Rate r(2);                                                     
00103     while (ros::ok() and r.sleep() and task_completed == false) {
00104         search_planner->moveToNextScene(search_planner->selectNextScene(search_planner->belief, next_goal_index));
00105         search_planner->analyzeScene(0.25*PI, PI/10.0);
00106         search_planner->updateBelief(next_goal_index);
00107     }
00108 }
00109 
00110 void ScavTaskWhiteBoard::visionThread() {
00111 
00112     ros::Subscriber sub_human = nh->subscribe("/segbot_pcl_person_detector/human_poses", 100, 
00113         &ScavTaskWhiteBoard::callback_human_detected, this); 
00114 
00115     image_transport::ImageTransport it(*nh);
00116     image_transport::Subscriber sub_image = it.subscribe("/nav_kinect/rgb/image_raw", 1, 
00117         &ScavTaskWhiteBoard::callback_image, this);
00118 
00119     ros::Rate r(10); 
00120     while (ros::ok() and r.sleep() and task_completed == false) {
00121         ros::spinOnce(); 
00122     }
00123 }
00124 
00125 void ScavTaskWhiteBoard::executeTask(int timeout, TaskResult &result, std::string &record) {
00126 
00127     boost::thread motion( &ScavTaskWhiteBoard::motionThread, this);
00128     boost::thread vision( &ScavTaskWhiteBoard::visionThread, this); 
00129 
00130     ros::Rate r(2);
00131     while (ros::ok() and r.sleep()) {
00132         if (task_completed) {
00133             search_planner->cancelCurrentGoal(); 
00134             break; 
00135         }
00136     }
00137 
00138     motion.detach();
00139     vision.detach(); 
00140     record = wb_path_to_image; 
00141     result = SUCCEEDED; 
00142 }
00143 
00144 void ScavTaskWhiteBoard::stopEarly() {
00145     task_completed = true; 
00146     search_planner->setTargetDetection(true); 
00147 }


bwi_scavenger
Author(s): Shiqi Zhang
autogenerated on Thu Jun 6 2019 17:57:53