cost_interpreter.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #include <dlux_global_planner/cost_interpreter.h>
00036 #include <algorithm>
00037 #include <limits>
00038 #include <stdexcept>
00039 #include <string>
00040 
00041 namespace dlux_global_planner
00042 {
00043 void CostInterpreter::initialize(ros::NodeHandle& nh, nav_core2::Costmap::Ptr costmap)
00044 {
00045   costmap_ = costmap;
00046   int neutral_cost;
00047   nh.param("neutral_cost", neutral_cost, 50);
00048   if (neutral_cost < 0 || neutral_cost > std::numeric_limits<unsigned char>::max())
00049   {
00050     throw std::invalid_argument("neutral_cost (" + std::to_string(neutral_cost) + ") must be a valid unsigned char!");
00051   }
00052 
00053   float scale;
00054   // The default value matches navfn's default value
00055   // https://github.com/ros-planning/navigation/blob/8ea5462f5395acf9741253ff38302d29175dd870/global_planner/cfg/GlobalPlanner.cfg#L10
00056   nh.param("scale", scale, 3.0f);
00057 
00058   UnknownInterpretation mode = UnknownInterpretation::EXPENSIVE;
00059   if (nh.hasParam("unknown_interpretation"))
00060   {
00061     if (nh.hasParam("allow_unknown"))
00062     {
00063       ROS_ERROR("allow_unknown can't be specified at the same time as unknown_interpretation.");
00064       ROS_ERROR("Using the value of unknown_interpretation.");
00065     }
00066     std::string unknown_str;
00067     nh.getParam("unknown_interpretation", unknown_str);
00068     if (unknown_str == "lethal")
00069     {
00070       mode = UnknownInterpretation::LETHAL;
00071     }
00072     else if (unknown_str == "expensive")
00073     {
00074       mode = UnknownInterpretation::EXPENSIVE;
00075     }
00076     else if (unknown_str == "free")
00077     {
00078       mode = UnknownInterpretation::FREE;
00079     }
00080     else
00081     {
00082       ROS_ERROR("Unknown value for unknown_interpretation '%s'. Using expensive instead.", unknown_str.c_str());
00083       mode = UnknownInterpretation::EXPENSIVE;
00084     }
00085   }
00086 
00087   setConfiguration(static_cast<unsigned char>(neutral_cost), scale, mode);
00088 }
00089 
00090 void CostInterpreter::setConfiguration(const unsigned char neutral_cost, const float scale,
00091                                        const UnknownInterpretation mode)
00092 {
00093   neutral_cost_ = neutral_cost;
00094   for (unsigned int i = 0; i < cached_costs_.size(); i++)
00095   {
00096     if (i == nav_core2::Costmap::NO_INFORMATION)
00097     {
00098       float c;
00099       switch (mode)
00100       {
00101       case UnknownInterpretation::LETHAL:
00102         c = LETHAL_COST_F;
00103         break;
00104       case UnknownInterpretation::EXPENSIVE:
00105         c = LETHAL_COST_F - 1.0;
00106         break;
00107       default:  // case FREE:
00108         c = neutral_cost_;
00109         break;
00110       }
00111       cached_costs_[i] = c;
00112     }
00113     else if (i <= LETHAL_COST - 1)
00114     {
00115       float c = i * scale + neutral_cost_;
00116       cached_costs_[i] = std::min(c, LETHAL_COST_F - 1.0f);
00117     }
00118     else
00119     {
00120       cached_costs_[i] = LETHAL_COST_F;
00121     }
00122   }
00123 }
00124 
00125 
00126 }  // namespace dlux_global_planner


dlux_global_planner
Author(s):
autogenerated on Wed Jun 26 2019 20:09:53