layered_costmap.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2008, 2013, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Eitan Marder-Eppstein
00036  *         David V. Lu!!
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   // Lock for the remainder of this function, some plugins (e.g. VoxelLayer)
00082   // implement thread unsafe updateBounds() functions.
00083   boost::unique_lock<Costmap2D::mutex_t> lock(*(costmap_.getMutex()));
00084 
00085   // if we're using a rolling buffer costmap... we need to update the origin using the robot's position
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 }  // namespace costmap_2d


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Wed Aug 2 2017 03:12:21