00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <costmap_2d/layered_costmap.h>
00039 #include <costmap_2d/footprint.h>
00040 #include <cstdio>
00041 #include <string>
00042 #include <algorithm>
00043 #include <vector>
00044
00045 using std::vector;
00046
00047 namespace costmap_2d
00048 {
00049
00050 LayeredCostmap::LayeredCostmap(std::string global_frame, bool rolling_window, bool track_unknown) :
00051 costmap_(), global_frame_(global_frame), rolling_window_(rolling_window), initialized_(false), size_locked_(false)
00052 {
00053 if (track_unknown)
00054 costmap_.setDefaultValue(255);
00055 else
00056 costmap_.setDefaultValue(0);
00057 }
00058
00059 LayeredCostmap::~LayeredCostmap()
00060 {
00061 while (plugins_.size() > 0)
00062 {
00063 plugins_.pop_back();
00064 }
00065 }
00066
00067 void LayeredCostmap::resizeMap(unsigned int size_x, unsigned int size_y, double resolution, double origin_x,
00068 double origin_y, bool size_locked)
00069 {
00070 size_locked_ = size_locked;
00071 costmap_.resizeMap(size_x, size_y, resolution, origin_x, origin_y);
00072 for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
00073 ++plugin)
00074 {
00075 (*plugin)->matchSize();
00076 }
00077 }
00078
00079 void LayeredCostmap::updateMap(double robot_x, double robot_y, double robot_yaw)
00080 {
00081
00082
00083 boost::unique_lock<Costmap2D::mutex_t> lock(*(costmap_.getMutex()));
00084
00085
00086 if (rolling_window_)
00087 {
00088 double new_origin_x = robot_x - costmap_.getSizeInMetersX() / 2;
00089 double new_origin_y = robot_y - costmap_.getSizeInMetersY() / 2;
00090 costmap_.updateOrigin(new_origin_x, new_origin_y);
00091 }
00092
00093 if (plugins_.size() == 0)
00094 return;
00095
00096 minx_ = miny_ = 1e30;
00097 maxx_ = maxy_ = -1e30;
00098
00099 for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
00100 ++plugin)
00101 {
00102 double prev_minx = minx_;
00103 double prev_miny = miny_;
00104 double prev_maxx = maxx_;
00105 double prev_maxy = maxy_;
00106 (*plugin)->updateBounds(robot_x, robot_y, robot_yaw, &minx_, &miny_, &maxx_, &maxy_);
00107 if (minx_ > prev_minx || miny_ > prev_miny || maxx_ < prev_maxx || maxy_ < prev_maxy)
00108 {
00109 ROS_WARN_THROTTLE(1.0, "Illegal bounds change, was [tl: (%f, %f), br: (%f, %f)], but "
00110 "is now [tl: (%f, %f), br: (%f, %f)]. The offending layer is %s",
00111 prev_minx, prev_miny, prev_maxx , prev_maxy,
00112 minx_, miny_, maxx_ , maxy_,
00113 (*plugin)->getName().c_str());
00114 }
00115 }
00116
00117 int x0, xn, y0, yn;
00118 costmap_.worldToMapEnforceBounds(minx_, miny_, x0, y0);
00119 costmap_.worldToMapEnforceBounds(maxx_, maxy_, xn, yn);
00120
00121 x0 = std::max(0, x0);
00122 xn = std::min(int(costmap_.getSizeInCellsX()), xn + 1);
00123 y0 = std::max(0, y0);
00124 yn = std::min(int(costmap_.getSizeInCellsY()), yn + 1);
00125
00126 ROS_DEBUG("Updating area x: [%d, %d] y: [%d, %d]", x0, xn, y0, yn);
00127
00128 if (xn < x0 || yn < y0)
00129 return;
00130
00131 costmap_.resetMap(x0, y0, xn, yn);
00132 for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
00133 ++plugin)
00134 {
00135 (*plugin)->updateCosts(costmap_, x0, y0, xn, yn);
00136 }
00137
00138 bx0_ = x0;
00139 bxn_ = xn;
00140 by0_ = y0;
00141 byn_ = yn;
00142
00143 initialized_ = true;
00144 }
00145
00146 bool LayeredCostmap::isCurrent()
00147 {
00148 current_ = true;
00149 for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
00150 ++plugin)
00151 {
00152 current_ = current_ && (*plugin)->isCurrent();
00153 }
00154 return current_;
00155 }
00156
00157 void LayeredCostmap::setFootprint(const std::vector<geometry_msgs::Point>& footprint_spec)
00158 {
00159 footprint_ = footprint_spec;
00160 costmap_2d::calculateMinAndMaxDistances(footprint_spec, inscribed_radius_, circumscribed_radius_);
00161
00162 for (vector<boost::shared_ptr<Layer> >::iterator plugin = plugins_.begin(); plugin != plugins_.end();
00163 ++plugin)
00164 {
00165 (*plugin)->onFootprintChanged();
00166 }
00167 }
00168
00169 }