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
00010
00011
00012
00013 this->cmd_vel_publisher_ = this->public_node_handle_.advertise<geometry_msgs::Twist>("cmd_vel", 100);
00014
00015
00016 this->current_pose_subscriber_ = this->public_node_handle_.subscribe("current_pose", 100, &DarwinSimpleNavAlgNode::current_pose_callback, this);
00017
00018
00019
00020
00021
00022
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
00031
00032 this->finished=true;
00033 }
00034
00035 DarwinSimpleNavAlgNode::~DarwinSimpleNavAlgNode(void)
00036 {
00037
00038 }
00039
00040 void DarwinSimpleNavAlgNode::mainNodeThread(void)
00041 {
00042
00043
00044
00045
00046
00047
00048
00049
00050 }
00051
00052
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
00062 if(first)
00063 {
00064 target_pose_aserver_.start();
00065 first=false;
00066 }
00067
00068
00069
00070 this->current_pose_mutex_.enter();
00071
00072
00073
00074 try{
00075
00076 this->current_pose.header=msg->header;
00077 this->current_pose.pose=msg->pose.pose;
00078
00079 if(!this->finished)
00080 {
00081
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
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
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
00103 this->cmd_vel_publisher_.publish(this->Twist_msg_);
00104
00105
00106 }
00107 }catch(tf::TransformException ex){
00108 ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00109 }
00110
00111 this->current_pose_mutex_.exit();
00112 }
00113
00114
00115
00116
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
00124
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
00131 this->finished=false;
00132 }catch(tf::TransformException ex){
00133 ROS_ERROR("NoCollisionAlgNode:: %s",ex.what());
00134 }
00135
00136 alg_.unlock();
00137 }
00138
00139 void DarwinSimpleNavAlgNode::target_poseStopCallback(void)
00140 {
00141 alg_.lock();
00142
00143 alg_.unlock();
00144 }
00145
00146 bool DarwinSimpleNavAlgNode::target_poseIsFinishedCallback(void)
00147 {
00148 bool ret = false;
00149
00150 alg_.lock();
00151
00152 ret=this->finished;
00153
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
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
00175
00176 alg_.unlock();
00177 }
00178
00179 void DarwinSimpleNavAlgNode::target_poseGetFeedbackCallback(move_base_msgs::MoveBaseFeedbackPtr& feedback)
00180 {
00181 alg_.lock();
00182
00183
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
00191 alg_.unlock();
00192 }
00193
00194
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
00208 int main(int argc,char *argv[])
00209 {
00210 return algorithm_base::main<DarwinSimpleNavAlgNode>(argc, argv, "darwin_simple_nav_alg_node");
00211 }