refactor(depth): keep DepthCameraObstacleLayer, drop the legacy depth clearing

Two depth-image clearing designs lived in this package: the frustum ray
clearing inherited from dev_cam_intel (DepthCameraData sources of
ObstacleLayer/VoxelLayer, frustum_* and column_* keys) and the observed_volume
evidence clearing (ROC) on top of it. Both are replaced on the robot by
DepthCameraObstacleLayer, a port of costmap_depth_camera that takes the
filtered PointCloud2 directly, so both are removed and the tree is rebuilt
from main (a63c084) plus the plugin.

- observation.h / observation_buffer: DepthFrustumConfig, DepthCameraObservation
  and the depth buffering API are gone; buffers hold point clouds only.
- ObstacleLayer/VoxelLayer: no frustum ray casting, column clearing or depth
  ray cache. A DepthCameraData source or frustum_clearing_enabled: true now
  fails at startup with a message pointing to DepthCameraObstacleLayer
  instead of silently dropping the camera; DepthCameraData reaching the layer
  through the gateway is ignored.
- copyParentParameters no longer forwards the frustum_* / column_* keys.
- ROC files (depth_evidence_store, depth_visibility, depth_frame_pairer) and
  their tests are not carried over; CloudProcessingConfig goes with them.
- PCL is needed by the plugin library only. The plugin test moves under
  BUILD_COSTMAP_TESTS; BUILD_DEPTH_MARKING_TESTS is gone.
- README documents DepthCameraObstacleLayer and its known limits.

The plugin code is unchanged from the version running on the KIT robot.
This commit is contained in:
2026-10-20 09:47:25 +07:00
parent 9a67eee148
commit b23ff27a3c
20 changed files with 222 additions and 3768 deletions

View File

@@ -41,41 +41,12 @@
#include <robot_sensor_msgs/point_cloud2_iterator.h>
#include <cstring>
#include <cmath>
#include <stdexcept>
#include <pcl/filters/voxel_grid.h>
#include <pcl/point_types.h>
#include <data_convert/pcl_convert.h>
using namespace std;
using namespace tf3;
namespace robot_costmap_2d
{
namespace
{
bool validTimestampCloud(const robot_sensor_msgs::PointCloud2& cloud)
{
if (cloud.header.stamp.isZero() || cloud.header.frame_id.empty() ||
cloud.is_bigendian || cloud.point_step == 0 || cloud.height != 1 ||
cloud.row_step != static_cast<size_t>(cloud.width) * cloud.point_step ||
cloud.data.size() != cloud.row_step)
return false;
unsigned int fields = 0;
for (const auto& field : cloud.fields)
{
const unsigned int bit = field.name == "x" ? 1u : field.name == "y" ? 2u : field.name == "z" ? 4u : 0u;
if (!bit) continue;
if ((fields & bit) || field.count != 1 ||
field.datatype != robot_sensor_msgs::PointField::FLOAT32 ||
static_cast<size_t>(field.offset) + sizeof(float) > cloud.point_step)
return false;
fields |= bit;
}
return fields == 7u;
}
} // namespace
ObservationBuffer::ObservationBuffer(string topic_name, double observation_keep_time, double expected_update_rate,
double min_obstacle_height, double max_obstacle_height, double obstacle_range,
double raytrace_range, tf3::BufferCore& tf3_buffer, string global_frame,
@@ -87,23 +58,6 @@ ObservationBuffer::ObservationBuffer(string topic_name, double observation_keep_
{
}
ObservationBuffer::ObservationBuffer(string topic_name, double observation_keep_time, double expected_update_rate,
double min_obstacle_height, double max_obstacle_height, double obstacle_range,
double raytrace_range, const DepthFrustumConfig& frustum_config,
tf3::BufferCore& tf3_buffer, string global_frame,
string sensor_frame, double tf_tolerance) :
tf3_buffer_(tf3_buffer), observation_keep_time_(observation_keep_time), expected_update_rate_(expected_update_rate),
last_updated_(robot::Time::now()), global_frame_(global_frame), sensor_frame_(sensor_frame), topic_name_(topic_name),
min_obstacle_height_(min_obstacle_height), max_obstacle_height_(max_obstacle_height),
obstacle_range_(obstacle_range), raytrace_range_(raytrace_range),
tf_tolerance_(tf_tolerance),
frustum_config_(frustum_config)
{
frustum_config_.pixel_step = std::max(1u, frustum_config_.pixel_step);
frustum_config_.min_range = std::max(0.0, frustum_config_.min_range);
frustum_config_.max_range = std::max(frustum_config_.max_range, frustum_config_.min_range);
}
ObservationBuffer::~ObservationBuffer()
{
}
@@ -172,43 +126,6 @@ bool ObservationBuffer::setGlobalFrame(const std::string new_global_frame)
}
void ObservationBuffer::bufferCloud(const robot_sensor_msgs::PointCloud2& cloud)
{
(void)tryBufferCloud(cloud);
}
void ObservationBuffer::setCloudProcessingConfig(const CloudProcessingConfig& config)
{
const float leaf = static_cast<float>(config.voxel_leaf_size);
if (config.voxel_filter_enabled &&
(!config.transform_at_stamp || !std::isfinite(leaf) || leaf <= 0.0f ||
!std::isfinite(config.voxel_leaf_size)))
throw std::invalid_argument("cloud voxel filter requires timestamp TF and a finite positive leaf [m]");
if (config.transform_at_stamp &&
(!std::isfinite(min_obstacle_height_) || !std::isfinite(max_obstacle_height_) ||
min_obstacle_height_ > max_obstacle_height_ ||
!std::isfinite(obstacle_range_) || obstacle_range_ <= 0.0))
throw std::invalid_argument("timestamp cloud processing requires finite height/range limits");
cloud_processing_config_ = config;
}
bool ObservationBuffer::tryBufferCloud(const robot_sensor_msgs::PointCloud2& cloud)
{
return tryBufferCloudImpl(cloud, nullptr);
}
bool ObservationBuffer::tryBufferCloudWithTransform(const robot_sensor_msgs::PointCloud2& cloud,
const tf3::TransformStampedMsg& transform)
{
if (!cloud_processing_config_.transform_at_stamp || transform.header.frame_id != global_frame_ ||
transform.header.stamp != data_convert::convertTime(cloud.header.stamp) ||
transform.child_frame_id != cloud.header.frame_id ||
(!sensor_frame_.empty() && sensor_frame_ != cloud.header.frame_id))
return false;
return tryBufferCloudImpl(cloud, &transform);
}
bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2& cloud,
const tf3::TransformStampedMsg* prepared_transform)
{
robot_geometry_msgs::PointStamped global_origin;
Observation observation;
@@ -216,13 +133,6 @@ bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2&
// check whether the origin frame has been set explicitly or whether we should get it from the cloud
string origin_frame = sensor_frame_ == "" ? cloud.header.frame_id : sensor_frame_;
if (cloud_processing_config_.transform_at_stamp && !validTimestampCloud(cloud))
{
robot::log_error_throttle(5.0, "ObservationBuffer [%s]: invalid timestamp cloud layout/header\n",
topic_name_.c_str());
return false;
}
try
{
// given these observations come from sensors... we'll need to store the origin pt of the sensor
@@ -233,16 +143,14 @@ bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2&
local_origin.point.y = 0;
local_origin.point.z = 0;
// tf3_buffer_.transform(local_origin, global_origin, global_frame_);
const tf3::Time query_time = cloud_processing_config_.transform_at_stamp
? tf3::Time(cloud.header.stamp.sec, cloud.header.stamp.nsec) : tf3::Time();
const tf3::TransformStampedMsg cloud_transform = prepared_transform ? *prepared_transform
: tf3_buffer_.lookupTransform(global_frame_, cloud.header.frame_id, query_time);
const tf3::TransformStampedMsg cloud_transform = tf3_buffer_.lookupTransform(
global_frame_, cloud.header.frame_id, tf3::Time());
if (origin_frame == cloud.header.frame_id)
tf3::doTransform(local_origin, global_origin, cloud_transform);
else
tf3::doTransform(
local_origin, global_origin,
tf3_buffer_.lookupTransform(global_frame_, origin_frame, query_time));
tf3_buffer_.lookupTransform(global_frame_, origin_frame, tf3::Time()));
tf3::convert(global_origin.point, observation.origin_);
observation.raytrace_range_ = raytrace_range_;
@@ -257,24 +165,11 @@ bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2&
const std::size_t point_step = observation_cloud.point_step;
std::size_t point_count = 0;
robot_sensor_msgs::PointCloud2Iterator<float> iter_z(observation_cloud, "z");
robot_sensor_msgs::PointCloud2Iterator<float> iter_x(observation_cloud, "x");
robot_sensor_msgs::PointCloud2Iterator<float> iter_y(observation_cloud, "y");
const double sq_range = obstacle_range_ * obstacle_range_;
// Compact accepted points in-place. This avoids allocating and copying a
// second full-size filtered cloud after the TF transform.
for (std::size_t read_index = 0; read_index < cloud_size;
++read_index, ++iter_x, ++iter_y, ++iter_z)
for (std::size_t read_index = 0; read_index < cloud_size; ++read_index, ++iter_z)
{
if (cloud_processing_config_.transform_at_stamp)
{
const double dx = *iter_x - observation.origin_.x;
const double dy = *iter_y - observation.origin_.y;
const double dz = *iter_z - observation.origin_.z;
if (!std::isfinite(*iter_x) || !std::isfinite(*iter_y) || !std::isfinite(*iter_z) ||
dx * dx + dy * dy + dz * dz >= sq_range)
continue;
}
if ((*iter_z) > max_obstacle_height_ || (*iter_z) < min_obstacle_height_)
continue;
@@ -292,36 +187,12 @@ bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2&
robot_sensor_msgs::PointCloud2Modifier modifier(observation_cloud);
modifier.resize(point_count);
}
// Floor/out-of-band points cannot pull a small obstacle's centroid below
// its marking threshold. Only this source's opted-in cloud is downsampled.
if (cloud_processing_config_.voxel_filter_enabled && point_count != 0)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr input(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromROBOTMsg(observation_cloud, *input);
pcl::VoxelGrid<pcl::PointXYZ> voxel;
const float leaf = static_cast<float>(cloud_processing_config_.voxel_leaf_size);
voxel.setLeafSize(leaf, leaf, leaf);
voxel.setInputCloud(input);
pcl::PointCloud<pcl::PointXYZ> output;
voxel.filter(output);
const robot_std_msgs::Header header = observation_cloud.header;
pcl::toROBOTMsg(output, observation_cloud);
observation_cloud.header = header; // PCL timestamps only have microseconds.
}
}
catch (TransformException& ex)
{
robot::log_error_throttle(5.0, "ObservationBuffer [%s] missing TF for frame %s at %u.%09u: %s\n",
topic_name_.c_str(), cloud.header.frame_id.c_str(),
cloud.header.stamp.sec, cloud.header.stamp.nsec, ex.what());
return false;
}
catch (const std::exception& ex)
{
robot::log_error_throttle(5.0, "ObservationBuffer [%s] rejected cloud: %s\n",
topic_name_.c_str(), ex.what());
return false;
robot::log_error("TF Exception that should never happen for sensor frame: %s, cloud frame: %s, %s\n", sensor_frame_.c_str(),
cloud.header.frame_id.c_str(), ex.what());
return;
}
if (observation_keep_time_ == robot::Duration(0.0) && !observation_list_.empty())
@@ -339,37 +210,6 @@ bool ObservationBuffer::tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2&
// we'll also remove any stale observations from the list
purgeStaleObservations();
return true;
}
void ObservationBuffer::bufferDepthCamera(const robot_sensor_msgs::DepthCameraData& depth_camera_data)
{
bufferDepthCamera(boost::make_shared<robot_sensor_msgs::DepthCameraData>(depth_camera_data));
}
void ObservationBuffer::bufferDepthCamera(robot_sensor_msgs::DepthCameraData::ConstPtr depth_camera_data)
{
if (!depth_camera_data)
return;
DepthCameraObservation observation(
std::move(depth_camera_data), topic_name_, robot::Time::now(), frustum_config_);
if (observation_keep_time_ == robot::Duration(0.0) && !depth_observation_list_.empty())
{
depth_observation_list_.front() = std::move(observation);
depth_observation_list_.erase(++depth_observation_list_.begin(), depth_observation_list_.end());
}
else
{
depth_observation_list_.push_front(std::move(observation));
}
// if the update was successful, we want to update the last updated time
last_updated_ = robot::Time::now();
// first... let's make sure that we don't have any stale observations
purgeStaleDepthObservations();
}
// returns a copy of the observations
@@ -386,26 +226,6 @@ void ObservationBuffer::getObservations(vector<Observation>& observations)
}
}
void ObservationBuffer::getDepthObservations(vector<DepthCameraObservation>& observations)
{
// first... let's make sure that we don't have any stale observations
purgeStaleDepthObservations();
// now we'll just copy the observations for the caller
if (observation_keep_time_ == robot::Duration(0.0))
{
if (!depth_observation_list_.empty())
{
observations.push_back(std::move(depth_observation_list_.front()));
depth_observation_list_.clear();
}
return;
}
observations.insert(
observations.end(), depth_observation_list_.begin(), depth_observation_list_.end());
}
void ObservationBuffer::purgeStaleObservations()
{
if (!observation_list_.empty())
@@ -432,30 +252,6 @@ void ObservationBuffer::purgeStaleObservations()
}
}
void ObservationBuffer::purgeStaleDepthObservations()
{
if (depth_observation_list_.empty())
return;
if (observation_keep_time_ == robot::Duration(0.0))
{
auto observation = depth_observation_list_.begin();
depth_observation_list_.erase(++observation, depth_observation_list_.end());
return;
}
const robot::Time now = robot::Time::now();
for (auto observation = depth_observation_list_.begin(); observation != depth_observation_list_.end(); ++observation)
{
DepthCameraObservation& obs = *observation;
if ((last_updated_ - obs.data_->header.stamp) > observation_keep_time_)
{
depth_observation_list_.erase(observation, depth_observation_list_.end());
return;
}
}
}
bool ObservationBuffer::isCurrent() const
{
if (expected_update_rate_ == robot::Duration(0.0))