costmap_model.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2008, 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 the Willow Garage 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 *********************************************************************/
00037 #include <base_local_planner/line_iterator.h>
00038 #include <base_local_planner/costmap_model.h>
00039 #include <costmap_2d/cost_values.h>
00040 
00041 using namespace std;
00042 using namespace costmap_2d;
00043 
00044 namespace base_local_planner {
00045   CostmapModel::CostmapModel(const Costmap2D& ma) : costmap_(ma) {}
00046 
00047   double CostmapModel::footprintCost(const geometry_msgs::Point& position, const std::vector<geometry_msgs::Point>& footprint, 
00048       double inscribed_radius, double circumscribed_radius){
00049 
00050     //used to put things into grid coordinates
00051     unsigned int cell_x, cell_y;
00052 
00053     //get the cell coord of the center point of the robot
00054     if(!costmap_.worldToMap(position.x, position.y, cell_x, cell_y))
00055       return -1.0;
00056 
00057     //if number of points in the footprint is less than 3, we'll just assume a circular robot
00058     if(footprint.size() < 3){
00059       unsigned char cost = costmap_.getCost(cell_x, cell_y);
00060       //if(cost == LETHAL_OBSTACLE || cost == INSCRIBED_INFLATED_OBSTACLE)
00061       if(cost == LETHAL_OBSTACLE || cost == INSCRIBED_INFLATED_OBSTACLE || cost == NO_INFORMATION)
00062         return -1.0;
00063       return cost;
00064     }
00065 
00066     //now we really have to lay down the footprint in the costmap grid
00067     unsigned int x0, x1, y0, y1;
00068     double line_cost = 0.0;
00069     double footprint_cost = 0.0;
00070 
00071     //we need to rasterize each line in the footprint
00072     for(unsigned int i = 0; i < footprint.size() - 1; ++i){
00073       //get the cell coord of the first point
00074       if(!costmap_.worldToMap(footprint[i].x, footprint[i].y, x0, y0))
00075         return -1.0;
00076 
00077       //get the cell coord of the second point
00078       if(!costmap_.worldToMap(footprint[i + 1].x, footprint[i + 1].y, x1, y1))
00079         return -1.0;
00080 
00081       line_cost = lineCost(x0, x1, y0, y1);
00082       footprint_cost = std::max(line_cost, footprint_cost);
00083 
00084       //if there is an obstacle that hits the line... we know that we can return false right away 
00085       if(line_cost < 0)
00086         return -1.0;
00087     }
00088 
00089     //we also need to connect the first point in the footprint to the last point
00090     //get the cell coord of the last point
00091     if(!costmap_.worldToMap(footprint.back().x, footprint.back().y, x0, y0))
00092       return -1.0;
00093 
00094     //get the cell coord of the first point
00095     if(!costmap_.worldToMap(footprint.front().x, footprint.front().y, x1, y1))
00096       return -1.0;
00097 
00098     line_cost = lineCost(x0, x1, y0, y1);
00099     footprint_cost = std::max(line_cost, footprint_cost);
00100 
00101     if(line_cost < 0)
00102       return -1.0;
00103 
00104     //if all line costs are legal... then we can return that the footprint is legal
00105     return footprint_cost;
00106 
00107   }
00108 
00109   //calculate the cost of a ray-traced line
00110   double CostmapModel::lineCost(int x0, int x1, 
00111       int y0, int y1){
00112     
00113     double line_cost = 0.0;
00114     double point_cost = -1.0;
00115 
00116     for( LineIterator line( x0, y0, x1, y1 ); line.isValid(); line.advance() )
00117     {
00118       point_cost = pointCost( line.getX(), line.getY() ); //Score the current point
00119 
00120       if(point_cost < 0)
00121         return -1;
00122 
00123       if(line_cost < point_cost)
00124         line_cost = point_cost;
00125     }
00126 
00127     return line_cost;
00128   }
00129 
00130   double CostmapModel::pointCost(int x, int y){
00131     unsigned char cost = costmap_.getCost(x, y);
00132     //if the cell is in an obstacle the path is invalid
00133     //if(cost == LETHAL_OBSTACLE){
00134     if(cost == LETHAL_OBSTACLE || cost == NO_INFORMATION){
00135       return -1;
00136     }
00137 
00138     return cost;
00139   }
00140 
00141 };


base_local_planner
Author(s): Eitan Marder-Eppstein, Eric Perko, contradict@gmail.com
autogenerated on Wed Aug 2 2017 03:12:38