layered_costmap.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  * 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  *********************************************************************/
39 #include <costmap_2d/footprint.h>
40 #include <cstdio>
41 #include <string>
42 #include <algorithm>
43 #include <vector>
44 
45 using std::vector;
46 
47 namespace costmap_2d
48 {
49 
50 LayeredCostmap::LayeredCostmap(std::string global_frame, bool rolling_window, bool track_unknown) :
51  costmap_(),
52  global_frame_(global_frame),
53  rolling_window_(rolling_window),
54  current_(false),
55  minx_(0.0),
56  miny_(0.0),
57  maxx_(0.0),
58  maxy_(0.0),
59  bx0_(0),
60  bxn_(0),
61  by0_(0),
62  byn_(0),
63  initialized_(false),
64  size_locked_(false),
65  circumscribed_radius_(1.0),
66  inscribed_radius_(0.1)
67 {
68  if (track_unknown)
70  else
72 }
73 
75 {
76  while (plugins_.size() > 0)
77  {
78  plugins_.pop_back();
79  }
80 }
81 
82 void LayeredCostmap::resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x,
83  double origin_y, bool size_locked)
84 {
85  boost::unique_lock<Costmap2D::mutex_t> lock(*(costmap_.getMutex()));
86  size_locked_ = size_locked;
87  costmap_.resizeMap(size_x, size_y, resolution, origin_x, origin_y);
88  for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
89  ++plugin)
90  {
91  (*plugin)->matchSize();
92  }
93 }
94 
95 void LayeredCostmap::updateMap(double robot_x, double robot_y, double robot_yaw)
96 {
97  // Lock for the remainder of this function, some plugins (e.g. VoxelLayer)
98  // implement thread unsafe updateBounds() functions.
99  boost::unique_lock<Costmap2D::mutex_t> lock(*(costmap_.getMutex()));
100 
101  // if we're using a rolling buffer costmap... we need to update the origin using the robot's position
102  if (rolling_window_)
103  {
104  double new_origin_x = robot_x - costmap_.getSizeInMetersX() / 2;
105  double new_origin_y = robot_y - costmap_.getSizeInMetersY() / 2;
106  costmap_.updateOrigin(new_origin_x, new_origin_y);
107  }
108 
109  if (plugins_.size() == 0)
110  return;
111 
112  minx_ = miny_ = 1e30;
113  maxx_ = maxy_ = -1e30;
114 
115  for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
116  ++plugin)
117  {
118  if(!(*plugin)->isEnabled())
119  continue;
120  double prev_minx = minx_;
121  double prev_miny = miny_;
122  double prev_maxx = maxx_;
123  double prev_maxy = maxy_;
124  (*plugin)->updateBounds(robot_x, robot_y, robot_yaw, &minx_, &miny_, &maxx_, &maxy_);
125  if (minx_ > prev_minx || miny_ > prev_miny || maxx_ < prev_maxx || maxy_ < prev_maxy)
126  {
127  ROS_WARN_THROTTLE(1.0, "Illegal bounds change, was [tl: (%f, %f), br: (%f, %f)], but "
128  "is now [tl: (%f, %f), br: (%f, %f)]. The offending layer is %s",
129  prev_minx, prev_miny, prev_maxx , prev_maxy,
130  minx_, miny_, maxx_ , maxy_,
131  (*plugin)->getName().c_str());
132  }
133  }
134 
135  int x0, xn, y0, yn;
138 
139  x0 = std::max(0, x0);
140  xn = std::min(int(costmap_.getSizeInCellsX()), xn + 1);
141  y0 = std::max(0, y0);
142  yn = std::min(int(costmap_.getSizeInCellsY()), yn + 1);
143 
144  ROS_DEBUG("Updating area x: [%d, %d] y: [%d, %d]", x0, xn, y0, yn);
145 
146  if (xn < x0 || yn < y0)
147  return;
148 
149  costmap_.resetMap(x0, y0, xn, yn);
150  for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
151  ++plugin)
152  {
153  if((*plugin)->isEnabled())
154  (*plugin)->updateCosts(costmap_, x0, y0, xn, yn);
155  }
156 
157  bx0_ = x0;
158  bxn_ = xn;
159  by0_ = y0;
160  byn_ = yn;
161 
162  initialized_ = true;
163 }
164 
166 {
167  current_ = true;
168  for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
169  ++plugin)
170  {
171  if((*plugin)->isEnabled())
172  current_ = current_ && (*plugin)->isCurrent();
173  }
174  return current_;
175 }
176 
177 void LayeredCostmap::setFootprint(const std::vector<geometry_msgs::Point>& footprint_spec)
178 {
179  footprint_ = footprint_spec;
181 
182  for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
183  ++plugin)
184  {
185  (*plugin)->onFootprintChanged();
186  }
187 }
188 
189 } // namespace costmap_2d
costmap_2d::Costmap2D::getSizeInMetersX
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition: costmap_2d.cpp:440
boost::shared_ptr
costmap_2d::Costmap2D::getSizeInMetersY
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition: costmap_2d.cpp:445
ROS_WARN_THROTTLE
#define ROS_WARN_THROTTLE(period,...)
costmap_2d::LayeredCostmap::by0_
unsigned int by0_
Definition: layered_costmap.h:201
costmap_2d::LayeredCostmap::inscribed_radius_
double inscribed_radius_
Definition: layered_costmap.h:207
costmap_2d::LayeredCostmap::setFootprint
void setFootprint(const std::vector< geometry_msgs::Point > &footprint_spec)
Updates the stored footprint, updates the circumscribed and inscribed radii, and calls onFootprintCha...
Definition: layered_costmap.cpp:177
costmap_2d::NO_INFORMATION
static const unsigned char NO_INFORMATION
Definition: cost_values.h:77
costmap_2d::LayeredCostmap::initialized_
bool initialized_
Definition: layered_costmap.h:205
costmap_2d::LayeredCostmap::resizeMap
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y, bool size_locked=false)
Definition: layered_costmap.cpp:82
layered_costmap.h
costmap_2d::LayeredCostmap::size_locked_
bool size_locked_
Definition: layered_costmap.h:206
costmap_2d::calculateMinAndMaxDistances
void calculateMinAndMaxDistances(const std::vector< geometry_msgs::Point > &footprint, double &min_dist, double &max_dist)
Calculate the extreme distances for the footprint.
Definition: footprint.cpp:41
costmap_2d::LayeredCostmap::rolling_window_
bool rolling_window_
Definition: layered_costmap.h:197
costmap_2d::LayeredCostmap::footprint_
std::vector< geometry_msgs::Point > footprint_
Definition: layered_costmap.h:208
costmap_2d::LayeredCostmap::circumscribed_radius_
double circumscribed_radius_
Definition: layered_costmap.h:207
costmap_2d::LayeredCostmap::bx0_
unsigned int bx0_
Definition: layered_costmap.h:201
ROS_DEBUG
#define ROS_DEBUG(...)
costmap_2d::Costmap2D::resizeMap
void resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x, double origin_y)
Definition: costmap_2d.cpp:72
costmap_2d::LayeredCostmap::LayeredCostmap
LayeredCostmap(std::string global_frame, bool rolling_window, bool track_unknown)
Constructor for a costmap.
Definition: layered_costmap.cpp:50
costmap_2d::LayeredCostmap::minx_
double minx_
Definition: layered_costmap.h:200
costmap_2d::LayeredCostmap::plugins_
std::vector< boost::shared_ptr< Layer > > plugins_
Definition: layered_costmap.h:203
costmap_2d::Costmap2D::getSizeInCellsY
unsigned int getSizeInCellsY() const
Accessor for the y size of the costmap in cells.
Definition: costmap_2d.cpp:435
costmap_2d::LayeredCostmap::maxy_
double maxy_
Definition: layered_costmap.h:200
costmap_2d::Costmap2D::resetMap
void resetMap(unsigned int x0, unsigned int y0, unsigned int xn, unsigned int yn)
Definition: costmap_2d.cpp:93
costmap_2d::LayeredCostmap::bxn_
unsigned int bxn_
Definition: layered_costmap.h:201
costmap_2d::LayeredCostmap::current_
bool current_
<
Definition: layered_costmap.h:199
costmap_2d::LayeredCostmap::isCurrent
bool isCurrent()
Definition: layered_costmap.cpp:165
costmap_2d::Costmap2D::getMutex
mutex_t * getMutex()
Definition: costmap_2d.h:331
footprint.h
costmap_2d::LayeredCostmap::byn_
unsigned int byn_
Definition: layered_costmap.h:201
costmap_2d::Costmap2D::worldToMapEnforceBounds
void worldToMapEnforceBounds(double wx, double wy, int &mx, int &my) const
Convert from world coordinates to map coordinates, constraining results to legal bounds.
Definition: costmap_2d.cpp:228
costmap_2d::LayeredCostmap::maxx_
double maxx_
Definition: layered_costmap.h:200
costmap_2d::LayeredCostmap::updateMap
void updateMap(double robot_x, double robot_y, double robot_yaw)
Update the underlying costmap with new data. If you want to update the map outside of the update loop...
Definition: layered_costmap.cpp:95
costmap_2d::LayeredCostmap::~LayeredCostmap
~LayeredCostmap()
Destructor.
Definition: layered_costmap.cpp:74
costmap_2d::Costmap2D::setDefaultValue
void setDefaultValue(unsigned char c)
Definition: costmap_2d.h:272
costmap_2d::Costmap2D::updateOrigin
virtual void updateOrigin(double new_origin_x, double new_origin_y)
Move the origin of the costmap to a new location.... keeping data when it can.
Definition: costmap_2d.cpp:260
costmap_2d::LayeredCostmap::costmap_
Costmap2D costmap_
Definition: layered_costmap.h:194
costmap_2d::FREE_SPACE
static const unsigned char FREE_SPACE
Definition: cost_values.h:80
costmap_2d::Costmap2D::getSizeInCellsX
unsigned int getSizeInCellsX() const
Accessor for the x size of the costmap in cells.
Definition: costmap_2d.cpp:430
costmap_2d::LayeredCostmap::miny_
double miny_
Definition: layered_costmap.h:200
costmap_2d
Definition: array_parser.h:37


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:17