people_follower_client_alg_node.cpp
Go to the documentation of this file.
00001 #include "people_follower_client_alg_node.h"
00002 #include <tf/transform_listener.h>
00003 
00004 PeopleFollowerClientAlgNode::PeopleFollowerClientAlgNode(void) :
00005   algorithm_base::IriBaseAlgorithm<PeopleFollowerClientAlgorithm>(),
00006   follow_client_("followTarget", true),
00007   action_active_(false),
00008   target_id_(-1)
00009 {
00010   //init class attributes if necessary
00011   loop_rate_ = 10;//in [Hz]
00012 
00013   // [init publishers]
00014   target_pos_publisher_ = public_node_handle_.advertise<geometry_msgs::PoseStamped>("target_pose", 10);
00015 
00016   // [init subscribers]
00017   people_tracker_subscriber_ = public_node_handle_.subscribe("people_tracker", 100, &PeopleFollowerClientAlgNode::people_tracker_callback, this);
00018 
00019   // [init services]
00020 
00021   // [init clients]
00022 
00023   // [init action servers]
00024 
00025   // [init action clients]
00026 }
00027 
00028 PeopleFollowerClientAlgNode::~PeopleFollowerClientAlgNode(void)
00029 {
00030   // [free dynamic memory]
00031 }
00032 
00033 void PeopleFollowerClientAlgNode::mainNodeThread(void)
00034 {
00035   // [fill msg structures]
00036 
00037   // [fill srv structure and make request to the server]
00038 
00039   // [fill action structure and make request to the action server]
00040 
00041   // [publish messages]
00042   if(action_active_)
00043     target_pos_publisher_.publish(PoseStamped_msg_);
00044 }
00045 
00046 /*  [subscriber callbacks] */
00047 void PeopleFollowerClientAlgNode::people_tracker_callback(const iri_people_tracking::peopleTrackingArray::ConstPtr& msg) 
00048 {
00049   //use appropiate mutex to shared variables if necessary
00050   people_tracker_mutex_.enter();
00051   ROS_DEBUG("PeopleFollowerClientAlgNode::people_tracker_callback: New Message Received (make_request=%d)", action_active_);
00052 
00053   if(!action_active_)
00054   {
00055     unsigned int target_index;
00056 
00057     //if there is someone standing
00058     if(alg_.isSomeoneStanding(msg, target_index))
00059     {
00060       target_id_ = msg->peopleSet[target_index].targetId;
00061       PoseStamped_msg_.header = msg->header;
00062       PoseStamped_msg_.pose.position.x = msg->peopleSet[target_index].x;
00063       PoseStamped_msg_.pose.position.y = msg->peopleSet[target_index].y;
00064       PoseStamped_msg_.pose.orientation = tf::createQuaternionMsgFromYaw(0.f);
00065 
00066       //make follow request
00067       followMakeActionRequest();
00068     }
00069   }
00070   //action is active
00071   else
00072   {
00073     bool found = false;
00074 
00075     //search for desired target id
00076     for(unsigned int ii=0; ii<msg->peopleSet.size(); ii++)
00077     {
00078       if(msg->peopleSet[ii].targetId == target_id_)
00079       {
00080         target_id_=msg->peopleSet[ii].targetId;
00081         PoseStamped_msg_.header = msg->header;
00082         PoseStamped_msg_.pose.position.x = msg->peopleSet[ii].x;
00083         PoseStamped_msg_.pose.position.y = msg->peopleSet[ii].y;
00084         PoseStamped_msg_.pose.orientation = tf::createQuaternionMsgFromYaw(0.f);
00085         found = true;
00086         break;
00087       }
00088     }
00089 
00090     //if target id not found
00091     if(!found)
00092     {
00093       ROS_WARN("PeopleFollowerClientAlgNode::people_tracker_callback: Target ID NOT found!");
00094       // cancel the current goal
00095       follow_client_.cancelGoal();
00096     }
00097   }
00098 
00099   people_tracker_mutex_.exit();
00100 }
00101 
00102 /*  [service callbacks] */
00103 
00104 /*  [action callbacks] */
00105 void PeopleFollowerClientAlgNode::followDone(const actionlib::SimpleClientGoalState& state,  const iri_nav_msgs::followTargetResultConstPtr& result)
00106 {
00107   if( state.toString().compare("SUCCEEDED") == 0 )
00108     ROS_WARN("PeopleFollowerClientAlgNode::followDone: Goal Achieved! %s", state.toString().c_str());
00109   else
00110     ROS_WARN("PeopleFollowerClientAlgNode::followDone: %s", state.toString().c_str());
00111 
00112   people_tracker_mutex_.enter();
00113   action_active_ = false;
00114   ROS_INFO("PeopleFollowerClientAlgNode::followDone");
00115   people_tracker_mutex_.exit();
00116 }
00117 
00118 void PeopleFollowerClientAlgNode::followActive()
00119 {
00120   //ROS_INFO("PeopleFollowerClientAlgNode::followActive: Goal just went active!");
00121 }
00122 
00123 void PeopleFollowerClientAlgNode::followFeedback(const iri_nav_msgs::followTargetFeedbackConstPtr& feedback)
00124 {
00125   //ROS_INFO("PeopleFollowerClientAlgNode::followFeedback: Got Feedback!");
00126 
00127   bool feedback_is_ok = true;
00128 
00129   //analyze feedback
00130   //my_var = feedback->var;
00131 
00132   //if feedback is not what expected, cancel requested goal
00133   if( !feedback_is_ok )
00134   {
00135     follow_client_.cancelGoal();
00136     //ROS_INFO("PeopleFollowerClientAlgNode::followFeedback: Cancelling Action!");
00137   }
00138 }
00139 
00140 /*  [action requests] */
00141 void PeopleFollowerClientAlgNode::followMakeActionRequest()
00142 {
00143   ROS_INFO("PeopleFollowerClientAlgNode::followMakeActionRequest: Starting New Request!");
00144 
00145   //wait for the action server to start
00146   //will wait for infinite time
00147   follow_client_.waitForServer();
00148   ROS_INFO("PeopleFollowerClientAlgNode::followMakeActionRequest: Server is Available!");
00149 
00150   //send a goal to the action
00151   ROS_INFO("PeopleFollowerClientAlgNode::followMakeActionRequest: target_id_=%d",this->target_id_);
00152   follow_client_.sendGoal(follow_goal_,
00153               boost::bind(&PeopleFollowerClientAlgNode::followDone,     this, _1, _2),
00154               boost::bind(&PeopleFollowerClientAlgNode::followActive,   this),
00155               boost::bind(&PeopleFollowerClientAlgNode::followFeedback, this, _1));
00156   action_active_=true;
00157   ROS_INFO("PeopleFollowerClientAlgNode::followMakeActionRequest: Goal Sent. Wait for Result!");
00158 }
00159 
00160 void PeopleFollowerClientAlgNode::node_config_update(Config &config, uint32_t level)
00161 {
00162   this->alg_.lock();
00163 
00164   this->alg_.unlock();
00165 }
00166 
00167 void PeopleFollowerClientAlgNode::addNodeDiagnostics(void)
00168 {
00169 }
00170 
00171 /* main function */
00172 int main(int argc,char *argv[])
00173 {
00174   return algorithm_base::main<PeopleFollowerClientAlgNode>(argc, argv, "people_follower_client_alg_node");
00175 }


iri_people_follower_client
Author(s):
autogenerated on Fri Dec 6 2013 21:02:34