clear_costmap_recovery.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2009, 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: Eitan Marder-Eppstein
36 *********************************************************************/
39 #include <vector>
40 
41 //register this planner as a RecoveryBehavior plugin
43 
44 using costmap_2d::NO_INFORMATION;
45 
46 namespace clear_costmap_recovery {
47 ClearCostmapRecovery::ClearCostmapRecovery(): global_costmap_(NULL), local_costmap_(NULL),
48  tf_(NULL), initialized_(false) {}
49 
51  costmap_2d::Costmap2DROS* global_costmap, costmap_2d::Costmap2DROS* local_costmap){
52  if(!initialized_){
53  name_ = name;
54  tf_ = tf;
55  global_costmap_ = global_costmap;
56  local_costmap_ = local_costmap;
57 
58  //get some parameters from the parameter server
59  ros::NodeHandle private_nh("~/" + name_);
60 
61  private_nh.param("reset_distance", reset_distance_, 3.0);
62 
63  std::vector<std::string> clearable_layers_default, clearable_layers;
64  clearable_layers_default.push_back( std::string("obstacles") );
65  private_nh.param("layer_names", clearable_layers, clearable_layers_default);
66 
67  for(unsigned i=0; i < clearable_layers.size(); i++) {
68  ROS_INFO("Recovery behavior will clear layer %s", clearable_layers[i].c_str());
69  clearable_layers_.insert(clearable_layers[i]);
70  }
71 
72 
73  initialized_ = true;
74  }
75  else{
76  ROS_ERROR("You should not call initialize twice on this object, doing nothing");
77  }
78 }
79 
81  if(!initialized_){
82  ROS_ERROR("This object must be initialized before runBehavior is called");
83  return;
84  }
85 
86  if(global_costmap_ == NULL || local_costmap_ == NULL){
87  ROS_ERROR("The costmaps passed to the ClearCostmapRecovery object cannot be NULL. Doing nothing.");
88  return;
89  }
90  ROS_WARN("Clearing costmap to unstuck robot (%fm).", reset_distance_);
93 }
94 
96  std::vector<boost::shared_ptr<costmap_2d::Layer> >* plugins = costmap->getLayeredCostmap()->getPlugins();
97 
99 
100  if(!costmap->getRobotPose(pose)){
101  ROS_ERROR("Cannot clear map because pose cannot be retrieved");
102  return;
103  }
104 
105  double x = pose.getOrigin().x();
106  double y = pose.getOrigin().y();
107 
108  for (std::vector<boost::shared_ptr<costmap_2d::Layer> >::iterator pluginp = plugins->begin(); pluginp != plugins->end(); ++pluginp) {
109  boost::shared_ptr<costmap_2d::Layer> plugin = *pluginp;
110  std::string name = plugin->getName();
111  int slash = name.rfind('/');
112  if( slash != std::string::npos ){
113  name = name.substr(slash+1);
114  }
115 
116  if(clearable_layers_.count(name)!=0){
118  costmap = boost::static_pointer_cast<costmap_2d::CostmapLayer>(plugin);
119  clearMap(costmap, x, y);
120  }
121  }
122 }
123 
124 
126  double pose_x, double pose_y){
127  boost::unique_lock<costmap_2d::Costmap2D::mutex_t> lock(*(costmap->getMutex()));
128 
129  double start_point_x = pose_x - reset_distance_ / 2;
130  double start_point_y = pose_y - reset_distance_ / 2;
131  double end_point_x = start_point_x + reset_distance_;
132  double end_point_y = start_point_y + reset_distance_;
133 
134  int start_x, start_y, end_x, end_y;
135  costmap->worldToMapNoBounds(start_point_x, start_point_y, start_x, start_y);
136  costmap->worldToMapNoBounds(end_point_x, end_point_y, end_x, end_y);
137 
138  unsigned char* grid = costmap->getCharMap();
139  for(int x=0; x<(int)costmap->getSizeInCellsX(); x++){
140  bool xrange = x>start_x && x<end_x;
141 
142  for(int y=0; y<(int)costmap->getSizeInCellsY(); y++){
143  if(xrange && y>start_y && y<end_y)
144  continue;
145  int index = costmap->getIndex(x,y);
146  if(grid[index]!=NO_INFORMATION){
147  grid[index] = NO_INFORMATION;
148  }
149  }
150  }
151 
152  double ox = costmap->getOriginX(), oy = costmap->getOriginY();
153  double width = costmap->getSizeInMetersX(), height = costmap->getSizeInMetersY();
154  costmap->addExtraBounds(ox, oy, ox + width, oy + height);
155  return;
156 }
157 
158 };
bool getRobotPose(tf::Stamped< tf::Pose > &global_pose) const
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
tf::TransformListener * tf_
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::vector< boost::shared_ptr< Layer > > * getPlugins()
#define ROS_WARN(...)
void clear(costmap_2d::Costmap2DROS *costmap)
A recovery behavior that reverts the navigation stack&#39;s costmaps to the static map outside of a user-...
std::set< std::string > clearable_layers_
Layer names which will be cleared.
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
TFSIMD_FORCE_INLINE const tfScalar & x() const
void runBehavior()
Run the ClearCostmapRecovery recovery behavior. Reverts the costmap to the static map outside of a us...
void initialize(std::string name, tf::TransformListener *tf, costmap_2d::Costmap2DROS *global_costmap, costmap_2d::Costmap2DROS *local_costmap)
Initialization function for the ClearCostmapRecovery recovery behavior.
void clearMap(boost::shared_ptr< costmap_2d::CostmapLayer > costmap, double pose_x, double pose_y)
#define ROS_ERROR(...)
LayeredCostmap * getLayeredCostmap()


clear_costmap_recovery
Author(s): Eitan Marder-Eppstein, contradict@gmail.com
autogenerated on Sun Mar 3 2019 03:44:33