Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
costmap_layer.cpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, 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  * David V. Lu!!
37  *********************************************************************/
38 
39 #include <nav2_costmap_2d/costmap_layer.hpp>
40 #include <stdexcept>
41 #include <algorithm>
42 
43 namespace nav2_costmap_2d
44 {
45 
47  double x, double y, double * min_x, double * min_y, double * max_x,
48  double * max_y)
49 {
50  *min_x = std::min(x, *min_x);
51  *min_y = std::min(y, *min_y);
52  *max_x = std::max(x, *max_x);
53  *max_y = std::max(y, *max_y);
54 }
55 
57 {
58  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
59  Costmap2D * master = layered_costmap_->getCostmap();
60  if (!master) {
61  RCLCPP_WARN(
62  rclcpp::get_logger("nav2_costmap_2d"),
63  "Cannot match size for layer, master costmap is not initialized yet.");
64  return;
65  }
66  resizeMap(
67  master->getSizeInCellsX(), master->getSizeInCellsY(), master->getResolution(),
68  master->getOriginX(), master->getOriginY());
69 }
70 
71 void CostmapLayer::clearArea(int start_x, int start_y, int end_x, int end_y, bool invert)
72 {
73  setCurrent(false);
74  unsigned char * grid = getCharMap();
75 
76  int size_x = getSizeInCellsX();
77  int size_y = getSizeInCellsY();
78 
79  start_x = std::clamp(start_x, 0, size_x);
80  start_y = std::clamp(start_y, 0, size_y);
81  end_x = std::clamp(end_x, 0, size_x);
82  end_y = std::clamp(end_y, 0, size_y);
83 
84  for (int x = 0; x < size_x; x++) {
85  bool xrange = x > start_x && x < end_x;
86 
87  for (int y = 0; y < size_y; y++) {
88  if ((xrange && y > start_y && y < end_y) == invert) {
89  continue;
90  }
91  int index = getIndex(x, y);
92  if (grid[index] != NO_INFORMATION) {
93  grid[index] = NO_INFORMATION;
94  }
95  }
96  }
97 }
98 
99 void CostmapLayer::addExtraBounds(double mx0, double my0, double mx1, double my1)
100 {
101  extra_min_x_ = std::min(mx0, extra_min_x_);
102  extra_max_x_ = std::max(mx1, extra_max_x_);
103  extra_min_y_ = std::min(my0, extra_min_y_);
104  extra_max_y_ = std::max(my1, extra_max_y_);
105  has_extra_bounds_ = true;
106 }
107 
108 void CostmapLayer::useExtraBounds(double * min_x, double * min_y, double * max_x, double * max_y)
109 {
110  if (!has_extra_bounds_) {
111  return;
112  }
113 
114  *min_x = std::min(extra_min_x_, *min_x);
115  *min_y = std::min(extra_min_y_, *min_y);
116  *max_x = std::max(extra_max_x_, *max_x);
117  *max_y = std::max(extra_max_y_, *max_y);
118  extra_min_x_ = 1e6;
119  extra_min_y_ = 1e6;
120  extra_max_x_ = -1e6;
121  extra_max_y_ = -1e6;
122  has_extra_bounds_ = false;
123 }
124 
125 void CostmapLayer::updateWithMax(
126  nav2_costmap_2d::Costmap2D & master_grid, int min_i, int min_j,
127  int max_i,
128  int max_j)
129 {
130  if (!enabled_) {
131  return;
132  }
133 
134  unsigned char * master_array = master_grid.getCharMap();
135  unsigned int span = master_grid.getSizeInCellsX();
136 
137  for (int j = min_j; j < max_j; j++) {
138  unsigned int it = j * span + min_i;
139  for (int i = min_i; i < max_i; i++) {
140  if (costmap_[it] == NO_INFORMATION) {
141  it++;
142  continue;
143  }
144 
145  unsigned char old_cost = master_array[it];
146  if (old_cost == NO_INFORMATION || old_cost < costmap_[it]) {
147  master_array[it] = costmap_[it];
148  }
149  it++;
150  }
151  }
152 }
153 
154 void CostmapLayer::updateWithMaxWithoutUnknownOverwrite(
155  nav2_costmap_2d::Costmap2D & master_grid, int min_i, int min_j,
156  int max_i,
157  int max_j)
158 {
159  if (!enabled_) {
160  return;
161  }
162 
163  unsigned char * master_array = master_grid.getCharMap();
164  unsigned int span = master_grid.getSizeInCellsX();
165 
166  for (int j = min_j; j < max_j; j++) {
167  unsigned int it = j * span + min_i;
168  for (int i = min_i; i < max_i; i++) {
169  if (costmap_[it] == NO_INFORMATION) {
170  it++;
171  continue;
172  }
173 
174  unsigned char old_cost = master_array[it];
175  if (old_cost != NO_INFORMATION && old_cost < costmap_[it]) {
176  master_array[it] = costmap_[it];
177  }
178  it++;
179  }
180  }
181 }
182 
183 void CostmapLayer::updateWithTrueOverwrite(
184  nav2_costmap_2d::Costmap2D & master_grid, int min_i,
185  int min_j,
186  int max_i,
187  int max_j)
188 {
189  if (!enabled_) {
190  return;
191  }
192 
193  if (costmap_ == nullptr) {
194  throw std::runtime_error("Can't update costmap layer: It has't been initialized yet!");
195  }
196 
197  unsigned char * master = master_grid.getCharMap();
198  unsigned int span = master_grid.getSizeInCellsX();
199 
200  for (int j = min_j; j < max_j; j++) {
201  unsigned int it = span * j + min_i;
202  for (int i = min_i; i < max_i; i++) {
203  master[it] = costmap_[it];
204  it++;
205  }
206  }
207 }
208 
209 void CostmapLayer::updateWithOverwrite(
210  nav2_costmap_2d::Costmap2D & master_grid,
211  int min_i, int min_j, int max_i, int max_j)
212 {
213  if (!enabled_) {
214  return;
215  }
216  unsigned char * master = master_grid.getCharMap();
217  unsigned int span = master_grid.getSizeInCellsX();
218 
219  for (int j = min_j; j < max_j; j++) {
220  unsigned int it = span * j + min_i;
221  for (int i = min_i; i < max_i; i++) {
222  if (costmap_[it] != NO_INFORMATION) {
223  master[it] = costmap_[it];
224  }
225  it++;
226  }
227  }
228 }
229 
230 void CostmapLayer::updateWithAddition(
231  nav2_costmap_2d::Costmap2D & master_grid,
232  int min_i, int min_j, int max_i, int max_j)
233 {
234  if (!enabled_) {
235  return;
236  }
237  unsigned char * master_array = master_grid.getCharMap();
238  unsigned int span = master_grid.getSizeInCellsX();
239 
240  for (int j = min_j; j < max_j; j++) {
241  unsigned int it = j * span + min_i;
242  for (int i = min_i; i < max_i; i++) {
243  if (costmap_[it] == NO_INFORMATION) {
244  it++;
245  continue;
246  }
247 
248  unsigned char old_cost = master_array[it];
249  if (old_cost == NO_INFORMATION) {
250  master_array[it] = costmap_[it];
251  } else {
252  int sum = old_cost + costmap_[it];
253  if (sum >= nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE) {
254  master_array[it] = nav2_costmap_2d::INSCRIBED_INFLATED_OBSTACLE - 1;
255  } else {
256  master_array[it] = sum;
257  }
258  }
259  it++;
260  }
261  }
262 }
263 
265 {
266  switch (value) {
267  case 0:
269  case 1:
270  return CombinationMethod::Max;
271  case 2:
273  default:
274  RCLCPP_WARN(
275  logger_,
276  "Param combination_method: %i. Possible values are 0 (Overwrite) or 1 (Maximum) or "
277  "2 (Maximum without overwriting the master's NO_INFORMATION values)."
278  "The default value 1 will be used", value);
279  return CombinationMethod::Max;
280  }
281 }
282 } // namespace nav2_costmap_2d
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:231
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Resize the costmap.
Definition: costmap_2d.cpp:111
unsigned char * getCharMap() const
Will return a pointer to the underlying unsigned char array used as the costmap.
Definition: costmap_2d.cpp:260
double getResolution() const
Accessor for the resolution of the costmap.
Definition: costmap_2d.cpp:578
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:548
double getOriginY() const
Accessor for the y origin of the costmap.
Definition: costmap_2d.cpp:573
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:553
double getOriginX() const
Accessor for the x origin of the costmap.
Definition: costmap_2d.cpp:568
void addExtraBounds(double mx0, double my0, double mx1, double my1)
void touch(double x, double y, double *min_x, double *min_y, double *max_x, double *max_y)
virtual void clearArea(int start_x, int start_y, int end_x, int end_y, bool invert)
Clear an are in the costmap with the given dimension if invert, then clear everything except these di...
virtual void matchSize()
Match the size of the master costmap.
CombinationMethod combination_method_from_int(const int value)
Converts an integer to a CombinationMethod enum and logs on failure.
void setCurrent(bool current)
Set whether the data in the layer is up to date.
Definition: layer.hpp:147
Costmap2D * getCostmap()
Get the costmap pointer to the master costmap.