feat(obstacle_layer): zero_is_valid for laser scans that report no return as 0

- New per-source flag zero_is_valid (default false): a 0 range is replaced by
  range_max - 0.1 mm, like inf_is_valid does for Inf, so the beam clears free
  space up to raytrace_range instead of being dropped.
- The flag is copied from the shared costmap block like inf_is_valid; it logs an
  error when set on a non-LaserScan source.
- Test obstacle_layer_scan_ranges_test covers the range replacement.
This commit is contained in:
2026-11-20 16:30:31 +07:00
parent 2695eb4e55
commit 55c7359632
5 changed files with 77 additions and 20 deletions

View File

@@ -306,6 +306,9 @@ if(BUILD_COSTMAP_TESTS)
add_executable(costmap_common_root_test test/costmap_common_root_test.cpp)
target_link_libraries(costmap_common_root_test PRIVATE robot_costmap_2d GTest::GTest GTest::Main)
add_executable(obstacle_layer_scan_ranges_test test/obstacle_layer_scan_ranges_test.cpp)
target_link_libraries(obstacle_layer_scan_ranges_test PRIVATE plugins robot_costmap_2d GTest::GTest GTest::Main)
add_executable(direction_field_test test/direction_field_test.cpp)
target_link_libraries(direction_field_test PRIVATE robot_costmap_2d GTest::GTest GTest::Main)

View File

@@ -64,8 +64,12 @@ struct CallBackInfo
std::string data_type;
std::string topic;
bool inf_is_valid;
bool zero_is_valid = false;
};
/** Replace enabled no-return ranges with range_max minus a small epsilon. */
void replaceNoReturnRanges(robot_sensor_msgs::LaserScan& scan, bool inf_is_valid, bool zero_is_valid);
class ObstacleLayer : public CostmapLayer
{
public:
@@ -107,12 +111,13 @@ protected:
const boost::shared_ptr<ObservationBuffer>& buffer);
/**
* @brief A callback to handle buffering LaserScan messages which need filtering to turn Inf values into range_max.
* @brief Buffer LaserScan messages after replacing enabled no-return ranges.
* @param message The message returned from a message notifier
* @param buffer A pointer to the observation buffer to update
*/
void laserScanValidInfCallback(const robot_sensor_msgs::LaserScan& message,
const boost::shared_ptr<ObservationBuffer>& buffer);
const boost::shared_ptr<ObservationBuffer>& buffer,
bool inf_is_valid, bool zero_is_valid);
/**
* @brief A callback to handle buffering PointCloud messages

View File

@@ -42,6 +42,7 @@
#include <tf3/exceptions.h>
#include <boost/dll/alias.hpp>
#include <cmath>
#include <stdexcept>
@@ -55,6 +56,16 @@ using robot_costmap_2d::Observation;
namespace robot_costmap_2d
{
void replaceNoReturnRanges(robot_sensor_msgs::LaserScan& scan, bool inf_is_valid, bool zero_is_valid)
{
constexpr float kEpsilon = 0.0001; // a tenth of a millimeter
for (float& range : scan.ranges)
{
if ((inf_is_valid && !std::isfinite(range) && range > 0) || (zero_is_valid && range == 0.0f))
range = scan.range_max - kEpsilon;
}
}
void ObstacleLayer::onInitialize()
{
robot::NodeHandle nh("~");
@@ -132,7 +143,7 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
// get the parameters for the specific topic
double observation_keep_time = 0, expected_update_rate = 0, min_obstacle_height = 0, max_obstacle_height = 2;
std::string topic = "map", sensor_frame = "laser_frame", data_type = "PointCloud";
bool inf_is_valid = false, clearing=false, marking=true;
bool inf_is_valid = false, zero_is_valid = false, clearing=false, marking=true;
robot::NodeHandle priv_nh(nh, source);
topic = loadParam(layer[source],"topic", topic);
@@ -143,6 +154,7 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
min_obstacle_height = loadParam(layer[source],"min_obstacle_height", 0.0);
max_obstacle_height = loadParam(layer[source],"max_obstacle_height", 2.0);
inf_is_valid = loadParam(layer[source],"inf_is_valid", false);
zero_is_valid = loadParam(layer[source],"zero_is_valid", false);
clearing = loadParam(layer[source],"clearing", false);
marking = loadParam(layer[source],"marking", true);
@@ -162,6 +174,8 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
priv_nh.getParam("max_obstacle_height", max_obstacle_height);
if (priv_nh.hasParam("inf_is_valid"))
priv_nh.getParam("inf_is_valid", inf_is_valid);
if (priv_nh.hasParam("zero_is_valid"))
priv_nh.getParam("zero_is_valid", zero_is_valid);
if (priv_nh.hasParam("clearing"))
priv_nh.getParam("clearing", clearing);
if (priv_nh.hasParam("marking"))
@@ -188,10 +202,13 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
info_tmp.data_type = data_type;
info_tmp.topic = topic;
info_tmp.inf_is_valid = inf_is_valid;
info_tmp.zero_is_valid = zero_is_valid;
callback_infos_.push_back(info_tmp);
robot::log_info("Creating an observation buffer for topic %s, frame %s\n", topic.c_str(),
priv_nh.getNamespace().c_str());
robot::log_info("Creating an observation buffer for topic %s, frame %s, zero_is_valid=%s\n", topic.c_str(),
priv_nh.getNamespace().c_str(), zero_is_valid ? "true" : "false");
if (zero_is_valid && data_type != "LaserScan")
robot::log_error("obstacle_layer: zero_is_valid option is not applicable to %s observations.\n", data_type.c_str());
// create an observation buffer
observation_buffers_.push_back(
@@ -240,7 +257,7 @@ void ObstacleLayer::handleImpl(const void* data,
if (type == typeid(robot_sensor_msgs::LaserScan) &&
callback_infos_[i].data_type == "LaserScan" &&
topic == callback_infos_[i].topic &&
!callback_infos_[i].inf_is_valid)
!callback_infos_[i].inf_is_valid && !callback_infos_[i].zero_is_valid)
{
// if(topic == "/f_scan") robot::log_error("DATA front laser! %d",i);
// if(topic == "/b_scan") robot::log_error("DATA back laser! %d",i);
@@ -249,10 +266,11 @@ void ObstacleLayer::handleImpl(const void* data,
else if (type == typeid(robot_sensor_msgs::LaserScan) &&
callback_infos_[i].data_type == "LaserScan" &&
topic == callback_infos_[i].topic &&
callback_infos_[i].inf_is_valid)
(callback_infos_[i].inf_is_valid || callback_infos_[i].zero_is_valid))
{
laserScanValidInfCallback(*static_cast<const robot_sensor_msgs::LaserScan*>(data), buffer);
laserScanValidInfCallback(*static_cast<const robot_sensor_msgs::LaserScan*>(data), buffer,
callback_infos_[i].inf_is_valid, callback_infos_[i].zero_is_valid);
}
else if (type == typeid(robot_sensor_msgs::PointCloud) &&
callback_infos_[i].data_type == "PointCloud" &&
@@ -316,19 +334,11 @@ void ObstacleLayer::laserScanCallback(const robot_sensor_msgs::LaserScan& messag
}
void ObstacleLayer::laserScanValidInfCallback(const robot_sensor_msgs::LaserScan& raw_message,
const boost::shared_ptr<ObservationBuffer>& buffer)
const boost::shared_ptr<ObservationBuffer>& buffer,
bool inf_is_valid, bool zero_is_valid)
{
// Filter positive infinities ("Inf"s) to max_range.
float epsilon = 0.0001; // a tenth of a millimeter
robot_sensor_msgs::LaserScan message = raw_message;
for (size_t i = 0; i < message.ranges.size(); i++)
{
float range = message.ranges[ i ];
if (!std::isfinite(range) && range > 0)
{
message.ranges[ i ] = message.range_max - epsilon;
}
}
replaceNoReturnRanges(message, inf_is_valid, zero_is_valid);
// project the laser into a point cloud
robot_sensor_msgs::PointCloud2 cloud;

View File

@@ -495,7 +495,7 @@ void Costmap2DROBOT::copyParentParameters(const std::string& costmap_name,
move_parameters<std::string>(from, to, {"topic", "sensor_frame", "data_type"});
move_parameters<double>(from, to, {"observation_persistence", "expected_update_rate", "min_obstacle_height",
"max_obstacle_height", "obstacle_range", "raytrace_range"});
move_parameters<bool>(from, to, {"clearing", "marking", "inf_is_valid"});
move_parameters<bool>(from, to, {"clearing", "marking", "inf_is_valid", "zero_is_valid"});
});
}
else if (plugin_type == "InflationLayer")

View File

@@ -0,0 +1,39 @@
#include <robot_costmap_2d/obstacle_layer.h>
#include <cmath>
#include <limits>
#include <gtest/gtest.h>
namespace
{
TEST(ObstacleLayerScanRanges, FlagsIndependentlyReplaceOnlyEnabledNoReturns)
{
const float inf = std::numeric_limits<float>::infinity();
const float nan = std::numeric_limits<float>::quiet_NaN();
for (bool inf_is_valid : {false, true})
{
for (bool zero_is_valid : {false, true})
{
SCOPED_TRACE(::testing::Message() << "inf=" << inf_is_valid << " zero=" << zero_is_valid);
robot_sensor_msgs::LaserScan scan;
scan.range_min = 0.2f;
scan.range_max = 10.0f;
scan.ranges = {0.0f, -0.0f, inf, -inf, nan, -1.0f, 0.1f, 1.0f};
robot_costmap_2d::replaceNoReturnRanges(scan, inf_is_valid, zero_is_valid);
const float replacement = scan.range_max - 0.0001f;
EXPECT_FLOAT_EQ(scan.ranges[0], zero_is_valid ? replacement : 0.0f);
EXPECT_FLOAT_EQ(scan.ranges[1], zero_is_valid ? replacement : -0.0f);
if (!zero_is_valid)
EXPECT_TRUE(std::signbit(scan.ranges[1]));
EXPECT_FLOAT_EQ(scan.ranges[2], inf_is_valid ? replacement : inf);
EXPECT_EQ(scan.ranges[3], -inf);
EXPECT_TRUE(std::isnan(scan.ranges[4]));
EXPECT_FLOAT_EQ(scan.ranges[5], -1.0f);
EXPECT_FLOAT_EQ(scan.ranges[6], 0.1f);
EXPECT_FLOAT_EQ(scan.ranges[7], 1.0f);
}
}
}
} // namespace