feat(costmap_2d): direction zones and map readiness

- DirectionalLayer rewritten as a DirectionFieldProvider. It no longer
  derives from StaticLayer and never writes the master grid. It takes an
  OccupancyGrid of direction codes (0 = no constraint, 1..8 =
  direction_index + 1) on direction_topic, validates it against the
  master geometry and global frame, and publishes an immutable
  DirectionField snapshot with a DISABLED / PENDING / READY state and a
  generation counter. Every static map on map_topic returns the layer to
  PENDING until a grid for that map arrives.
- direction_field.h is the core API for planners: grid validation,
  bounds-checked codeAt(), a half-open-cell DDA (traverseGridSegment),
  allowedDirectionMask() and checkSegment().
  LayeredCostmap::getDirectionFieldSnapshot() exposes the snapshot
  without adding data members.
- max_deviation_deg (default 90, valid 0 < x < 180). The field carries
  the value so every planner uses the same rule; 90 keeps the previous
  masks bit for bit.
- StaticLayer ignores an OccupancyGrid addressed to another layer's
  topic instead of logging two unthrottled errors per push.
- StaticLayer and its subclasses report current only after updateCosts
  has written the newest map (markMapWritten, also used by
  CriticalLayer). Readiness flags are written under the master mutex;
  invalid partial updates are rejected with a throttled error.

Full history: branch feature/direction-zone.
This commit is contained in:
2026-11-04 13:24:16 +07:00
parent 3539afbfb4
commit 40488b7632
13 changed files with 1466 additions and 448 deletions

View File

@@ -65,10 +65,19 @@ StaticLayer::~StaticLayer()
void StaticLayer::onInitialize()
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
robot::NodeHandle nh("~");
robot::NodeHandle priv_nh(nh, name_);
current_ = true;
current_ = false;
if (map_received_)
{
// Reinitialization must request another master write even if the old map is retained.
has_updated_data_ = true;
x_ = y_ = 0;
width_ = size_x_;
height_ = size_y_;
}
global_frame_ = layered_costmap_->getGlobalFrameID();
std::string config_file_name = "static_layer_params.yaml";
@@ -159,6 +168,12 @@ void StaticLayer::matchSize()
// unrelated to the size of the layered costmap
if (!layered_costmap_->isRolling())
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
// resizeMap discards this layer's cached cells, even when the geometry is unchanged.
// A new map is required before the layer can write meaningful data again.
current_ = false;
map_received_ = false;
has_updated_data_ = false;
Costmap2D* master = layered_costmap_->getCostmap();
resizeMap(master->getSizeInCellsX(), master->getSizeInCellsY(), master->getResolution(),
master->getOriginX(), master->getOriginY());
@@ -198,9 +213,14 @@ void StaticLayer::handleImpl(const void* data,
if (type == typeid(robot_nav_msgs::OccupancyGrid) &&
(topic == map_topic_ || topic == "/" + map_topic_)) {
incomingMap(*static_cast<const robot_nav_msgs::OccupancyGrid*>(data));
} else if (type == typeid(robot_map_msgs::OccupancyGridUpdate) &&
} else if (type == typeid(robot_map_msgs::OccupancyGridUpdate) &&
(topic == map_topic_ + "_updates" || topic == "/" + map_topic_ + "_updates")) {
incomingUpdate(*static_cast<const robot_map_msgs::OccupancyGridUpdate*>(data));
} else if (type == typeid(robot_nav_msgs::OccupancyGrid) ||
type == typeid(robot_map_msgs::OccupancyGridUpdate)) {
// Right type, another layer's topic: the gateway sends every grid to every layer of this
// type (other StaticLayers, the direction grid). Normal traffic, not an error.
return;
} else {
std::string readable = boost::core::demangle(type.name());
@@ -209,7 +229,6 @@ void StaticLayer::handleImpl(const void* data,
{
readable = readable.substr(0, pos);
}
robot::log_error("[] con1: %x, con2: %x ", type == typeid(robot_nav_msgs::OccupancyGrid), (topic == map_topic_ || topic == "/" + map_topic_));
robot::log_error("[StaticLayer] Received data of unknown type: %s on topic: %s, map_topic_: %s\n", readable.c_str(), topic.c_str(), map_topic_.c_str());
// std::cout << "[StaticLayer] Unknown type: " << type.name() << " on topic: " << topic << std::endl;
}
@@ -217,8 +236,10 @@ void StaticLayer::handleImpl(const void* data,
void StaticLayer::incomingMap(const robot_nav_msgs::OccupancyGrid& new_map)
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
if(!map_shutdown_)
{
current_ = false;
std::cout << "[StaticLayer] Received new map!" << std::endl;
unsigned int size_x = new_map.info.width, size_y = new_map.info.height;
@@ -300,8 +321,25 @@ void StaticLayer::incomingMap(const robot_nav_msgs::OccupancyGrid& new_map)
void StaticLayer::incomingUpdate(const robot_map_msgs::OccupancyGridUpdate& update)
{
if(!map_update_shutdown_)
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
if(!map_update_shutdown_ && map_received_)
{
const bool outside = update.x < 0 || update.y < 0 || update.width == 0 || update.height == 0 ||
static_cast<unsigned int>(update.x) > size_x_ || static_cast<unsigned int>(update.y) > size_y_ ||
update.width > size_x_ - static_cast<unsigned int>(update.x) ||
update.height > size_y_ - static_cast<unsigned int>(update.y);
if (outside || update.data.size() != static_cast<std::size_t>(update.width) * update.height)
{
const auto now = std::chrono::steady_clock::now();
if (last_invalid_update_log_ == std::chrono::steady_clock::time_point{} ||
std::chrono::duration<double>(now - last_invalid_update_log_).count() >= 5.0)
{
robot::log_error("StaticLayer %s: ignoring an invalid map update", name_.c_str());
last_invalid_update_log_ = now;
}
return;
}
current_ = false;
robot::log_info("Update new map!\n");
unsigned int di = 0;
for (unsigned int y = 0; y < update.height ; y++)
@@ -313,16 +351,34 @@ void StaticLayer::incomingUpdate(const robot_map_msgs::OccupancyGridUpdate& upda
costmap_[index] = interpretValue(update.data[di++]);
}
}
x_ = update.x;
y_ = update.y;
width_ = update.width;
height_ = update.height;
if (has_updated_data_)
{
// A full map or another update may still be awaiting updateBounds(). Preserve all dirty
// cells; otherwise a partial update could make the layer current while part of the new map
// has never reached the master.
const unsigned int update_x = static_cast<unsigned int>(update.x);
const unsigned int update_y = static_cast<unsigned int>(update.y);
const unsigned int max_x = std::max(x_ + width_, update_x + update.width);
const unsigned int max_y = std::max(y_ + height_, update_y + update.height);
x_ = std::min(x_, update_x);
y_ = std::min(y_, update_y);
width_ = max_x - x_;
height_ = max_y - y_;
}
else
{
x_ = update.x;
y_ = update.y;
width_ = update.width;
height_ = update.height;
}
has_updated_data_ = true;
}
}
void StaticLayer::activate()
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
map_shutdown_ = false;
map_update_shutdown_ = false;
onInitialize();
@@ -330,6 +386,7 @@ void StaticLayer::activate()
void StaticLayer::deactivate()
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
map_shutdown_ = true;
if (subscribe_to_updates_)
map_update_shutdown_ = true;
@@ -337,11 +394,16 @@ void StaticLayer::deactivate()
void StaticLayer::reset()
{
boost::unique_lock<Costmap2D::mutex_t> lock(*layered_costmap_->getCostmap()->getMutex());
if (first_map_only_)
{
map_shutdown_ = false;
map_update_shutdown_ = false;
current_ = false;
has_updated_data_ = true;
x_ = y_ = 0;
width_ = size_x_;
height_ = size_y_;
}
else
{
@@ -428,6 +490,13 @@ void StaticLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int min_
}
}
}
markMapWritten(min_i, min_j, max_i, max_j);
}
void StaticLayer::markMapWritten(int min_i, int min_j, int max_i, int max_j)
{
if (map_received_ && min_i < max_i && min_j < max_j)
current_ = true;
}
// Export factory function