tibi_dabo_head_tracking_alg_node.cpp
Go to the documentation of this file.
00001 #include "tibi_dabo_head_tracking_alg_node.h"
00002 
00003 TibiDaboHeadTrackingAlgNode::TibiDaboHeadTrackingAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<TibiDaboHeadTrackingAlgorithm>(),
00005   follow_target_aserver_(public_node_handle_, "follow_target")
00006 {
00007   //init class attributes if necessary
00008   this->loop_rate_ = 50;//in [Hz]
00009 
00010   // [init publishers]
00011   this->joint_position_publisher_ = this->public_node_handle_.advertise<trajectory_msgs::JointTrajectoryPoint>("joint_position", 10);
00012   
00013   // [init subscribers]
00014   this->target_joints_subscriber_ = this->public_node_handle_.subscribe("target_joints", 10, &TibiDaboHeadTrackingAlgNode::target_joints_callback, this);
00015   this->joint_feedback_subscriber_ = this->public_node_handle_.subscribe("joint_feedback", 10, &TibiDaboHeadTrackingAlgNode::joint_feedback_callback, this);
00016   
00017   // [init services]
00018   
00019   // [init clients]
00020   
00021   // [init action servers]
00022   follow_target_aserver_.registerStartCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetStartCallback, this, _1)); 
00023   follow_target_aserver_.registerStopCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetStopCallback, this)); 
00024   follow_target_aserver_.registerIsFinishedCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetIsFinishedCallback, this)); 
00025   follow_target_aserver_.registerHasSucceedCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetHasSucceedCallback, this)); 
00026   follow_target_aserver_.registerGetResultCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetGetResultCallback, this, _1)); 
00027   follow_target_aserver_.registerGetFeedbackCallback(boost::bind(&TibiDaboHeadTrackingAlgNode::follow_targetGetFeedbackCallback, this, _1)); 
00028   follow_target_aserver_.start();
00029   
00030   // [init action clients]
00031   this->mode = 0; //0=relative, 1=absolute
00032   this->target_pan=0.0;
00033   this->target_tilt=0.0;
00034   this->pan_angle  = 0.0;
00035   this->tilt_angle = 0.0;
00036   this->current_state_=IDLE_STATE;
00037   this->event_server=CEventServer::instance();
00038   this->feedback_on_event_id="head_tracking_feedback_event";
00039   this->event_server->create_event(this->feedback_on_event_id);
00040   // initialize the state machine events
00041   this->new_req_event_id_="head_tracking_new_request_event";
00042   this->event_server->create_event(this->new_req_event_id_);
00043   // initialize the topic message
00044   this->JointTrajectoryPoint_msg_.positions.resize(3);
00045   this->JointTrajectoryPoint_msg_.positions[0]=0.0*3.14159/180.0;
00046   this->JointTrajectoryPoint_msg_.positions[1]=0.0*3.14159/180.0;
00047   this->JointTrajectoryPoint_msg_.positions[2]=0.0*3.14159/180.0;
00048   this->JointTrajectoryPoint_msg_.velocities.resize(3);
00049   this->JointTrajectoryPoint_msg_.velocities[0]=200.0*3.14159/180.0;
00050   this->JointTrajectoryPoint_msg_.velocities[1]=200.0*3.14159/180.0;
00051   this->JointTrajectoryPoint_msg_.velocities[2]=200.0*3.14159/180.0;
00052   this->JointTrajectoryPoint_msg_.accelerations.resize(3);
00053   this->JointTrajectoryPoint_msg_.accelerations[0]=10.0*3.14159/180.0;
00054   this->JointTrajectoryPoint_msg_.accelerations[1]=10.0*3.14159/180.0;
00055   this->JointTrajectoryPoint_msg_.accelerations[2]=10.0*3.14159/180.0;
00056   this->JointTrajectoryPoint_msg_.time_from_start=ros::Duration(0,0);
00057 
00058   // move the head to the stand by position
00059   this->joint_position_publisher_.publish(this->JointTrajectoryPoint_msg_);
00060 }
00061 
00062 TibiDaboHeadTrackingAlgNode::~TibiDaboHeadTrackingAlgNode(void)
00063 {
00064   this->current_state_=IDLE_STATE;
00065   // [free dynamic memory]
00066   if(this->feedback_on_event_id!="")
00067   {
00068     this->event_server->delete_event(this->feedback_on_event_id);
00069     this->feedback_on_event_id="";
00070   }
00071   if(this->new_req_event_id_!="")
00072   { 
00073     this->event_server->delete_event(this->new_req_event_id_);
00074     this->new_req_event_id_!="";
00075   }
00076 }
00077 
00078 void TibiDaboHeadTrackingAlgNode::mainNodeThread(void)
00079 {
00080   float pan=0.0, tilt=0.0;
00081 
00082   // [fill msg structures]
00083   //this->JointTrajectoryPoint_msg.data = my_var;
00084   
00085   // [fill srv structure and make request to the server]
00086   
00087   // [fill action structure and make request to the action server]
00088 
00089   // [publish messages]
00090   switch(this->current_state_)
00091   {
00092     case IDLE_STATE:
00093 //      ROS_INFO("IDLE_STATE");
00094       //once we receive a follow target request
00095       //jumpt to request move base request state
00096       if(this->event_server->event_is_set(this->new_req_event_id_))
00097         current_state_ = FOLLOW_STATE;
00098       break;
00099 
00100     case FOLLOW_STATE:
00101       if(this->event_server->event_is_set(this->feedback_on_event_id))
00102       {
00103 //        ROS_INFO("FOLLOW_STATE");
00104         // compute head position
00105         ROS_INFO("target pan,tilt: %f,%f", this->target_pan, this->target_tilt);
00106         this->alg_.compute_head_position(this->target_pan,this->target_tilt,&pan,&tilt);
00107         this->JointTrajectoryPoint_msg_.positions[0]=pan;
00108         this->JointTrajectoryPoint_msg_.positions[1]=tilt;
00109         this->joint_position_publisher_.publish(this->JointTrajectoryPoint_msg_);
00110       }
00111       if(this->event_server->event_is_set(this->new_req_event_id_))
00112         this->current_state_=FOLLOW_STATE;
00113       else
00114         this->current_state_=IDLE_STATE;
00115       break;
00116     case PREEMPT_STATE:
00117 //      ROS_WARN("PREEMPT_STATE");
00118       follow_target_aserver_.setPreempted();
00119       if(this->event_server->event_is_set(this->new_req_event_id_))
00120         this->event_server->reset_event(this->new_req_event_id_);
00121       current_state_ = IDLE_STATE;
00122       break;
00123   }
00124 }
00125 
00126 /*  [subscriber callbacks] */
00127 void TibiDaboHeadTrackingAlgNode::target_joints_callback(const trajectory_msgs::JointTrajectoryPoint::ConstPtr& msg) 
00128 { 
00129 //  ROS_INFO("TibiDaboHeadTrackingAlgNode::target_pos_callback: New Message Received"); 
00130 
00131   //use appropiate mutex to shared variables if necessary 
00132   this->alg_.lock(); 
00133   //this->target_pos_mutex_.enter(); 
00134 
00135   //  std::cout << "new target position" << std::endl;
00136   if(this->mode==0) //relative
00137   {
00138     this->target_pan=msg->positions[0]+this->pan_angle;
00139     this->target_tilt=msg->positions[1]+this->tilt_angle;
00140   }
00141   else //absolute
00142   {
00143     this->target_pan=msg->positions[0];
00144     this->target_tilt=msg->positions[1];
00145   }
00146   //unlock previously blocked shared variables 
00147   this->alg_.unlock(); 
00148   //this->target_pos_mutex_.exit(); 
00149 }
00150 void TibiDaboHeadTrackingAlgNode::joint_feedback_callback(const sensor_msgs::JointState::ConstPtr& msg) 
00151 { 
00152   unsigned int i=0;
00153   //double pan_angle=0.0,tilt_angle=0.0;
00154 
00155   //use appropiate mutex to shared variables if necessary 
00156   //this->joint_feedback_mutex_.enter(); 
00157 
00158   for(i=0;i<msg->name.size();i++)
00159   {
00160     if(msg->name[i]=="pan")
00161       this->pan_angle=msg->position[i];
00162     else if(msg->name[i]=="tilt")
00163       this->tilt_angle=msg->position[i];
00164   }
00165   this->alg_.set_current_head_position(this->pan_angle,this->tilt_angle);
00166   if(!this->event_server->event_is_set(this->feedback_on_event_id))
00167     this->event_server->set_event(this->feedback_on_event_id);
00168 
00169   //unlock previously blocked shared variables 
00170   //this->joint_feedback_mutex_.exit(); 
00171 }
00172 
00173 /*  [service callbacks] */
00174 
00175 /*  [action callbacks] */
00176 void TibiDaboHeadTrackingAlgNode::follow_targetStartCallback(const iri_nav_msgs::followTargetGoalConstPtr& goal)
00177 { 
00178   alg_.lock(); 
00179     //check goal 
00180   if(!this->event_server->event_is_set(this->new_req_event_id_))
00181     this->event_server->set_event(this->new_req_event_id_);
00182     //execute goal 
00183   alg_.unlock(); 
00184 } 
00185 
00186 void TibiDaboHeadTrackingAlgNode::follow_targetStopCallback(void) 
00187 { 
00188   alg_.lock(); 
00189   if(this->event_server->event_is_set(this->new_req_event_id_))
00190     this->event_server->reset_event(this->new_req_event_id_);
00191     //stop action 
00192   alg_.unlock(); 
00193 } 
00194 
00195 bool TibiDaboHeadTrackingAlgNode::follow_targetIsFinishedCallback(void) 
00196 { 
00197   bool ret = false; 
00198 
00199   alg_.lock(); 
00200     //if action has finish for any reason 
00201     //ret = true; 
00202   alg_.unlock(); 
00203 
00204   return ret; 
00205 } 
00206 
00207 bool TibiDaboHeadTrackingAlgNode::follow_targetHasSucceedCallback(void) 
00208 { 
00209   bool ret = true; 
00210 
00211   alg_.lock(); 
00212     //if goal was accomplished 
00213     //ret = true 
00214   alg_.unlock(); 
00215 
00216   return ret; 
00217 } 
00218 
00219 void TibiDaboHeadTrackingAlgNode::follow_targetGetResultCallback(iri_nav_msgs::followTargetResultPtr& result) 
00220 { 
00221   alg_.lock(); 
00222     //update result data to be sent to client 
00223     //result->data = data; 
00224   alg_.unlock(); 
00225 } 
00226 
00227 void TibiDaboHeadTrackingAlgNode::follow_targetGetFeedbackCallback(iri_nav_msgs::followTargetFeedbackPtr& feedback) 
00228 { 
00229   float pan_error,tilt_error;
00230 
00231   alg_.lock(); 
00232     //keep track of feedback 
00233   this->alg_.get_current_error(&pan_error,&tilt_error);  
00234   feedback->current_dist=sqrt(pan_error*pan_error+tilt_error*tilt_error);
00235     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00236   alg_.unlock(); 
00237 }
00238 
00239 /*  [action requests] */
00240 
00241 void TibiDaboHeadTrackingAlgNode::node_config_update(Config &config, uint32_t level)
00242 {
00243   this->alg_.lock();
00244   this->JointTrajectoryPoint_msg_.velocities[0]=config.velocity*3.14159/180.0;
00245   this->JointTrajectoryPoint_msg_.velocities[1]=config.velocity*3.14159/180.0;
00246   this->JointTrajectoryPoint_msg_.velocities[2]=config.velocity*3.14159/180.0;
00247   this->mode = config.mode;
00248   this->alg_.unlock();
00249 }
00250 
00251 void TibiDaboHeadTrackingAlgNode::addNodeDiagnostics(void)
00252 {
00253 }
00254 
00255 /* main function */
00256 int main(int argc,char *argv[])
00257 {
00258   return algorithm_base::main<TibiDaboHeadTrackingAlgNode>(argc, argv, "tibi_dabo_head_tracking_alg_node");
00259 }


tibi_dabo_head_tracking
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 22:00:28