PathCostHeuristic.cpp
Go to the documentation of this file.
1 /*
2  * A footstep planner for humanoid robots
3  *
4  * Copyright 2010-2011 Johannes Garimort, Armin Hornung, University of Freiburg
5  * http://www.ros.org/wiki/footstep_planner
6  *
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, version 3.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
22 
23 
24 namespace footstep_planner
25 {
27  int num_angle_bins,
28  double step_cost,
29  double diff_angle_cost,
30  double max_step_width,
31  double inflation_radius)
32 : Heuristic(cell_size, num_angle_bins, PATH_COST),
33  ivpGrid(NULL),
34  ivStepCost(step_cost),
35  ivDiffAngleCost(diff_angle_cost),
36  ivMaxStepWidth(max_step_width),
37  ivInflationRadius(inflation_radius),
38  ivGoalX(-1),
39  ivGoalY(-1)
40 {}
41 
42 
44 {
45  if (ivpGrid)
46  resetGrid();
47 }
48 
49 
50 double
52  const PlanningState& to)
53 const
54 {
55  assert(ivGoalX >= 0 && ivGoalY >= 0);
56 
57  if (current == to)
58  return 0.0;
59 
60  unsigned int from_x;
61  unsigned int from_y;
62  // could be removed after more testing (then use ...noBounds... again)
63  ivMapPtr->worldToMapNoBounds(cell_2_state(current.getX(), ivCellSize),
64  cell_2_state(current.getY(), ivCellSize),
65  from_x, from_y);
66 
67  unsigned int to_x;
68  unsigned int to_y;
69  // could be removed after more testing (then use ...noBounds... again)
70  ivMapPtr->worldToMapNoBounds(cell_2_state(to.getX(), ivCellSize),
72  to_x, to_y);
73 
74  // cast to unsigned int is safe since ivGoalX/ivGoalY are checked to be >= 0
75  if ((unsigned int)ivGoalX != to_x || (unsigned int)ivGoalY != to_y)
76  {
77  ROS_ERROR("PathCostHeuristic::getHValue to a different value than "
78  "precomputed, heuristic values will be wrong. You need to call "
79  "calculateDistances() before!");
80  }
81  assert((unsigned int)ivGoalX == to_x && (unsigned int)ivGoalY == to_y);
82 
83  double dist = double(ivGridSearchPtr->getlowerboundoncostfromstart_inmm(
84  from_x, from_y)) / 1000.0;
85 
86  double expected_steps = dist / ivMaxStepWidth;
87  double diff_angle = 0.0;
88  if (ivDiffAngleCost > 0.0)
89  {
90  // get the number of bins between from.theta and to.theta
91  int diff_angle_disc = (
92  ((to.getTheta() - current.getTheta()) % ivNumAngleBins) +
94  // get the rotation independent from the rotation direction
95  diff_angle = std::abs(angles::normalize_angle(
96  angle_cell_2_state(diff_angle_disc, ivNumAngleBins)));
97  }
98 
99  return (dist + expected_steps * ivStepCost + diff_angle * ivDiffAngleCost);
100 }
101 
102 
103 bool
105  const PlanningState& to)
106 {
107  assert(ivMapPtr);
108 
109  unsigned int from_x;
110  unsigned int from_y;
111  ivMapPtr->worldToMapNoBounds(cell_2_state(from.getX(), ivCellSize),
112  cell_2_state(from.getY(), ivCellSize),
113  from_x, from_y);
114 
115  unsigned int to_x;
116  unsigned int to_y;
117  ivMapPtr->worldToMapNoBounds(cell_2_state(to.getX(), ivCellSize),
119  to_x, to_y);
120 
121  if ((int)to_x != ivGoalX || (int)to_y != ivGoalY)
122  {
123  ivGoalX = to_x;
124  ivGoalY = to_y;
126  ivGoalX, ivGoalY, from_x, from_y,
127  SBPL_2DGRIDSEARCH_TERM_CONDITION_ALLCELLS);
128  }
129 
130  return true;
131 }
132 
133 
134 void
136 {
137  if (ivpGrid) // reset map before change it's sizes (in other case we will get SEGMENT ERROR)
138  resetGrid();
139 
140  ivMapPtr.reset();
141  ivMapPtr = map;
142 
143  ivGoalX = ivGoalY = -1;
144 
145  unsigned width = ivMapPtr->getInfo().width;
146  unsigned height = ivMapPtr->getInfo().height;
147 
148  if (ivGridSearchPtr)
149  ivGridSearchPtr->destroy();
150  ivGridSearchPtr.reset(new SBPL2DGridSearch(width, height,
151  ivMapPtr->getResolution()));
152 
153  ivpGrid = new unsigned char* [width];
154 
155  for (unsigned x = 0; x < width; ++x)
156  ivpGrid[x] = new unsigned char [height];
157  for (unsigned y = 0; y < height; ++y)
158  {
159  for (unsigned x = 0; x < width; ++x)
160  {
161  float dist = ivMapPtr->distanceMapAtCell(x,y);
162  if (dist < 0.0f)
163  ROS_ERROR("Distance map at %d %d out of bounds", x, y);
164  else if (dist <= ivInflationRadius)
165  ivpGrid[x][y] = 255;
166  else
167  ivpGrid[x][y] = 0;
168  }
169  }
170 }
171 
172 
173 void
175 {
176  // CvSize size = ivMapPtr->size(); // here we get (height; width) instead of (width; height)
177  int width = ivMapPtr->getInfo().width;
178  for (int x = 0; x < width; ++x)
179  {
180  if (ivpGrid[x])
181  {
182  delete[] ivpGrid[x];
183  ivpGrid[x] = NULL;
184  }
185  }
186  delete[] ivpGrid;
187  ivpGrid = NULL;
188 }
189 } // end of namespace
double cell_2_state(int value, double cell_size)
Calculate the respective (continuous) state value for a cell. (Should be used to get a State from a d...
Definition: helper.h:122
PathCostHeuristic(double cell_size, int num_angle_bins, double step_cost, double diff_angle_cost, double max_step_width, double inflation_radius)
f
bool calculateDistances(const PlanningState &from, const PlanningState &to)
Calculates for each grid cell of the map a 2D path to the cell (to.x, to.y). For forward planning &#39;to...
void updateMap(gridmap_2d::GridMap2DPtr map)
An abstract super class providing methods necessary to be used as heuristic function within the Foots...
Definition: Heuristic.h:34
gridmap_2d::GridMap2DPtr ivMapPtr
double angle_cell_2_state(int angle, int angle_bin_num)
Calculate the respective (continuous) angle for a bin.
Definition: helper.h:101
virtual double getHValue(const PlanningState &current, const PlanningState &to) const
def normalize_angle(angle)
boost::shared_ptr< SBPL2DGridSearch > ivGridSearchPtr
A class representing the robot&#39;s pose (i.e. position and orientation) in the underlying SBPL...
Definition: PlanningState.h:45
#define ROS_ERROR(...)


footstep_planner
Author(s): Johannes Garimort, Armin Hornung
autogenerated on Mon Jun 10 2019 13:38:24