feat(static_layer): report current only after the map reaches the master

current_ is false before the first map, after every new map, update or
resize, and becomes true only once updateCosts has written the map
(shared markMapWritten helper, also called by CriticalLayer). Readiness
flags are written under the master mutex so the planner can trust
isCurrent() taken under the same lock. Invalid partial updates are
rejected with a throttled error.
This commit is contained in:
2026-11-04 11:14:15 +07:00
parent 52089a0e90
commit 7c6d070bd5
4 changed files with 163 additions and 9 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());
@@ -221,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;
@@ -304,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++)
@@ -317,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();
@@ -334,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;
@@ -341,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
{
@@ -432,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