path_cost_heuristic.cpp
Go to the documentation of this file.
2 
3 #include <pluginlib/class_list_macros.h>
4 
5 
6 
8 {
10  : HeuristicPlugin("path_cost_heuristic")
11  , ivpGrid(NULL)
12  , ivGoalX(-1)
13  , ivGoalY(-1)
14 {
15 }
16 
18 {
19  if (ivpGrid)
20  resetGrid();
21 }
22 
23 bool PathCostHeuristic::loadParams(const vigir_generic_params::ParameterSet& params)
24 {
25  if (!HeuristicPlugin::loadParams(params))
26  return false;
27 
28  params.getParam("collision_check/cell_size", ivCellSize);
29  params.getParam("collision_check/num_angle_bins", ivNumAngleBins);
30  ivAngleBinSize = 2.0*M_PI / static_cast<double>(ivNumAngleBins);
31 
32  params.getParam("const_step_cost_estimator/step_cost", ivStepCost, 0.1);
33  params.getParam("diff_angle_cost", ivDiffAngleCost);
34  params.getParam("max_step_dist/x", ivMaxStepWidth);
37  return true;
38 }
39 
40 double PathCostHeuristic::getHeuristicValue(const State& from, const State& to, const State& /*start*/, const State& /*goal*/) const
41 {
42  if (from == to)
43  return 0.0;
44 
45  assert(ivGoalX >= 0 && ivGoalY >= 0);
46 
47  if (from == to)
48  return 0.0;
49 
50  unsigned int from_x;
51  unsigned int from_y;
52  // could be removed after more testing (then use ...noBounds... again)
53  ivMapPtr->worldToMapNoBounds(cell_2_state(from.getX(), ivCellSize),
54  cell_2_state(from.getY(), ivCellSize),
55  from_x, from_y);
56 
57  unsigned int to_x;
58  unsigned int to_y;
59  // could be removed after more testing (then use ...noBounds... again)
60  ivMapPtr->worldToMapNoBounds(cell_2_state(to.getX(), ivCellSize),
61  cell_2_state(to.getY(), ivCellSize),
62  to_x, to_y);
63 
64  // cast to unsigned int is safe since ivGoalX/ivGoalY are checked to be >= 0
65  if ((unsigned int)ivGoalX != to_x || (unsigned int)ivGoalY != to_y)
66  {
67  ROS_ERROR("PathCostHeuristic::getHValue to a different value than "
68  "precomputed, heuristic values will be wrong. You need to call "
69  "calculateDistances() before!");
70  }
71  assert((unsigned int)ivGoalX == to_x && (unsigned int)ivGoalY == to_y);
72 
73  double dist = double(ivGridSearchPtr->getlowerboundoncostfromstart_inmm(from_x, from_y)) / 1000.0;
74 
75  double expected_steps = dist / ivMaxStepWidth;
76  double diff_angle = 0.0;
77  if (ivDiffAngleCost > 0.0)
78  diff_angle = std::abs(angles::shortest_angular_distance(to.getYaw(), from.getYaw()));
79 
80  return dist + expected_steps * ivStepCost + diff_angle * ivDiffAngleCost;
81 }
82 
83 bool PathCostHeuristic::calculateDistances(const State& from, const State& to)
84 {
85  assert(ivMapPtr);
86 
87  unsigned int from_x;
88  unsigned int from_y;
89  ivMapPtr->worldToMapNoBounds(cell_2_state(from.getX(), ivCellSize),
90  cell_2_state(from.getY(), ivCellSize),
91  from_x, from_y);
92 
93  unsigned int to_x;
94  unsigned int to_y;
95  ivMapPtr->worldToMapNoBounds(cell_2_state(to.getX(), ivCellSize),
96  cell_2_state(to.getY(), ivCellSize),
97  to_x, to_y);
98 
99  if ((int)to_x != ivGoalX || (int)to_y != ivGoalY)
100  {
101  ivGoalX = to_x;
102  ivGoalY = to_y;
104  ivGoalX, ivGoalY, from_x, from_y,
105  SBPL_2DGRIDSEARCH_TERM_CONDITION_ALLCELLS);
106  }
107 
108  return true;
109 }
110 
112 {
113  ivMapPtr.reset();
114  ivMapPtr = map;
115 
116  ivGoalX = ivGoalY = -1;
117 
118  unsigned width = ivMapPtr->getInfo().width;
119  unsigned height = ivMapPtr->getInfo().height;
120 
121  if (ivGridSearchPtr)
122  ivGridSearchPtr->destroy();
123  ivGridSearchPtr.reset(new SBPL2DGridSearch(width, height,
124  ivMapPtr->getResolution()));
125  if (ivpGrid)
126  resetGrid();
127  ivpGrid = new unsigned char* [width];
128 
129  for (unsigned x = 0; x < width; ++x)
130  ivpGrid[x] = new unsigned char [height];
131  for (unsigned y = 0; y < height; ++y)
132  {
133  for (unsigned x = 0; x < width; ++x)
134  {
135  float dist = ivMapPtr->distanceMapAtCell(x,y);
136  if (dist < 0.0f)
137  ROS_ERROR("Distance map at %d %d out of bounds", x, y);
138  else if (dist <= ivInflationRadius)
139  ivpGrid[x][y] = 255;
140  else
141  ivpGrid[x][y] = 0;
142  }
143  }
144 }
145 
147 {
148  CvSize size = ivMapPtr->size();
149  for (int x = 0; x < size.width; ++x)
150  {
151  if (ivpGrid[x])
152  {
153  delete[] ivpGrid[x];
154  ivpGrid[x] = NULL;
155  }
156  }
157  delete[] ivpGrid;
158  ivpGrid = NULL;
159 }
160 }
161 
162 PLUGINLIB_EXPORT_CLASS(vigir_footstep_planning::PathCostHeuristic, vigir_footstep_planning::HeuristicPlugin)
f
bool loadParams(const vigir_generic_params::ParameterSet &params=vigir_generic_params::ParameterSet()) override
double getHeuristicValue(const State &from, const State &to, const State &start, const State &goal) const override
bool calculateDistances(const State &from, const State &to)
Calculates for each grid cell of the map a 2D path to the cell (to.x, to.y). For forward planning &#39;to...
Determining the heuristic value by calculating a 2D path from each grid cell of the map to the goal a...
void updateMap(vigir_gridmap_2d::GridMap2DPtr map)
boost::shared_ptr< SBPL2DGridSearch > ivGridSearchPtr
#define ROS_ERROR(...)


vigir_footstep_planning_default_plugins
Author(s): Alexander Stumpf
autogenerated on Sun Nov 17 2019 03:30:01