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 <boost/pointer_cast.hpp>
40 #include <vector>
41 
42 //register this planner as a RecoveryBehavior plugin
44 
46 
47 namespace clear_costmap_recovery {
48 ClearCostmapRecovery::ClearCostmapRecovery(): global_costmap_(NULL), local_costmap_(NULL),
49  tf_(NULL), initialized_(false) {}
50 
52  costmap_2d::Costmap2DROS* global_costmap, costmap_2d::Costmap2DROS* local_costmap){
53  if(!initialized_){
54  name_ = name;
55  tf_ = tf;
56  global_costmap_ = global_costmap;
57  local_costmap_ = local_costmap;
58 
59  //get some parameters from the parameter server
60  ros::NodeHandle private_nh("~/" + name_);
61 
62  private_nh.param("reset_distance", reset_distance_, 3.0);
63  private_nh.param("invert_area_to_clear", invert_area_to_clear_, false);
64  private_nh.param("force_updating", force_updating_, false);
65  private_nh.param("affected_maps", affected_maps_, std::string("both"));
66  if (affected_maps_ != "local" && affected_maps_ != "global" && affected_maps_ != "both")
67  {
68  ROS_WARN("Wrong value for affected_maps parameter: '%s'; valid values are 'local', 'global' or 'both'; " \
69  "defaulting to 'both'", affected_maps_.c_str());
70  affected_maps_ = "both";
71  }
72 
73  std::vector<std::string> clearable_layers_default, clearable_layers;
74  clearable_layers_default.push_back( std::string("obstacles") );
75  private_nh.param("layer_names", clearable_layers, clearable_layers_default);
76 
77  for(unsigned i=0; i < clearable_layers.size(); i++) {
78  ROS_INFO("Recovery behavior will clear layer '%s'", clearable_layers[i].c_str());
79  clearable_layers_.insert(clearable_layers[i]);
80  }
81 
82  initialized_ = true;
83  }
84  else{
85  ROS_ERROR("You should not call initialize twice on this object, doing nothing");
86  }
87 }
88 
90  if(!initialized_){
91  ROS_ERROR("This object must be initialized before runBehavior is called");
92  return;
93  }
94 
95  if(global_costmap_ == NULL || local_costmap_ == NULL){
96  ROS_ERROR("The costmaps passed to the ClearCostmapRecovery object cannot be NULL. Doing nothing.");
97  return;
98  }
99 
100  if (!invert_area_to_clear_){
101  ROS_WARN("Clearing %s costmap%s outside a square (%.2fm) large centered on the robot.", affected_maps_.c_str(),
102  affected_maps_ == "both" ? "s" : "", reset_distance_);
103  }else {
104  ROS_WARN("Clearing %s costmap%s inside a square (%.2fm) large centered on the robot.", affected_maps_.c_str(),
105  affected_maps_ == "both" ? "s" : "", reset_distance_);
106  }
107 
109  if (affected_maps_ == "global" || affected_maps_ == "both")
110  {
112 
113  if (force_updating_)
115 
116  ROS_DEBUG("Global costmap cleared in %fs", (ros::WallTime::now() - t0).toSec());
117  }
118 
119  t0 = ros::WallTime::now();
120  if (affected_maps_ == "local" || affected_maps_ == "both")
121  {
123 
124  if (force_updating_)
126 
127  ROS_DEBUG("Local costmap cleared in %fs", (ros::WallTime::now() - t0).toSec());
128  }
129 }
130 
132  std::vector<boost::shared_ptr<costmap_2d::Layer> >* plugins = costmap->getLayeredCostmap()->getPlugins();
133 
134  geometry_msgs::PoseStamped pose;
135 
136  if(!costmap->getRobotPose(pose)){
137  ROS_ERROR("Cannot clear map because pose cannot be retrieved");
138  return;
139  }
140 
141  double x = pose.pose.position.x;
142  double y = pose.pose.position.y;
143 
144  for (std::vector<boost::shared_ptr<costmap_2d::Layer> >::iterator pluginp = plugins->begin(); pluginp != plugins->end(); ++pluginp) {
145  boost::shared_ptr<costmap_2d::Layer> plugin = *pluginp;
146  std::string name = plugin->getName();
147  int slash = name.rfind('/');
148  if( slash != std::string::npos ){
149  name = name.substr(slash+1);
150  }
151 
152  if(clearable_layers_.count(name)!=0){
153 
154  // check if the value is convertable
155  if(!dynamic_cast<costmap_2d::CostmapLayer*>(plugin.get())){
156  ROS_ERROR_STREAM("Layer " << name << " is not derived from costmap_2d::CostmapLayer");
157  continue;
158  }
159 
161  costmap = boost::static_pointer_cast<costmap_2d::CostmapLayer>(plugin);
162  clearMap(costmap, x, y);
163  }
164  }
165 }
166 
167 
169  double pose_x, double pose_y){
170  boost::unique_lock<costmap_2d::Costmap2D::mutex_t> lock(*(costmap->getMutex()));
171 
172  double start_point_x = pose_x - reset_distance_ / 2;
173  double start_point_y = pose_y - reset_distance_ / 2;
174  double end_point_x = start_point_x + reset_distance_;
175  double end_point_y = start_point_y + reset_distance_;
176 
177  int start_x, start_y, end_x, end_y;
178  costmap->worldToMapNoBounds(start_point_x, start_point_y, start_x, start_y);
179  costmap->worldToMapNoBounds(end_point_x, end_point_y, end_x, end_y);
180 
181  costmap->clearArea(start_x, start_y, end_x, end_y, invert_area_to_clear_);
182 
183  double ox = costmap->getOriginX(), oy = costmap->getOriginY();
184  double width = costmap->getSizeInMetersX(), height = costmap->getSizeInMetersY();
185  costmap->addExtraBounds(ox, oy, ox + width, oy + height);
186  return;
187 }
188 
189 };
clear_costmap_recovery::ClearCostmapRecovery::clearMap
void clearMap(boost::shared_ptr< costmap_2d::CostmapLayer > costmap, double pose_x, double pose_y)
Definition: clear_costmap_recovery.cpp:168
clear_costmap_recovery::ClearCostmapRecovery::invert_area_to_clear_
bool invert_area_to_clear_
Definition: clear_costmap_recovery.h:118
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
boost::shared_ptr
clear_costmap_recovery::ClearCostmapRecovery::clearable_layers_
std::set< std::string > clearable_layers_
Layer names which will be cleared.
Definition: clear_costmap_recovery.h:120
clear_costmap_recovery
Definition: clear_costmap_recovery.h:44
clear_costmap_recovery::ClearCostmapRecovery::force_updating_
bool force_updating_
force costmap update after clearing, so we don't need to wait for update thread
Definition: clear_costmap_recovery.h:116
costmap_2d::NO_INFORMATION
static const unsigned char NO_INFORMATION
clear_costmap_recovery::ClearCostmapRecovery::initialized_
bool initialized_
Definition: clear_costmap_recovery.h:115
clear_costmap_recovery::ClearCostmapRecovery::local_costmap_
costmap_2d::Costmap2DROS * local_costmap_
Definition: clear_costmap_recovery.h:112
clear_costmap_recovery::ClearCostmapRecovery::runBehavior
void runBehavior()
Run the ClearCostmapRecovery recovery behavior. Reverts the costmap to the static map outside of a us...
Definition: clear_costmap_recovery.cpp:89
clear_costmap_recovery::ClearCostmapRecovery::ClearCostmapRecovery
ClearCostmapRecovery()
Constructor, make sure to call initialize in addition to actually initialize the object.
Definition: clear_costmap_recovery.cpp:48
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
costmap_2d::LayeredCostmap::getPlugins
std::vector< boost::shared_ptr< Layer > > * getPlugins()
ROS_DEBUG
#define ROS_DEBUG(...)
ros::WallTime::now
static WallTime now()
costmap_2d::Costmap2DROS::getLayeredCostmap
LayeredCostmap * getLayeredCostmap() const
clear_costmap_recovery::ClearCostmapRecovery
A recovery behavior that reverts the navigation stack's costmaps to the static map outside of a user-...
Definition: clear_costmap_recovery.h:84
ROS_WARN
#define ROS_WARN(...)
tf2_ros::Buffer
costmap_2d::CostmapLayer
ros::WallTime
costmap_2d::Costmap2DROS::getRobotPose
bool getRobotPose(geometry_msgs::PoseStamped &global_pose) const
clear_costmap_recovery::ClearCostmapRecovery::initialize
void initialize(std::string name, tf2_ros::Buffer *tf, costmap_2d::Costmap2DROS *global_costmap, costmap_2d::Costmap2DROS *local_costmap)
Initialization function for the ClearCostmapRecovery recovery behavior.
Definition: clear_costmap_recovery.cpp:51
ROS_ERROR
#define ROS_ERROR(...)
class_list_macros.hpp
clear_costmap_recovery.h
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
clear_costmap_recovery::ClearCostmapRecovery::name_
std::string name_
Definition: clear_costmap_recovery.h:113
tf
clear_costmap_recovery::ClearCostmapRecovery::global_costmap_
costmap_2d::Costmap2DROS * global_costmap_
Definition: clear_costmap_recovery.h:112
clear_costmap_recovery::ClearCostmapRecovery::tf_
tf2_ros::Buffer * tf_
Definition: clear_costmap_recovery.h:114
costmap_2d::Costmap2DROS::updateMap
void updateMap()
clear_costmap_recovery::ClearCostmapRecovery::clear
void clear(costmap_2d::Costmap2DROS *costmap)
Definition: clear_costmap_recovery.cpp:131
tf_
tf2_ros::Buffer * tf_
ROS_INFO
#define ROS_INFO(...)
clear_costmap_recovery::ClearCostmapRecovery::affected_maps_
std::string affected_maps_
clear only local, global or both costmaps
Definition: clear_costmap_recovery.h:119
clear_costmap_recovery::ClearCostmapRecovery::reset_distance_
double reset_distance_
Definition: clear_costmap_recovery.h:117
costmap_2d::Costmap2DROS
nav_core::RecoveryBehavior
ros::NodeHandle


clear_costmap_recovery
Author(s): Eitan Marder-Eppstein, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:30