ray_trace_2d.cpp
Go to the documentation of this file.
1 
6 #include <filters.hpp>
7 
8 // Gridmap
10 
11 // Ros
13 
14 namespace mitre_fast_layered_map
15 {
16 
18 {
19 }
20 
22 {
23 }
24 
26 {
27  if(!RayTrace2d::getParam(std::string("nonground_layer"), nongroundLayer_))
28  {
29  ROS_ERROR("Unable to find obstacle layer parameter.");
30  return false;
31  }
32 
33  if(!RayTrace2d::getParam(std::string("ground_layer"), groundLayer_))
34  {
35  ROS_ERROR("Unable to find ground layer parameter.");
36  return false;
37  }
38 
39  if(!RayTrace2d::getParam(std::string("mode"), mode_))
40  {
41  mode_ = "prod"; // Default is production mode
42  }
43 
44  ROS_INFO("Configured RayTrace2d with nonground layer: %s, and ground layer: %s", nongroundLayer_.c_str(), groundLayer_.c_str());
45 
46  return true;
47 }
48 
50 {
51  if(!_mapIn.exists(nongroundLayer_))
52  {
53  ROS_ERROR("Layer %s does not exist within the map.", nongroundLayer_.c_str());
54  return false;
55  }
56  else if(!_mapIn.exists(groundLayer_))
57  {
58  ROS_ERROR("Layer %s does not exist within the map.", groundLayer_.c_str());
59  return false;
60  }
61 
62  _mapOut = _mapIn;
63 
64  /*
65  The trace function below takes a start and an end point for its ray calculations.
66  To ensure that we check every cell with rays emitted from the vehicles position,
67  we draw the lines from the vehicles position to each of the outermost cells.
68 
69  Example: For the grid below with the vehicle positioned at x, we want to draw
70  lines from x to i so that we can interpolate which cells are clear or unknown
71  due to a barrier blocking the way.
72 
73  [i], [i], [i], [i], [i],
74  [i], [], [], [], [i],
75  [i], [], [x], [], [i],
76  [i], [], [], [], [i],
77  [i], [i], [i], [i], [i]
78 
79  GridMap does not give any nice way (that we know of) for calculating the
80  boundary cells of the map. We cannot use constant indices due to their implementation
81  of the grid as a circular buffer. The calculations below get each of the cells
82  and then runs the tracing algorithm.
83 
84  ASSUMPTION: The grid is square
85 
86  To get the outermost rows and columns, we start with the gridMap.getStartIndex() function
87  which gives us the index of the top left cell in the grid. this immediately gives us the top
88  row and the left column.
89 
90  From there, we can calculate the bottom row and right column by adding the number of rows - 1
91  (to offset for the current row) modulus the number of rows to account for the wrapping. If this seems
92  strange, try drawing a 5x5 grid and manually doing the calculations.
93  */
94 
95  grid_map::Position vehPosition = _mapOut.getPosition();
96 
97  grid_map::Index vehIndex;
98  _mapOut.getIndex(vehPosition, vehIndex); // Lines always start at the vehicle
99 
100  int numRows = _mapOut.getSize()(0);
101 
102  grid_map::Index startIdx = _mapOut.getStartIndex(); // Index of cell at top left corner
103  int topRow = startIdx(0);
104  int bottomRow = (startIdx(0) + (numRows - 1)) % numRows;
105  int leftCol = startIdx(1);
106  int rightCol = (startIdx(1) + (numRows - 1)) % numRows;
107 
108  for(int i = 0; i < numRows; i++)
109  {
110  trace(_mapOut, vehIndex, grid_map::Index(topRow, i)); // Ray traces going towards top row
111  trace(_mapOut, vehIndex, grid_map::Index(bottomRow, i)); // Ray traces going towards bottom row
112  trace(_mapOut, vehIndex, grid_map::Index(i, leftCol)); // Ray traces going towards left column
113  trace(_mapOut, vehIndex, grid_map::Index(i, rightCol)); // Ray traces going towards right column
114  }
115 
116  return true;
117 
118 }
119 
121 {
122  grid_map::Matrix& ground = _map[groundLayer_];
123  grid_map::Matrix& nonground = _map[nongroundLayer_];
124  bool obstacleFound = false;
125 
126  for (grid_map::LineIterator lit(_map, start, end); !lit.isPastEnd(); ++lit)
127  {
128  const grid_map::Index lineIndex(*lit);
129 
130  // If we find an obstacle, assume it blocks sight along the line past here
131  if (nonground(lineIndex(0), lineIndex(1)) == 100)
132  {
133  obstacleFound = true;
134  }
135  else if (obstacleFound && ground(lineIndex(0), lineIndex(1)) > 0)
136  {
137  // We had found an obstacle, but are also registering ground points beyond it
138  // We might be seeing over, around, or sometimes through the obstacle
139  obstacleFound = false;
140  nonground(lineIndex(0), lineIndex(1)) = 0;
141  // ground(lineIndex(0), lineIndex(1)) = 0;
142  }
143  else if (obstacleFound) // If obstacle found but no ground points registered, mark as unknown
144  {
145  // ground(lineIndex(0), lineIndex(1)) = 20;
146  nonground(lineIndex(0), lineIndex(1)) = 20;
147  }
148  else // If no obstacle found yet, mark as clear
149  {
150  nonground(lineIndex(0), lineIndex(1)) = 0;
151  // ground(lineIndex(0), lineIndex(1)) = 0;
152  }
153  }
154 
155  return true;
156 }
157 
158 
159 } // namespace mitre_fast_layered_map
160 
Eigen::Array2i Index
PLUGINLIB_EXPORT_CLASS(mitre_fast_layered_map::RayTrace2d, filters::FilterBase< grid_map::GridMap >)
const Index & getStartIndex() const
bool getPosition(const Index &index, Position &position) const
std::string groundLayer_
Layer for setting ground plane points.
Definition: filters.hpp:102
Eigen::MatrixXf Matrix
bool getParam(const std::string &name, std::string &value)
bool exists(const std::string &layer) const
Eigen::Vector2d Position
#define ROS_INFO(...)
bool trace(grid_map::GridMap &_map, const grid_map::Index &start, const grid_map::Index &end)
std::string mode_
Production or testing. If testing mode will throw errors if calculations break bounds.
Definition: filters.hpp:103
Filters that operate on a grid map instance.
virtual bool update(const grid_map::GridMap &_mapIn, grid_map::GridMap &_mapOut)
bool getIndex(const Position &position, Index &index) const
#define ROS_ERROR(...)
std::string nongroundLayer_
blocking rays
Definition: filters.hpp:100
const Size & getSize() const
bool isPastEnd() const


mitre_fast_layered_map
Author(s):
autogenerated on Thu Mar 11 2021 03:06:49