von_neumann_path.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_plugins/von_neumann_path.h>
00036 #include <nav_grid/coordinate_conversion.h>
00037 #include <nav_core2/exceptions.h>
00038 #include <limits>
00039 #include <pluginlib/class_list_macros.h>
00040 
00041 PLUGINLIB_EXPORT_CLASS(dlux_plugins::VonNeumannPath, dlux_global_planner::Traceback)
00042 
00043 namespace dlux_plugins
00044 {
00045 nav_2d_msgs::Path2D VonNeumannPath::getPath(const dlux_global_planner::PotentialGrid& potential_grid,
00046                                             const geometry_msgs::Pose2D& start, const geometry_msgs::Pose2D& goal,
00047                                             double& path_cost)
00048 {
00049   nav_2d_msgs::Path2D path;
00050   path_cost = 0.0;
00051 
00052   unsigned int current_x = 0, current_y = 0;
00053   worldToGridBounded(potential_grid.getInfo(), start.x, start.y, current_x, current_y);
00054 
00055   // Add 0.5 to represent center of the cell
00056   geometry_msgs::Pose2D current;
00057   current.x = current_x + 0.5;
00058   current.y = current_y + 0.5;
00059   path.poses.push_back(current);
00060   path_cost += cost_interpreter_->getCost(current_x, current_y);
00061 
00062   unsigned int goal_index = potential_grid.getIndex(goal.x, goal.y);
00063 
00064   // Main Loop
00065   while (potential_grid.getIndex(current_x, current_y) != goal_index)
00066   {
00067     float min_val = std::numeric_limits<float>::max();
00068     unsigned int min_x = 0, min_y = 0;
00069     unsigned int x, y;
00070     int index;
00071 
00072     // Check the four neighbors
00073     if (current_x > 0)
00074     {
00075       x = current_x - 1;
00076       y = current_y;
00077       index = potential_grid.getIndex(x, y);
00078       if (potential_grid[index] < min_val)
00079       {
00080         min_val = potential_grid[index];
00081         min_x = x;
00082         min_y = y;
00083       }
00084     }
00085     if (current_x < potential_grid.getWidth() - 1)
00086     {
00087       x = current_x + 1;
00088       y = current_y;
00089       index = potential_grid.getIndex(x, y);
00090       if (potential_grid[index] < min_val)
00091       {
00092         min_val = potential_grid[index];
00093         min_x = x;
00094         min_y = y;
00095       }
00096     }
00097     if (current_y > 0)
00098     {
00099       x = current_x;
00100       y = current_y - 1;
00101       index = potential_grid.getIndex(x, y);
00102       if (potential_grid[index] < min_val)
00103       {
00104         min_val = potential_grid[index];
00105         min_x = x;
00106         min_y = y;
00107       }
00108     }
00109     if (current_y < potential_grid.getHeight() - 1)
00110     {
00111       x = current_x;
00112       y = current_y + 1;
00113       index = potential_grid.getIndex(x, y);
00114       if (potential_grid[index] < min_val)
00115       {
00116         min_val = potential_grid[index];
00117         min_x = x;
00118         min_y = y;
00119       }
00120     }
00121 
00122     if (min_val == std::numeric_limits<float>::max())
00123       throw nav_core2::PlannerException("Reached dead end in traceback.");
00124 
00125     // Move to the Min Neighbor
00126     current_x = min_x;
00127     current_y = min_y;
00128 
00129     // Add 0.5 to represent center of the cell
00130     current.x = current_x + 0.5;
00131     current.y = current_y + 0.5;
00132     path.poses.push_back(current);
00133     path_cost += cost_interpreter_->getCost(current_x, current_y);
00134   }
00135   return mapPathToWorldPath(path, potential_grid.getInfo());
00136 }
00137 
00138 }  // namespace dlux_plugins


dlux_plugins
Author(s):
autogenerated on Wed Jun 26 2019 20:09:55