people_tracking_mht_alg_node.cpp
Go to the documentation of this file.
00001 #include "people_tracking_mht_alg_node.h"
00002 
00003 PeopleTrackingMhtAlgNode::PeopleTrackingMhtAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<PeopleTrackingMhtAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   this->loop_rate_ = 24;//in [Hz]
00008   this->detectionsReceived=false;
00009 
00010   // [init publishers]
00011   this->tracksMarkers_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("tracksMarkers", 100);
00012   this->tracks_publisher_ = this->public_node_handle_.advertise<iri_perception_msgs::detectionArray>("tracks", 100);
00013   
00014   // [init subscribers]
00015   this->detections_subscriber_ = this->public_node_handle_.subscribe("detections", 100, &PeopleTrackingMhtAlgNode::detections_callback, this);
00016   
00017   // [init services]
00018   
00019   // [init clients]
00020   
00021   // [init action servers]
00022   
00023   // [init action clients]
00024 }
00025 
00026 PeopleTrackingMhtAlgNode::~PeopleTrackingMhtAlgNode(void)
00027 {
00028   // [free dynamic memory]
00029 }
00030 
00031 void PeopleTrackingMhtAlgNode::mainNodeThread(void)
00032 {
00033   //ROS_INFO("PeopleTrackingMhtAlgNode::mainNodeThread"); 
00034   this->detections_mutex_.enter();
00035   if(this->detectionsReceived)
00036   {
00037     this->alg_.iteration(this->detections, this->tracks);
00038     //ROS_INFO("PeopleTrackingMhtAlgNode::mainNodeThread: tracks.size()=%lu", this->tracks.size());
00039     this->detectionsReceived=false;
00040     this->fillTracksMsg();
00041     this->tracks_publisher_.publish(this->detectionArray_msg_);
00042     this->publishMarkers();
00043   }
00044   this->detections_mutex_.exit();
00045   // [fill msg structures]
00046   //this->MarkerArray_msg_.data = my_var;
00047   //this->detectionArray_msg_.data = my_var;
00048 
00049   // [fill srv structure and make request to the server]
00050 
00051   // [fill action structure and make request to the action server]
00052 
00053   // [publish messages]
00054   //this->tracksMarkers_publisher_.publish(this->MarkerArray_msg_);
00055   //this->tracks_publisher_.publish(this->detectionArray_msg_);
00056 }
00057 
00058 /*  [subscriber callbacks] */
00059 void PeopleTrackingMhtAlgNode::detections_callback(const iri_perception_msgs::detectionArray::ConstPtr& msg) 
00060 { 
00061   //ROS_INFO("PeopleTrackingMhtAlgNode::detections_callback: New Message Received"); 
00062 
00063   //use appropiate mutex to shared variables if necessary 
00064   //this->alg_.lock(); 
00065   this->detections_mutex_.enter(); 
00066 
00067   //std::cout << msg->data << std::endl;
00068   if(!this->detectionsReceived)
00069   {
00070     this->detectionArray_msg_.header = msg->header;
00071     double detection_time = msg->header.stamp.toSec();
00072     this->detections.clear();
00073     for(unsigned int i=0; i<msg->detection.size(); i++)
00074     {
00075       double x    = msg->detection[i].position.x;
00076       double y    = msg->detection[i].position.y;
00077       int    type = msg->type;
00078       double p    = msg->detection[i].probability;
00079       //Sdetection detection(x,y,type,p);
00080       Sdetection detection(x,y,detection_time,type,p);
00081       this->detections.push_back(detection);
00082       //ROS_INFO("PeopleTrackingMhtAlgNode::detections_callback: x,y,type,prob,time: %f, %f, %d, %f, %f", x, y, type, p, detection_time); 
00083     }
00084     if(this->detections.size()>0)
00085       this->detectionsReceived=true;
00086   }
00087   //unlock previously blocked shared variables 
00088   //this->alg_.unlock(); 
00089   this->detections_mutex_.exit(); 
00090 }
00091 
00092 /*  [service callbacks] */
00093 
00094 /*  [action callbacks] */
00095 
00096 /*  [action requests] */
00097 
00098 void PeopleTrackingMhtAlgNode::node_config_update(Config &config, uint32_t level)
00099 {
00100   this->alg_.lock();
00101 
00102   this->alg_.unlock();
00103 }
00104 
00105 void PeopleTrackingMhtAlgNode::addNodeDiagnostics(void)
00106 {
00107 }
00108 
00109 /* main function */
00110 int main(int argc,char *argv[])
00111 {
00112   return algorithm_base::main<PeopleTrackingMhtAlgNode>(argc, argv, "people_tracking_mht_alg_node");
00113 }
00114 
00115 void PeopleTrackingMhtAlgNode::fillTracksMsg()
00116 {
00117   this->detectionArray_msg_.type=123; //TODO tracker type?
00118   this->detectionArray_msg_.detection.resize(this->tracks.size());
00119   for(unsigned int i=0; i<this->tracks.size(); i++)
00120   {
00121     double x=tracks[i].x;
00122     double y=tracks[i].y;
00123     int   id=tracks[i].id;
00124     double p=tracks[i].probability_of_detection;
00125     this->detectionArray_msg_.detection[i].position.x = x;
00126     this->detectionArray_msg_.detection[i].position.y = y;
00127     this->detectionArray_msg_.detection[i].id         = id;
00128     this->detectionArray_msg_.detection[i].probability= p;
00129   }
00130 }
00131 
00132 void PeopleTrackingMhtAlgNode::publishMarkers()
00133 {
00134   static unsigned int lastSize=0;
00135   this->MarkerArray_msg_.markers.resize(std::max(uint(this->detectionArray_msg_.detection.size()), lastSize));
00136   for(unsigned int i=0; i<this->detectionArray_msg_.detection.size(); i++)
00137   {
00138     this->MarkerArray_msg_.markers[i].header = this->detectionArray_msg_.header;
00139     this->MarkerArray_msg_.markers[i].ns     = "id";
00140     this->MarkerArray_msg_.markers[i].id     = this->detectionArray_msg_.detection[i].id;
00141     this->MarkerArray_msg_.markers[i].type   = visualization_msgs::Marker::TEXT_VIEW_FACING;
00142     this->MarkerArray_msg_.markers[i].action = visualization_msgs::Marker::ADD;
00143 
00144     this->MarkerArray_msg_.markers[i].pose.position.x = this->detectionArray_msg_.detection[i].position.x;
00145     this->MarkerArray_msg_.markers[i].pose.position.y = this->detectionArray_msg_.detection[i].position.y;
00146     this->MarkerArray_msg_.markers[i].pose.position.z = this->detectionArray_msg_.detection[i].position.z; //0.2
00147 
00148     //double yaw;
00149     //geometry_msgs::Quaternion myQ = tf::createQuaternionMsgFromYaw(yaw);
00150     //this->MarkerArray_msg_.markers[i].pose.orientation = myQ;
00151 
00152     this->MarkerArray_msg_.markers[i].scale.x = 0.6;
00153     this->MarkerArray_msg_.markers[i].scale.y = 0.6;
00154     this->MarkerArray_msg_.markers[i].scale.z = 0.6;
00155 
00156     this->MarkerArray_msg_.markers[i].color.r = 1.0;
00157     this->MarkerArray_msg_.markers[i].color.g = 0.0;
00158     this->MarkerArray_msg_.markers[i].color.b = 0.0;
00159     this->MarkerArray_msg_.markers[i].color.a = 1.0;
00160     this->MarkerArray_msg_.markers[i].lifetime = ros::Duration(0.5f);
00161 
00162     std::stringstream idText;
00163     idText << this->detectionArray_msg_.detection[i].id;
00164     this->MarkerArray_msg_.markers[i].text = idText.str();
00165   }
00166 
00167   //Delete extra previous markers;
00168   for(unsigned int i=this->detectionArray_msg_.detection.size(); i<this->MarkerArray_msg_.markers.size(); i++)
00169   {
00170     this->MarkerArray_msg_.markers[i].header = this->detectionArray_msg_.header;
00171     this->MarkerArray_msg_.markers[i].action = visualization_msgs::Marker::DELETE;
00172   }
00173 
00174   this->tracksMarkers_publisher_.publish(this->MarkerArray_msg_);
00175 }


iri_people_tracking_mht
Author(s): fherrero
autogenerated on Fri Dec 6 2013 22:31:00