#pragma once #include #include #include #include #include #include #include #include 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 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(cloud); expire(); } void addDepth(robot_sensor_msgs::DepthCameraData::ConstPtr depth) { std::lock_guard 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 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 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 lock(mutex_); pending_.erase(stamp); ++counts_.invalid; } void noteTfWait() { std::lock_guard lock(mutex_); ++counts_.tf_wait; } bool isCurrent() { std::lock_guard 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 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 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(it->first) * 1e-9; if (age > config_.observation_max_age_s || age < -.05) { ++counts_.stale; it = pending_.erase(it); } else if (std::chrono::duration(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 pending_; Counts counts_; mutable std::mutex mutex_; }; } // namespace robot_costmap_2d