costmap_layer.cpp
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <fstream>
5 
8 
10 
11 namespace nav_layer_from_points {
12 
13 void NavLayerFromPoints::onInitialize() {
14  current_ = true;
15  first_time_ = true;
16 
17  ros::NodeHandle nh("~/" + name_), g_nh;
18  sub_points_ = nh.subscribe("/downstairs_detector/points", 1,
19  &NavLayerFromPoints::pointsCallback, this);
20 
21  rec_server_ = new dynamic_reconfigure::Server<NavLayerFromPointsConfig>(nh);
22  f_ = boost::bind(&NavLayerFromPoints::configure, this, _1, _2);
23  rec_server_->setCallback(f_);
24 }
25 
26 void NavLayerFromPoints::configure(NavLayerFromPointsConfig &config, [[maybe_unused]] uint32_t level) {
27  points_keep_time_ = ros::Duration(config.keep_time);
28  enabled_ = config.enabled;
29 
30  point_radius_ = config.point_radius;
31  robot_radius_ = config.robot_radius;
32 }
33 
34 void NavLayerFromPoints::pointsCallback(const depth_nav_msgs::Point32List& points) {
35  boost::recursive_mutex::scoped_lock lock(lock_);
36  points_list_ = points;
37 }
38 
39 void NavLayerFromPoints::clearTransformedPoints() {
40  std::list<geometry_msgs::PointStamped>::iterator p_it;
41  p_it = transformed_points_.begin();
42 
43  if (transformed_points_.size() > 10000)
44  transformed_points_.clear();
45 
46  while (p_it != transformed_points_.end()) {
47  if (ros::Time::now() - (*p_it).header.stamp > points_keep_time_) {
48  p_it = transformed_points_.erase(p_it);
49  }
50  else {
51  ++p_it;
52  }
53  }
54 }
55 
56 void NavLayerFromPoints::updateBounds([[maybe_unused]] double origin_x,
57  [[maybe_unused]] double origin_y,
58  [[maybe_unused]] double origin_z,
59  double* min_x, double* min_y, double* max_x, double* max_y) {
60  boost::recursive_mutex::scoped_lock lock(lock_);
61 
62  std::string global_frame = layered_costmap_->getGlobalFrameID();
63 
64  // Check if there are points to remove in transformed_points list and if so, then remove it
65  if (!transformed_points_.empty())
66  clearTransformedPoints();
67 
68  // Add points to PointStamped list transformed_points_
69  for (const auto point : points_list_.points) {
70  geometry_msgs::PointStamped tpt;
71  geometry_msgs::PointStamped pt, out_pt;
72 
73  tpt.point = costmap_2d::toPoint(point);
74 
75  try {
76  pt.point.x = tpt.point.x;
77  pt.point.y = 0;
78  pt.point.z = tpt.point.z;
79  pt.header.frame_id = points_list_.header.frame_id;
80 
81  tf_.transformPoint(global_frame, pt, out_pt);
82  tpt.point.x = out_pt.point.x;
83  tpt.point.y = out_pt.point.y;
84  tpt.point.z = out_pt.point.z;
85 
86  tpt.header.stamp = pt.header.stamp;
87  //s << " ( " << tpt.point.x << " , " << tpt.point.y << " , " << tpt.point.z << " ) ";
88  transformed_points_.push_back(tpt);
89  }
90  catch(tf::LookupException& ex) {
91  ROS_ERROR("No Transform available Error: %s\n", ex.what());
92  continue;
93  }
94  catch(tf::ConnectivityException& ex) {
95  ROS_ERROR("Connectivity Error: %s\n", ex.what());
96  continue;
97  }
98  catch(tf::ExtrapolationException& ex) {
99  ROS_ERROR("Extrapolation Error: %s\n", ex.what());
100  continue;
101  }
102  }
103 
104  updateBoundsFromPoints(min_x, min_y, max_x, max_y);
105 
106  if (first_time_) {
107  last_min_x_ = *min_x;
108  last_min_y_ = *min_y;
109  last_max_x_ = *max_x;
110  last_max_y_ = *max_y;
111  first_time_ = false;
112  }
113  else {
114  double a = *min_x, b = *min_y, c = *max_x, d = *max_y;
115  *min_x = std::min(last_min_x_, *min_x);
116  *min_y = std::min(last_min_y_, *min_y);
117  *max_x = std::max(last_max_x_, *max_x);
118  *max_y = std::max(last_max_y_, *max_y);
119  last_min_x_ = a;
120  last_min_y_ = b;
121  last_max_x_ = c;
122  last_max_y_ = d;
123  }
124  std::ostringstream s;
125  s << " list_size = " << transformed_points_.size() << " ";
126  s << " min_x = " << *min_x << " max_x = " << *max_x <<
127  " min_y = " << *min_y << " max_y = " << *max_y << " ";
128  ROS_INFO_STREAM_THROTTLE(2,"transformed_points = " << s.str());
129 
130 }
131 
132 void NavLayerFromPoints::updateBoundsFromPoints(double* min_x, double* min_y, double* max_x, double* max_y) {
133  std::list<geometry_msgs::PointStamped>::iterator p_it;
134 
135  double radius = point_radius_ + robot_radius_;
136 
137  for (p_it = transformed_points_.begin(); p_it != transformed_points_.end(); ++p_it) {
138  geometry_msgs::PointStamped pt = *p_it;
139 
140  *min_x = std::min(*min_x, pt.point.x - radius);
141  *min_y = std::min(*min_y, pt.point.y - radius);
142  *max_x = std::max(*max_x, pt.point.x + radius);
143  *max_y = std::max(*max_y, pt.point.y + radius);
144  }
145 }
146 
147 void NavLayerFromPoints::updateCosts([[maybe_unused]] costmap_2d::Costmap2D& master_grid,
148  int min_i, int min_j, int max_i, int max_j) {
149  boost::recursive_mutex::scoped_lock lock(lock_);
150 
151  if (!enabled_)
152  return;
153 
154  if (points_list_.points.empty())
155  return;
156 
157  std::list<geometry_msgs::PointStamped>::iterator p_it;
158 
159  costmap_2d::Costmap2D* costmap = layered_costmap_->getCostmap();
160  double res = costmap->getResolution();
161 
162  for (p_it = transformed_points_.begin(); p_it != transformed_points_.end(); ++p_it) {
163  geometry_msgs::Point pt = (*p_it).point;
164 
165  unsigned int size = std::max(1, int( (point_radius_ + robot_radius_) / res ));
166  unsigned int map_x, map_y;
167  unsigned int size_x = size, size_y = size;
168  unsigned int start_x, start_y, end_x, end_y;
169 
170  // Check if point is on map
171  // Convert from world coordinates to map coordinates with checking for legal bounds
172  if (costmap->worldToMap(pt.x, pt.y, map_x, map_y)) {
173  start_x = map_x - size_x / 2;
174  start_y = map_y - size_y / 2;
175  end_x = map_x + size_x / 2;
176  end_y = map_y + size_y / 2;
177 
178  if (start_x < min_i)
179  start_x = min_i;
180  if (end_x > max_i)
181  end_x = max_i;
182  if (start_y < min_j)
183  start_y = min_j;
184  if (end_y > max_j)
185  end_y = max_j;
186 
187  for(int j = start_y; j < end_y; j++) {
188  for(int i = start_x; i < end_x; i++) {
189  unsigned char old_cost = costmap->getCost(i, j);
190 
191  if(old_cost == costmap_2d::NO_INFORMATION)
192  continue;
193 
194  costmap->setCost(i, j, costmap_2d::LETHAL_OBSTACLE);
195  }
196  }
197  }
198  }
199 }
200 
201 } // namespace nav_layer_from_points
d
void setCost(unsigned int mx, unsigned int my, unsigned char cost)
XmlRpcServer s
PLUGINLIB_EXPORT_CLASS(BAGReader, nodelet::Nodelet)
void transformPoint(const std::string &target_frame, const geometry_msgs::PointStamped &stamped_in, geometry_msgs::PointStamped &stamped_out) const
tf::TransformListener * tf_
unsigned char getCost(unsigned int mx, unsigned int my) const
geometry_msgs::Point toPoint(geometry_msgs::Point32 pt)
static const unsigned char LETHAL_OBSTACLE
static const unsigned char NO_INFORMATION
static Time now()
double getResolution() const
#define ROS_INFO_STREAM_THROTTLE(rate, args)
#define ROS_ERROR(...)
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const


nav_layer_from_points
Author(s): Michal Drwiega (http://www.mdrwiega.com)
autogenerated on Wed May 5 2021 02:56:19