laser_people_detection_alg_node.cpp
Go to the documentation of this file.
00001 #include "laser_people_detection_alg_node.h"
00002 
00003 LaserPeopleDetectionAlgNode::LaserPeopleDetectionAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<LaserPeopleDetectionAlgorithm>(),
00005   new_laser_event_id_("new_laser_event"),
00006   tf_listener_(ros::Duration(10.f)),
00007   target_frame("/base_link")
00008 {
00009   //init class attributes if necessary
00010   this->loop_rate_ = 50;//in [Hz]
00011   this->init();
00012 
00013   // [init publishers]
00014   this->peopleMarkers_array_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("peopleMarkers_array", 100);
00015   this->people_publisher_ = this->public_node_handle_.advertise<iri_perception_msgs::detectionArray>("people", 100);
00016 
00017   // [init subscribers]
00018   this->scan_subscriber_ = this->public_node_handle_.subscribe("scan", 100, &LaserPeopleDetectionAlgNode::scan_callback, this);
00019 
00020   // [init services]
00021 
00022   // [init clients]
00023 
00024   // [init action servers]
00025 
00026   // [init action clients]
00027 
00028 }
00029 
00030 LaserPeopleDetectionAlgNode::~LaserPeopleDetectionAlgNode(void)
00031 {
00032   // [free dynamic memory]
00033 }
00034 
00035 void LaserPeopleDetectionAlgNode::init()
00036 {
00037   this->selectPosesFrame=false;
00038   this->selectScanFrame=false;
00039   this->scanFrame="/front_laser";
00040   this->markerWidth=0.5;
00041   this->markerHeight=0.2;
00042   this->markerR=1.0;
00043   this->markerG=0.0;
00044   this->markerB=0.0;
00045   this->markerA=0.75;
00046 
00047   this->filterPosesMode=false;
00048   this->filterR=0.0;
00049   this->filterX=0.0;
00050   this->filterY=0.0;
00051   
00052   event_server_ = CEventServer::instance();
00053   event_server_->create_event(new_laser_event_id_);
00054 
00055   public_node_handle_.getParam("posesFrame", this->target_frame);
00056   public_node_handle_.getParam("scanFrame", this->scanFrame);
00057 }
00058 
00059 void LaserPeopleDetectionAlgNode::mainNodeThread(void)
00060 {
00061   if(event_server_->event_is_set(new_laser_event_id_))
00062   {
00063     event_server_->reset_event(new_laser_event_id_);
00064     scan_mutex_.enter();
00065     //Detection iteration
00066     this->detectionArray_msg_.detection.clear();
00067     this->alg_.iteration(this->currentScan, this->detectionArray_msg_);
00068     scan_mutex_.exit();
00069 
00070     //Filter people (by radius or xy)
00071     if(this->filterPosesMode)
00072       this->filterPoses(this->detectionArray_msg_);
00073 
00074     //Get and Publish Markers
00075     this->getPeopleMarkers(this->detectionArray_msg_, this->MarkerArray_msg_);
00076     this->peopleMarkers_array_publisher_.publish(this->MarkerArray_msg_);
00077 
00078     //Transform people to desired frame
00079     if(this->selectPosesFrame)
00080       this->transformPoses(this->detectionArray_msg_);
00081     //Publish people
00082     this->people_publisher_.publish(this->detectionArray_msg_);
00083   }
00084 
00085   // [fill msg structures]
00086 
00087   // [fill srv structure and make request to the server]
00088 
00089   // [fill action structure and make request to the action server]
00090 
00091   // [publish messages]
00092 
00093 }
00094 
00095 /*  [subscriber callbacks] */
00096 void LaserPeopleDetectionAlgNode::scan_callback(const sensor_msgs::LaserScan::ConstPtr& msg)
00097 {
00098   //ROS_INFO("LaserPeopleDetectionAlgNode::scan_callback: New Message Received");
00099 
00100   //use appropiate mutex to shared variables if necessary
00101   //this->alg_.lock();
00102   this->scan_mutex_.enter();
00103   if(this->selectScanFrame && msg->header.frame_id==this->scanFrame)
00104     this->currentScan = *msg;
00105 
00106   if(!this->selectScanFrame)
00107     this->currentScan = *msg;
00108   //unlock previously blocked shared variables
00109   //this->alg_.unlock();
00110   this->scan_mutex_.exit();
00111 
00112   if( !event_server_->event_is_set(new_laser_event_id_) )
00113     event_server_->set_event(new_laser_event_id_);
00114 }
00115 
00116 /*  [service callbacks] */
00117 
00118 /*  [action callbacks] */
00119 
00120 /*  [action requests] */
00121 
00122 /*  [class functions] */
00123 void LaserPeopleDetectionAlgNode::transformPoses(iri_perception_msgs::detectionArray & peoplePoses)
00124 {
00125   iri_perception_msgs::detectionArray transformedPeoplePoses(peoplePoses);
00126   transformedPeoplePoses.header          = peoplePoses.header;
00127   std::string source_frame               = transformedPeoplePoses.header.frame_id;
00128   std::string target_frame               = this->target_frame;
00129   ros::Time   source_time                = transformedPeoplePoses.header.stamp;
00130   transformedPeoplePoses.header.frame_id = this->target_frame;
00131 
00132   try
00133   {
00134     ros::Time target_time = transformedPeoplePoses.header.stamp;
00135     bool tf_exists = tf_listener_.waitForTransform(target_frame, source_frame, target_time, ros::Duration(5), ros::Duration(0.01));
00136     if(tf_exists)
00137     {
00138       for(unsigned int i=0; i<transformedPeoplePoses.detection.size(); i++)
00139       {
00140         geometry_msgs::PoseStamped poseIn;
00141         geometry_msgs::PoseStamped poseOut;
00142         poseIn.header             = transformedPeoplePoses.header;
00143         poseIn.header.frame_id    = source_frame;
00144         poseIn.pose.position      = transformedPeoplePoses.detection[i].position;
00145         poseIn.pose.orientation.z = 1.0;
00146         tf_listener_.transformPose(target_frame,     poseIn,     poseOut);
00147         transformedPeoplePoses.detection[i].position = poseOut.pose.position;
00148         transformedPeoplePoses.detection[i].probability   = transformedPeoplePoses.detection[i].probability;
00149       }
00150     }
00151     else
00152     {
00153       ROS_INFO("LaserPeopleDetectionAlgNode::No transform: %s-->%s", source_frame.c_str(), target_frame.c_str());
00154     }
00155   }
00156   catch (tf::TransformException &ex)
00157   {
00158     ROS_INFO("LaserPeopleDetectionAlgNode:: %s",ex.what());
00159   }
00160   peoplePoses = transformedPeoplePoses;
00161 }
00162 
00163 void LaserPeopleDetectionAlgNode::filterPoses(iri_perception_msgs::detectionArray & peoplePoses)
00164 {
00165   iri_perception_msgs::detectionArray filteredPoses;
00166   filteredPoses.header = peoplePoses.header;
00167 
00168   for(unsigned int i=0; i<peoplePoses.detection.size(); i++)
00169   {
00170     double x=peoplePoses.detection[i].position.x;
00171     double y=peoplePoses.detection[i].position.y;
00172     bool goodPose=true;
00173     if( (this->filterX!=0) && (abs(x)>this->filterX))
00174       goodPose=false;
00175     if( (this->filterY!=0) && (abs(y)>this->filterY) )
00176       goodPose=false;
00177     if( (this->filterR!=0) && (x*x+y*y > this->filterR*this->filterR ))
00178       goodPose=false;
00179     if(goodPose)
00180       filteredPoses.detection.push_back(peoplePoses.detection[i]);
00181   }
00182   peoplePoses = filteredPoses;
00183 }
00184 
00185 void LaserPeopleDetectionAlgNode::getPeopleMarkers(iri_perception_msgs::detectionArray & peopleList, visualization_msgs::MarkerArray & people)
00186 {
00187   static unsigned int lastSize=0;
00188   people.markers.resize(std::max(uint(peopleList.detection.size()), lastSize));
00189   lastSize = peopleList.detection.size();
00190 
00191   for(unsigned int i=0; i<peopleList.detection.size(); i++)
00192   {
00193     people.markers[i].header.frame_id = peopleList.header.frame_id;
00194     people.markers[i].header.stamp    = this->currentScan.header.stamp;
00195     people.markers[i].ns              = "people";
00196     people.markers[i].id              = i;
00197     people.markers[i].type            = visualization_msgs::Marker::CYLINDER;
00198     people.markers[i].action          = visualization_msgs::Marker::ADD;
00199 
00200     people.markers[i].scale.x = this->markerWidth;
00201     people.markers[i].scale.y = this->markerWidth;
00202     people.markers[i].scale.z = this->markerHeight + peopleList.detection[i].probability;
00203 
00204     people.markers[i].pose.position.x = peopleList.detection[i].position.x;
00205     people.markers[i].pose.position.y = peopleList.detection[i].position.y;
00206     people.markers[i].pose.position.z = people.markers[i].scale.z/2.0 -0.44;
00207 
00208     people.markers[i].pose.orientation.x = 0.0;
00209     people.markers[i].pose.orientation.y = 0.0;
00210     people.markers[i].pose.orientation.z = 0.0;
00211     people.markers[i].pose.orientation.w = 1.0;
00212 
00213     people.markers[i].color.r = this->markerR;
00214     people.markers[i].color.g = this->markerG;
00215     people.markers[i].color.b = this->markerB;
00216     people.markers[i].color.a = this->markerA;
00217     people.markers[i].lifetime = ros::Duration(0.5f);
00218   }
00219 
00220   //Delete extra previous markers;
00221   for(unsigned int i=peopleList.detection.size(); i<people.markers.size(); i++)
00222   {
00223     people.markers[i].action = visualization_msgs::Marker::DELETE;
00224   }
00225 
00226 }
00227 
00228 void LaserPeopleDetectionAlgNode::node_config_update(Config &config, uint32_t level)
00229 {
00230   this->alg_.lock();
00231   this->selectPosesFrame  = config.selectPosesFrame;
00232   this->target_frame      = config.posesFrame;
00233   this->selectScanFrame   = config.selectScanFrame;
00234   this->scanFrame         = config.scanFrame;
00235   this->markerWidth       = config.markerWidth;
00236   this->markerHeight      = config.markerHeight;
00237   this->markerR           = config.markerR;
00238   this->markerG           = config.markerG;
00239   this->markerB           = config.markerB;
00240   this->markerA           = config.markerA;
00241   this->filterPosesMode   = config.filterPosesMode;
00242   this->filterR           = config.filterR;
00243   this->filterX           = config.filterX;
00244   this->filterY           = config.filterY;
00245   this->alg_.config_      = config;
00246 
00247   this->alg_.unlock();
00248 }
00249 
00250 void LaserPeopleDetectionAlgNode::addNodeDiagnostics(void)
00251 {
00252 }
00253 
00254 /* main function */
00255 int main(int argc,char *argv[])
00256 {
00257   return algorithm_base::main<LaserPeopleDetectionAlgNode>(argc, argv, "laser_people_detection_alg_node");
00258 }


iri_laser_people_detection
Author(s): fherrero
autogenerated on Fri Dec 6 2013 23:39:52