feat(depth): observed_volume depth clearing as deployed 2026-10-14

Baseline commit of the working-tree refactor built on 2026-10-14 17:24 and
running on the KIT robot, recorded in one commit before further changes.

- VoxelLayer clearing looks up TF at the depth image stamp.
- ObservationBuffer: per-source cloud preparation at the measurement stamp,
  height/range gate before the PCL VoxelGrid.
- DepthFramePairer: pairs the marking cloud with the clearing image by
  source and stamp, bounded queue and age.
- DepthVisibilityChecker and the observed_volume clearing mode in VoxelLayer;
  the legacy frustum mode stays the default.
- Tests: depth_observation_buffer_test, depth_visibility_test.
This commit is contained in:
2026-10-15 11:37:56 +07:00
parent a63c084002
commit 2ca0bcc1c1
15 changed files with 1336 additions and 19 deletions

View File

@@ -36,6 +36,7 @@
* David V. Lu!!
*********************************************************************/
#include <robot_costmap_2d/voxel_layer.h>
#include <robot_costmap_2d/depth_visibility.h>
#include <robot_sensor_msgs/point_cloud2_iterator.h>
#include <robot_tf3_geometry_msgs/tf3_geometry_msgs.h>
#include <robot_geometry_msgs/Vector3.h>
@@ -45,6 +46,7 @@
#include <cmath>
#include <cstdint>
#include <cstring>
#include <array>
#define VOXEL_BITS 16
@@ -71,6 +73,18 @@ void VoxelLayer::onInitialize()
VoxelLayer::~VoxelLayer()
{}
VoxelGrid VoxelLayer::voxelSnapshot()
{
VoxelGrid out;
out.header.frame_id = global_frame_;
out.header.stamp = robot::Time::now();
out.size_x = size_x_; out.size_y = size_y_; out.size_z = size_z_;
out.origin.x = origin_x_; out.origin.y = origin_y_; out.origin.z = origin_z_;
out.resolutions.x = out.resolutions.y = resolution_; out.resolutions.z = z_resolution_;
out.data.assign(robot_voxel_grid_.getData(), robot_voxel_grid_.getData() + size_x_ * size_y_);
return out;
}
bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandle &nh)
{
try
@@ -126,6 +140,16 @@ bool VoxelLayer::getParams(const std::string& config_file_name, robot::NodeHandl
if (nh.hasParam("combination_method"))
nh.getParam("combination_method", combination_method_);
if (!paired_depth_sources_.empty())
{
z_resolution_ = paired_depth_sources_.front()->config.voxel_z_resolution_m;
const auto& source = paired_depth_sources_.front();
if (!size_z_ || size_z_ > VOXEL_BITS || source->min_height < origin_z_ ||
source->max_height > origin_z_ + size_z_ * z_resolution_)
throw std::invalid_argument("observed_volume marking height band must fit its voxel grid");
robot::log_info("observed_volume grid: z_resolution=%.4f m bins=%u span=[%.3f, %.3f) m\n",
z_resolution_, size_z_, origin_z_, origin_z_ + size_z_ * z_resolution_);
}
this->matchSize();
}
catch (const YAML::BadFile& e) {
@@ -159,6 +183,7 @@ void VoxelLayer::reset()
void VoxelLayer::resetMaps()
{
resetPairedDepth();
Costmap2D::resetMaps();
robot_voxel_grid_.reset();
}
@@ -170,6 +195,13 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
updateOrigin(robot_x - getSizeInMetersX() / 2, robot_y - getSizeInMetersY() / 2);
useExtraBounds(min_x, min_y, max_x, max_y);
if (!paired_depth_sources_.empty() &&
(!static_marking_observations_.empty() || !static_clearing_observations_.empty()))
{
current_ = false;
robot::log_error_throttle(5.0, "observed_volume cannot share its voxel grid with static observations\n");
return;
}
bool current = true;
std::vector<Observation> observations, clearing_observations;
std::vector<DepthCameraObservation> depth_observations;
@@ -196,10 +228,17 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
raytraceFreespace(clearing_observations[i], min_x, min_y, max_x, max_y);
}
// place the new obstacles into a priority queue... each with a priority of zero to begin with
for (std::vector<Observation>::const_iterator it = observations.begin(); it != observations.end(); ++it)
{
const Observation& obs = *it;
for (const auto& observation : observations)
markObservation(observation, min_x, min_y, max_x, max_y);
processPairedDepth(min_x, min_y, max_x, max_y);
current_ = current_ && pairedDepthCurrent();
updateFootprint(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
}
void VoxelLayer::markObservation(const Observation& obs, double* min_x, double* min_y,
double* max_x, double* max_y)
{
const robot_sensor_msgs::PointCloud2& cloud = *(obs.cloud_);
@@ -245,8 +284,96 @@ void VoxelLayer::updateBounds(double robot_x, double robot_y, double robot_yaw,
touch(double(*iter_x), double(*iter_y), min_x, min_y, max_x, max_y);
}
}
}
void VoxelLayer::processPairedDepth(double* min_x, double* min_y, double* max_x, double* max_y)
{
for (const auto& source : paired_depth_sources_)
{
DepthFramePairer::Candidate pair;
while (source->pairer.next(pair))
{
tf3::TransformStampedMsg pose;
try
{
pose = tf_->lookupTransform(global_frame_, pair.cloud->header.frame_id,
data_convert::convertTime(pair.cloud->header.stamp));
}
catch (const tf3::TransformException& ex)
{
source->pairer.noteTfWait();
robot::log_error_throttle(5.0, "paired depth waiting for measurement TF: %s\n", ex.what());
break; // retain occupancy and bounded pending pair, never use latest TF
}
DepthVisibilityChecker checker;
if (!checker.prepare(pair.depth, pose, source->min_height,
std::min(source->max_height, max_obstacle_height_), source->config))
{
source->pairer.discard(pair.stamp);
robot::log_error_throttle(5.0, "paired depth rejected invalid image/model/pose\n");
continue;
}
std::vector<Observation> observations;
bool buffered;
{
std::lock_guard<ObservationBuffer> guard(*source->marking_buffer);
buffered = source->marking_buffer->tryBufferCloudWithTransform(*pair.cloud, pose);
if (buffered) source->marking_buffer->getObservations(observations);
}
if (!buffered || observations.size() != 1 ||
observations.front().cloud_->header.stamp.toNSec() != pair.stamp)
{
source->pairer.discard(pair.stamp);
continue; // no partial clear when marking branch cannot be prepared
}
// No occupied cache: the live voxel grid already contains rolling-window overlap.
// Plan all clears before applying this frame; both branches share exactly one pose.
std::vector<std::array<unsigned int, 3>> free_voxels;
for (unsigned int y = 0; y < size_y_; ++y)
for (unsigned int x = 0; x < size_x_; ++x)
{
if (!(robot_voxel_grid_.getData()[getIndex(x, y)] >> 16)) continue;
for (unsigned int z = 0; z < size_z_; ++z)
{
if (robot_voxel_grid_.getVoxel(x, y, z) != robot_voxel_grid::MARKED) continue;
const Eigen::Vector3d lower(origin_x_ + x * resolution_,
origin_y_ + y * resolution_, origin_z_ + z * z_resolution_);
const Eigen::Vector3d upper = lower + Eigen::Vector3d(resolution_, resolution_, z_resolution_);
const auto visibility = checker.classify(lower, upper);
if (visibility.state == DepthVisibility::Free)
{ free_voxels.push_back({x, y, z}); ++visibility_counts_.free; }
else if (visibility.state == DepthVisibility::Surface) ++visibility_counts_.surface;
else ++visibility_counts_.unknown;
}
}
for (const auto& voxel : free_voxels)
{
const auto x = voxel[0], y = voxel[1], z = voxel[2];
robot_voxel_grid_.clearVoxel(x, y, z);
const auto status = robot_voxel_grid_.getVoxelColumn(x, y, unknown_threshold_, mark_threshold_);
costmap_[getIndex(x, y)] = status == robot_voxel_grid::MARKED ? LETHAL_OBSTACLE :
status == robot_voxel_grid::FREE ? FREE_SPACE : NO_INFORMATION;
// Include whole cell so master/inflation can remove the former obstacle.
touch(origin_x_ + x * resolution_, origin_y_ + y * resolution_, min_x, min_y, max_x, max_y);
touch(origin_x_ + (x + 1) * resolution_, origin_y_ + (y + 1) * resolution_, min_x, min_y, max_x, max_y);
}
markObservation(observations.front(), min_x, min_y, max_x, max_y);
source->pairer.applied(pair.stamp);
++visibility_counts_.frames;
}
const auto counts = source->pairer.counts();
robot::log_info_throttle(5.0, "paired depth [%s]: applied=%llu tf_wait=%llu "
"invalid=%llu stale=%llu expired=%llu overflow=%llu duplicate=%llu; "
"voxel decisions free=%llu surface=%llu unknown=%llu (cumulative)\n",
name_.c_str(), static_cast<unsigned long long>(counts.applied),
static_cast<unsigned long long>(counts.tf_wait), static_cast<unsigned long long>(counts.invalid),
static_cast<unsigned long long>(counts.stale), static_cast<unsigned long long>(counts.expired),
static_cast<unsigned long long>(counts.overflow), static_cast<unsigned long long>(counts.duplicate),
static_cast<unsigned long long>(visibility_counts_.free),
static_cast<unsigned long long>(visibility_counts_.surface),
static_cast<unsigned long long>(visibility_counts_.unknown));
}
updateFootprint(robot_x, robot_y, robot_yaw, min_x, min_y, max_x, max_y);
}
void VoxelLayer::clearNonLethal(double wx, double wy, double w_size_x, double w_size_y, bool clear_no_info)
@@ -606,7 +733,13 @@ bool VoxelLayer::raytraceDepthFrustum(const DepthCameraObservation& observation,
tf3::TransformStampedMsg tfm;
try
{
tfm = tf_->lookupTransform(global_frame_, depth_frame, tf3::Time());
const robot::Time& depth_stamp = local_origin.header.stamp;
const tf3::Time query_time =
depth_stamp.isZero()
? tf3::Time()
: tf3::Time(depth_stamp.sec, depth_stamp.nsec);
tfm = tf_->lookupTransform(global_frame_, depth_frame, query_time);
tf3::doTransform(local_origin, global_origin, tfm);
}
catch (tf3::TransformException& ex)
@@ -977,8 +1110,10 @@ void VoxelLayer::updateOrigin(double new_origin_x, double new_origin_y)
cell_size_x, cell_size_x, cell_size_y);
}
// we'll reset our maps to unknown space if appropriate
resetMaps();
// Shift storage without resetting the paired input queue. A newly received
// measurement must still apply after the rolling origin moves this cycle.
Costmap2D::resetMaps();
robot_voxel_grid_.reset();
// update the origin with the appropriate world coordinates
origin_x_ = new_grid_ox;