static_layer.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * Copyright (c) 2015, Fetch Robotics, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Author: Eitan Marder-Eppstein
37  * David V. Lu!!
38  *********************************************************************/
42 
44 
47 using costmap_2d::FREE_SPACE;
48 
49 namespace costmap_2d
50 {
51 
52 StaticLayer::StaticLayer() : dsrv_(NULL) {}
53 
55 {
56  if (dsrv_)
57  delete dsrv_;
58 }
59 
61 {
62  ros::NodeHandle nh("~/" + name_), g_nh;
63  current_ = true;
64 
66 
67  std::string map_topic;
68  nh.param("map_topic", map_topic, std::string("map"));
69  nh.param("first_map_only", first_map_only_, false);
70  nh.param("subscribe_to_updates", subscribe_to_updates_, false);
71 
72  nh.param("track_unknown_space", track_unknown_space_, true);
73  nh.param("use_maximum", use_maximum_, false);
74 
75  int temp_lethal_threshold, temp_unknown_cost_value;
76  nh.param("lethal_cost_threshold", temp_lethal_threshold, int(100));
77  nh.param("unknown_cost_value", temp_unknown_cost_value, int(-1));
78  nh.param("trinary_costmap", trinary_costmap_, true);
79 
80  lethal_threshold_ = std::max(std::min(temp_lethal_threshold, 100), 0);
81  unknown_cost_value_ = temp_unknown_cost_value;
82 
83  // Only resubscribe if topic has changed
84  if (map_sub_.getTopic() != ros::names::resolve(map_topic))
85  {
86  // we'll subscribe to the latched topic that the map server uses
87  ROS_INFO("Requesting the map...");
88  map_sub_ = g_nh.subscribe(map_topic, 1, &StaticLayer::incomingMap, this);
89  map_received_ = false;
90  has_updated_data_ = false;
91 
92  ros::Rate r(10);
93  while (!map_received_ && g_nh.ok())
94  {
95  ros::spinOnce();
96  r.sleep();
97  }
98 
99  ROS_INFO("Received a %d X %d map at %f m/pix", getSizeInCellsX(), getSizeInCellsY(), getResolution());
100 
102  {
103  ROS_INFO("Subscribing to updates");
104  map_update_sub_ = g_nh.subscribe(map_topic + "_updates", 10, &StaticLayer::incomingUpdate, this);
105 
106  }
107  }
108  else
109  {
110  has_updated_data_ = true;
111  }
112 
113  if (dsrv_)
114  {
115  delete dsrv_;
116  }
117 
118  dsrv_ = new dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig>(nh);
119  dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig>::CallbackType cb = boost::bind(
120  &StaticLayer::reconfigureCB, this, _1, _2);
121  dsrv_->setCallback(cb);
122 }
123 
124 void StaticLayer::reconfigureCB(costmap_2d::GenericPluginConfig &config, uint32_t level)
125 {
126  if (config.enabled != enabled_)
127  {
128  enabled_ = config.enabled;
129  has_updated_data_ = true;
130  x_ = y_ = 0;
131  width_ = size_x_;
132  height_ = size_y_;
133  }
134 }
135 
137 {
138  // If we are using rolling costmap, the static map size is
139  // unrelated to the size of the layered costmap
140  if (!layered_costmap_->isRolling())
141  {
143  resizeMap(master->getSizeInCellsX(), master->getSizeInCellsY(), master->getResolution(),
144  master->getOriginX(), master->getOriginY());
145  }
146 }
147 
148 unsigned char StaticLayer::interpretValue(unsigned char value)
149 {
150  // check if the static value is above the unknown or lethal thresholds
152  return NO_INFORMATION;
153  else if (!track_unknown_space_ && value == unknown_cost_value_)
154  return FREE_SPACE;
155  else if (value >= lethal_threshold_)
156  return LETHAL_OBSTACLE;
157  else if (trinary_costmap_)
158  return FREE_SPACE;
159 
160  double scale = (double) value / lethal_threshold_;
161  return scale * LETHAL_OBSTACLE;
162 }
163 
164 void StaticLayer::incomingMap(const nav_msgs::OccupancyGridConstPtr& new_map)
165 {
166  unsigned int size_x = new_map->info.width, size_y = new_map->info.height;
167 
168  ROS_DEBUG("Received a %d X %d map at %f m/pix", size_x, size_y, new_map->info.resolution);
169 
170  // resize costmap if size, resolution or origin do not match
172  if (!layered_costmap_->isRolling() && (master->getSizeInCellsX() != size_x ||
173  master->getSizeInCellsY() != size_y ||
174  master->getResolution() != new_map->info.resolution ||
175  master->getOriginX() != new_map->info.origin.position.x ||
176  master->getOriginY() != new_map->info.origin.position.y ||
178  {
179  // Update the size of the layered costmap (and all layers, including this one)
180  ROS_INFO("Resizing costmap to %d X %d at %f m/pix", size_x, size_y, new_map->info.resolution);
181  layered_costmap_->resizeMap(size_x, size_y, new_map->info.resolution, new_map->info.origin.position.x,
182  new_map->info.origin.position.y, true);
183  }
184  else if (size_x_ != size_x || size_y_ != size_y ||
185  resolution_ != new_map->info.resolution ||
186  origin_x_ != new_map->info.origin.position.x ||
187  origin_y_ != new_map->info.origin.position.y)
188  {
189  // only update the size of the costmap stored locally in this layer
190  ROS_INFO("Resizing static layer to %d X %d at %f m/pix", size_x, size_y, new_map->info.resolution);
191  resizeMap(size_x, size_y, new_map->info.resolution,
192  new_map->info.origin.position.x, new_map->info.origin.position.y);
193  }
194 
195  unsigned int index = 0;
196 
197  // initialize the costmap with static data
198  for (unsigned int i = 0; i < size_y; ++i)
199  {
200  for (unsigned int j = 0; j < size_x; ++j)
201  {
202  unsigned char value = new_map->data[index];
203  costmap_[index] = interpretValue(value);
204  ++index;
205  }
206  }
207  map_frame_ = new_map->header.frame_id;
208 
209  // we have a new map, update full size of map
210  x_ = y_ = 0;
211  width_ = size_x_;
212  height_ = size_y_;
213  map_received_ = true;
214  has_updated_data_ = true;
215 
216  // shutdown the map subscrber if firt_map_only_ flag is on
217  if (first_map_only_)
218  {
219  ROS_INFO("Shutting down the map subscriber. first_map_only flag is on");
220  map_sub_.shutdown();
221  }
222 }
223 
224 void StaticLayer::incomingUpdate(const map_msgs::OccupancyGridUpdateConstPtr& update)
225 {
226  unsigned int di = 0;
227  for (unsigned int y = 0; y < update->height ; y++)
228  {
229  unsigned int index_base = (update->y + y) * size_x_;
230  for (unsigned int x = 0; x < update->width ; x++)
231  {
232  unsigned int index = index_base + x + update->x;
233  costmap_[index] = interpretValue(update->data[di++]);
234  }
235  }
236  x_ = update->x;
237  y_ = update->y;
238  width_ = update->width;
239  height_ = update->height;
240  has_updated_data_ = true;
241 }
242 
244 {
245  onInitialize();
246 }
247 
249 {
250  map_sub_.shutdown();
253 }
254 
256 {
257  if (first_map_only_)
258  {
259  has_updated_data_ = true;
260  }
261  else
262  {
263  onInitialize();
264  }
265 }
266 
267 void StaticLayer::updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x, double* min_y,
268  double* max_x, double* max_y)
269 {
270 
271  if( !layered_costmap_->isRolling() ){
273  return;
274  }
275 
276  useExtraBounds(min_x, min_y, max_x, max_y);
277 
278  double wx, wy;
279 
280  mapToWorld(x_, y_, wx, wy);
281  *min_x = std::min(wx, *min_x);
282  *min_y = std::min(wy, *min_y);
283 
284  mapToWorld(x_ + width_, y_ + height_, wx, wy);
285  *max_x = std::max(wx, *max_x);
286  *max_y = std::max(wy, *max_y);
287 
288  has_updated_data_ = false;
289 }
290 
291 void StaticLayer::updateCosts(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
292 {
293  if (!map_received_)
294  return;
295 
296  if (!enabled_)
297  return;
298 
299  if (!layered_costmap_->isRolling())
300  {
301  // if not rolling, the layered costmap (master_grid) has same coordinates as this layer
302  if (!use_maximum_)
303  updateWithTrueOverwrite(master_grid, min_i, min_j, max_i, max_j);
304  else
305  updateWithMax(master_grid, min_i, min_j, max_i, max_j);
306  }
307  else
308  {
309  // If rolling window, the master_grid is unlikely to have same coordinates as this layer
310  unsigned int mx, my;
311  double wx, wy;
312  // Might even be in a different frame
313  tf::StampedTransform transform;
314  try
315  {
317  }
318  catch (tf::TransformException ex)
319  {
320  ROS_ERROR("%s", ex.what());
321  return;
322  }
323  // Copy map data given proper transformations
324  for (unsigned int i = min_i; i < max_i; ++i)
325  {
326  for (unsigned int j = min_j; j < max_j; ++j)
327  {
328  // Convert master_grid coordinates (i,j) into global_frame_(wx,wy) coordinates
329  layered_costmap_->getCostmap()->mapToWorld(i, j, wx, wy);
330  // Transform from global_frame_ to map_frame_
331  tf::Point p(wx, wy, 0);
332  p = transform(p);
333  // Set master_grid with cell from map
334  if (worldToMap(p.x(), p.y(), mx, my))
335  {
336  if (!use_maximum_)
337  master_grid.setCost(i, j, getCost(mx, my));
338  else
339  master_grid.setCost(i, j, std::max(getCost(mx, my), master_grid.getCost(i, j)));
340  }
341  }
342  }
343  }
344 }
345 
346 } // namespace costmap_2d
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
Set the cost of a cell in the costmap.
Definition: costmap_2d.cpp:197
virtual void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y)
This is called by the LayeredCostmap to poll this plugin as to how much of the costmap it needs to up...
LayeredCostmap * layered_costmap_
Definition: layer.h:122
virtual void activate()
Restart publishers if they&#39;ve been stopped.
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Definition: costmap_2d.cpp:72
virtual void onInitialize()
This is called at the end of initialize(). Override to implement subclass-specific initialization...
virtual void updateCosts(costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j)
Actually update the underlying costmap, only within the bounds calculated during UpdateBounds().
void mapToWorld(unsigned int mx, unsigned int my, double &wx, double &wy) const
Convert from map coordinates to world coordinates.
Definition: costmap_2d.cpp:202
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y, bool size_locked=false)
unsigned int size_y_
Definition: costmap_2d.h:422
virtual void deactivate()
Stop publishers.
std::string getGlobalFrameID() const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
dynamic_reconfigure::Server< costmap_2d::GenericPluginConfig > * dsrv_
Definition: static_layer.h:96
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:446
unsigned char lethal_threshold_
Definition: static_layer.h:94
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
unsigned int size_x_
Definition: costmap_2d.h:421
TFSIMD_FORCE_INLINE const tfScalar & y() const
static const unsigned char FREE_SPACE
Definition: cost_values.h:45
bool first_map_only_
Store the first static map and reuse it on reinitializing.
Definition: static_layer.h:90
void useExtraBounds(double *min_x, double *min_y, double *max_x, double *max_y)
std::string name_
Definition: layer.h:125
bool subscribe_to_updates_
frame that map is located in
Definition: static_layer.h:84
tf::TransformListener * tf_
Definition: layer.h:126
TFSIMD_FORCE_INLINE const tfScalar & x() const
unsigned char getCost(unsigned int mx, unsigned int my) const
Get the cost of a cell in the costmap.
Definition: costmap_2d.cpp:192
std::string global_frame_
The global frame for the costmap.
Definition: static_layer.h:82
#define ROS_INFO(...)
void reconfigureCB(costmap_2d::GenericPluginConfig &config, uint32_t level)
std::string getTopic() const
TFSIMD_FORCE_INLINE const tfScalar & y() const
void updateWithTrueOverwrite(costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j)
TFSIMD_FORCE_INLINE const tfScalar & x() const
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:451
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:431
void incomingMap(const nav_msgs::OccupancyGridConstPtr &new_map)
Callback to update the costmap&#39;s map from the map_server.
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
bool sleep()
unsigned char interpretValue(unsigned char value)
bool enabled_
Currently this var is managed by subclasses. TODO: make this managed by this class and/or container c...
Definition: layer.h:124
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:426
void incomingUpdate(const map_msgs::OccupancyGridUpdateConstPtr &update)
static const unsigned char LETHAL_OBSTACLE
Definition: cost_values.h:43
static const unsigned char NO_INFORMATION
Definition: cost_values.h:42
ros::Subscriber map_sub_
Definition: static_layer.h:92
bool ok() const
void updateWithMax(costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j)
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.h:60
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:456
unsigned char * costmap_
Definition: costmap_2d.h:426
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:208
ros::Subscriber map_update_sub_
Definition: static_layer.h:92
unsigned char unknown_cost_value_
Definition: static_layer.h:94
virtual void matchSize()
Implement this to make this layer match the size of the parent costmap.
#define ROS_DEBUG(...)


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Sun Mar 3 2019 03:44:17