grid_path.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <dlux_plugins/grid_path.h>
37 #include <nav_core2/exceptions.h>
38 #include <limits>
40 
42 
43 namespace dlux_plugins
44 {
45 
46 nav_2d_msgs::Path2D GridPath::getPath(const dlux_global_planner::PotentialGrid& potential_grid,
47  const geometry_msgs::Pose2D& start, const geometry_msgs::Pose2D& goal,
48  double& path_cost)
49 {
50  const nav_grid::NavGridInfo& info = potential_grid.getInfo();
51  nav_2d_msgs::Path2D path;
52  path_cost = 0.0;
53 
54  unsigned int current_x = 0, current_y = 0;
55  worldToGridBounded(info, start.x, start.y, current_x, current_y);
56 
57  // Add 0.5 to represent center of the cell
58  geometry_msgs::Pose2D current;
59  current.x = current_x + 0.5;
60  current.y = current_y + 0.5;
61  path.poses.push_back(current);
62 
63  unsigned int goal_index = potential_grid.getIndex(goal.x, goal.y);
64 
65  // Main Loop
66  while (potential_grid.getIndex(current_x, current_y) != goal_index)
67  {
68  float min_val = std::numeric_limits<float>::max();
69  unsigned int min_x = 0, min_y = 0;
70  int distance_sq = 0;
71  for (int xd = -1; xd <= 1; xd++)
72  {
73  if ((current_x == 0 && xd == -1) || (current_x == info.width - 1 && xd == 1)) continue;
74  for (int yd = -1; yd <= 1; yd++)
75  {
76  if ((current_y == 0 && yd == -1) || (current_y == info.height - 1 && yd == 1)) continue;
77  if (xd == 0 && yd == 0)
78  continue;
79  unsigned int x = current_x + xd, y = current_y + yd;
80  int index = potential_grid.getIndex(x, y);
81  if (potential_grid[index] < min_val)
82  {
83  min_val = potential_grid[index];
84  min_x = x;
85  min_y = y;
86  distance_sq = abs(xd) + abs(yd);
87  }
88  }
89  }
90  if (distance_sq == 0)
91  throw nav_core2::PlannerException("Reached dead end in traceback.");
92 
93  double distance;
94  if (distance_sq == 1)
95  distance = 0.5;
96  else
97  distance = M_SQRT1_2; // sqrt(2)/2
98 
99  path_cost += distance * cost_interpreter_->getCost(current_x, current_y);
100 
101  // Move to the Min Neighbor
102  current_x = min_x;
103  current_y = min_y;
104 
105  // Add 0.5 to represent center of the cell
106  current.x = current_x + 0.5;
107  current.y = current_y + 0.5;
108  path.poses.push_back(current);
109  path_cost += distance * cost_interpreter_->getCost(current_x, current_y);
110  }
111  return mapPathToWorldPath(path, info);
112 }
113 
114 } // namespace dlux_plugins
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
unsigned int getIndex(unsigned int mx, unsigned int my) const
TFSIMD_FORCE_INLINE const tfScalar & y() const
Traceback function that moves from cell to cell using any of the eight neighbors. ...
Definition: grid_path.h:46
TFSIMD_FORCE_INLINE const tfScalar & x() const
NavGridInfo getInfo() const
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
bool worldToGridBounded(const NavGridInfo &info, double wx, double wy, unsigned int &mx, unsigned int &my)


dlux_plugins
Author(s):
autogenerated on Wed Jun 26 2019 20:06:32