Skip to content

Commit 80b72d2

Browse files
authored
Update obstacle layer usage of max ranges (#5697)
* Use cell distance for obstacle marking max range Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Don't raytrace clear the cell containing the current observation Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Add tests for max marking and clearing ranges Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Fix cpplint failures Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Fix distance calculation Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * fix casting, formatting Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * check origin is in map, update CMakeLists Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * use hypot instead of squared dist Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Move origin calc out of loop Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Revert don't raytrace observation cell Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Revert don't raytrace origin if it is observation cell Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> * Remove new line Signed-off-by: Simon Dathan <simon.dathan@kudan.eu> --------- Signed-off-by: Simon Dathan <simon.dathan@kudan.eu>
1 parent 9f4094b commit 80b72d2

File tree

3 files changed

+427
-17
lines changed

3 files changed

+427
-17
lines changed

nav2_costmap_2d/plugins/obstacle_layer.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,14 @@ ObstacleLayer::updateBounds(
527527

528528
const sensor_msgs::msg::PointCloud2 & cloud = obs.cloud_;
529529

530-
double sq_obstacle_max_range = obs.obstacle_max_range_ * obs.obstacle_max_range_;
531-
double sq_obstacle_min_range = obs.obstacle_min_range_ * obs.obstacle_min_range_;
530+
const unsigned int max_range_cells = cellDistance(obs.obstacle_max_range_);
531+
const unsigned int min_range_cells = cellDistance(obs.obstacle_min_range_);
532+
533+
unsigned int x0, y0;
534+
if (!worldToMap(obs.origin_.x, obs.origin_.y, x0, y0)) {
535+
RCLCPP_DEBUG(logger_, "Sensor origin is out of map bounds");
536+
continue;
537+
}
532538

533539
sensor_msgs::PointCloud2ConstIterator<float> iter_x(cloud, "x");
534540
sensor_msgs::PointCloud2ConstIterator<float> iter_y(cloud, "y");
@@ -549,31 +555,33 @@ ObstacleLayer::updateBounds(
549555
continue;
550556
}
551557

552-
// compute the squared distance from the hitpoint to the pointcloud's origin
553-
double sq_dist =
554-
(px -
555-
obs.origin_.x) * (px - obs.origin_.x) + (py - obs.origin_.y) * (py - obs.origin_.y) +
556-
(pz - obs.origin_.z) * (pz - obs.origin_.z);
558+
// now we need to compute the map coordinates for the observation
559+
unsigned int mx, my;
560+
if (!worldToMap(px, py, mx, my)) {
561+
RCLCPP_DEBUG(logger_, "Computing map coords failed");
562+
continue;
563+
}
564+
565+
// compute the distance from the hitpoint to the pointcloud's origin
566+
// Calculate the distance in cell space to match the ray trace algorithm
567+
// used for clearing obstacles (see Costmap2D::raytraceLine).
568+
const int dx = static_cast<int>(mx) - static_cast<int>(x0);
569+
const int dy = static_cast<int>(my) - static_cast<int>(y0);
570+
const unsigned int dist = static_cast<unsigned int>(
571+
std::hypot(static_cast<double>(dx), static_cast<double>(dy)));
557572

558573
// if the point is far enough away... we won't consider it
559-
if (sq_dist >= sq_obstacle_max_range) {
574+
if (dist > max_range_cells) {
560575
RCLCPP_DEBUG(logger_, "The point is too far away");
561576
continue;
562577
}
563578

564-
// if the point is too close, do not conisder it
565-
if (sq_dist < sq_obstacle_min_range) {
579+
// if the point is too close, do not consider it
580+
if (dist < min_range_cells) {
566581
RCLCPP_DEBUG(logger_, "The point is too close");
567582
continue;
568583
}
569584

570-
// now we need to compute the map coordinates for the observation
571-
unsigned int mx, my;
572-
if (!worldToMap(px, py, mx, my)) {
573-
RCLCPP_DEBUG(logger_, "Computing map coords failed");
574-
continue;
575-
}
576-
577585
unsigned int index = getIndex(mx, my);
578586
costmap_[index] = LETHAL_OBSTACLE;
579587
touch(px, py, min_x, min_y, max_x, max_y);

nav2_costmap_2d/test/unit/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,9 @@ nav2_add_gtest(observation_buffer_test observation_buffer_test.cpp)
7171
target_link_libraries(observation_buffer_test
7272
nav2_costmap_2d_core
7373
)
74+
75+
nav2_add_gtest(obstacle_layer_test obstacle_layer_test.cpp)
76+
target_link_libraries(obstacle_layer_test
77+
nav2_costmap_2d_core
78+
layers
79+
)

0 commit comments

Comments
 (0)