people_tracking_rai_alg_node.cpp
Go to the documentation of this file.
00001 #include "people_tracking_rai_alg_node.h"
00002 
00003 PeopleTrackingRaiAlgNode::PeopleTrackingRaiAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<PeopleTrackingRaiAlgorithm>(), it_(this->public_node_handle_)
00005 {
00006       int intParam;
00007            
00008       //init class attributes if necessary
00009         //this->loop_rate_ = 2;//in [Hz]
00010         //hogFile.open("/home/andreu/Desktop/hogFile.txt");
00011         
00012         //relax 
00013         sleep(1);
00014       
00015       //general prupose variables
00016       tldMessageFilled = false;
00017       exeMode = MULTI_TRACKING;
00018       std::cout << "TRACKER EXE MODE INIT TO: " << exeMode << std::endl;
00019       tracker.setFollowMeTargetId(-1);//indicates no follow me target yet
00020         
00021       //init user parameters. general running parameters
00022       this->public_node_handle_.getParam("verbose_mode", intParam); this->verboseMode = (bool)intParam;
00023       
00024       //init user parameters. filter parameters
00025       this->public_node_handle_.getParam("num_particles", intParam); this->filterParams.numParticles = (unsigned int)intParam;
00026       this->public_node_handle_.getParam("init_delta_xy", this->filterParams.initDeltaXY);
00027       this->public_node_handle_.getParam("init_delta_vxy", this->filterParams.initDeltaVxy);
00028       this->public_node_handle_.getParam("sigma_resampling_xy", this->filterParams.sigmaResamplingXY);
00029       this->public_node_handle_.getParam("sigma_resampling_vxy", this->filterParams.sigmaResamplingVxy);
00030       this->public_node_handle_.getParam("person_radius", this->filterParams.personRadius);
00031       this->public_node_handle_.getParam("matching_legs_alpha", this->filterParams.matchingLegsAlpha);
00032       this->public_node_handle_.getParam("matching_legs_beta", this->filterParams.matchingLegsBeta);
00033       this->public_node_handle_.getParam("matching_bearing_alpha", this->filterParams.matchingBearingAlpha);
00034       this->public_node_handle_.getParam("matching_bearing_beta", this->filterParams.matchingBearingBeta);
00035 //    this->public_node_handle_.getParam("power_rgb_cos", intParam); this->filterParams.powerRGBcos = (unsigned int)intParam;
00036 
00037       //init user parameters. tracker parameters
00038       this->public_node_handle_.getParam("minimum_distance_between_people", this->trackerParams.minDistanceBetweenPeople);
00039       this->public_node_handle_.getParam("minimum_association_prob", this->trackerParams.minAssociationProb);
00040       this->public_node_handle_.getParam("max_detection_distance_accepted", this->trackerParams.maxDetectionDistance);
00041       this->public_node_handle_.getParam("min_detection_distance_accepted", this->trackerParams.minDetectionDistance);
00042       this->public_node_handle_.getParam("max_detection_azimut_accepted", this->trackerParams.maxDetectionAzimut);      
00043       this->trackerParams.maxDetectionAzimut = this->trackerParams.maxDetectionAzimut*M_PI/180.;
00044       this->public_node_handle_.getParam("max_consecutive_uncorrected_iterations", intParam); this->trackerParams.maxConsecutiveUncorrected = (unsigned int)intParam;
00045       this->public_node_handle_.getParam("minimum_iterations_to_be_target", intParam); this->trackerParams.minIterationsToBeTarget = (unsigned int)intParam;
00046       this->public_node_handle_.getParam("minimum_iterations_to_be_visually_confirmed", intParam); this->trackerParams.iterationsToBeVisuallyConfirmed = (unsigned int)intParam;
00047       this->public_node_handle_.getParam("minimum_iterations_to_be_friend", intParam); this->trackerParams.iterationsToBeFriend = (unsigned int)intParam;                
00048       this->public_node_handle_.getParam("minimum_appearance_region_size", intParam); this->trackerParams.minAppearanceRegionSize = (unsigned int)intParam;
00049 
00050       //visualization parameters
00051       this->public_node_handle_.getParam("view_body_detections", intParam); this->viewBodyDetections = (bool)intParam;
00052       this->public_node_handle_.getParam("ratio_particles_displayed", this->ratioParticlesDisplayed);
00053       
00054       //set parameters to tracker
00055       this->tracker.setParameters(trackerParams);
00056       this->tracker.setFilterParameters(filterParams);
00057 
00058       //initializes random generator;
00059       srand ( time(NULL) ); 
00060       
00061       //debug counters
00062       frameCount = 0;
00063       hogDetCount = 0;
00064         
00065       //initializes camera mouting position and camera matrix
00066       //initCamera();   
00067 
00068   // [init publishers]
00069         this->particleSet_publisher_ = this->public_node_handle_.advertise<visualization_msgs::MarkerArray>("debug", 100);
00070         this->peopleSet_publisher_ = this->public_node_handle_.advertise<iri_perception_msgs::peopleTrackingArray>("peopleSet", 100);
00071         this->image_pub_ = it_.advertise("image_out", 1);
00072       this->tldBB_publisher_ = this->public_node_handle_.advertise<tld_msgs::Target>("tld_init", 1000, true);
00073   
00074   // [init subscribers]
00075       this->followMe_subscriber_ = this->public_node_handle_.subscribe("followMe", 100, &PeopleTrackingRaiAlgNode::followMe_callback, this);      
00076       this->tldDetections_subscriber_ = this->public_node_handle_.subscribe("tldDetections", 100, &PeopleTrackingRaiAlgNode::tldDetections_callback, this);
00077       this->faceDetections_subscriber_ = this->public_node_handle_.subscribe("faceDetections", 100, &PeopleTrackingRaiAlgNode::faceDetections_callback, this);
00078         this->bodyDetections_subscriber_ = this->public_node_handle_.subscribe("bodyDetections", 100, &PeopleTrackingRaiAlgNode::bodyDetections_callback, this);
00079         this->odometry_subscriber_ = this->public_node_handle_.subscribe("odometry", 100, &PeopleTrackingRaiAlgNode::odometry_callback, this);
00080         this->legDetections_subscriber_ = this->public_node_handle_.subscribe("legDetections", 100, &PeopleTrackingRaiAlgNode::legDetections_callback, this);
00081         this->image_sub_ = it_.subscribe("image_in", 1, &PeopleTrackingRaiAlgNode::image_callback, this);       
00082   
00083   // [init services]
00084   
00085   // [init clients]
00086   
00087   // [init action servers]
00088   
00089   // [init action clients]
00090 }
00091 
00092 PeopleTrackingRaiAlgNode::~PeopleTrackingRaiAlgNode(void)
00093 {
00094   // [free dynamic memory]
00095   
00096   //close files
00097   //hogFile.close();
00098 }
00099 
00100 void PeopleTrackingRaiAlgNode::initCamera()
00101 {
00102       tf::StampedTransform stf;
00103       Cposition3d camINbase;
00104       tf::TransformListener tfListener;
00105       
00106       //sleep(1);//??
00107       
00108       tfListener.waitForTransform("/base_link", "/left_stereo_optical_frame", ros::Time(0), ros::Duration(10.0),ros::Duration(1.0));
00109       tfListener.lookupTransform("/base_link", "/left_stereo_optical_frame", ros::Time(0), stf);
00110       camINbase.setXYZ(stf.getOrigin().x(), stf.getOrigin().y(), stf.getOrigin().z());
00111       camINbase.setQuaternion(stf.getRotation().getW(),stf.getRotation().getX(),stf.getRotation().getY(),stf.getRotation().getZ());
00112       tracker.setOnBoardCamPose(camINbase);
00113       //tracker->setOnBoardCamCalMatrix();//to do
00114 }
00115 
00116 void PeopleTrackingRaiAlgNode::fillMessages()
00117 {
00118         std::list<CpersonTarget>::iterator iiF;
00119         std::list<CpersonParticle>::iterator iiP;
00120         std::list<CbodyObservation>::iterator iiB;
00121         std::list<Cpoint3dObservation>::iterator iiL;
00122         filterEstimate iiEst;
00123         ostringstream markerText;
00124       CbodyObservation tldDet;
00125         double bodyBearing, transpFactor;
00126         unsigned int ii=0;//, jj=0;
00127       unsigned int bbx,bby,bbw,bbh;
00128         
00129         //1. Main output message: peopleTrackingArray
00130         peopleTrackingArray_msg_.peopleSet.clear();
00131         peopleTrackingArray_msg_.header.frame_id = "/base_link";
00132         peopleTrackingArray_msg_.header.stamp = ros::Time::now();
00133         std::list<CpersonTarget> & targets = tracker.getTargetList();
00134         peopleTrackingArray_msg_.peopleSet.resize(targets.size());
00135         for (iiF=targets.begin(); iiF!=targets.end(); iiF++)
00136         {
00137                 if ( iiF->getMaxStatus() > CANDIDATE )
00138                 {
00139                         iiF->getEstimate(iiEst);
00140                         peopleTrackingArray_msg_.peopleSet[ii].targetId = iiF->getId();
00141                         peopleTrackingArray_msg_.peopleSet[ii].targetStatus = iiF->getStatus();
00142                         peopleTrackingArray_msg_.peopleSet[ii].x = iiEst.position.getX();
00143                         peopleTrackingArray_msg_.peopleSet[ii].y = iiEst.position.getY();
00144                         peopleTrackingArray_msg_.peopleSet[ii].vx = iiEst.velocity.getX();
00145                         peopleTrackingArray_msg_.peopleSet[ii].vy = iiEst.velocity.getY();
00146                         peopleTrackingArray_msg_.peopleSet[ii].covariances[0] = iiEst.position.getMatrixElement(0,0);
00147                         peopleTrackingArray_msg_.peopleSet[ii].covariances[5] = iiEst.position.getMatrixElement(1,1);
00148                         peopleTrackingArray_msg_.peopleSet[ii].covariances[10] = iiEst.velocity.getMatrixElement(0,0);
00149                         peopleTrackingArray_msg_.peopleSet[ii].covariances[15] = iiEst.velocity.getMatrixElement(1,1);
00150                         ii++;
00151                 }
00152         }
00153         
00154         //erase message data if previous iteration had greater array size
00155         peopleTrackingArray_msg_.peopleSet.erase(peopleTrackingArray_msg_.peopleSet.begin()+ii,peopleTrackingArray_msg_.peopleSet.end());
00156         
00157         //2. VISUALIZATION MESSAGE: Marker array
00158         if (this->verboseMode) std::cout << std::endl << "*** Filling Debug Markers Message" << std::endl;
00159         std::list<Cpoint3dObservation> & laserDetSet = tracker.getLaserDetSet();
00160         std::list<CbodyObservation> & bodyDetSet = tracker.getBodyDetSet();
00161         MarkerArray_msg_.markers.clear();
00162         //MarkerArray_msg_.markers.resize( laserDetSet.size() + bodyDetSet.size()*4 + 2*targets.size() + filterParams.numParticles*targets.size() );
00163       MarkerArray_msg_.markers.resize( laserDetSet.size() + bodyDetSet.size()*4 + targets.size() + filterParams.numParticles*targets.size() +1 ); //ALERT: add particleRatioToBeDrawn ???        
00164         ii = 0;
00165         //if (this->verboseMode) std::cout << "\tlaserDetSet.size(): " << laserDetSet.size() << "\tbodyDetSet.size(): " << bodyDetSet.size() << "\ttargets.size(): " << targets.size() << "\tMarkerArray_msg_.markers.size() " << MarkerArray_msg_.markers.size() << std::endl; 
00166 
00167         //2a. laser detections
00168         for (iiL=laserDetSet.begin(); iiL!=laserDetSet.end(); iiL++)
00169         {
00170                 //if (this->verboseMode) std::cout << "mainNodeThread: " << __LINE__ << std::endl;
00171                 this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00172                 this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00173                 this->MarkerArray_msg_.markers[ii].id = ii;
00174                 this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::CYLINDER;
00175                 this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00176                 this->MarkerArray_msg_.markers[ii].pose.position.x = iiL->point.getX();
00177                 this->MarkerArray_msg_.markers[ii].pose.position.y = iiL->point.getY();
00178                 this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00179                 this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE/3;
00180                 this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE/3;
00181                 this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE/3;
00182                 this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY;
00183                 this->MarkerArray_msg_.markers[ii].color.r = 1;
00184                 this->MarkerArray_msg_.markers[ii].color.g = 0.2;
00185                 this->MarkerArray_msg_.markers[ii].color.b = 0.2;
00186                 this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION);
00187                 ii++;                           
00188         }
00189         
00190         //2b. body detections
00191         if (this->viewBodyDetections)
00192         {
00193                 for (iiB=bodyDetSet.begin(); iiB!=bodyDetSet.end(); iiB++)
00194                 {
00195                   //if (this->verboseMode) std::cout << "mainNodeThread: " << __LINE__ << std::endl;
00196                   //arrow indicating bearing and rgb eigen
00197                   this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00198                   this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00199                   this->MarkerArray_msg_.markers[ii].id = ii;
00200                   this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::ARROW;
00201                   this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00202                   this->MarkerArray_msg_.markers[ii].pose.position.x = 0;
00203                   this->MarkerArray_msg_.markers[ii].pose.position.y = 0;
00204                   this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00205                   this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE;
00206                   this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE;
00207                   this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE*6;
00208                   this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY;
00209                   this->MarkerArray_msg_.markers[ii].color.r = 0;
00210                   this->MarkerArray_msg_.markers[ii].color.g = 1;
00211                   this->MarkerArray_msg_.markers[ii].color.b = 1;
00212                   this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION*3);
00213                   bodyBearing = atan2(iiB->direction.getY(),iiB->direction.getX());
00214                   geometry_msgs::Quaternion bearingQuaternion = tf::createQuaternionMsgFromYaw(bodyBearing);
00215                   this->MarkerArray_msg_.markers[ii].pose.orientation.x = bearingQuaternion.x;
00216                   this->MarkerArray_msg_.markers[ii].pose.orientation.y = bearingQuaternion.y;
00217                   this->MarkerArray_msg_.markers[ii].pose.orientation.z = bearingQuaternion.z;
00218                   this->MarkerArray_msg_.markers[ii].pose.orientation.w = bearingQuaternion.w;
00219                   ii++;
00220                         
00221                         //three cylinders indicating RGB clusters
00222                         /*
00223                         for (jj=0; (jj<iiB->rgbCenters.size()) || (jj<3); jj++)
00224                         {
00225                                 //if (this->verboseMode) std::cout << "rgbCenters.size(): " << iiB->rgbCenters.size() << std::endl;
00226                                 this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00227                                 this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00228                                 this->MarkerArray_msg_.markers[ii].id = ii;
00229                                 this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::CYLINDER;
00230                                 this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00231                                 this->MarkerArray_msg_.markers[ii].pose.position.x = 2*cos(bodyBearing);
00232                                 this->MarkerArray_msg_.markers[ii].pose.position.y = 2*sin(bodyBearing);
00233                                 this->MarkerArray_msg_.markers[ii].pose.position.z = jj*MARKER_SIZE;
00234                                 this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE/2;
00235                                 this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE/2;
00236                                 this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE;
00237                                 this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY;
00238                                 this->MarkerArray_msg_.markers[ii].color.r = iiB->rgbCenters.at(jj).getX();
00239                                 this->MarkerArray_msg_.markers[ii].color.g = iiB->rgbCenters.at(jj).getY();
00240                                 this->MarkerArray_msg_.markers[ii].color.b = iiB->rgbCenters.at(jj).getZ();
00241                                 this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION*3);
00242                                 ii++;
00243                         }
00244                         */
00245                 }
00246         }
00247 
00248         //2c. target positions, target Id's and particles
00249         for (iiF=targets.begin(); iiF!=targets.end(); iiF++)
00250       {
00251             //if (this->verboseMode) std::cout << "mainNodeThread: " << __LINE__ << std::endl;
00252             if ( iiF->getMaxStatus() > CANDIDATE )
00253             {
00254                   iiF->getEstimate(iiEst);
00255                   
00256                   //cylinder: target position
00257                   this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00258                   this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00259                   this->MarkerArray_msg_.markers[ii].id = ii;
00260                   this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::CYLINDER;
00261                   this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00262                   this->MarkerArray_msg_.markers[ii].pose.position.x = iiEst.position.getX();
00263                   this->MarkerArray_msg_.markers[ii].pose.position.y = iiEst.position.getY();
00264                   if (iiF->pOcclusion > 0.5) this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00265                   else this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00266                   this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE;
00267       
00268                   //marker size depending on occlusion
00269                   this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE;
00270                   if (iiF->pOcclusion > 0.5) this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE*2;
00271                   else this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE;
00272                   
00273                   //marker transp depending on status
00274                   switch(iiF->getMaxStatus())
00275                   {
00276                         case LEGGED_TARGET: transpFactor = 0.1; break;
00277                         case VISUALLY_CONFIRMED: transpFactor = 0.5; break;
00278                         case FRIEND_IN_SIGHT: transpFactor = 1; break;
00279                         //case FRIEND_OUT_OF_RANGE: transpFactor = 1; break;
00280                         default: transpFactor = 0.1; break; 
00281                   }
00282                   this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY*transpFactor;
00283                   this->MarkerArray_msg_.markers[ii].color.r = 0;
00284                   this->MarkerArray_msg_.markers[ii].color.g = 0;
00285                   this->MarkerArray_msg_.markers[ii].color.b = 1.0;
00286                   this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION);
00287                   ii++;
00288                   
00289                   //text: target ID
00290                   this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00291                   this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00292                   this->MarkerArray_msg_.markers[ii].id = ii;
00293                   this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00294                   this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00295                   this->MarkerArray_msg_.markers[ii].pose.position.x = iiEst.position.getX()+0.4;
00296                   this->MarkerArray_msg_.markers[ii].pose.position.y = iiEst.position.getY()+0.4;
00297                   this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00298                   this->MarkerArray_msg_.markers[ii].scale.z = MARKER_TEXT_SIZE;
00299                   this->MarkerArray_msg_.markers[ii].color.a = 1;
00300                   this->MarkerArray_msg_.markers[ii].color.r = 0.0;
00301                   this->MarkerArray_msg_.markers[ii].color.g = 0.0;
00302                   this->MarkerArray_msg_.markers[ii].color.b = 0.0;
00303                   this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION);
00304                   markerText.str("");
00305                   markerText << iiF->getId() << "/";
00306                   for (unsigned int jj=0; jj<NUM_DETECTORS; jj++)
00307                   {
00308                         for (unsigned int kk=0; kk<iiF->aDecisions[jj].size(); kk++)
00309                         {
00310                               if ( iiF->aDecisions[jj].at(kk) )
00311                               {
00312                                     markerText << jj;
00313                                     break;
00314                               }
00315                         }
00316                   }
00317                   this->MarkerArray_msg_.markers[ii].text = markerText.str();
00318                   ii++;
00319 
00320                   //text: debugging ! Velocities
00321 //                   this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00322 //                   this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00323 //                   this->MarkerArray_msg_.markers[ii].id = ii;
00324 //                   this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::TEXT_VIEW_FACING;
00325 //                   this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00326 //                   this->MarkerArray_msg_.markers[ii].pose.position.x = iiEst.position.getX()+0.4;
00327 //                   this->MarkerArray_msg_.markers[ii].pose.position.y = iiEst.position.getY()-0.4;
00328 //                   this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00329 //                   this->MarkerArray_msg_.markers[ii].scale.z = MARKER_TEXT_SIZE;
00330 //                   this->MarkerArray_msg_.markers[ii].color.a = 1;
00331 //                   this->MarkerArray_msg_.markers[ii].color.r = 0.0;
00332 //                   this->MarkerArray_msg_.markers[ii].color.g = 0.0;
00333 //                   this->MarkerArray_msg_.markers[ii].color.b = 0.0;
00334 //                   this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION);
00335 //                   markerText.precision(2);
00336 //                   markerText.str("");
00337 //                   markerText << "(" << iiEst.velocity.getX() << "," << iiEst.velocity.getY() << ")";
00338 //                   this->MarkerArray_msg_.markers[ii].text = markerText.str();
00339 //                   ii++;                  
00340                   
00341                   //particles
00342                   std::list<CpersonParticle> & pSet = iiF->getParticleSet();
00343                   //if (this->verboseMode) std::cout << "mainNodeThread: " << __LINE__ << "; pSet.size(): " << pSet.size() << std::endl;
00344                   for (iiP=pSet.begin();iiP!=pSet.end();iiP++)
00345                   {
00346                         //if (this->verboseMode) std::cout << "mainNodeThread: " << __LINE__ << "; ii: " << ii << std::endl;
00347                         double rnd = (double)rand()/(double)RAND_MAX;
00348                         if ( rnd < ratioParticlesDisplayed )
00349                         {
00350                               this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00351                               this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00352                               this->MarkerArray_msg_.markers[ii].id = ii;
00353                               this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::CYLINDER;
00354                               this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00355                               this->MarkerArray_msg_.markers[ii].pose.position.x = iiP->position.getX();
00356                               this->MarkerArray_msg_.markers[ii].pose.position.y = iiP->position.getY();
00357                               this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00358                               this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE/10;
00359                               this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE/10;
00360                               this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE/10;
00361                               this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY;
00362                               this->MarkerArray_msg_.markers[ii].color.r = 0.5;
00363                               this->MarkerArray_msg_.markers[ii].color.g = 1.5;
00364                               this->MarkerArray_msg_.markers[ii].color.b = 0.0;
00365                               this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION);
00366                               ii++;
00367                         }
00368                   }             
00369             }
00370       }
00371       
00372       //2d. Arrow mark for the current TLD detection
00373       tracker.getTLDdetection(tldDet);
00374       if (tldDet.bbW != 0) //check if a bounding box is set
00375       {
00376             this->MarkerArray_msg_.markers[ii].header.frame_id = "/base_link";
00377             this->MarkerArray_msg_.markers[ii].header.stamp = ros::Time::now();
00378             this->MarkerArray_msg_.markers[ii].id = ii;
00379             this->MarkerArray_msg_.markers[ii].type = visualization_msgs::Marker::ARROW;
00380             this->MarkerArray_msg_.markers[ii].action = visualization_msgs::Marker::ADD;
00381             this->MarkerArray_msg_.markers[ii].pose.position.x = 0;
00382             this->MarkerArray_msg_.markers[ii].pose.position.y = 0;
00383             this->MarkerArray_msg_.markers[ii].pose.position.z = MARKER_Z;
00384             this->MarkerArray_msg_.markers[ii].scale.x = MARKER_SIZE;
00385             this->MarkerArray_msg_.markers[ii].scale.y = MARKER_SIZE;
00386             this->MarkerArray_msg_.markers[ii].scale.z = MARKER_SIZE*3;
00387             this->MarkerArray_msg_.markers[ii].color.r = 240./255.;
00388             this->MarkerArray_msg_.markers[ii].color.g = 180./255.;
00389             this->MarkerArray_msg_.markers[ii].color.b = 20./255.;      
00390             this->MarkerArray_msg_.markers[ii].color.a = MARKER_TRANSPARENCY;
00391             this->MarkerArray_msg_.markers[ii].lifetime = ros::Duration(MARKER_DURATION*3);
00392             bodyBearing = atan2(tldDet.direction.getY(),tldDet.direction.getX());
00393             geometry_msgs::Quaternion bearingQuaternion = tf::createQuaternionMsgFromYaw(bodyBearing);
00394             this->MarkerArray_msg_.markers[ii].pose.orientation.x = bearingQuaternion.x;
00395             this->MarkerArray_msg_.markers[ii].pose.orientation.y = bearingQuaternion.y;
00396             this->MarkerArray_msg_.markers[ii].pose.orientation.z = bearingQuaternion.z;
00397             this->MarkerArray_msg_.markers[ii].pose.orientation.w = bearingQuaternion.w;
00398             ii++;
00399       }
00400       
00401       //3. TLD message
00402       if ( (exeMode == SHOOT_TLD) && (cv_ptr!=NULL) )
00403       {
00404             tldBB_msg_.bb.header.frame_id = "/base_link";
00405             tldBB_msg_.bb.header.stamp = ros::Time::now();
00406             tracker.getTLDbb(bbx,bby,bbw,bbh);
00407             tldBB_msg_.bb.x = bbx;
00408             tldBB_msg_.bb.y = bby;
00409             tldBB_msg_.bb.width = bbw;
00410             tldBB_msg_.bb.height = bbh;
00411             tldBB_msg_.bb.confidence = 1.0;
00412             cv_ptr->toImageMsg(tldBB_msg_.img);
00413             tldMessageFilled = true;
00414       }
00415 
00416       //erase message data if previous iteration had greater array size
00417       MarkerArray_msg_.markers.erase(MarkerArray_msg_.markers.begin()+ii,MarkerArray_msg_.markers.end());       
00418 }
00419 
00420 void PeopleTrackingRaiAlgNode::mainNodeThread(void)
00421 {       
00422       if (this->verboseMode) std::cout << std::endl << "************* NEW ITERATION **************" << std::endl;
00423 
00424       //LOCK data reception mutexes
00425       this->legDetections_mutex_.enter(); 
00426       this->bodyDetections_mutex_.enter(); 
00427       this->faceDetections_mutex_.enter(); 
00428       this->tldDetections_mutex_.enter(); 
00429       this->followMe_mutex_.enter(); 
00430       this->image_mutex_.enter();
00431 
00432       //PRINT DETECTIONS AVAILABLE AT THIS ITERATION
00433       if (this->verboseMode) tracker.printDetectionSets();
00434       
00435       //FILTER PREDICTION
00436       if (this->verboseMode) std::cout << std::endl << "*** Prior peopleSet:" << std::endl;
00437       this->odometry_mutex_.enter(); 
00438       tracker.propagateFilters(platformOdometry); //platform motion
00439       platformOdometry.resetDeltas();
00440       this->odometry_mutex_.exit(); 
00441       tracker.propagateFilters(); //people motion
00442       
00443       //OCCLUSIONS
00444       tracker.computeOcclusions();
00445       
00446       //UPDATE STATUS & ESTIMATES
00447       tracker.updateFilterEstimates();
00448       tracker.updateTargetStatus();
00449       if (this->verboseMode) tracker.printPeopleSet();
00450 
00451       //DATA ASSOCIATION
00452       //if (this->verboseMode) std::cout << std::endl << "*** Target/Detection association" << std::endl;
00453       tracker.updateAssociationTables();
00454             
00455       //MARK BOUNDING BOXES & LEARN CURRENT DETECTED APPEARANCES
00456       if (cv_ptr!=NULL)
00457       {
00458             //debug
00459             //frameCount++;
00460             //std::cout << "det/frames = " << (double)hogDetCount/(double)frameCount << std::endl;
00461             
00462             //Set image to the tracker
00463             tracker.setCurrentImage(cv_ptr->image);
00464             
00465             //compute appearances of body detections
00466             //tracker.computeTargetAppearance();
00467             
00468             //mark box bodies on image
00469             tracker.markBodies();            
00470             
00471             //mark box faces on image
00472             tracker.markFaces();
00473             
00474             //mark tld box
00475             tracker.markTld();
00476             
00477             //get marked image from the tracker
00478             tracker.getCurrentImage(cv_ptr->image);
00479       }                                
00480                         
00481       //CORRECTION
00482       if (this->verboseMode) std::cout << std::endl << "*** Posterior peopleSet:" << std::endl;
00483       tracker.correctFilters();
00484             
00485       //UPDATE FILTER ESTIMATES AND ADD THEM TO EACH TARGET TRACK
00486       tracker.updateFilterEstimates();
00487       tracker.addEstimatesToTracks();
00488       if (this->verboseMode) tracker.printPeopleSet();
00489                   
00490       //LAUNCH NEW FILTERS IF NEW DETECTIONS ARE NOT ASSOCIATED
00491       if (this->verboseMode) std::cout << std::endl << "*** Create Filters" << std::endl;
00492       tracker.createFilters();
00493 
00494       //REMOVE UNSUPPORTED TARGETS
00495       if (this->verboseMode) std::cout << std::endl << "*** Delete Filters" << std::endl;
00496       tracker.deleteFilters();
00497             
00498       //RESAMPLING PARTICLE SETS 
00499       if (this->verboseMode) std::cout << std::endl << "*** Resampling" << std::endl;
00500       tracker.resampleFilters();
00501       
00502       //Check if TLD tracker can be initialized, if so, initTLD
00503       if ( (exeMode == MULTI_TRACKING) && (tracker.getFollowMeTargetId()>0) )
00504       {
00505             exeMode = SHOOT_TLD;
00506             std::cout << "TRACKER EXE MODE UPDATED TO: " << exeMode << "(" << __LINE__ << ")" << std::endl;
00507             tracker.initTLD();
00508       }
00509 
00510       // [fill msg structures]
00511       if (this->verboseMode) std::cout << std::endl << "*** Filling Output Messages" << std::endl;
00512       this->fillMessages();
00513 
00514       //RESET DETECTION SETS
00515       tracker.resetDetectionSets(LEGS);
00516       tracker.resetDetectionSets(BODY);
00517       tracker.resetDetectionSets(FACE);
00518       tracker.resetDetectionSets(TLD);
00519 
00520       //UNLOCK data reception mutexes (except image mutex which will be unlocked below)
00521       this->legDetections_mutex_.exit(); 
00522       this->bodyDetections_mutex_.exit(); 
00523       this->faceDetections_mutex_.exit();
00524       this->tldDetections_mutex_.exit();
00525       this->followMe_mutex_.exit(); 
00526 
00527       // [fill srv structure and make request to the server]
00528 
00529       // [fill action structure and make request to the action server]
00530 
00531       // [publish messages]
00532       this->particleSet_publisher_.publish(this->MarkerArray_msg_);
00533       this->peopleSet_publisher_.publish(this->peopleTrackingArray_msg_);
00534       if (cv_ptr!=NULL) image_pub_.publish(cv_ptr->toImageMsg());
00535       if ( tldMessageFilled )
00536       {
00537             tldMessageFilled = false;
00538             exeMode = FOLLOW_ME;
00539             std::cout << "TRACKER EXE MODE UPDATED TO: " << exeMode << "(" << __LINE__ << ")" << std::endl;
00540             tldBB_publisher_.publish(tldBB_msg_);//this message is published once, just to start-up tld tracker
00541       }
00542 
00543       //unlock image mutex
00544       this->image_mutex_.exit();        
00545 }
00546 
00547 /*  [subscriber callbacks] */
00548 void PeopleTrackingRaiAlgNode::followMe_callback(const std_msgs::Int32::ConstPtr& msg) 
00549 {
00550       followMe_mutex_.enter();
00551       std::cout << "*************************************************" << std::endl;
00552       tracker.setFollowMeTargetId(msg->data);
00553       followMe_mutex_.exit();
00554 }
00555 
00556 void PeopleTrackingRaiAlgNode::tldDetections_callback(const tld_msgs::BoundingBox::ConstPtr& msg) 
00557 { 
00558       double dirX, dirY, dirMod;
00559       double cx,fx; //camera calibration params cx = P[0,2], fx = P[0,0].
00560       CbodyObservation newDetection;
00561       
00562       //hardcoded for REEM head cameras!! ToDo: get it from camera_info message !!
00563       cx = 286.37;
00564       fx = 338.49;
00565       
00566       //ROS_INFO("PeopleTrackingRaiAlgNode::tldDetections_callback: New Message Received"); 
00567       
00568       //blocks tld detection mutex
00569       this->tldDetections_mutex_.enter(); 
00570 
00571       //time stamp
00572       newDetection.timeStamp.set(msg->header.stamp.sec, msg->header.stamp.nsec);
00573       
00574       //bounding box
00575       newDetection.bbX = msg->x;
00576       newDetection.bbY = msg->y;
00577       newDetection.bbW = msg->width;
00578       newDetection.bbH = msg->height;
00579       
00580       //confidence set in the rgbEigen.X field (to be improved !)
00581       newDetection.rgbEigen.setX(msg->confidence);
00582       
00583       //direction (robot: X->forward, Y->left. Camera: x->horizontal, y->vertical, z->depth)
00584       dirX = 1.0;
00585       dirY = -((msg->x+msg->width/2.0)-cx)/fx;
00586       dirMod = sqrt(dirX*dirX+dirY*dirY);
00587       newDetection.direction.setXYZ(dirX/dirMod, dirY/dirMod, 0);
00588       
00589       //sets TLD detection to tracker
00590       tracker.setTLDdetection(newDetection);
00591 
00592       //unblocks tld detection mutex
00593       this->tldDetections_mutex_.exit(); 
00594 }
00595 
00596 void PeopleTrackingRaiAlgNode::faceDetections_callback(const pal_vision_msgs::FaceDetections::ConstPtr& msg) 
00597 { 
00598       unsigned int ii;
00599       CfaceObservation newDetection;
00600       std::list<CfaceObservation>::iterator iiB;
00601 
00602       //ROS_INFO("PeopleTrackingRaiAlgNode::faceDetections_callback: New Message Received"); 
00603       
00604       //blocks face detection mutex
00605       this->faceDetections_mutex_.enter(); 
00606       
00607       //sets current (received) detections
00608       for (ii=0; ii<msg->faces.size(); ii++)
00609       {
00610             //set time stamp
00611             newDetection.timeStamp.set(msg->header.stamp.sec, msg->header.stamp.nsec);
00612             
00613             //get message data
00614             newDetection.faceLoc.setX(msg->faces[ii].position3D.x);
00615             newDetection.faceLoc.setY(msg->faces[ii].position3D.y);
00616             newDetection.faceLoc.setZ(msg->faces[ii].position3D.z);
00617             newDetection.bbX = msg->faces[ii].imageBoundingBox.x;
00618             newDetection.bbY = msg->faces[ii].imageBoundingBox.y;
00619             newDetection.bbW = msg->faces[ii].imageBoundingBox.width;
00620             newDetection.bbH = msg->faces[ii].imageBoundingBox.height;
00621             
00622             //add detection to tracker list
00623             tracker.addDetection(newDetection);
00624       }
00625       
00626       //unblocks face detection mutex
00627       this->faceDetections_mutex_.exit();
00628 }
00629 
00630 void PeopleTrackingRaiAlgNode::bodyDetections_callback(const pal_vision_msgs::HogDetections::ConstPtr& msg) 
00631 { 
00632 //   ROS_INFO("PeopleTrackingRaiAlgNode::bodyDetections_callback: New Message Received"); 
00633         
00634       unsigned int ii, jj;
00635       CbodyObservation newDetection;
00636       std::list<CbodyObservation>::iterator iiB;
00637       
00638       //blocks body detection mutex
00639       this->bodyDetections_mutex_.enter(); 
00640 
00641       //sets current (received) detections
00642       for (ii=0; ii<msg->persons.size(); ii++)
00643       {
00644 //              CbodyObservation newDetection;
00645                 
00646                 //debug
00647                 hogDetCount ++;
00648                 
00649                 //time stamp
00650                 newDetection.timeStamp.set(msg->header.stamp.sec, msg->header.stamp.nsec);
00651                 
00652                 //bounding box
00653                 newDetection.bbX = msg->persons[ii].imageBoundingBox.x;
00654                 newDetection.bbY = msg->persons[ii].imageBoundingBox.y;
00655                 newDetection.bbW = msg->persons[ii].imageBoundingBox.width;
00656                 newDetection.bbH = msg->persons[ii].imageBoundingBox.height;
00657                 
00658                 //direction
00659                 newDetection.direction.setXYZ(msg->persons[ii].direction.x, msg->persons[ii].direction.y, msg->persons[ii].direction.z);
00660                 
00661                 //rgb Eigen
00662                 newDetection.rgbEigen.setXYZ(msg->persons[ii].principalEigenVectorRGB.r, msg->persons[ii].principalEigenVectorRGB.g, msg->persons[ii].principalEigenVectorRGB.b);
00663                 
00664                 //rgb centers
00665                 newDetection.rgbCenters.resize(msg->persons[ii].rgbClusterCenters.size());
00666                 for(jj=0; jj<newDetection.rgbCenters.size(); jj++)
00667                 {
00668                         newDetection.rgbCenters.at(jj).setXYZ(msg->persons[ii].rgbClusterCenters[jj].r, msg->persons[ii].rgbClusterCenters[jj].g, msg->persons[ii].rgbClusterCenters[jj].b);
00669                 }       
00670                 
00671                 //hog descriptor
00672                 newDetection.hog.resize(msg->persons[ii].hog.size());
00673                 for(jj=0; jj<newDetection.hog.size(); jj++)
00674                 {
00675                         newDetection.hog[jj] = msg->persons[ii].hog[jj];
00676                         //hogFile << msg->persons[ii].hog[jj] << " ";
00677                 }
00678                 //hogFile << std::endl;
00679                 
00680                 //rgbDescriptors
00681                 if (this->verboseMode) std::cout << "rgbDescriptor1.size(): " << msg->persons[ii].rgbDescriptor1.size() << std::endl;
00682                                 
00683                 //add detection to tracker list
00684                 tracker.addDetection(newDetection);
00685         }
00686         //if (this->verboseMode) tracker.printDetectionSets();
00687         this->bodyDetections_mutex_.exit();
00688         //hogFile << "#" << std::endl;
00689 }
00690 
00691 void PeopleTrackingRaiAlgNode::odometry_callback(const nav_msgs::Odometry::ConstPtr& msg) 
00692 { 
00693         //ROS_INFO("PeopleTrackingRaiAlgNode::odometry_callback: New Message Received"); 
00694         
00695         double vx,vy,vz,vTrans;
00696         double tLast, dT;
00697 
00698         //use appropiate mutex to shared variables if necessary 
00699         this->odometry_mutex_.enter(); 
00700 
00701         //if (this->verboseMode) std::cout << msg->data << std::endl; 
00702         tLast = platformOdometry.timeStamp.get();
00703         platformOdometry.timeStamp.setToNow();
00704         dT = platformOdometry.timeStamp.get() - tLast;
00705         //if (this->verboseMode) std::cout << "******************** dT = " << dT << std::endl;
00706         vx = msg->twist.twist.linear.x; 
00707         vy = msg->twist.twist.linear.y;
00708         vz = msg->twist.twist.linear.z;
00709         vTrans = sqrt(vx*vx+vy*vy+vz*vz);  //odometry observation considers only forward velocity
00710         platformOdometry.accumDeltaTrans(dT*vTrans);
00711         platformOdometry.accumDeltaH(dT*msg->twist.twist.angular.z);
00712         
00713         //unlock previously blocked shared variables 
00714         //this->alg_.unlock(); 
00715         this->odometry_mutex_.exit(); 
00716 }
00717 
00718 void PeopleTrackingRaiAlgNode::legDetections_callback(const pal_vision_msgs::LegDetections::ConstPtr& msg) 
00719 { 
00720   //ROS_INFO("PeopleTrackingRaiAlgNode::legDetections_callback: New Message Received"); 
00721         Cpoint3dObservation newDetection;
00722         
00723         //locks leg detection mutex
00724         this->legDetections_mutex_.enter(); 
00725   
00726         //tracker.resetDetectionSets(LEGS);//deletes previous detections
00727         
00728         //sets current (received) detections
00729         for (unsigned int ii=0; ii<msg->position3D.size(); ii++)
00730         {
00731 //              Cpoint3dObservation newDetection;
00732                 newDetection.timeStamp.set(msg->header.stamp.sec, msg->header.stamp.nsec);
00733                 newDetection.point.setXYZ(msg->position3D[ii].x, msg->position3D[ii].y, 0.0);
00734                 newDetection.point.setXYcov(0.2,0.2,0);
00735                 tracker.addDetection(newDetection);
00736         }
00737         
00738         //unlocks leg detection mutex
00739         this->legDetections_mutex_.exit();
00740 }
00741 
00742 void PeopleTrackingRaiAlgNode::image_callback(const sensor_msgs::ImageConstPtr& msg)
00743 {
00744 //      ROS_INFO("PeopleTrackingRaiAlgNode::image_callback: New Message Received");
00745 //      this->alg_.lock();
00746         this->image_mutex_.enter();
00747         try
00748         {
00749                 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00750         }
00751         catch (cv_bridge::Exception& e)
00752         {
00753                 ROS_ERROR("cv_bridge exception: %s", e.what());
00754                 return;
00755         }
00756 //      this->alg_.unlock();
00757         this->image_mutex_.exit();
00758 }
00759 
00760 /*  [service callbacks] */
00761 
00762 /*  [action callbacks] */
00763 
00764 /*  [action requests] */
00765 
00766 void PeopleTrackingRaiAlgNode::node_config_update(Config &config, uint32_t level)
00767 {
00768   this->alg_.lock();
00769 
00770   this->alg_.unlock();
00771 }
00772 
00773 void PeopleTrackingRaiAlgNode::addNodeDiagnostics(void)
00774 {
00775 }
00776 
00777 /* main function */
00778 int main(int argc,char *argv[])
00779 {
00780   return algorithm_base::main<PeopleTrackingRaiAlgNode>(argc, argv, "people_tracking_rai_alg_node");
00781 }


iri_people_tracking_rai
Author(s): Andreu Corominas Murtra, andreu@beta-robots.com
autogenerated on Fri Dec 6 2013 23:46:51