Go to the documentation of this file.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 #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   
00055   
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:  
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 }