tibi_dabo_face_tracking_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_face_tracking_alg_node.h"
00002 
00003 TibiDaboFaceTrackingAlgNode::TibiDaboFaceTrackingAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboFaceTrackingAlgorithm>(),
00005   follow_target_client_("follow_target", true)
00006 {
00007   //init class attributes if necessary
00008   this->loop_rate_ = 20;//in [Hz]
00009 
00010   // [init publishers]
00011   this->target_joints_publisher_ = this->public_node_handle_.advertise<trajectory_msgs::JointTrajectoryPoint>("target_joints", 10);
00012   
00013   // [init subscribers]
00014   this->face_detections_subscriber_ = this->public_node_handle_.subscribe("face_detections", 100, &TibiDaboFaceTrackingAlgNode::face_detections_callback, this);
00015   
00016   // [init services]
00017   
00018   // [init clients]
00019   
00020   // [init action servers]
00021   
00022   // [init action clients]
00023 
00024   this->JointTrajectoryPoint_msg_.positions.resize(2);
00025   this->JointTrajectoryPoint_msg_.positions[0]=0;
00026   this->JointTrajectoryPoint_msg_.positions[1]=0;
00027   this->JointTrajectoryPoint_msg_.velocities.resize(2);
00028   this->JointTrajectoryPoint_msg_.velocities[0]=2.61;
00029   this->JointTrajectoryPoint_msg_.velocities[1]=2.61;
00030   
00031   //TODO: read from camera_info
00032   this->image_width    = 512;
00033   this->image_height   = 384;
00034   this->focus_length_x = 833;
00035   this->focus_length_y = 835;
00036 
00037   this->first=true;
00038 }
00039 
00040 TibiDaboFaceTrackingAlgNode::~TibiDaboFaceTrackingAlgNode(void)
00041 {
00042   // [free dynamic memory]
00043 }
00044 
00045 void TibiDaboFaceTrackingAlgNode::mainNodeThread(void)
00046 {
00047   // [fill msg structures]
00048   //this->JointTrajectoryPoint_msg_.data = my_var;
00049   
00050   // [fill srv structure and make request to the server]
00051   
00052   // [fill action structure and make request to the action server]
00053 }
00054 
00055 /*  [subscriber callbacks] */
00056 void TibiDaboFaceTrackingAlgNode::face_detections_callback(const face_detector_mono::RectArray::ConstPtr& msg) 
00057 { 
00058   //static bool first=true;
00059   double x_mm=0.0, pan_angle=0.0, y_mm=0.0, tilt_angle=0.0;
00060   ROS_INFO("TibiDaboFaceTrackingAlgNode::face_detections_callback: New Message Received"); 
00061 
00062   //use appropiate mutex to shared variables if necessary 
00063   //this->alg_.lock(); 
00064   //this->face_detections_mutex_.enter(); 
00065 
00066   //std::cout << msg->data << std::endl;
00067 
00068   if(msg->rects.size()!=0)
00069   {
00070     x_mm = 2*(msg->rects[0].x + msg->rects[0].width/2.0  - this->image_width/2.0);
00071     pan_angle = -atan2(x_mm, focus_length_x);
00072 
00073     y_mm = 2*(msg->rects[0].y  +msg->rects[0].height/2.0 - this->image_height/2.0);
00074     tilt_angle = -atan2(y_mm, focus_length_y);
00075 
00076     this->JointTrajectoryPoint_msg_.positions[0]=pan_angle;
00077     this->JointTrajectoryPoint_msg_.positions[1]=tilt_angle;
00078 
00079     ROS_INFO("TibiDaboFaceTrackingAlgNode::face_detections_callback: pan %f, tilt %f", pan_angle, tilt_angle);
00080 
00081     if(this->first==true)
00082     {
00083       this->target_joints_publisher_.publish(this->JointTrajectoryPoint_msg_);
00084       this->first=false;
00085       follow_targetMakeActionRequest();
00086     }
00087     else
00088     {
00089       this->target_joints_publisher_.publish(this->JointTrajectoryPoint_msg_);
00090     }
00091   }
00092 
00093   /*if(first==true)
00094   {
00095     if(msg->rects.size()!=0)
00096     {
00097       x_mm=2*(msg->rects[0].x+msg->rects[0].width/2.0-256);
00098       pan_angle=-atan2(x_mm,832);
00099       y_mm=2*(msg->rects[0].y+msg->rects[0].height/2.0-192);
00100       tilt_angle=-atan2(y_mm,832);
00101       this->JointTrajectoryPoint_msg_.positions[0]=pan_angle;
00102       this->JointTrajectoryPoint_msg_.positions[1]=tilt_angle;
00103       // [publish messages]
00104       this->target_joints_publisher_.publish(this->JointTrajectoryPoint_msg_);
00105       first=false;
00106       follow_targetMakeActionRequest();
00107     }
00108   }
00109   else
00110   {
00111     if(msg->rects.size()!=0)
00112     {
00113       x_mm=2*(msg->rects[0].x+msg->rects[0].width/2.0-256);
00114       pan_angle=-atan2(x_mm,832);
00115       y_mm=2*(msg->rects[0].y+msg->rects[0].height/2.0-192);
00116       tilt_angle=-atan2(y_mm,832);
00117       this->JointTrajectoryPoint_msg_.positions[0]=pan_angle;
00118       this->JointTrajectoryPoint_msg_.positions[1]=tilt_angle;
00119       // [publish messages]
00120       this->target_joints_publisher_.publish(this->JointTrajectoryPoint_msg_);
00121     }
00122   }
00123   */
00124 
00125   //unlock previously blocked shared variables 
00126   //this->alg_.unlock(); 
00127   //this->face_detections_mutex_.exit(); 
00128 }
00129 
00130 /*  [service callbacks] */
00131 
00132 /*  [action callbacks] */
00133 void TibiDaboFaceTrackingAlgNode::follow_targetDone(const actionlib::SimpleClientGoalState& state,  const iri_nav_msgs::followTargetResultConstPtr& result) 
00134 { 
00135   if( state.toString().compare("SUCCEEDED") == 0 ) 
00136     ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetDone: Goal Achieved!"); 
00137   else 
00138     ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetDone: %s", state.toString().c_str()); 
00139 
00140   //copy & work with requested result 
00141 } 
00142 
00143 void TibiDaboFaceTrackingAlgNode::follow_targetActive() 
00144 { 
00145   //ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetActive: Goal just went active!"); 
00146 } 
00147 
00148 void TibiDaboFaceTrackingAlgNode::follow_targetFeedback(const iri_nav_msgs::followTargetFeedbackConstPtr& feedback) 
00149 { 
00150   //ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetFeedback: Got Feedback!"); 
00151 
00152   bool feedback_is_ok = true; 
00153 
00154   //analyze feedback 
00155   //my_var = feedback->var; 
00156   
00157   //if feedback is not what expected, cancel requested goal 
00158   if( !feedback_is_ok ) 
00159   { 
00160     follow_target_client_.cancelGoal(); 
00161     //ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetFeedback: Cancelling Action!"); 
00162   } 
00163 }
00164 
00165 /*  [action requests] */
00166 void TibiDaboFaceTrackingAlgNode::follow_targetMakeActionRequest() 
00167 { 
00168   ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetMakeActionRequest: Starting New Request!"); 
00169 
00170   //wait for the action server to start 
00171   //will wait for infinite time 
00172   follow_target_client_.waitForServer();  
00173   ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetMakeActionRequest: Server is Available!"); 
00174 
00175   //send a goal to the action 
00176   //follow_target_goal_.data = my_desired_goal; 
00177   follow_target_client_.sendGoal(follow_target_goal_, 
00178               boost::bind(&TibiDaboFaceTrackingAlgNode::follow_targetDone,     this, _1, _2), 
00179               boost::bind(&TibiDaboFaceTrackingAlgNode::follow_targetActive,   this), 
00180               boost::bind(&TibiDaboFaceTrackingAlgNode::follow_targetFeedback, this, _1)); 
00181   ROS_INFO("TibiDaboFaceTrackingAlgNode::follow_targetMakeActionRequest: Goal Sent. Wait for Result!"); 
00182 }
00183 
00184 void TibiDaboFaceTrackingAlgNode::node_config_update(Config &config, uint32_t level)
00185 {
00186   this->alg_.lock();
00187   //this->first = config.first;
00188   this->alg_.unlock();
00189 }
00190 
00191 void TibiDaboFaceTrackingAlgNode::addNodeDiagnostics(void)
00192 {
00193 }
00194 
00195 /* main function */
00196 int main(int argc,char *argv[])
00197 {
00198   return algorithm_base::main<TibiDaboFaceTrackingAlgNode>(argc, argv, "tibi_dabo_face_tracking_alg_node");
00199 }


tibi_dabo_face_tracking
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:23:46