eband_local_planner_ros.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Christian Connette
36  *********************************************************************/
37 
39 
40 // pluginlib macros (defines, ...)
42 
43 // abstract class from which our plugin inherits
45 
46 
47 // register this planner as a BaseGlobalPlanner plugin
48 // (see http://www.ros.org/wiki/pluginlib/Tutorials/Writing%20and%20Using%20a%20Simple%20Plugin)
50 
51 
52  namespace eband_local_planner{
53 
54  EBandPlannerROS::EBandPlannerROS() : costmap_ros_(NULL), tf_(NULL), initialized_(false) {}
55 
56 
58  : costmap_ros_(NULL), tf_(NULL), initialized_(false)
59  {
60  // initialize planner
61  initialize(name, tf, costmap_ros);
62  }
63 
64 
66 
67 
69  {
70  // check if the plugin is already initialized
71  if(!initialized_)
72  {
73  // copy adress of costmap and Transform Listener (handed over from move_base)
74  costmap_ros_ = costmap_ros;
75  tf_ = tf;
76 
77 
78  // create Node Handle with name of plugin (as used in move_base for loading)
79  ros::NodeHandle pn("~/" + name);
80 
81  // advertise topics (adapted global plan and predicted local trajectory)
82  g_plan_pub_ = pn.advertise<nav_msgs::Path>("global_plan", 1);
83  l_plan_pub_ = pn.advertise<nav_msgs::Path>("local_plan", 1);
84 
85 
86  // subscribe to topics (to get odometry information, we need to get a handle to the topic in the global namespace)
87  ros::NodeHandle gn;
88  odom_sub_ = gn.subscribe<nav_msgs::Odometry>("odom", 1, boost::bind(&EBandPlannerROS::odomCallback, this, _1));
89 
90 
91  // create the actual planner that we'll use. Pass Name of plugin and pointer to global costmap to it.
92  // (configuration is done via parameter server)
94 
95  // create the according controller
97 
98  // create object for visualization
100 
101  // pass visualization object to elastic band
102  eband_->setVisualization(eband_visual_);
103 
104  // pass visualization object to controller
105  eband_trj_ctrl_->setVisualization(eband_visual_);
106 
107  // initialize visualization - set node handle and pointer to costmap
108  eband_visual_->initialize(pn, costmap_ros);
109 
110  // create and initialize dynamic reconfigure
111  drs_.reset(new drs(pn));
112  drs::CallbackType cb = boost::bind(&EBandPlannerROS::reconfigureCallback, this, _1, _2);
113  drs_->setCallback(cb);
114 
115  // set initialized flag
116  initialized_ = true;
117 
118  // this is only here to make this process visible in the rxlogger right from the start
119  ROS_DEBUG("Elastic Band plugin initialized.");
120  }
121  else
122  {
123  ROS_WARN("This planner has already been initialized, doing nothing.");
124  }
125  }
126 
127 
128  void EBandPlannerROS::reconfigureCallback(EBandPlannerConfig& config,
129  uint32_t level)
130  {
131  xy_goal_tolerance_ = config.xy_goal_tolerance;
132  yaw_goal_tolerance_ = config.yaw_goal_tolerance;
133  rot_stopped_vel_ = config.rot_stopped_vel;
134  trans_stopped_vel_ = config.trans_stopped_vel;
135 
136  if (eband_)
137  eband_->reconfigure(config);
138  else
139  ROS_ERROR("Reconfigure CB called before eband planner initialization");
140 
141  if (eband_trj_ctrl_)
142  eband_trj_ctrl_->reconfigure(config);
143  else
144  ROS_ERROR("Reconfigure CB called before trajectory controller initialization!");
145 
146  if (eband_visual_)
147  eband_visual_->reconfigure(config);
148  else
149  ROS_ERROR("Reconfigure CB called before eband visualizer initialization");
150  }
151 
152 
153  // set global plan to wrapper and pass it to eband
154  bool EBandPlannerROS::setPlan(const std::vector<geometry_msgs::PoseStamped>& orig_global_plan)
155  {
156 
157  // check if plugin initialized
158  if(!initialized_)
159  {
160  ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
161  return false;
162  }
163 
164  //reset the global plan
165  global_plan_.clear();
166  global_plan_ = orig_global_plan;
167 
168  // transform global plan to the map frame we are working in
169  // this also cuts the plan off (reduces it to local window)
170  std::vector<int> start_end_counts (2, (int) global_plan_.size()); // counts from the end() of the plan
172  {
173  // if plan could not be tranformed abort control and local planning
174  ROS_WARN("Could not transform the global plan to the frame of the controller");
175  return false;
176  }
177 
178  // also check if there really is a plan
179  if(transformed_plan_.empty())
180  {
181  // if global plan passed in is empty... we won't do anything
182  ROS_WARN("Transformed plan is empty. Aborting local planner!");
183  return false;
184  }
185 
186  // set plan - as this is fresh from the global planner robot pose should be identical to start frame
187  if(!eband_->setPlan(transformed_plan_))
188  {
189  // We've had some difficulty where the global planner keeps returning a valid path that runs through an obstacle
190  // in the local costmap. See issue #5. Here we clear the local costmap and try one more time.
192  if (!eband_->setPlan(transformed_plan_)) {
193  ROS_ERROR("Setting plan to Elastic Band method failed!");
194  return false;
195  }
196  }
197  ROS_DEBUG("Global plan set to elastic band for optimization");
198 
199  // plan transformed and set to elastic band successfully - set counters to global variable
200  plan_start_end_counter_ = start_end_counts;
201 
202  // let eband refine the plan before starting continuous operation (to smooth sampling based plans)
203  eband_->optimizeBand();
204 
205 
206  // display result
207  std::vector<eband_local_planner::Bubble> current_band;
208  if(eband_->getBand(current_band))
209  eband_visual_->publishBand("bubbles", current_band);
210 
211  // set goal as not reached
212  goal_reached_ = false;
213 
214  return true;
215  }
216 
217 
218  bool EBandPlannerROS::computeVelocityCommands(geometry_msgs::Twist& cmd_vel)
219  {
220  // check if plugin initialized
221  if(!initialized_)
222  {
223  ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
224  return false;
225  }
226 
227  // instantiate local variables
228  //std::vector<geometry_msgs::PoseStamped> local_plan;
229  tf::Stamped<tf::Pose> global_pose;
230  geometry_msgs::PoseStamped global_pose_msg;
231  std::vector<geometry_msgs::PoseStamped> tmp_plan;
232 
233  // get curent robot position
234  ROS_DEBUG("Reading current robot Position from costmap and appending it to elastic band.");
235  if(!costmap_ros_->getRobotPose(global_pose))
236  {
237  ROS_WARN("Could not retrieve up to date robot pose from costmap for local planning.");
238  return false;
239  }
240 
241  // convert robot pose to frame in plan and set position in band at which to append
242  tf::poseStampedTFToMsg(global_pose, global_pose_msg);
243  tmp_plan.assign(1, global_pose_msg);
245 
246  // set it to elastic band and let eband connect it
247  if(!eband_->addFrames(tmp_plan, add_frames_at))
248  {
249  ROS_WARN("Could not connect robot pose to existing elastic band.");
250  return false;
251  }
252 
253  // get additional path-frames which are now in moving window
254  ROS_DEBUG("Checking for new path frames in moving window");
255  std::vector<int> plan_start_end_counter = plan_start_end_counter_;
256  std::vector<geometry_msgs::PoseStamped> append_transformed_plan;
257  // transform global plan to the map frame we are working in - careful this also cuts the plan off (reduces it to local window)
259  {
260  // if plan could not be tranformed abort control and local planning
261  ROS_WARN("Could not transform the global plan to the frame of the controller");
262  return false;
263  }
264 
265  // also check if there really is a plan
266  if(transformed_plan_.empty())
267  {
268  // if global plan passed in is empty... we won't do anything
269  ROS_WARN("Transformed plan is empty. Aborting local planner!");
270  return false;
271  }
272 
273  ROS_DEBUG("Retrieved start-end-counts are: (%d, %d)", plan_start_end_counter.at(0), plan_start_end_counter.at(1));
274  ROS_DEBUG("Current start-end-counts are: (%d, %d)", plan_start_end_counter_.at(0), plan_start_end_counter_.at(1));
275 
276  // identify new frames - if there are any
277  append_transformed_plan.clear();
278  // did last transformed plan end futher away from end of complete plan than this transformed plan?
279  if(plan_start_end_counter_.at(1) > plan_start_end_counter.at(1)) // counting from the back (as start might be pruned)
280  {
281  // new frames in moving window
282  if(plan_start_end_counter_.at(1) > plan_start_end_counter.at(0)) // counting from the back (as start might be pruned)
283  {
284  // append everything
285  append_transformed_plan = transformed_plan_;
286  }
287  else
288  {
289  // append only the new portion of the plan
290  int discarded_frames = plan_start_end_counter.at(0) - plan_start_end_counter_.at(1);
291  ROS_ASSERT(transformed_plan_.begin() + discarded_frames + 1 >= transformed_plan_.begin());
292  ROS_ASSERT(transformed_plan_.begin() + discarded_frames + 1 < transformed_plan_.end());
293  append_transformed_plan.assign(transformed_plan_.begin() + discarded_frames + 1, transformed_plan_.end());
294  }
295 
296  // set it to elastic band and let eband connect it
297  ROS_DEBUG("Adding %d new frames to current band", (int) append_transformed_plan.size());
298  add_frames_at = add_back;
299  if(eband_->addFrames(append_transformed_plan, add_back))
300  {
301  // appended frames succesfully to global plan - set new start-end counts
302  ROS_DEBUG("Sucessfully added frames to band");
303  plan_start_end_counter_ = plan_start_end_counter;
304  }
305  else {
306  ROS_WARN("Failed to add frames to existing band");
307  return false;
308  }
309  }
310  else
311  ROS_DEBUG("Nothing to add");
312 
313  // update Elastic Band (react on obstacle from costmap, ...)
314  ROS_DEBUG("Calling optimization method for elastic band");
315  std::vector<eband_local_planner::Bubble> current_band;
316  if(!eband_->optimizeBand())
317  {
318  ROS_WARN("Optimization failed - Band invalid - No controls availlable");
319  // display current band
320  if(eband_->getBand(current_band))
321  eband_visual_->publishBand("bubbles", current_band);
322  return false;
323  }
324 
325  // get current Elastic Band and
326  eband_->getBand(current_band);
327  // set it to the controller
328  if(!eband_trj_ctrl_->setBand(current_band))
329  {
330  ROS_DEBUG("Failed to to set current band to Trajectory Controller");
331  return false;
332  }
333 
334  // set Odometry to controller
335  if(!eband_trj_ctrl_->setOdometry(base_odom_))
336  {
337  ROS_DEBUG("Failed to to set current odometry to Trajectory Controller");
338  return false;
339  }
340 
341  // get resulting commands from the controller
342  geometry_msgs::Twist cmd_twist;
343  if(!eband_trj_ctrl_->getTwist(cmd_twist, goal_reached_))
344  {
345  ROS_DEBUG("Failed to calculate Twist from band in Trajectory Controller");
346  return false;
347  }
348 
349 
350  // set retrieved commands to reference variable
351  ROS_DEBUG("Retrieving velocity command: (%f, %f, %f)", cmd_twist.linear.x, cmd_twist.linear.y, cmd_twist.angular.z);
352  cmd_vel = cmd_twist;
353 
354 
355  // publish plan
356  std::vector<geometry_msgs::PoseStamped> refined_plan;
357  if(eband_->getPlan(refined_plan))
358  // TODO publish local and current gloabl plan
360  //base_local_planner::publishPlan(local_plan, l_plan_pub_, 0.0, 0.0, 1.0, 0.0);
361 
362  // display current band
363  if(eband_->getBand(current_band))
364  eband_visual_->publishBand("bubbles", current_band);
365 
366  return true;
367  }
368 
369 
371  {
372  // check if plugin initialized
373  if(!initialized_)
374  {
375  ROS_ERROR("This planner has not been initialized, please call initialize() before using this planner");
376  return false;
377  }
378 
379  return goal_reached_;
380 
381  // // copy odometry information to local variable
382  // nav_msgs::Odometry base_odom;
383  // {
384  // // make sure we do not read new date from topic right at the moment
385  // boost::mutex::scoped_lock lock(odom_mutex_);
386  // base_odom = base_odom_;
387  // }
388 
389  // tf::Stamped<tf::Pose> global_pose;
390  // costmap_ros_->getRobotPose(global_pose);
391 
392  // // analogous to dwa_planner the actual check uses the routine implemented in trajectory_planner (trajectory rollout)
393  // bool is_goal_reached = base_local_planner::isGoalReached(
394  // *tf_, global_plan_, *(costmap_ros_->getCostmap()),
395  // costmap_ros_->getGlobalFrameID(), global_pose, base_odom,
396  // rot_stopped_vel_, trans_stopped_vel_, xy_goal_tolerance_,
397  // yaw_goal_tolerance_
398  // );
399 
400  // return is_goal_reached;
401 
402  }
403 
404 
405  void EBandPlannerROS::odomCallback(const nav_msgs::Odometry::ConstPtr& msg)
406  {
407  // lock Callback while reading data from topic
408  boost::mutex::scoped_lock lock(odom_mutex_);
409 
410  // get odometry and write it to member variable (we assume that the odometry is published in the frame of the base)
411  base_odom_.twist.twist.linear.x = msg->twist.twist.linear.x;
412  base_odom_.twist.twist.linear.y = msg->twist.twist.linear.y;
413  base_odom_.twist.twist.angular.z = msg->twist.twist.angular.z;
414  }
415 
416 
417  }
418 
419 
bool getRobotPose(tf::Stamped< tf::Pose > &global_pose) const
boost::shared_ptr< EBandPlanner > eband_
bool isGoalReached()
Check if the goal pose has been achieved.
boost::shared_ptr< EBandTrajectoryCtrl > eband_trj_ctrl_
tf::TransformListener * tf_
pointer to Transform Listener
static void poseStampedTFToMsg(const Stamped< Pose > &bt, geometry_msgs::PoseStamped &msg)
EBandPlannerROS()
Default constructor for the ros wrapper.
std::string getGlobalFrameID()
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
bool setPlan(const std::vector< geometry_msgs::PoseStamped > &orig_global_plan)
Set the plan that the controller is following; also reset eband-planner.
double trans_stopped_vel_
lower bound for absolute value of velocity (with respect to stick-slip behaviour) ...
double xy_goal_tolerance_
parameters to define region in which goal is treated as reached
tf::TransformListener * tf_
Implements the Elastic Band Method for SE2-Manifold (mobile Base)
#define ROS_WARN(...)
Plugin to the ros base_local_planner. Implements a wrapper for the Elastic Band Method.
dynamic_reconfigure::Server< eband_local_planner::EBandPlannerConfig > drs
std::vector< geometry_msgs::PoseStamped > transformed_plan_
bool computeVelocityCommands(geometry_msgs::Twist &cmd_vel)
Given the current position, orientation, and velocity of the robot, compute velocity commands to send...
boost::shared_ptr< EBandVisualization > eband_visual_
ros::Publisher g_plan_pub_
publishes modified global plan
void odomCallback(const nav_msgs::Odometry::ConstPtr &msg)
Odometry-Callback: function is called whenever a new odometry msg is published on that topic...
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
~EBandPlannerROS()
Destructor for the wrapper.
std::vector< geometry_msgs::PoseStamped > global_plan_
void initialize(std::string name, tf::TransformListener *tf, costmap_2d::Costmap2DROS *costmap_ros)
Initializes the ros wrapper.
void publishPlan(const std::vector< geometry_msgs::PoseStamped > &path, const ros::Publisher &pub)
void reconfigureCallback(EBandPlannerConfig &config, uint32_t level)
Reconfigures node parameters.
ros::Publisher l_plan_pub_
publishes prediction for local commands
#define ROS_ASSERT(cond)
costmap_2d::Costmap2DROS * costmap_ros_
pointer to costmap
ros::Subscriber odom_sub_
subscribes to the odometry topic in global namespace
#define ROS_ERROR(...)
boost::shared_ptr< drs > drs_
dynamic reconfigure server ptr
bool transformGlobalPlan(const tf::TransformListener &tf, const std::vector< geometry_msgs::PoseStamped > &global_plan, costmap_2d::Costmap2DROS &costmap, const std::string &global_frame, std::vector< geometry_msgs::PoseStamped > &transformed_plan, std::vector< int > &start_end_counts_from_end)
Transforms the global plan of the robot from the planner frame to the local frame. This replaces the transformGlobalPlan as defined in the base_local_planner/goal_functions.h main difference is that it additionally outputs counter indicating which part of the plan has been transformed.
#define ROS_DEBUG(...)


eband_local_planner
Author(s): Christian Connette, Bhaskara Marthi, Piyush Khandelwal
autogenerated on Sat Feb 22 2020 04:03:28