feat(costmap_2d): background reference graph, shared keys under root:

- ReferenceGraph / ReferenceGraphWorker: Voronoi ridges and one-way lanes are
  computed on a low-priority thread when the map changes (reference_graph_*
  keys, off with rolling_window), with unit tests.
- Shared costmap keys can live in a root: map or flat at the top of the YAML;
  costmap_common_root_test covers both layouts.
- Static and directional layers report map changes to the layered costmap.
This commit is contained in:
2026-11-10 16:37:55 +07:00
parent f5d190a3d0
commit 5034029c6e
25 changed files with 3445 additions and 21 deletions

View File

@@ -37,7 +37,10 @@
*********************************************************************/
#include <robot_costmap_2d/layered_costmap.h>
#include <robot_costmap_2d/footprint.h>
#include <robot_costmap_2d/reference_graph_worker.h>
#include <cstdio>
#include <exception>
#include <utility>
#include <string>
#include <algorithm>
#include <vector>
@@ -186,6 +189,8 @@ namespace robot_costmap_2d
LayeredCostmap::~LayeredCostmap()
{
// The worker reads the plugins, so it stops first.
reference_graph_worker_.reset();
while (plugins_.size() > 0)
{
plugins_.pop_back();
@@ -385,6 +390,88 @@ namespace robot_costmap_2d
return result;
}
bool LayeredCostmap::enableReferenceGraph(const ReferenceGraphParams& params)
{
if (rolling_window_)
{
robot::log_error("Reference graph needs a static map; it cannot run on a rolling costmap\n");
return false;
}
if (reference_graph_worker_)
{
robot::log_error("Reference graph is already enabled\n");
return false;
}
ReferenceGraphWorker::Source source;
source.capture = [this](bool with_grid, ReferenceGraphInput* input) {
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap_.getMutex());
input->geometry = {costmap_.getSizeInCellsX(), costmap_.getSizeInCellsY(), costmap_.getResolution(),
costmap_.getOriginX(), costmap_.getOriginY(), global_frame_};
input->footprint = footprint_;
if (!with_grid)
return ReferenceCaptureStatus::kReady;
// Copy under the lock and merge outside it: a large map must not stall the costmap update.
std::vector<const Costmap2D*> grids;
for (const auto& plugin : plugins_)
{
if (!plugin->isEnabled() || plugin->getType() != LayerType::STATIC_LAYER)
continue;
const auto* grid = dynamic_cast<const Costmap2D*>(plugin.get());
if (grid == nullptr || !plugin->isCurrent() || grid->getSizeInCellsX() != costmap_.getSizeInCellsX() ||
grid->getSizeInCellsY() != costmap_.getSizeInCellsY())
return ReferenceCaptureStatus::kWait;
grids.push_back(grid);
}
if (grids.empty())
return ReferenceCaptureStatus::kNoStaticLayer;
const std::size_t cells = static_cast<std::size_t>(costmap_.getSizeInCellsX()) * costmap_.getSizeInCellsY();
if (cells == 0)
return ReferenceCaptureStatus::kWait;
input->static_layers.reserve(grids.size());
for (const Costmap2D* grid : grids)
input->static_layers.emplace_back(grid->getCharMap(), grid->getCharMap() + cells);
return ReferenceCaptureStatus::kReady;
};
source.direction = [this]() { return getDirectionFieldSnapshot(); };
try
{
reference_graph_worker_.reset(new ReferenceGraphWorker(std::move(source), params));
}
catch (const std::exception& error)
{
robot::log_error("Reference graph not started: %s\n", error.what());
return false;
}
return true;
}
ReferenceGraphSnapshot LayeredCostmap::getReferenceGraphSnapshot() const
{
return reference_graph_worker_ ? reference_graph_worker_->snapshot() : ReferenceGraphSnapshot{};
}
static_assert(static_cast<std::size_t>(LayerType::DEPTH_CAMERA_LAYER) < 16, "grow kMaxLayerTypes");
uint64_t LayeredCostmap::getStaticMapRevision(LayerType type) const
{
return static_map_revisions_[static_cast<std::size_t>(type)].load();
}
void LayeredCostmap::notifyStaticMapChanged(LayerType type)
{
++static_map_revisions_[static_cast<std::size_t>(type)];
if (type == LayerType::STATIC_LAYER && reference_graph_worker_)
reference_graph_worker_->notifyStaticChanged();
}
void LayeredCostmap::notifyDirectionFieldChanged()
{
if (reference_graph_worker_)
reference_graph_worker_->notifyDirectionChanged();
}
bool LayeredCostmap::isCurrent()
{
current_ = true;
@@ -399,7 +486,16 @@ namespace robot_costmap_2d
void LayeredCostmap::setFootprint(const std::vector<robot_geometry_msgs::Point> &footprint_spec)
{
footprint_ = footprint_spec;
bool changed = footprint_.size() != footprint_spec.size();
for (std::size_t i = 0; !changed && i < footprint_.size(); ++i)
changed = footprint_[i].x != footprint_spec[i].x || footprint_[i].y != footprint_spec[i].y;
{
// The reference graph thread copies the footprint under this lock.
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap_.getMutex());
footprint_ = footprint_spec;
}
if (changed && reference_graph_worker_)
reference_graph_worker_->notifyFootprintChanged();
robot_costmap_2d::calculateMinAndMaxDistances(footprint_spec, inscribed_radius_, circumscribed_radius_);
for (vector<boost::shared_ptr<Layer>>::iterator plugin = plugins_.begin(); plugin != plugins_.end();