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

@@ -0,0 +1,166 @@
#pragma once
#include <robot_costmap_2d/observation.h>
#include <boost/make_shared.hpp>
#include <chrono>
#include <map>
#include <mutex>
#include <cmath>
#include <stdexcept>
#include <algorithm>
namespace robot_costmap_2d
{
// A source pair owns immutable measurement messages; the update thread alone
// acknowledges applied frames. Queue timeout/freshness never expires occupancy.
class DepthFramePairer
{
public:
using Clock = std::chrono::steady_clock;
struct Candidate
{
uint64_t stamp{0};
robot_sensor_msgs::PointCloud2::ConstPtr cloud;
robot_sensor_msgs::DepthCameraData::ConstPtr depth;
};
struct Counts
{
uint64_t applied{0}, expired{0}, overflow{0}, stale{0}, duplicate{0}, invalid{0}, tf_wait{0};
};
explicit DepthFramePairer(const DepthFrustumConfig& config, double expected_period)
: config_(config), expected_period_(expected_period), started_(robot::Time::now()), last_clock_(started_)
{
if (!std::isfinite(expected_period_) || expected_period_ < 0 ||
!config_.pair_queue_size || !std::isfinite(config_.pair_max_wait_s) || config_.pair_max_wait_s <= 0 ||
!std::isfinite(config_.observation_max_age_s) || config_.observation_max_age_s <= 0)
throw std::invalid_argument("invalid depth pair queue/age configuration");
}
void addCloud(const robot_sensor_msgs::PointCloud2& cloud)
{
std::lock_guard<std::mutex> lock(mutex_);
observeClock();
if (cloud.header.stamp.isZero()) { ++counts_.invalid; return; }
const auto key = cloud.header.stamp.toNSec();
if (key <= applied_stamp_) { ++counts_.duplicate; return; }
auto& p = pending_[key];
if (p.cloud) { ++counts_.duplicate; return; }
if (!p.depth) p.arrival = Clock::now();
p.cloud = boost::make_shared<robot_sensor_msgs::PointCloud2>(cloud);
expire();
}
void addDepth(robot_sensor_msgs::DepthCameraData::ConstPtr depth)
{
std::lock_guard<std::mutex> lock(mutex_);
observeClock();
if (!depth || depth->depth.header.stamp.isZero()) { ++counts_.invalid; return; }
const auto key = depth->depth.header.stamp.toNSec();
if (key <= applied_stamp_) { ++counts_.duplicate; return; }
auto& p = pending_[key];
if (p.depth) { ++counts_.duplicate; return; }
if (!p.cloud) p.arrival = Clock::now();
p.depth = std::move(depth);
expire();
}
bool next(Candidate& out)
{
std::lock_guard<std::mutex> lock(mutex_);
observeClock(); expire();
for (auto it = pending_.begin(); it != pending_.end();)
{
auto& p = it->second;
if (!p.cloud || !p.depth) { ++it; continue; }
if (p.cloud->header.frame_id != p.depth->depth.header.frame_id ||
p.depth->depth.header.stamp != p.depth->camera_info.header.stamp ||
p.depth->depth.header.stamp != p.depth->header.stamp)
{
++counts_.invalid;
it = pending_.erase(it);
continue;
}
out = {it->first, p.cloud, p.depth};
return true;
}
return false;
}
void applied(uint64_t stamp)
{
std::lock_guard<std::mutex> lock(mutex_);
applied_stamp_ = std::max(applied_stamp_, stamp);
last_applied_ = robot::Time::now();
++counts_.applied;
for (auto it = pending_.begin(); it != pending_.end() && it->first <= applied_stamp_;)
it = pending_.erase(it);
}
void discard(uint64_t stamp)
{
std::lock_guard<std::mutex> lock(mutex_);
pending_.erase(stamp); ++counts_.invalid;
}
void noteTfWait()
{
std::lock_guard<std::mutex> lock(mutex_); ++counts_.tf_wait;
}
bool isCurrent()
{
std::lock_guard<std::mutex> lock(mutex_);
observeClock();
const auto last = last_applied_.isZero() ? started_ : last_applied_;
return expected_period_ <= 0 || (robot::Time::now() - last).toSec() <= expected_period_;
}
void reset()
{
std::lock_guard<std::mutex> lock(mutex_);
const auto watermark = applied_stamp_;
resetUnlocked(robot::Time::now());
applied_stamp_ = watermark; // map resets must not replay a cached old frame
}
Counts counts() const
{
std::lock_guard<std::mutex> lock(mutex_); return counts_;
}
private:
struct Pending
{
robot_sensor_msgs::PointCloud2::ConstPtr cloud;
robot_sensor_msgs::DepthCameraData::ConstPtr depth;
Clock::time_point arrival;
};
void resetUnlocked(const robot::Time& now)
{
pending_.clear(); applied_stamp_ = 0; last_applied_ = robot::Time();
started_ = now; last_clock_ = now;
}
void observeClock()
{
const auto now = robot::Time::now();
if (now < last_clock_) resetUnlocked(now);
last_clock_ = now;
}
void expire()
{
const auto now = Clock::now();
const double measurement_now = robot::Time::now().toSec();
for (auto it = pending_.begin(); it != pending_.end();)
{
const double age = measurement_now - static_cast<double>(it->first) * 1e-9;
if (age > config_.observation_max_age_s || age < -.05)
{ ++counts_.stale; it = pending_.erase(it); }
else if (std::chrono::duration<double>(now - it->second.arrival).count() > config_.pair_max_wait_s)
{ ++counts_.expired; it = pending_.erase(it); }
else ++it;
}
while (pending_.size() > config_.pair_queue_size)
{ pending_.erase(pending_.begin()); ++counts_.overflow; }
}
DepthFrustumConfig config_;
double expected_period_;
robot::Time started_, last_clock_, last_applied_;
uint64_t applied_stamp_{0};
std::map<uint64_t, Pending> pending_;
Counts counts_;
mutable std::mutex mutex_;
};
} // namespace robot_costmap_2d

View File

@@ -0,0 +1,35 @@
#pragma once
#include <robot_costmap_2d/observation.h>
#include <tf3/compat.h>
#include <Eigen/Geometry>
#include <vector>
namespace robot_costmap_2d
{
enum class DepthVisibility { Free, Unknown, Surface };
enum class DepthVisibilityReason { Free, OutsideView, InvalidDepth, Surface, EmptyRegion };
struct DepthVisibilityResult
{
DepthVisibility state{DepthVisibility::Unknown};
DepthVisibilityReason reason{DepthVisibilityReason::OutsideView};
};
// Pure geometry/measurement helper. It never mutates occupancy or queries TF.
class DepthVisibilityChecker
{
public:
bool prepare(robot_sensor_msgs::DepthCameraData::ConstPtr data,
const tf3::TransformStampedMsg& global_from_camera,
double min_height, double max_height, const DepthFrustumConfig& config);
DepthVisibilityResult classify(const Eigen::Vector3d& lower,
const Eigen::Vector3d& upper) const;
private:
robot_sensor_msgs::DepthCameraData::ConstPtr data_;
Eigen::Isometry3d camera_from_global_{Eigen::Isometry3d::Identity()};
double min_height_{0}, max_height_{0};
DepthFrustumConfig config_;
unsigned int bytes_per_pixel_{0};
};
} // namespace robot_costmap_2d

View File

@@ -81,6 +81,17 @@ struct DepthFrustumConfig
/// stereo no-disparity strip sits on the right instead of the left. Measured
/// from the last image column inward. 0 disables.
unsigned int clear_right_border_px = 0;
// Opt-in observed-volume mode. Legacy range/column keys are not used by it.
bool observed_volume_clearing = false;
double voxel_z_resolution_m = 0.0625; // depth-only grid: low objects must not occupy an entire 20 cm slab
std::string paired_marking_source;
unsigned int pair_queue_size = 4;
double pair_max_wait_s = 0.25;
double observation_max_age_s = 0.5;
double valid_min_z_m = 0.1;
double valid_max_z_m = 3.0;
double free_margin_m = 0.02;
unsigned int projection_margin_px = 1;
};
class DepthCameraObservation

View File

@@ -50,6 +50,14 @@
namespace robot_costmap_2d
{
// Opt-in per source: legacy callers keep their TF and cloud policy.
struct CloudProcessingConfig
{
bool transform_at_stamp{false};
bool voxel_filter_enabled{false};
double voxel_leaf_size{0.05}; // [m], applied after height/range gates
};
/**
* @class ObservationBuffer
* @brief Takes in point clouds from sensors, transforms them to the desired frame, and stores them
@@ -104,6 +112,14 @@ public:
*/
void bufferCloud(const robot_sensor_msgs::PointCloud2& cloud);
// Configure before receiving samples. Empty valid clouds return true;
// missing TF/malformed observations return false and do not refresh the buffer.
void setCloudProcessingConfig(const CloudProcessingConfig& config);
bool tryBufferCloud(const robot_sensor_msgs::PointCloud2& cloud);
bool tryBufferCloudWithTransform(const robot_sensor_msgs::PointCloud2& cloud,
const tf3::TransformStampedMsg& transform);
const std::string& sensorFrame() const { return sensor_frame_; }
/**
* @brief Store the newest depth frame without converting it to PointCloud2.
*/
@@ -156,6 +172,8 @@ private:
void purgeStaleObservations();
void purgeStaleDepthObservations();
bool tryBufferCloudImpl(const robot_sensor_msgs::PointCloud2& cloud,
const tf3::TransformStampedMsg* prepared_transform);
tf3::BufferCore& tf3_buffer_;
const robot::Duration observation_keep_time_;
@@ -172,6 +190,7 @@ private:
double obstacle_range_, raytrace_range_;
double tf_tolerance_;
DepthFrustumConfig frustum_config_;
CloudProcessingConfig cloud_processing_config_;
};
} // namespace robot_costmap_2d
#endif // ROBOT_COSTMAP_2D_OBSERVATION_BUFFER_H_

View File

@@ -42,6 +42,7 @@
#include <robot_costmap_2d/costmap_layer.h>
#include <robot_costmap_2d/layered_costmap.h>
#include <robot_costmap_2d/observation_buffer.h>
#include <robot_costmap_2d/depth_frame_pairer.h>
#include <robot_costmap_2d/footprint.h>
#include <robot_nav_msgs/OccupancyGrid.h>
@@ -202,6 +203,20 @@ protected:
mutable std::mutex depth_camera_data_mutex_;
robot_sensor_msgs::DepthCameraData::ConstPtr pending_depth_camera_data_;
struct PairedDepthSource
{
PairedDepthSource(const DepthFrustumConfig& config, double expected_period)
: config(config), pairer(config, expected_period) {}
std::string cloud_topic, depth_topic;
boost::shared_ptr<ObservationBuffer> marking_buffer, depth_buffer;
double min_height{0}, max_height{0};
DepthFrustumConfig config;
DepthFramePairer pairer;
};
std::vector<boost::shared_ptr<PairedDepthSource>> paired_depth_sources_;
bool pairedDepthCurrent() const;
void resetPairedDepth();
private:
bool getParams(const std::string& config_file_name, robot::NodeHandle &nh);
};

View File

@@ -73,6 +73,8 @@ public:
double* max_x, double* max_y);
void updateOrigin(double new_origin_x, double new_origin_y);
// Diagnostic snapshot; caller must hold the layer/update lock, as with getCharMap().
VoxelGrid voxelSnapshot();
bool isDiscretized()
{
return true;
@@ -109,6 +111,13 @@ private:
double min_range, double max_range, double skip_dist,
double* min_x, double* min_y, double* max_x, double* max_y);
bool clipColumnSegment(double& sx, double& sy, double& ex, double& ey) const;
void markObservation(const Observation& observation, double* min_x, double* min_y,
double* max_x, double* max_y);
void processPairedDepth(double* min_x, double* min_y, double* max_x, double* max_y);
struct VisibilityCounts
{
uint64_t free{0}, unknown{0}, surface{0}, frames{0};
} visibility_counts_;
bool publish_voxel_;