robin_people_detection.cpp
Go to the documentation of this file.
00001 
00010 #include <ros/ros.h>
00011 #include <tf/transform_listener.h>
00012 #include <tf/transform_broadcaster.h>
00013 #include <string>
00014 #include <limits>
00015 
00016 tf::StampedTransform zTransform(const tf::StampedTransform transform);
00017 float getCosts(const tf::StampedTransform transform);
00018 int getMinIndex (int n, float *values);
00019 
00020 //ROS Params
00021 std::string base_name; 
00022 
00023 // global Constants
00024 const int MAX_PERSONS = 20;
00025 const std::string DOM_PERSON_NAME = "/dominant_person";
00026 const std::string GROUP_NAME = "/group";
00027 
00028 
00029 
00030 
00031 int main( int argc, char** argv )
00032 {
00033   ros::init(argc, argv, "robin_people_detection");
00034   // Set Markernode and publish rate 10Hz
00035   ros::NodeHandle node("~");
00036   ros::Rate r(10);
00037   
00038   tf::TransformListener li;
00039   tf::TransformBroadcaster br;
00040   std::string torso_name;
00041   std::string err_msg;
00042   std::string parent;
00043   
00044   node.param<std::string>("base_name", base_name,"/base_footprint");
00045   
00046   float costs[MAX_PERSONS] = {std::numeric_limits<float>::max()};
00047    
00048   float sum_x;
00049   float sum_y;
00050   int nTransforms;
00051    
00052   int i;
00053    
00054   // Initialize Transformation
00055   tf::StampedTransform transforms[MAX_PERSONS];
00056   tf::StampedTransform transformZ0;
00057     
00058   ros::Time now;
00059  
00060   
00061   
00062   while (ros::ok()) {
00063     now = ros::Time::now();
00064     sum_x = 0;
00065     sum_y = 0;
00066     nTransforms = 0;
00067        
00068     for(i=1; i <= MAX_PERSONS; i++){
00069       // get name of next torso-tf
00070       std::stringstream ss;
00071       ss << i;
00072       torso_name="torso_"+ss.str();
00073       
00074       costs[i-1] = std::numeric_limits<float>::max();
00075       
00076       if(li.frameExists(torso_name)&&li.getParent(torso_name, now-ros::Duration(1), parent)) {
00077         try{
00078           //get transformation
00079           li.waitForTransform(base_name ,torso_name, now, ros::Duration(1) );
00080           li.lookupTransform(base_name ,torso_name , now, transforms[i-1]);
00081           costs[i-1] = getCosts(transforms[i-1]);
00082           nTransforms++;
00083           sum_x += transforms[i-1].getOrigin().getX();
00084           sum_y += transforms[i-1].getOrigin().getY();          
00085               
00086         }//try{...
00087         catch (tf::TransformException ex) {
00088           ROS_ERROR("%s",ex.what());
00089         }//catch (tf::TransformException ex)
00090       }// if(li.frameExists(torso_name)&&li.getParent(to....
00091     }//for(i=1; i <= MAX_PERSONS; i++)
00092        
00093     // publish dominant person
00094     i = getMinIndex(MAX_PERSONS,costs);
00095     if(i >= 0) {
00096       std::stringstream ss;
00097       ss << (i+1);
00098       torso_name="torso_"+ss.str();
00099         
00100       transformZ0 = zTransform(transforms[i]);
00101       br.sendTransform(tf::StampedTransform(transformZ0, ros::Time::now(), base_name, DOM_PERSON_NAME)); 
00102         
00103     }//if(i >= 0)
00104        
00105     // publish average position (group)
00106     if(nTransforms > 0) {
00107       tf::StampedTransform groupTransf;
00108       groupTransf.setOrigin(tf::Vector3(sum_x/nTransforms,sum_y/nTransforms,0));
00109       groupTransf.setRotation(tf::Quaternion(tf::Vector3(0,0,1),0.0));
00110       br.sendTransform(tf::StampedTransform(groupTransf, ros::Time::now(), base_name, GROUP_NAME));
00111     }//if(nTransforms > 0)
00112        
00113     // Call Callbacks for good measure 
00114     ros::spinOnce();
00115     
00116     r.sleep();
00117   }// while (ros::ok())
00118 }// main
00119 
00120 tf::StampedTransform zTransform(const tf::StampedTransform transform) {
00124   tf::StampedTransform newTransform;
00125   newTransform.setOrigin(tf::Vector3(transform.getOrigin().getX(),transform.getOrigin().getY(),0));
00126   newTransform.setRotation(transform.getRotation());
00127   return newTransform;
00128 }
00129 
00130 float getCosts(const tf::StampedTransform transform) {
00134   return transform.getOrigin().getX()+transform.getOrigin().getY()*0.4;
00135 }
00136 
00137 int getMinIndex (int n, float *values) {
00141   float min = values[0];
00142   int ind = 0;
00143   for(int i=1; i < n; i++) {
00144     if(values[i] < min) {
00145       min = values[i];
00146       ind = i;  
00147     } 
00148   }
00149   
00150   if(min >= std::numeric_limits<float>::max())
00151   {
00152     ind = -1;
00153   }
00154   return ind;
00155 }
00156 
00157 
00158 
00159 
00160 


robin_people_detection
Author(s): Reiter Andreas , Reisenberger Johannes
autogenerated on Fri Aug 28 2015 13:20:58