Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
obstacle_layer.cpp
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  * David V. Lu!!
37  * Steve Macenski
38  *********************************************************************/
39 #include "nav2_costmap_2d/obstacle_layer.hpp"
40 
41 #include <algorithm>
42 #include <memory>
43 #include <string>
44 #include <vector>
45 
46 #include "pluginlib/class_list_macros.hpp"
47 #include "sensor_msgs/point_cloud2_iterator.hpp"
48 #include "nav2_util/raytrace_line_2d.hpp"
49 #include "nav2_costmap_2d/costmap_math.hpp"
50 #include "nav2_ros_common/node_utils.hpp"
51 #include "rclcpp/version.h"
52 
54 
55 using nav2_costmap_2d::NO_INFORMATION;
56 using nav2_costmap_2d::LETHAL_OBSTACLE;
57 using nav2_costmap_2d::FREE_SPACE;
58 
61 using rcl_interfaces::msg::ParameterType;
62 
63 namespace nav2_costmap_2d
64 {
65 
67 {
68  auto node = node_.lock();
69  if (dyn_params_handler_ && node) {
70  node->remove_on_set_parameters_callback(dyn_params_handler_.get());
71  }
72  dyn_params_handler_.reset();
73  for (auto & notifier : observation_notifiers_) {
74  notifier.reset();
75  }
76 }
77 
79 {
80  bool track_unknown_space;
81  double transform_tolerance;
82 
83  // The topics that we'll subscribe to from the parameter server
84  std::string topics_string;
85 
86  declareParameter("enabled", rclcpp::ParameterValue(true));
87  declareParameter("footprint_clearing_enabled", rclcpp::ParameterValue(true));
88  declareParameter("min_obstacle_height", rclcpp::ParameterValue(0.0));
89  declareParameter("max_obstacle_height", rclcpp::ParameterValue(2.0));
90  declareParameter("combination_method", rclcpp::ParameterValue(1));
91  declareParameter("observation_sources", rclcpp::ParameterValue(std::string("")));
92 
93  auto node = node_.lock();
94  if (!node) {
95  throw std::runtime_error{"Failed to lock node"};
96  }
97 
98  node->get_parameter(name_ + "." + "enabled", enabled_);
99  node->get_parameter(name_ + "." + "footprint_clearing_enabled", footprint_clearing_enabled_);
100  node->get_parameter(name_ + "." + "min_obstacle_height", min_obstacle_height_);
101  node->get_parameter(name_ + "." + "max_obstacle_height", max_obstacle_height_);
102  node->get_parameter("track_unknown_space", track_unknown_space);
103  node->get_parameter("transform_tolerance", transform_tolerance);
104  node->get_parameter(name_ + "." + "observation_sources", topics_string);
105  double tf_filter_tolerance = nav2::declare_or_get_parameter(node, name_ + "." +
106  "tf_filter_tolerance", 0.05);
107 
108  int combination_method_param{};
109  node->get_parameter(name_ + "." + "combination_method", combination_method_param);
110  combination_method_ = combination_method_from_int(combination_method_param);
111 
112  dyn_params_handler_ = node->add_on_set_parameters_callback(
113  std::bind(
115  this,
116  std::placeholders::_1));
117 
118  RCLCPP_INFO(
119  logger_,
120  "Subscribed to Topics: %s", topics_string.c_str());
121 
122  rolling_window_ = layered_costmap_->isRolling();
123 
124  if (track_unknown_space) {
125  default_value_ = NO_INFORMATION;
126  } else {
127  default_value_ = FREE_SPACE;
128  }
129 
131  current_ = true;
132  was_reset_ = false;
133 
134  global_frame_ = layered_costmap_->getGlobalFrameID();
135 
136  auto sub_opt = rclcpp::SubscriptionOptions();
137  sub_opt.callback_group = callback_group_;
138 
139  // now we need to split the topics based on whitespace which we can use a stringstream for
140  std::stringstream ss(topics_string);
141 
142  std::string source;
143  while (ss >> source) {
144  // get the parameters for the specific topic
145  double observation_keep_time, expected_update_rate, min_obstacle_height, max_obstacle_height;
146  std::string topic, sensor_frame, data_type, transport_type;
147  bool inf_is_valid, clearing, marking;
148 
149  declareParameter(source + "." + "topic", rclcpp::ParameterValue(source));
150  declareParameter(source + "." + "sensor_frame", rclcpp::ParameterValue(std::string("")));
151  declareParameter(source + "." + "observation_persistence", rclcpp::ParameterValue(0.0));
152  declareParameter(source + "." + "expected_update_rate", rclcpp::ParameterValue(0.0));
153  declareParameter(source + "." + "data_type", rclcpp::ParameterValue(std::string("LaserScan")));
154  declareParameter(source + "." + "min_obstacle_height", rclcpp::ParameterValue(0.0));
155  declareParameter(source + "." + "max_obstacle_height", rclcpp::ParameterValue(0.0));
156  declareParameter(source + "." + "inf_is_valid", rclcpp::ParameterValue(false));
157  declareParameter(source + "." + "marking", rclcpp::ParameterValue(true));
158  declareParameter(source + "." + "clearing", rclcpp::ParameterValue(false));
159  declareParameter(source + "." + "obstacle_max_range", rclcpp::ParameterValue(2.5));
160  declareParameter(source + "." + "obstacle_min_range", rclcpp::ParameterValue(0.0));
161  declareParameter(source + "." + "raytrace_max_range", rclcpp::ParameterValue(3.0));
162  declareParameter(source + "." + "raytrace_min_range", rclcpp::ParameterValue(0.0));
163  declareParameter(source + "." + "transport_type", rclcpp::ParameterValue(std::string("raw")));
164 
165  node->get_parameter(name_ + "." + source + "." + "topic", topic);
166  node->get_parameter(name_ + "." + source + "." + "sensor_frame", sensor_frame);
167  node->get_parameter(
168  name_ + "." + source + "." + "observation_persistence",
169  observation_keep_time);
170  node->get_parameter(
171  name_ + "." + source + "." + "expected_update_rate",
172  expected_update_rate);
173  node->get_parameter(name_ + "." + source + "." + "data_type", data_type);
174  node->get_parameter(name_ + "." + source + "." + "min_obstacle_height", min_obstacle_height);
175  node->get_parameter(name_ + "." + source + "." + "max_obstacle_height", max_obstacle_height);
176  node->get_parameter(name_ + "." + source + "." + "inf_is_valid", inf_is_valid);
177  node->get_parameter(name_ + "." + source + "." + "marking", marking);
178  node->get_parameter(name_ + "." + source + "." + "clearing", clearing);
179  node->get_parameter(name_ + "." + source + "." + "transport_type", transport_type);
180 
181  if (!(data_type == "PointCloud2" || data_type == "LaserScan")) {
182  RCLCPP_FATAL(
183  logger_,
184  "Only topics that use point cloud2s or laser scans are currently supported");
185  throw std::runtime_error(
186  "Only topics that use point cloud2s or laser scans are currently supported");
187  }
188 
189  // get the obstacle range for the sensor
190  double obstacle_max_range, obstacle_min_range;
191  node->get_parameter(name_ + "." + source + "." + "obstacle_max_range", obstacle_max_range);
192  node->get_parameter(name_ + "." + source + "." + "obstacle_min_range", obstacle_min_range);
193 
194  // get the raytrace ranges for the sensor
195  double raytrace_max_range, raytrace_min_range;
196  node->get_parameter(name_ + "." + source + "." + "raytrace_min_range", raytrace_min_range);
197  node->get_parameter(name_ + "." + source + "." + "raytrace_max_range", raytrace_max_range);
198 
199  topic = joinWithParentNamespace(topic);
200 
201  RCLCPP_DEBUG(
202  logger_,
203  "Creating an observation buffer for source %s, topic %s, frame %s",
204  source.c_str(), topic.c_str(),
205  sensor_frame.c_str());
206 
207  // create an observation buffer
208  observation_buffers_.push_back(
209  std::shared_ptr<ObservationBuffer
210  >(
211  new ObservationBuffer(
212  node, topic, observation_keep_time, expected_update_rate,
213  min_obstacle_height,
214  max_obstacle_height, obstacle_max_range, obstacle_min_range, raytrace_max_range,
215  raytrace_min_range, *tf_,
217  sensor_frame, tf2::durationFromSec(transform_tolerance))));
218 
219  // check if we'll add this buffer to our marking observation buffers
220  if (marking) {
221  marking_buffers_.push_back(observation_buffers_.back());
222  }
223 
224  // check if we'll also add this buffer to our clearing observation buffers
225  if (clearing) {
226  clearing_buffers_.push_back(observation_buffers_.back());
227  }
228 
229  RCLCPP_DEBUG(
230  logger_,
231  "Created an observation buffer for source %s, topic %s, global frame: %s, "
232  "expected update rate: %.2f, observation persistence: %.2f",
233  source.c_str(), topic.c_str(),
234  global_frame_.c_str(), expected_update_rate, observation_keep_time);
235 
236  const auto custom_qos_profile = nav2::qos::SensorDataQoS(50);
237 
238  // create a callback for the topic
239  if (data_type == "LaserScan") {
240  // For Kilted and Older Support from Message Filters API change
241  #if RCLCPP_VERSION_GTE(29, 6, 0)
242  std::shared_ptr<message_filters::Subscriber<sensor_msgs::msg::LaserScan>> sub;
243  #else
244  std::shared_ptr<message_filters::Subscriber<sensor_msgs::msg::LaserScan,
245  rclcpp_lifecycle::LifecycleNode>> sub;
246  #endif
247 
248  // For Kilted compatibility in Message Filters API change
249  #if RCLCPP_VERSION_GTE(29, 6, 0)
250  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::LaserScan>>(
251  node, topic, custom_qos_profile, sub_opt);
252  // For Jazzy compatibility in Message Filters API change
253  #elif RCLCPP_VERSION_GTE(29, 0, 0)
254  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::LaserScan,
255  rclcpp_lifecycle::LifecycleNode>>(
256  std::static_pointer_cast<rclcpp_lifecycle::LifecycleNode>(node),
257  topic, custom_qos_profile, sub_opt);
258  // For Humble and Older compatibility in Message Filters API change
259  #else
260  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::LaserScan,
261  rclcpp_lifecycle::LifecycleNode>>(
262  std::static_pointer_cast<rclcpp_lifecycle::LifecycleNode>(node),
263  topic, custom_qos_profile.get_rmw_qos_profile(), sub_opt);
264  #endif
265 
266  sub->unsubscribe();
267 
268  auto filter = std::make_shared<tf2_ros::MessageFilter<sensor_msgs::msg::LaserScan>>(
269  *sub, *tf_, global_frame_, 50,
270  node->get_node_logging_interface(),
271  node->get_node_clock_interface(),
272  tf2::durationFromSec(transform_tolerance));
273 
274  if (inf_is_valid) {
275  filter->registerCallback(
276  std::bind(
277  &ObstacleLayer::laserScanValidInfCallback, this, std::placeholders::_1,
278  observation_buffers_.back()));
279 
280  } else {
281  filter->registerCallback(
282  std::bind(
283  &ObstacleLayer::laserScanCallback, this, std::placeholders::_1,
284  observation_buffers_.back()));
285  }
286 
287  observation_subscribers_.push_back(sub);
288 
289  observation_notifiers_.push_back(filter);
290  observation_notifiers_.back()->setTolerance(rclcpp::Duration::from_seconds(
291  tf_filter_tolerance));
292 
293  } else {
294  // For Rolling and Newer Support from PointCloudTransport API change
295  #if RCLCPP_VERSION_GTE(30, 0, 0)
296  std::shared_ptr<point_cloud_transport::SubscriberFilter> sub;
297  // For Kilted and Older Support from Message Filters API change
298  #elif RCLCPP_VERSION_GTE(29, 6, 0)
299  std::shared_ptr<message_filters::Subscriber<sensor_msgs::msg::PointCloud2>> sub;
300  #else
301  std::shared_ptr<message_filters::Subscriber<sensor_msgs::msg::PointCloud2,
302  rclcpp_lifecycle::LifecycleNode>> sub;
303  #endif
304 
305  // For Rolling compatibility in PointCloudTransport API change
306  #if RCLCPP_VERSION_GTE(30, 0, 0)
307  sub = std::make_shared<point_cloud_transport::SubscriberFilter>(
308  *node, topic, transport_type, custom_qos_profile, sub_opt);
309  // For Kilted compatibility in Message Filters API change
310  #elif RCLCPP_VERSION_GTE(29, 6, 0)
311  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::PointCloud2>>(
312  node, topic, custom_qos_profile, sub_opt);
313  // For Jazzy compatibility in Message Filters API change
314  #elif RCLCPP_VERSION_GTE(29, 0, 0)
315  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::PointCloud2,
316  rclcpp_lifecycle::LifecycleNode>>(
317  std::static_pointer_cast<rclcpp_lifecycle::LifecycleNode>(node),
318  topic, custom_qos_profile, sub_opt);
319  // For Humble and Older compatibility in Message Filters API change
320  #else
321  sub = std::make_shared<message_filters::Subscriber<sensor_msgs::msg::PointCloud2,
322  rclcpp_lifecycle::LifecycleNode>>(
323  std::static_pointer_cast<rclcpp_lifecycle::LifecycleNode>(node),
324  topic, custom_qos_profile.get_rmw_qos_profile(), sub_opt);
325  #endif
326 
327  sub->unsubscribe();
328 
329  if (inf_is_valid) {
330  RCLCPP_WARN(
331  logger_,
332  "obstacle_layer: inf_is_valid option is not applicable to PointCloud observations.");
333  }
334 
335  auto filter = std::make_shared<tf2_ros::MessageFilter<sensor_msgs::msg::PointCloud2>>(
336  *sub, *tf_, global_frame_, 50,
337  node->get_node_logging_interface(),
338  node->get_node_clock_interface(),
339  tf2::durationFromSec(transform_tolerance));
340 
341  filter->registerCallback(
342  std::bind(
343  &ObstacleLayer::pointCloud2Callback, this, std::placeholders::_1,
344  observation_buffers_.back()));
345 
346  observation_subscribers_.push_back(sub);
347  observation_notifiers_.push_back(filter);
348  }
349 
350  if (sensor_frame != "") {
351  std::vector<std::string> target_frames;
352  target_frames.push_back(global_frame_);
353  target_frames.push_back(sensor_frame);
354  observation_notifiers_.back()->setTargetFrames(target_frames);
355  }
356  }
357 }
358 
359 rcl_interfaces::msg::SetParametersResult
361  std::vector<rclcpp::Parameter> parameters)
362 {
363  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
364  rcl_interfaces::msg::SetParametersResult result;
365 
366  for (auto parameter : parameters) {
367  const auto & param_type = parameter.get_type();
368  const auto & param_name = parameter.get_name();
369  if (param_name.find(name_ + ".") != 0) {
370  continue;
371  }
372 
373  if (param_type == ParameterType::PARAMETER_DOUBLE) {
374  if (param_name == name_ + "." + "min_obstacle_height") {
375  min_obstacle_height_ = parameter.as_double();
376  } else if (param_name == name_ + "." + "max_obstacle_height") {
377  max_obstacle_height_ = parameter.as_double();
378  }
379  } else if (param_type == ParameterType::PARAMETER_BOOL) {
380  if (param_name == name_ + "." + "enabled" && enabled_ != parameter.as_bool()) {
381  enabled_ = parameter.as_bool();
382  if (enabled_) {
383  current_ = false;
384  }
385  } else if (param_name == name_ + "." + "footprint_clearing_enabled") {
386  footprint_clearing_enabled_ = parameter.as_bool();
387  }
388  } else if (param_type == ParameterType::PARAMETER_INTEGER) {
389  if (param_name == name_ + "." + "combination_method") {
390  combination_method_ = combination_method_from_int(parameter.as_int());
391  }
392  }
393  }
394 
395  result.successful = true;
396  return result;
397 }
398 
399 void
401  sensor_msgs::msg::LaserScan::ConstSharedPtr message,
402  const std::shared_ptr<nav2_costmap_2d::ObservationBuffer> & buffer)
403 {
404  // project the laser into a point cloud
405  sensor_msgs::msg::PointCloud2 cloud;
406  cloud.header = message->header;
407 
408  // project the scan into a point cloud
409  try {
410  projector_.transformLaserScanToPointCloud(message->header.frame_id, *message, cloud, *tf_);
411  } catch (tf2::TransformException & ex) {
412  RCLCPP_WARN(
413  logger_,
414  "High fidelity enabled, but TF returned a transform exception to frame %s: %s",
415  global_frame_.c_str(),
416  ex.what());
417  projector_.projectLaser(*message, cloud);
418  } catch (std::runtime_error & ex) {
419  RCLCPP_WARN(
420  logger_,
421  "transformLaserScanToPointCloud error, it seems the message from laser is malformed."
422  " Ignore this message. what(): %s",
423  ex.what());
424  return;
425  }
426 
427  // buffer the point cloud
428  buffer->lock();
429  buffer->bufferCloud(cloud);
430  buffer->unlock();
431 }
432 
433 void
435  sensor_msgs::msg::LaserScan::ConstSharedPtr raw_message,
436  const std::shared_ptr<nav2_costmap_2d::ObservationBuffer> & buffer)
437 {
438  // Filter positive infinities ("Inf"s) to max_range.
439  float epsilon = 0.0001; // a tenth of a millimeter
440  sensor_msgs::msg::LaserScan message = *raw_message;
441  for (size_t i = 0; i < message.ranges.size(); i++) {
442  float range = message.ranges[i];
443  if (!std::isfinite(range) && range > 0) {
444  message.ranges[i] = message.range_max - epsilon;
445  }
446  }
447 
448  // project the laser into a point cloud
449  sensor_msgs::msg::PointCloud2 cloud;
450  cloud.header = message.header;
451 
452  // project the scan into a point cloud
453  try {
454  projector_.transformLaserScanToPointCloud(message.header.frame_id, message, cloud, *tf_);
455  } catch (tf2::TransformException & ex) {
456  RCLCPP_WARN(
457  logger_,
458  "High fidelity enabled, but TF returned a transform exception to frame %s: %s",
459  global_frame_.c_str(), ex.what());
460  projector_.projectLaser(message, cloud);
461  } catch (std::runtime_error & ex) {
462  RCLCPP_WARN(
463  logger_,
464  "transformLaserScanToPointCloud error, it seems the message from laser is malformed."
465  " Ignore this message. what(): %s",
466  ex.what());
467  return;
468  }
469 
470  // buffer the point cloud
471  buffer->lock();
472  buffer->bufferCloud(cloud);
473  buffer->unlock();
474 }
475 
476 void
478  sensor_msgs::msg::PointCloud2::ConstSharedPtr message,
479  const std::shared_ptr<ObservationBuffer> & buffer)
480 {
481  // buffer the point cloud
482  buffer->lock();
483  buffer->bufferCloud(*message);
484  buffer->unlock();
485 }
486 
487 void
489  double robot_x, double robot_y, double robot_yaw, double * min_x,
490  double * min_y, double * max_x, double * max_y)
491 {
492  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
493  if (rolling_window_) {
494  updateOrigin(robot_x - getSizeInMetersX() / 2, robot_y - getSizeInMetersY() / 2);
495  }
496  if (!enabled_) {
497  return;
498  }
499  useExtraBounds(min_x, min_y, max_x, max_y);
500 
501  bool current = true;
502  std::vector<Observation> observations, clearing_observations;
503 
504  // get the marking observations
505  current = current && getMarkingObservations(observations);
506 
507  // get the clearing observations
508  current = current && getClearingObservations(clearing_observations);
509 
510  // update the global current status
511  current_ = current;
512 
513  // raytrace freespace
514  for (unsigned int i = 0; i < clearing_observations.size(); ++i) {
515  raytraceFreespace(clearing_observations[i], min_x, min_y, max_x, max_y);
516  }
517 
518  // place the new obstacles into a priority queue... each with a priority of zero to begin with
519  for (std::vector<Observation>::const_iterator it = observations.begin();
520  it != observations.end(); ++it)
521  {
522  const Observation & obs = *it;
523 
524  const sensor_msgs::msg::PointCloud2 & cloud = *(obs.cloud_);
525 
526  double sq_obstacle_max_range = obs.obstacle_max_range_ * obs.obstacle_max_range_;
527  double sq_obstacle_min_range = obs.obstacle_min_range_ * obs.obstacle_min_range_;
528 
529  sensor_msgs::PointCloud2ConstIterator<float> iter_x(cloud, "x");
530  sensor_msgs::PointCloud2ConstIterator<float> iter_y(cloud, "y");
531  sensor_msgs::PointCloud2ConstIterator<float> iter_z(cloud, "z");
532 
533  for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
534  double px = *iter_x, py = *iter_y, pz = *iter_z;
535 
536  // if the obstacle is too low, we won't add it
537  if (pz < min_obstacle_height_) {
538  RCLCPP_DEBUG(logger_, "The point is too low");
539  continue;
540  }
541 
542  // if the obstacle is too high or too far away from the robot we won't add it
543  if (pz > max_obstacle_height_) {
544  RCLCPP_DEBUG(logger_, "The point is too high");
545  continue;
546  }
547 
548  // compute the squared distance from the hitpoint to the pointcloud's origin
549  double sq_dist =
550  (px -
551  obs.origin_.x) * (px - obs.origin_.x) + (py - obs.origin_.y) * (py - obs.origin_.y) +
552  (pz - obs.origin_.z) * (pz - obs.origin_.z);
553 
554  // if the point is far enough away... we won't consider it
555  if (sq_dist >= sq_obstacle_max_range) {
556  RCLCPP_DEBUG(logger_, "The point is too far away");
557  continue;
558  }
559 
560  // if the point is too close, do not conisder it
561  if (sq_dist < sq_obstacle_min_range) {
562  RCLCPP_DEBUG(logger_, "The point is too close");
563  continue;
564  }
565 
566  // now we need to compute the map coordinates for the observation
567  unsigned int mx, my;
568  if (!worldToMap(px, py, mx, my)) {
569  RCLCPP_DEBUG(logger_, "Computing map coords failed");
570  continue;
571  }
572 
573  unsigned int index = getIndex(mx, my);
574  costmap_[index] = LETHAL_OBSTACLE;
575  touch(px, py, min_x, min_y, max_x, max_y);
576  }
577  }
578 
579  updateFootprint(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
580 }
581 
582 void
584  double robot_x, double robot_y, double robot_yaw,
585  double * min_x, double * min_y,
586  double * max_x,
587  double * max_y)
588 {
589  if (!footprint_clearing_enabled_) {return;}
590  transformFootprint(robot_x, robot_y, robot_yaw, getFootprint(), transformed_footprint_);
591 
592  for (unsigned int i = 0; i < transformed_footprint_.size(); i++) {
593  touch(transformed_footprint_[i].x, transformed_footprint_[i].y, min_x, min_y, max_x, max_y);
594  }
595 }
596 
597 void
599  nav2_costmap_2d::Costmap2D & master_grid, int min_i, int min_j,
600  int max_i,
601  int max_j)
602 {
603  std::lock_guard<Costmap2D::mutex_t> guard(*getMutex());
604  if (!enabled_) {
605  return;
606  }
607 
608  // if not current due to reset, set current now after clearing
609  if (!current_ && was_reset_) {
610  was_reset_ = false;
611  current_ = true;
612  }
613 
614  if (footprint_clearing_enabled_) {
615  setConvexPolygonCost(transformed_footprint_, nav2_costmap_2d::FREE_SPACE);
616  }
617 
618  switch (combination_method_) {
620  updateWithOverwrite(master_grid, min_i, min_j, max_i, max_j);
621  break;
623  updateWithMax(master_grid, min_i, min_j, max_i, max_j);
624  break;
626  updateWithMaxWithoutUnknownOverwrite(master_grid, min_i, min_j, max_i, max_j);
627  break;
628  default: // Nothing
629  break;
630  }
631 }
632 
633 void
634 ObstacleLayer::addStaticObservation(
636  bool marking, bool clearing)
637 {
638  if (marking) {
639  static_marking_observations_.push_back(obs);
640  }
641  if (clearing) {
642  static_clearing_observations_.push_back(obs);
643  }
644 }
645 
646 void
647 ObstacleLayer::clearStaticObservations(bool marking, bool clearing)
648 {
649  if (marking) {
650  static_marking_observations_.clear();
651  }
652  if (clearing) {
653  static_clearing_observations_.clear();
654  }
655 }
656 
657 bool
658 ObstacleLayer::getMarkingObservations(std::vector<Observation> & marking_observations) const
659 {
660  bool current = true;
661  // get the marking observations
662  for (unsigned int i = 0; i < marking_buffers_.size(); ++i) {
663  marking_buffers_[i]->lock();
664  marking_buffers_[i]->getObservations(marking_observations);
665  current = marking_buffers_[i]->isCurrent() && current;
666  marking_buffers_[i]->unlock();
667  }
668  marking_observations.insert(
669  marking_observations.end(),
670  static_marking_observations_.begin(), static_marking_observations_.end());
671  return current;
672 }
673 
674 bool
675 ObstacleLayer::getClearingObservations(std::vector<Observation> & clearing_observations) const
676 {
677  bool current = true;
678  // get the clearing observations
679  for (unsigned int i = 0; i < clearing_buffers_.size(); ++i) {
680  clearing_buffers_[i]->lock();
681  clearing_buffers_[i]->getObservations(clearing_observations);
682  current = clearing_buffers_[i]->isCurrent() && current;
683  clearing_buffers_[i]->unlock();
684  }
685  clearing_observations.insert(
686  clearing_observations.end(),
687  static_clearing_observations_.begin(), static_clearing_observations_.end());
688  return current;
689 }
690 
691 void
693  const Observation & clearing_observation, double * min_x,
694  double * min_y,
695  double * max_x,
696  double * max_y)
697 {
698  double ox = clearing_observation.origin_.x;
699  double oy = clearing_observation.origin_.y;
700  const sensor_msgs::msg::PointCloud2 & cloud = *(clearing_observation.cloud_);
701 
702  // get the map coordinates of the origin of the sensor
703  unsigned int x0, y0;
704  if (!worldToMap(ox, oy, x0, y0)) {
705  RCLCPP_WARN(
706  logger_,
707  "Sensor origin at (%.2f, %.2f) is out of map bounds (%.2f, %.2f) to (%.2f, %.2f). "
708  "The costmap cannot raytrace for it.",
709  ox, oy,
710  origin_x_, origin_y_,
711  origin_x_ + getSizeInMetersX(), origin_y_ + getSizeInMetersY());
712  return;
713  }
714 
715  // we can pre-compute the endpoints of the map outside of the inner loop... we'll need these later
716  double origin_x = origin_x_, origin_y = origin_y_;
717  double map_end_x = origin_x + size_x_ * resolution_;
718  double map_end_y = origin_y + size_y_ * resolution_;
719 
720 
721  touch(ox, oy, min_x, min_y, max_x, max_y);
722 
723  // for each point in the cloud, we want to trace a line from the origin
724  // and clear obstacles along it
725  sensor_msgs::PointCloud2ConstIterator<float> iter_x(cloud, "x");
726  sensor_msgs::PointCloud2ConstIterator<float> iter_y(cloud, "y");
727 
728  for (; iter_x != iter_x.end(); ++iter_x, ++iter_y) {
729  double wx = *iter_x;
730  double wy = *iter_y;
731 
732  // now we also need to make sure that the endpoint we're raytracing
733  // to isn't off the costmap and scale if necessary
734  double a = wx - ox;
735  double b = wy - oy;
736 
737  // the minimum value to raytrace from is the origin
738  if (wx < origin_x) {
739  double t = (origin_x - ox) / a;
740  wx = origin_x;
741  wy = oy + b * t;
742  }
743  if (wy < origin_y) {
744  double t = (origin_y - oy) / b;
745  wx = ox + a * t;
746  wy = origin_y;
747  }
748 
749  // the maximum value to raytrace to is the end of the map
750  if (wx > map_end_x) {
751  double t = (map_end_x - ox) / a;
752  wx = map_end_x - .001;
753  wy = oy + b * t;
754  }
755  if (wy > map_end_y) {
756  double t = (map_end_y - oy) / b;
757  wx = ox + a * t;
758  wy = map_end_y - .001;
759  }
760 
761  // now that the vector is scaled correctly... we'll get the map coordinates of its endpoint
762  unsigned int x1, y1;
763 
764  // check for legality just in case
765  if (!worldToMap(wx, wy, x1, y1)) {
766  continue;
767  }
768 
769  unsigned int cell_raytrace_max_range = cellDistance(clearing_observation.raytrace_max_range_);
770  unsigned int cell_raytrace_min_range = cellDistance(clearing_observation.raytrace_min_range_);
771  MarkCell marker(costmap_, FREE_SPACE);
772  // and finally... we can execute our trace to clear obstacles along that line
773  nav2_util::raytraceLine(
774  marker, x0, y0, x1, y1, size_x_, cell_raytrace_max_range, cell_raytrace_min_range);
775 
777  ox, oy, wx, wy, clearing_observation.raytrace_max_range_,
778  clearing_observation.raytrace_min_range_, min_x, min_y, max_x,
779  max_y);
780  }
781 }
782 
783 void
785 {
786  for (auto & notifier : observation_notifiers_) {
787  notifier->clear();
788  }
789 
790  // if we're stopped we need to re-subscribe to topics
791  for (unsigned int i = 0; i < observation_subscribers_.size(); ++i) {
792  if (observation_subscribers_[i] != NULL) {
793  observation_subscribers_[i]->subscribe();
794  }
795  }
797 }
798 
799 void
801 {
802  for (unsigned int i = 0; i < observation_subscribers_.size(); ++i) {
803  if (observation_subscribers_[i] != NULL) {
804  observation_subscribers_[i]->unsubscribe();
805  }
806  }
807 }
808 
809 void
811  double ox, double oy, double wx, double wy, double max_range, double min_range,
812  double * min_x, double * min_y, double * max_x, double * max_y)
813 {
814  double dx = wx - ox, dy = wy - oy;
815  double full_distance = hypot(dx, dy);
816  if (full_distance < min_range) {
817  return;
818  }
819  double scale = std::min(1.0, max_range / full_distance);
820  double ex = ox + dx * scale, ey = oy + dy * scale;
821  touch(ex, ey, min_x, min_y, max_x, max_y);
822 }
823 
824 void
826 {
827  resetMaps();
829  current_ = false;
830  was_reset_ = true;
831 }
832 
833 void
835 {
836  for (unsigned int i = 0; i < observation_buffers_.size(); ++i) {
837  if (observation_buffers_[i]) {
838  observation_buffers_[i]->resetLastUpdated();
839  }
840  }
841 }
842 
843 } // namespace nav2_costmap_2d
A QoS profile for best-effort sensor data with a history of 10 messages.
A 2D costmap provides a mapping between points in the world and their associated "costs".
Definition: costmap_2d.hpp:69
unsigned int getIndex(unsigned int mx, unsigned int my) const
Given two map coordinates... compute the associated index.
Definition: costmap_2d.hpp:231
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Convert from world coordinates to map coordinates.
Definition: costmap_2d.cpp:292
virtual void updateOrigin(double new_origin_x, double new_origin_y)
Move the origin of the costmap to a new location.... keeping data when it can.
Definition: costmap_2d.cpp:350
bool setConvexPolygonCost(const std::vector< geometry_msgs::msg::Point > &polygon, unsigned char cost_value)
Sets the cost of a convex polygon to a desired value.
Definition: costmap_2d.cpp:406
double getSizeInMetersY() const
Accessor for the y size of the costmap in meters.
Definition: costmap_2d.cpp:563
double getSizeInMetersX() const
Accessor for the x size of the costmap in meters.
Definition: costmap_2d.cpp:558
virtual void resetMaps()
Resets the costmap and static_map to be unknown space.
Definition: costmap_2d.cpp:125
unsigned int cellDistance(double world_dist)
Given distance in the world... convert it to cells.
Definition: costmap_2d.cpp:254
void touch(double x, double y, double *min_x, double *min_y, double *max_x, double *max_y)
virtual void matchSize()
Match the size of the master costmap.
CombinationMethod combination_method_from_int(const int value)
Converts an integer to a CombinationMethod enum and logs on failure.
Abstract class for layered costmap plugin implementations.
Definition: layer.hpp:59
std::string joinWithParentNamespace(const std::string &topic)
Definition: layer.cpp:121
void declareParameter(const std::string &param_name, const rclcpp::ParameterValue &value)
Convenience functions for declaring ROS parameters.
Definition: layer.cpp:76
const std::vector< geometry_msgs::msg::Point > & getFootprint() const
Convenience function for layered_costmap_->getFootprint().
Definition: layer.cpp:70
bool isRolling()
If this costmap is rolling or not.
Takes in point clouds from sensors, transforms them to the desired frame, and stores them.
Stores an observation in terms of a point cloud and the origin of the source.
Definition: observation.hpp:47
Takes in laser and pointcloud data to populate into 2D costmap.
void pointCloud2Callback(sensor_msgs::msg::PointCloud2::ConstSharedPtr message, const std::shared_ptr< nav2_costmap_2d::ObservationBuffer > &buffer)
A callback to handle buffering PointCloud2 messages.
std::vector< std::shared_ptr< nav2_costmap_2d::ObservationBuffer > > marking_buffers_
Used to store observation buffers used for marking obstacles.
virtual void activate()
Activate the layer.
std::vector< std::shared_ptr< tf2_ros::MessageFilterBase > > observation_notifiers_
Used to make sure that transforms are available for each sensor.
bool getMarkingObservations(std::vector< nav2_costmap_2d::Observation > &marking_observations) const
Get the observations used to mark space.
std::string global_frame_
The global frame for the costmap.
bool getClearingObservations(std::vector< nav2_costmap_2d::Observation > &clearing_observations) const
Get the observations used to clear space.
void updateRaytraceBounds(double ox, double oy, double wx, double wy, double max_range, double min_range, double *min_x, double *min_y, double *max_x, double *max_y)
Process update costmap with raytracing the window bounds.
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr dyn_params_handler_
Dynamic parameters handler.
void laserScanCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr message, const std::shared_ptr< nav2_costmap_2d::ObservationBuffer > &buffer)
A callback to handle buffering LaserScan messages.
void resetBuffersLastUpdated()
triggers the update of observations buffer
std::vector< std::shared_ptr< nav2_costmap_2d::ObservationBuffer > > clearing_buffers_
Used to store observation buffers used for clearing obstacles.
void laserScanValidInfCallback(sensor_msgs::msg::LaserScan::ConstSharedPtr message, const std::shared_ptr< nav2_costmap_2d::ObservationBuffer > &buffer)
A callback to handle buffering LaserScan messages which need filtering to turn Inf values into range_...
std::vector< std::shared_ptr< message_filters::SubscriberBase< rclcpp_lifecycle::LifecycleNode > > > observation_subscribers_
Used for the observation message filters.
void updateFootprint(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y)
Clear costmap layer info below the robot's footprint.
virtual void deactivate()
Deactivate the layer.
virtual void updateCosts(nav2_costmap_2d::Costmap2D &master_grid, int min_i, int min_j, int max_i, int max_j)
Update the costs in the master costmap in the window.
rcl_interfaces::msg::SetParametersResult dynamicParametersCallback(std::vector< rclcpp::Parameter > parameters)
Callback executed when a parameter change is detected.
virtual ~ObstacleLayer()
A destructor.
std::vector< std::shared_ptr< nav2_costmap_2d::ObservationBuffer > > observation_buffers_
Used to store observations from various sensors.
virtual void onInitialize()
Initialization process of layer on startup.
double min_obstacle_height_
Max Obstacle Height.
laser_geometry::LaserProjection projector_
Used to project laser scans into point clouds.
double max_obstacle_height_
Max Obstacle Height.
virtual void reset()
Reset this costmap.
virtual void raytraceFreespace(const nav2_costmap_2d::Observation &clearing_observation, double *min_x, double *min_y, double *max_x, double *max_y)
Clear freespace based on one observation.
virtual void updateBounds(double robot_x, double robot_y, double robot_yaw, double *min_x, double *min_y, double *max_x, double *max_y)
Update the bounds of the master costmap by this layer's update dimensions.
void transformFootprint(double x, double y, double theta, const std::vector< geometry_msgs::msg::Point > &footprint_spec, std::vector< geometry_msgs::msg::Point > &oriented_footprint)
Given a pose and base footprint, build the oriented footprint of the robot (list of Points)
Definition: footprint.cpp:112