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


base_local_planner
Author(s): Eitan Marder-Eppstein, Eric Perko
autogenerated on Mon Oct 6 2014 02:45:34