darwin_simple_nav_alg_node.cpp
Go to the documentation of this file.
00001 #include "darwin_simple_nav_alg_node.h"
00002 #include <tf/transform_broadcaster.h>
00003 
00004 DarwinSimpleNavAlgNode::DarwinSimpleNavAlgNode(void) :
00005   algorithm_base::IriBaseAlgorithm<DarwinSimpleNavAlgorithm>(),
00006   target_pose_aserver_(public_node_handle_, "target_pose"),
00007   tf_listener(ros::Duration(10.0))
00008 {
00009   //init class attributes if necessary
00010   //this->loop_rate_ = 2;//in [Hz]
00011 
00012   // [init publishers]
00013   this->cmd_vel_publisher_ = this->public_node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 100);
00014   
00015   // [init subscribers]
00016   this->current_pose_subscriber_ = this->public_node_handle_.subscribe("current_pose", 100, &DarwinSimpleNavAlgNode::current_pose_callback, this);
00017   
00018   // [init services]
00019   
00020   // [init clients]
00021   
00022   // [init action servers]
00023   target_pose_aserver_.registerStartCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseStartCallback, this, _1)); 
00024   target_pose_aserver_.registerStopCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseStopCallback, this)); 
00025   target_pose_aserver_.registerIsFinishedCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseIsFinishedCallback, this)); 
00026   target_pose_aserver_.registerHasSucceedCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseHasSucceedCallback, this)); 
00027   target_pose_aserver_.registerGetResultCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseGetResultCallback, this, _1)); 
00028   target_pose_aserver_.registerGetFeedbackCallback(boost::bind(&DarwinSimpleNavAlgNode::target_poseGetFeedbackCallback, this, _1)); 
00029   
00030   // [init action clients]
00031 
00032   this->finished=true;
00033 }
00034 
00035 DarwinSimpleNavAlgNode::~DarwinSimpleNavAlgNode(void)
00036 {
00037   // [free dynamic memory]
00038 }
00039 
00040 void DarwinSimpleNavAlgNode::mainNodeThread(void)
00041 {
00042   // [fill msg structures]
00043   //this->Twist_msg_.data = my_var;
00044   
00045   // [fill srv structure and make request to the server]
00046   
00047   // [fill action structure and make request to the action server]
00048 
00049   // [publish messages]
00050 }
00051 
00052 /*  [subscriber callbacks] */
00053 void DarwinSimpleNavAlgNode::current_pose_callback(const nav_msgs::Odometry::ConstPtr& msg) 
00054 { 
00055   geometry_msgs::PoseStamped current_pose;
00056   T2DPose target_pose;
00057   static bool first=true;
00058   double vt=0.0,vr=0.0;
00059   bool tf_exists;
00060 
00061   // the first time an odometry message is received, start the action server
00062   if(first)
00063   {
00064     target_pose_aserver_.start();
00065     first=false;
00066   }
00067 
00068   //use appropiate mutex to shared variables if necessary 
00069   //this->alg_.lock(); 
00070   this->current_pose_mutex_.enter(); 
00071 
00072   //std::cout << msg->data << std::endl; 
00073   // store the current pose
00074   try{
00075     // save the current pose of the robot
00076     this->current_pose.header=msg->header;
00077     this->current_pose.pose=msg->pose.pose;
00078     // compute the platform velocities
00079     if(!this->finished)
00080     {
00081       // translate the target goal to the current MP_BODY frame
00082       this->target_pose.header.stamp=ros::Time::now();
00083       tf_exists = tf_listener.waitForTransform(this->target_pose.header.frame_id,std::string("/MP_BODY"),this->target_pose.header.stamp ,
00084                                                ros::Duration(10),ros::Duration(0.01));
00085       tf_listener.transformPose(std::string("/MP_BODY"),this->target_pose,current_pose);
00086       // generate the updated target goal
00087       target_pose.x=current_pose.pose.position.x;
00088       target_pose.y=current_pose.pose.position.y;
00089       target_pose.theta=atan2(target_pose.y,target_pose.x);
00090       ROS_INFO("Target pos: x: %f, y: %f, theta: %f",target_pose.x,target_pose.y,target_pose.theta);
00091 
00092       if(this->alg_.compute_robot_speed(target_pose,vt,vr))
00093         this->finished=true;
00094       ROS_INFO("vt: %f, vr: %f",vt,vr);
00095       // create the twist
00096       this->Twist_msg_.linear.x=vt;
00097       this->Twist_msg_.linear.y=0.0;
00098       this->Twist_msg_.linear.z=0.0;
00099       this->Twist_msg_.angular.x=0.0;
00100       this->Twist_msg_.angular.y=0.0;
00101       this->Twist_msg_.angular.z=vr;
00102       // send the twist
00103       this->cmd_vel_publisher_.publish(this->Twist_msg_);
00104       //unlock previously blocked shared variables 
00105       //this->alg_.unlock(); 
00106     }
00107   }catch(tf::TransformException ex){
00108     ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00109   }
00110 
00111   this->current_pose_mutex_.exit(); 
00112 }
00113 
00114 /*  [service callbacks] */
00115 
00116 /*  [action callbacks] */
00117 void DarwinSimpleNavAlgNode::target_poseStartCallback(const move_base_msgs::MoveBaseGoalConstPtr& goal)
00118 { 
00119   geometry_msgs::PoseStamped local_goal;
00120   bool tf_exists;
00121 
00122   alg_.lock(); 
00123     //check goal 
00124   // transform the goal the the MP_BODY frame
00125   try{
00126     tf_exists = tf_listener.waitForTransform(goal->target_pose.header.frame_id,std::string("/odom"), goal->target_pose.header.stamp,
00127                                              ros::Duration(10),ros::Duration(0.01));
00128     tf_listener.transformPose(std::string("/odom"),goal->target_pose,this->target_pose);
00129 
00130     //save the target position
00131     this->finished=false;
00132   }catch(tf::TransformException ex){
00133     ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00134   }
00135     //execute goal 
00136   alg_.unlock(); 
00137 } 
00138 
00139 void DarwinSimpleNavAlgNode::target_poseStopCallback(void) 
00140 { 
00141   alg_.lock(); 
00142     //stop action 
00143   alg_.unlock(); 
00144 } 
00145 
00146 bool DarwinSimpleNavAlgNode::target_poseIsFinishedCallback(void) 
00147 { 
00148   bool ret = false; 
00149 
00150   alg_.lock(); 
00151     //if action has finish for any reason 
00152   ret=this->finished;
00153     //ret = true; 
00154   alg_.unlock(); 
00155 
00156   return ret; 
00157 } 
00158 
00159 bool DarwinSimpleNavAlgNode::target_poseHasSucceedCallback(void) 
00160 { 
00161   bool ret = false; 
00162 
00163   alg_.lock(); 
00164     //if goal was accomplished 
00165     ret = true; 
00166   alg_.unlock(); 
00167 
00168   return ret; 
00169 } 
00170 
00171 void DarwinSimpleNavAlgNode::target_poseGetResultCallback(move_base_msgs::MoveBaseResultPtr& result) 
00172 { 
00173   alg_.lock(); 
00174     //update result data to be sent to client 
00175     //result->data = data; 
00176   alg_.unlock(); 
00177 } 
00178 
00179 void DarwinSimpleNavAlgNode::target_poseGetFeedbackCallback(move_base_msgs::MoveBaseFeedbackPtr& feedback) 
00180 {
00181   alg_.lock(); 
00182     //keep track of feedback 
00183   // return the current platform position
00184   this->current_pose_mutex_.enter();
00185   feedback->base_position.header.seq=0;
00186   feedback->base_position.header.stamp=ros::Time::now();
00187   feedback->base_position.header.frame_id="/odom";
00188   feedback->base_position.pose=this->current_pose.pose;
00189   this->current_pose_mutex_.exit();
00190     //ROS_INFO("feedback: %s", feedback->data.c_str()); 
00191   alg_.unlock(); 
00192 }
00193 
00194 /*  [action requests] */
00195 
00196 void DarwinSimpleNavAlgNode::node_config_update(Config &config, uint32_t level)
00197 {
00198   this->alg_.lock();
00199 
00200   this->alg_.unlock();
00201 }
00202 
00203 void DarwinSimpleNavAlgNode::addNodeDiagnostics(void)
00204 {
00205 }
00206 
00207 /* main function */
00208 int main(int argc,char *argv[])
00209 {
00210   return algorithm_base::main<DarwinSimpleNavAlgNode>(argc, argv, "darwin_simple_nav_alg_node");
00211 }


darwin_simple_nav
Author(s): Sergi Hernandez Juan
autogenerated on Fri Dec 6 2013 23:08:48