camera_object_learner_server_alg_node.cpp
Go to the documentation of this file.
00001 #include "camera_object_learner_server_alg_node.h"
00002 
00003 CameraObjectLearnerServerAlgNode::CameraObjectLearnerServerAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<CameraObjectLearnerServerAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   //this->loop_rate_ = 2;//in [Hz]
00008   this->received_camera_info = false;
00009   this->received_person      = false;
00010   this->init = false;
00011   // [init publishers]
00012   
00013   // [init subscribers]
00014   this->camera_info_subscriber_ = this->public_node_handle_.subscribe("camera_info", 100, &CameraObjectLearnerServerAlgNode::camera_info_callback, this);
00015   this->people_subscriber_ = this->public_node_handle_.subscribe("people", 100, &CameraObjectLearnerServerAlgNode::people_callback, this);
00016   
00017   // [init services]
00018   this->init_server_ = this->public_node_handle_.advertiseService("init", &CameraObjectLearnerServerAlgNode::initCallback, this);
00019   
00020   // [init clients]
00021   
00022   // [init action servers]
00023   
00024   // [init action clients]
00025 }
00026 
00027 CameraObjectLearnerServerAlgNode::~CameraObjectLearnerServerAlgNode(void)
00028 {
00029   // [free dynamic memory]
00030 }
00031 
00032 void CameraObjectLearnerServerAlgNode::mainNodeThread(void)
00033 {
00034   // [fill msg structures]
00035   
00036   // [fill srv structure and make request to the server]
00037   
00038   // [fill action structure and make request to the action server]
00039 
00040   // [publish messages]
00041 }
00042 
00043 /*  [subscriber callbacks] */
00044 
00045 void CameraObjectLearnerServerAlgNode::camera_info_callback(const sensor_msgs::CameraInfo::ConstPtr& msg) 
00046 { 
00047   static bool first=true;
00048   ROS_DEBUG("CameraObjectLearnerServerAlgNode::camera_info_callback: New Message Received"); 
00049 
00050   //use appropiate mutex to shared variables if necessary 
00051   //this->alg_.lock(); 
00052   //this->camera_info_mutex_.enter(); 
00053 
00054   //std::cout << msg->data << std::endl;
00055   if(first)
00056   {
00057     ROS_INFO("CameraObjectLearnerServerAlgNode::camera_info_callback: camera_info loaded!"); 
00058     this->camera_model.fromCameraInfo(msg);
00059     this->received_camera_info = true;
00060     first=false;
00061   }
00062 
00063   //unlock previously blocked shared variables 
00064   //this->alg_.unlock(); 
00065   //this->camera_info_mutex_.exit(); 
00066 }
00067 
00068 void CameraObjectLearnerServerAlgNode::people_callback(const iri_perception_msgs::detectionArray::ConstPtr& msg) 
00069 { 
00070   ROS_DEBUG("CameraObjectLearnerServerAlgNode::people_callback: New Message Received"); 
00071 
00072   //use appropiate mutex to shared variables if necessary 
00073   this->alg_.lock(); 
00074   //this->people_mutex_.enter(); 
00075 
00076   //std::cout << msg->data << std::endl;
00077   double mindist = 100;
00078   double dist    = 100;
00079   for(unsigned int i=0; i<msg->detection.size(); i++)
00080   {
00081     double x = msg->detection[i].pose.pose.position.x;
00082     double y = msg->detection[i].pose.pose.position.y;
00083     double z = msg->detection[i].pose.pose.position.z;
00084     //TODO: subscribe to tracker and choose person with a subscriber to target_id
00085     //for the moment: chosen nearest person within limits
00086     dist = x*x+y*y;
00087     if(dist < mindist*mindist && x>0.5 && x<1.5 && y>-0.25 && y<0.25)
00088     {
00089       mindist = dist;
00090       this->init_point.header  = msg->header;
00091       this->init_point.point.x = x;
00092       this->init_point.point.y = y;
00093       this->init_point.point.z = z;
00094     }
00095   }
00096   if(mindist==100)
00097     this->received_person = false;
00098   else
00099     this->received_person = true;
00100   //unlock previously blocked shared variables 
00101   this->alg_.unlock(); 
00102   //this->people_mutex_.exit(); 
00103 }
00104 
00105 /*  [service callbacks] */
00106 bool CameraObjectLearnerServerAlgNode::initCallback(iri_camera_object_learner_server::init::Request &req, iri_camera_object_learner_server::init::Response &res) 
00107 { 
00108   ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: New Request Received!"); 
00109 
00110   //use appropiate mutex to shared variables if necessary 
00111   this->alg_.lock();
00112   res.x      = 0;
00113   res.y      = 0;
00114   res.width  = 0;
00115   res.height = 0;
00116   //this->init_mutex_.enter();
00117   if(this->init)
00118   {
00119     if(this->received_camera_info && this->received_person)
00120     {
00121       if(this->compute_init_rect())
00122       {
00123         res.x      = this->init_rect.x;
00124         res.y      = this->init_rect.y;
00125         res.width  = this->init_rect.width;
00126         res.height = this->init_rect.height;
00127         ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: res x,y,w,h: %d, %d, %d, %d", res.x, res.y, res.width, res.height);
00128       }
00129       else
00130       {
00131         ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: Rectangle not loaded!"); 
00132       }
00133     }
00134     else
00135     {
00136       ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: camera_info or person_detection not received yet!"); 
00137     }
00138   }
00139   else
00140   {
00141     ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: init param false!"); 
00142   }
00143   //if(this->alg_.isRunning()) 
00144   //{ 
00145     //ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: Processin New Request!"); 
00146     //do operations with req and output on res 
00147     //res.data2 = req.data1 + my_var; 
00148   //} 
00149   //else 
00150   //{ 
00151     //ROS_INFO("CameraObjectLearnerServerAlgNode::initCallback: ERROR: alg is not on run mode yet."); 
00152   //} 
00153 
00154   //unlock previously blocked shared variables 
00155   this->alg_.unlock(); 
00156   //this->init_mutex_.exit(); 
00157 
00158   return true; 
00159 }
00160 
00161 /*  [action callbacks] */
00162 
00163 /*  [action requests] */
00164 
00165 void CameraObjectLearnerServerAlgNode::node_config_update(Config &config, uint32_t level)
00166 {
00167   this->alg_.lock();
00168   this->init = config.init;
00169   this->window_width  = config.window_width;
00170   this->window_height = config.window_height;
00171   this->window_offset = config.window_offset;
00172   this->alg_.unlock();
00173 }
00174 
00175 void CameraObjectLearnerServerAlgNode::addNodeDiagnostics(void)
00176 {
00177 }
00178 
00179 /* main function */
00180 int main(int argc,char *argv[])
00181 {
00182   return algorithm_base::main<CameraObjectLearnerServerAlgNode>(argc, argv, "camera_object_learner_server_alg_node");
00183 }
00184 
00185 bool CameraObjectLearnerServerAlgNode::compute_init_rect()
00186 {
00187   ROS_INFO("CameraObjectLearnerServerAlgNode::compute_init_rect");
00188   bool ok = false;
00189   try
00190   {
00191     tf::StampedTransform laser_to_camera_transf;
00192     ros::Time now = ros::Time::now();
00193     ros::Duration timeout(5.0f);
00194     std::string target_frame = this->camera_model.tfFrame();
00195     std::string source_frame = init_point.header.frame_id;
00196     bool tf_exists = tf_listener.waitForTransform(target_frame, source_frame, now, timeout, ros::Duration(0.01));
00197     if(tf_exists)
00198     {
00199       //transform laser detection point from point_frame to camera_frame
00200       tf_listener.lookupTransform(target_frame, source_frame, now, laser_to_camera_transf);
00201       //ROS_INFO("laser_point: %f, %f, %f", this->init_point.point.x,this->init_point.point.y,this->init_point.point.z);
00202       tf::Point laser_point(this->init_point.point.x,this->init_point.point.y,this->init_point.point.z);
00203       tf::Point cam_point(laser_to_camera_transf * laser_point);
00204       //transform camera point to image point
00205       //window width and height in meters
00206       double width_m         = this->window_width; //0.6;
00207       double height_m        = this->window_height; //0.9;
00208       double height_offset_m = this->window_offset; //0.4; //distance from leg point to bottom of window
00209       ROS_INFO("cam_point: %f, %f, %f", cam_point.x(), cam_point.y(), cam_point.z());
00210       //cv::Point3d cv_cam_point(cam_point.x(),             cam_point.y(), cam_point.z());  
00211       cv::Point3d cv_cam_point0(cam_point.x(),             cam_point.y()-height_offset_m-height_m/2.0, cam_point.z()); //0 center 
00212       cv::Point3d cv_cam_point1(cam_point.x()-width_m/2.0, cam_point.y()-height_offset_m-height_m,     cam_point.z()); //1 top left 
00213       cv::Point3d cv_cam_point2(cam_point.x()+width_m/2.0, cam_point.y()-height_offset_m,              cam_point.z()); //2 bottom right
00214       //cv::Point2d image_point = this->camera_model.project3dToPixel(cv_cam_point);
00215       cv::Point2d image_point0 = this->camera_model.project3dToPixel(cv_cam_point0);
00216       cv::Point2d image_point1 = this->camera_model.project3dToPixel(cv_cam_point1);
00217       cv::Point2d image_point2 = this->camera_model.project3dToPixel(cv_cam_point2);
00218       //ROS_INFO("image_point: %f, %f", image_point.x, image_point.y);
00219       //ROS_INFO("image_point0: %f, %f", image_point0.x, image_point0.y);
00220       //ROS_INFO("image_point1: %f, %f", image_point1.x, image_point1.y);
00221       //ROS_INFO("image_point2: %f, %f", image_point2.x, image_point2.y);
00222       int width  = abs(image_point1.x - image_point2.x);
00223       int height = abs(image_point1.y - image_point2.y);
00224       this->init_rect.width  = width;
00225       this->init_rect.height = height;
00226       this->init_rect.x      = image_point0.x;
00227       this->init_rect.y      = image_point0.y;
00228       ok = true;
00229     }
00230     else
00231     {
00232       ROS_INFO("CameraObjectLearnerServerAlgNode::compute_init_rect::No transform: %s-->%s", target_frame.c_str(), source_frame.c_str());
00233     }
00234   }
00235   catch (tf::TransformException& ex)
00236   {
00237     ROS_WARN("TF exception:\n%s", ex.what());
00238   }
00239   return ok;
00240 }


iri_camera_object_learner_server
Author(s): fherrero
autogenerated on Fri Dec 6 2013 20:57:32