eband_local_planner_ros.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2010, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Christian Connette
00036  *********************************************************************/
00037 
00038 #include <eband_local_planner/eband_local_planner_ros.h>
00039 
00040 // pluginlib macros (defines, ...)
00041 #include <pluginlib/class_list_macros.h>
00042 
00043 // abstract class from which our plugin inherits
00044 #include <nav_core/base_local_planner.h>
00045 
00046 
00047 // register this planner as a BaseGlobalPlanner plugin
00048 // (see http://www.ros.org/wiki/pluginlib/Tutorials/Writing%20and%20Using%20a%20Simple%20Plugin)
00049 PLUGINLIB_EXPORT_CLASS(eband_local_planner::EBandPlannerROS, nav_core::BaseLocalPlanner)
00050 
00051 
00052   namespace eband_local_planner{
00053 
00054     EBandPlannerROS::EBandPlannerROS() : costmap_ros_(NULL), tf_(NULL), initialized_(false) {}
00055 
00056 
00057     EBandPlannerROS::EBandPlannerROS(std::string name, tf::TransformListener* tf, costmap_2d::Costmap2DROS* costmap_ros)
00058       : costmap_ros_(NULL), tf_(NULL), initialized_(false)
00059     {
00060       // initialize planner
00061       initialize(name, tf, costmap_ros);
00062     }
00063 
00064 
00065     EBandPlannerROS::~EBandPlannerROS() {}
00066 
00067 
00068     void EBandPlannerROS::initialize(std::string name, tf::TransformListener* tf, costmap_2d::Costmap2DROS* costmap_ros)
00069     {
00070       // check if the plugin is already initialized
00071       if(!initialized_)
00072       {
00073         // copy adress of costmap and Transform Listener (handed over from move_base)
00074         costmap_ros_ = costmap_ros;
00075         tf_ = tf;
00076 
00077 
00078         // create Node Handle with name of plugin (as used in move_base for loading)
00079         ros::NodeHandle pn("~/" + name);
00080 
00081         // advertise topics (adapted global plan and predicted local trajectory)
00082         g_plan_pub_ = pn.advertise<nav_msgs::Path>("global_plan", 1);
00083         l_plan_pub_ = pn.advertise<nav_msgs::Path>("local_plan", 1);
00084 
00085 
00086         // subscribe to topics (to get odometry information, we need to get a handle to the topic in the global namespace)
00087         ros::NodeHandle gn;
00088         odom_sub_ = gn.subscribe<nav_msgs::Odometry>("odom", 1, boost::bind(&EBandPlannerROS::odomCallback, this, _1));
00089 
00090 
00091         // create the actual planner that we'll use. Pass Name of plugin and pointer to global costmap to it.
00092         // (configuration is done via parameter server)
00093         eband_ = boost::shared_ptr<EBandPlanner>(new EBandPlanner(name, costmap_ros_));
00094 
00095         // create the according controller
00096         eband_trj_ctrl_ = boost::shared_ptr<EBandTrajectoryCtrl>(new EBandTrajectoryCtrl(name, costmap_ros_));
00097 
00098         // create object for visualization
00099         eband_visual_ = boost::shared_ptr<EBandVisualization>(new EBandVisualization);
00100 
00101         // pass visualization object to elastic band
00102         eband_->setVisualization(eband_visual_);
00103 
00104         // pass visualization object to controller
00105         eband_trj_ctrl_->setVisualization(eband_visual_);
00106 
00107         // initialize visualization - set node handle and pointer to costmap
00108         eband_visual_->initialize(pn, costmap_ros);
00109 
00110         // create and initialize dynamic reconfigure
00111         drs_.reset(new drs(pn));
00112         drs::CallbackType cb = boost::bind(&EBandPlannerROS::reconfigureCallback, this, _1, _2);
00113         drs_->setCallback(cb);
00114 
00115         // set initialized flag
00116         initialized_ = true;
00117 
00118         // this is only here to make this process visible in the rxlogger right from the start
00119         ROS_DEBUG("Elastic Band plugin initialized.");
00120       }
00121       else
00122       {
00123         ROS_WARN("This planner has already been initialized, doing nothing.");
00124       }
00125     }
00126 
00127 
00128     void EBandPlannerROS::reconfigureCallback(EBandPlannerConfig& config,
00129       uint32_t level)
00130     {
00131       xy_goal_tolerance_ = config.xy_goal_tolerance;
00132       yaw_goal_tolerance_ = config.yaw_goal_tolerance;
00133       rot_stopped_vel_ = config.rot_stopped_vel;
00134       trans_stopped_vel_ = config.trans_stopped_vel;
00135 
00136       if (eband_)
00137         eband_->reconfigure(config);
00138       else
00139         ROS_ERROR("Reconfigure CB called before eband planner initialization");
00140 
00141       if (eband_trj_ctrl_)
00142         eband_trj_ctrl_->reconfigure(config);
00143       else
00144         ROS_ERROR("Reconfigure CB called before trajectory controller initialization!");
00145 
00146       if (eband_visual_)
00147         eband_visual_->reconfigure(config);
00148       else
00149         ROS_ERROR("Reconfigure CB called before eband visualizer initialization");
00150     }
00151 
00152 
00153     // set global plan to wrapper and pass it to eband
00154     bool EBandPlannerROS::setPlan(const std::vector<geometry_msgs::PoseStamped>& orig_global_plan)
00155     {
00156 
00157       // check if plugin initialized
00158       if(!initialized_)
00159       {
00160         ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
00161         return false;
00162       }
00163 
00164       //reset the global plan
00165       global_plan_.clear();
00166       global_plan_ = orig_global_plan;
00167 
00168       // transform global plan to the map frame we are working in
00169       // this also cuts the plan off (reduces it to local window)
00170       std::vector<int> start_end_counts (2, (int) global_plan_.size()); // counts from the end() of the plan
00171       if(!eband_local_planner::transformGlobalPlan(*tf_, global_plan_, *costmap_ros_, costmap_ros_->getGlobalFrameID(), transformed_plan_, start_end_counts))
00172       {
00173         // if plan could not be tranformed abort control and local planning
00174         ROS_WARN("Could not transform the global plan to the frame of the controller");
00175         return false;
00176       }
00177 
00178       // also check if there really is a plan
00179       if(transformed_plan_.empty())
00180       {
00181         // if global plan passed in is empty... we won't do anything
00182         ROS_WARN("Transformed plan is empty. Aborting local planner!");
00183         return false;
00184       }
00185 
00186       // set plan - as this is fresh from the global planner robot pose should be identical to start frame
00187       if(!eband_->setPlan(transformed_plan_))
00188       {
00189         // We've had some difficulty where the global planner keeps returning a valid path that runs through an obstacle
00190         // in the local costmap. See issue #5. Here we clear the local costmap and try one more time.
00191         costmap_ros_->resetLayers();
00192         if (!eband_->setPlan(transformed_plan_)) {
00193           ROS_ERROR("Setting plan to Elastic Band method failed!");
00194           return false;
00195         }
00196       }
00197       ROS_DEBUG("Global plan set to elastic band for optimization");
00198 
00199       // plan transformed and set to elastic band successfully - set counters to global variable
00200       plan_start_end_counter_ = start_end_counts;
00201 
00202       // let eband refine the plan before starting continuous operation (to smooth sampling based plans)
00203       eband_->optimizeBand();
00204 
00205 
00206       // display result
00207       std::vector<eband_local_planner::Bubble> current_band;
00208       if(eband_->getBand(current_band))
00209         eband_visual_->publishBand("bubbles", current_band);
00210 
00211       // set goal as not reached
00212       goal_reached_ = false;
00213 
00214       return true;
00215     }
00216 
00217 
00218     bool EBandPlannerROS::computeVelocityCommands(geometry_msgs::Twist& cmd_vel)
00219     {
00220       // check if plugin initialized
00221       if(!initialized_)
00222       {
00223         ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
00224         return false;
00225       }
00226 
00227       // instantiate local variables
00228       //std::vector<geometry_msgs::PoseStamped> local_plan;
00229       tf::Stamped<tf::Pose> global_pose;
00230       geometry_msgs::PoseStamped global_pose_msg;
00231       std::vector<geometry_msgs::PoseStamped> tmp_plan;
00232 
00233       // get curent robot position
00234       ROS_DEBUG("Reading current robot Position from costmap and appending it to elastic band.");
00235       if(!costmap_ros_->getRobotPose(global_pose))
00236       {
00237         ROS_WARN("Could not retrieve up to date robot pose from costmap for local planning.");
00238         return false;
00239       }
00240 
00241       // convert robot pose to frame in plan and set position in band at which to append
00242       tf::poseStampedTFToMsg(global_pose, global_pose_msg);
00243       tmp_plan.assign(1, global_pose_msg);
00244       eband_local_planner::AddAtPosition add_frames_at = add_front;
00245 
00246       // set it to elastic band and let eband connect it
00247       if(!eband_->addFrames(tmp_plan, add_frames_at))
00248       {
00249         ROS_WARN("Could not connect robot pose to existing elastic band.");
00250         return false;
00251       }
00252 
00253       // get additional path-frames which are now in moving window
00254       ROS_DEBUG("Checking for new path frames in moving window");
00255       std::vector<int> plan_start_end_counter = plan_start_end_counter_;
00256       std::vector<geometry_msgs::PoseStamped> append_transformed_plan;
00257       // transform global plan to the map frame we are working in - careful this also cuts the plan off (reduces it to local window)
00258       if(!eband_local_planner::transformGlobalPlan(*tf_, global_plan_, *costmap_ros_, costmap_ros_->getGlobalFrameID(), transformed_plan_, plan_start_end_counter))
00259       {
00260         // if plan could not be tranformed abort control and local planning
00261         ROS_WARN("Could not transform the global plan to the frame of the controller");
00262         return false;
00263       }
00264 
00265       // also check if there really is a plan
00266       if(transformed_plan_.empty())
00267       {
00268         // if global plan passed in is empty... we won't do anything
00269         ROS_WARN("Transformed plan is empty. Aborting local planner!");
00270         return false;
00271       }
00272 
00273       ROS_DEBUG("Retrieved start-end-counts are: (%d, %d)", plan_start_end_counter.at(0), plan_start_end_counter.at(1));
00274       ROS_DEBUG("Current start-end-counts are: (%d, %d)", plan_start_end_counter_.at(0), plan_start_end_counter_.at(1));
00275 
00276       // identify new frames - if there are any
00277       append_transformed_plan.clear();
00278       // did last transformed plan end futher away from end of complete plan than this transformed plan?
00279       if(plan_start_end_counter_.at(1) > plan_start_end_counter.at(1)) // counting from the back (as start might be pruned)
00280       {
00281         // new frames in moving window
00282         if(plan_start_end_counter_.at(1) > plan_start_end_counter.at(0)) // counting from the back (as start might be pruned)
00283         {
00284           // append everything
00285           append_transformed_plan = transformed_plan_;
00286         }
00287         else
00288         {
00289           // append only the new portion of the plan
00290           int discarded_frames = plan_start_end_counter.at(0) - plan_start_end_counter_.at(1);
00291           ROS_ASSERT(transformed_plan_.begin() + discarded_frames + 1 >= transformed_plan_.begin());
00292           ROS_ASSERT(transformed_plan_.begin() + discarded_frames + 1 < transformed_plan_.end());
00293           append_transformed_plan.assign(transformed_plan_.begin() + discarded_frames + 1, transformed_plan_.end());
00294         }
00295 
00296         // set it to elastic band and let eband connect it
00297         ROS_DEBUG("Adding %d new frames to current band", (int) append_transformed_plan.size());
00298         add_frames_at = add_back;
00299         if(eband_->addFrames(append_transformed_plan, add_back))
00300         {
00301           // appended frames succesfully to global plan - set new start-end counts
00302           ROS_DEBUG("Sucessfully added frames to band");
00303           plan_start_end_counter_ = plan_start_end_counter;
00304         }
00305         else {
00306           ROS_WARN("Failed to add frames to existing band");
00307           return false;
00308         }
00309       }
00310       else
00311         ROS_DEBUG("Nothing to add");
00312 
00313       // update Elastic Band (react on obstacle from costmap, ...)
00314       ROS_DEBUG("Calling optimization method for elastic band");
00315       std::vector<eband_local_planner::Bubble> current_band;
00316       if(!eband_->optimizeBand())
00317       {
00318         ROS_WARN("Optimization failed - Band invalid - No controls availlable");
00319         // display current band
00320         if(eband_->getBand(current_band))
00321           eband_visual_->publishBand("bubbles", current_band);
00322         return false;
00323       }
00324 
00325       // get current Elastic Band and
00326       eband_->getBand(current_band);
00327       // set it to the controller
00328       if(!eband_trj_ctrl_->setBand(current_band))
00329       {
00330         ROS_DEBUG("Failed to to set current band to Trajectory Controller");
00331         return false;
00332       }
00333 
00334       // set Odometry to controller
00335       if(!eband_trj_ctrl_->setOdometry(base_odom_))
00336       {
00337         ROS_DEBUG("Failed to to set current odometry to Trajectory Controller");
00338         return false;
00339       }
00340 
00341       // get resulting commands from the controller
00342       geometry_msgs::Twist cmd_twist;
00343       if(!eband_trj_ctrl_->getTwist(cmd_twist, goal_reached_))
00344       {
00345         ROS_DEBUG("Failed to calculate Twist from band in Trajectory Controller");
00346         return false;
00347       }
00348 
00349 
00350       // set retrieved commands to reference variable
00351       ROS_DEBUG("Retrieving velocity command: (%f, %f, %f)", cmd_twist.linear.x, cmd_twist.linear.y, cmd_twist.angular.z);
00352       cmd_vel = cmd_twist;
00353 
00354 
00355       // publish plan
00356       std::vector<geometry_msgs::PoseStamped> refined_plan;
00357       if(eband_->getPlan(refined_plan))
00358         // TODO publish local and current gloabl plan
00359         base_local_planner::publishPlan(refined_plan, g_plan_pub_);
00360       //base_local_planner::publishPlan(local_plan, l_plan_pub_, 0.0, 0.0, 1.0, 0.0);
00361 
00362       // display current band
00363       if(eband_->getBand(current_band))
00364         eband_visual_->publishBand("bubbles", current_band);
00365 
00366       return true;
00367     }
00368 
00369 
00370     bool EBandPlannerROS::isGoalReached()
00371     {
00372       // check if plugin initialized
00373       if(!initialized_)
00374       {
00375         ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
00376         return false;
00377       }
00378 
00379       return goal_reached_;
00380 
00381       // // copy odometry information to local variable
00382       // nav_msgs::Odometry base_odom;
00383       // {
00384       //        // make sure we do not read new date from topic right at the moment
00385       //        boost::mutex::scoped_lock lock(odom_mutex_);
00386       //        base_odom = base_odom_;
00387       // }
00388 
00389       // tf::Stamped<tf::Pose> global_pose;
00390       // costmap_ros_->getRobotPose(global_pose);
00391 
00392       // // analogous to dwa_planner the actual check uses the routine implemented in trajectory_planner (trajectory rollout)
00393       // bool is_goal_reached = base_local_planner::isGoalReached(
00394       //     *tf_, global_plan_, *(costmap_ros_->getCostmap()),
00395       //     costmap_ros_->getGlobalFrameID(), global_pose, base_odom,
00396       //                rot_stopped_vel_, trans_stopped_vel_, xy_goal_tolerance_,
00397       //     yaw_goal_tolerance_
00398       // );
00399 
00400       // return is_goal_reached;
00401 
00402     }
00403 
00404 
00405     void EBandPlannerROS::odomCallback(const nav_msgs::Odometry::ConstPtr& msg)
00406     {
00407       // lock Callback while reading data from topic
00408       boost::mutex::scoped_lock lock(odom_mutex_);
00409 
00410       // get odometry and write it to member variable (we assume that the odometry is published in the frame of the base)
00411       base_odom_.twist.twist.linear.x = msg->twist.twist.linear.x;
00412       base_odom_.twist.twist.linear.y = msg->twist.twist.linear.y;
00413       base_odom_.twist.twist.angular.z = msg->twist.twist.angular.z;
00414     }
00415 
00416 
00417   }
00418 
00419 


eband_local_planner
Author(s): Christian Connette, Bhaskara Marthi, Piyush Khandelwal
autogenerated on Thu Jun 6 2019 18:50:52