diff --git a/include/robot_costmap_2d/static_layer.h b/include/robot_costmap_2d/static_layer.h index b766883..c056c15 100755 --- a/include/robot_costmap_2d/static_layer.h +++ b/include/robot_costmap_2d/static_layer.h @@ -43,6 +43,7 @@ #include #include +#include #include namespace robot_costmap_2d @@ -76,14 +77,16 @@ protected: virtual unsigned char interpretValue(unsigned char value); virtual void incomingMap(const robot_nav_msgs::OccupancyGrid& new_map); virtual void incomingUpdate(const robot_map_msgs::OccupancyGridUpdate& update); + /// Called under the master costmap mutex after a nonempty region has been written. + void markMapWritten(int min_i, int min_j, int max_i, int max_j); unsigned char* threshold_; std::string base_frame_id_; std::string global_frame_; ///< @brief The global frame for the costmap std::string map_frame_; /// @brief frame that map is located in std::string map_topic_; // Hiep thêm vào mục đich phân biết zones bool subscribe_to_updates_; - bool map_received_; - bool has_updated_data_; + bool map_received_ = false; + bool has_updated_data_ = false; unsigned int x_, y_, width_, height_; bool track_unknown_space_; bool use_maximum_; @@ -91,6 +94,7 @@ protected: bool trinary_costmap_; bool map_shutdown_ = false; bool map_update_shutdown_ = false; + std::chrono::steady_clock::time_point last_invalid_update_log_; unsigned char lethal_threshold_, unknown_cost_value_; private: diff --git a/plugins/critical_layer.cpp b/plugins/critical_layer.cpp index 1b1bcf2..1d6427d 100644 --- a/plugins/critical_layer.cpp +++ b/plugins/critical_layer.cpp @@ -30,6 +30,7 @@ void CriticalLayer::updateCosts(robot_costmap_2d::Costmap2D& master_grid, int mi StaticLayer::CostmapLayer::updateWithOverwrite(master_grid, min_i, min_j, max_i, max_j); else updateWithMax(master_grid, min_i, min_j, max_i, max_j); + markMapWritten(min_i, min_j, max_i, max_j); } @@ -42,4 +43,4 @@ static boost::shared_ptr create_critical_plugin() { BOOST_DLL_ALIAS(create_critical_plugin, CriticalLayer) -} \ No newline at end of file +} diff --git a/plugins/static_layer.cpp b/plugins/static_layer.cpp index 520dca6..69352d2 100755 --- a/plugins/static_layer.cpp +++ b/plugins/static_layer.cpp @@ -65,10 +65,19 @@ StaticLayer::~StaticLayer() void StaticLayer::onInitialize() { + boost::unique_lock 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 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 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 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(update.x) > size_x_ || static_cast(update.y) > size_y_ || + update.width > size_x_ - static_cast(update.x) || + update.height > size_y_ - static_cast(update.y); + if (outside || update.data.size() != static_cast(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(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(update.x); + const unsigned int update_y = static_cast(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 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 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 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 diff --git a/test/directional_layer_test.cpp b/test/directional_layer_test.cpp index 00360be..d1be6a9 100644 --- a/test/directional_layer_test.cpp +++ b/test/directional_layer_test.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include @@ -42,6 +44,9 @@ protected: std::filesystem::create_directory(directory_ + "/config"); ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", directory_.c_str(), 1), 0); config(true); + std::ofstream static_config(directory_ + "/config/static_layer_params.yaml"); + static_config << "static_layer:\n enabled: true\n map_topic: map\n" + " first_map_only: false\n subscribe_to_updates: true\n"; } void TearDown() override @@ -73,6 +78,85 @@ protected: } }; +TEST_F(LayerTest, StaticAndCriticalReadinessTracksWrites) +{ + for (bool critical : {false, true}) + { + LayeredCostmap master("map", false, false); + master.resizeMap(4, 4, 1, 0, 0); + boost::shared_ptr layer = critical + ? boost::static_pointer_cast(boost::make_shared()) + : boost::static_pointer_cast(boost::make_shared()); + layer->initialize(&master, critical ? "critical" : "static", &tf_); + master.addPlugin(layer); + EXPECT_FALSE(layer->isCurrent()); + EXPECT_FALSE(master.isCurrent()); + + robot_map_msgs::OccupancyGridUpdate early_update; + early_update.x = early_update.y = 0; + early_update.width = early_update.height = 1; + early_update.data.assign(1, 100); + layer->dataCallBack(early_update, "map_updates"); + EXPECT_FALSE(layer->isCurrent()); + + auto map = grid(); + map.data[0] = 100; + layer->dataCallBack(map, "map"); + EXPECT_FALSE(layer->isCurrent()); + layer->updateCosts(*master.getCostmap(), 0, 0, 0, 0); + EXPECT_FALSE(layer->isCurrent()); + master.updateMap(0, 0, 0); + EXPECT_TRUE(layer->isCurrent()); + + layer->reset(); + EXPECT_FALSE(layer->isCurrent()); + master.updateMap(0, 0, 0); + EXPECT_TRUE(layer->isCurrent()); + EXPECT_TRUE(master.isCurrent()); + EXPECT_NE(master.getCostmap()->getCost(0, 0), FREE_SPACE); + + map.data[1] = 100; + map.data[15] = 100; + layer->dataCallBack(map, "map"); + EXPECT_FALSE(layer->isCurrent()); + + robot_map_msgs::OccupancyGridUpdate update; + update.x = 0; + update.y = 0; + update.width = 1; + update.height = 1; + update.data.assign(1, 100); + layer->dataCallBack(update, "map_updates"); + EXPECT_FALSE(layer->isCurrent()); + master.updateMap(0, 0, 0); + EXPECT_TRUE(layer->isCurrent()); + EXPECT_EQ(master.getCostmap()->getCost(3, 3), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE); + + update.x = 0; + update.y = 3; + layer->dataCallBack(update, "map_updates"); + update.x = 3; + update.y = 0; + layer->dataCallBack(update, "map_updates"); + EXPECT_FALSE(layer->isCurrent()); + master.updateMap(0, 0, 0); + EXPECT_TRUE(layer->isCurrent()); + EXPECT_EQ(master.getCostmap()->getCost(0, 3), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE); + EXPECT_EQ(master.getCostmap()->getCost(3, 0), critical ? CRITICAL_SPACE : LETHAL_OBSTACLE); + + update.x = -1; + layer->dataCallBack(update, "map_updates"); + EXPECT_TRUE(layer->isCurrent()); + + master.resizeMap(4, 4, 1, 0, 0); + EXPECT_FALSE(layer->isCurrent()); + layer->dataCallBack(map, "map"); + EXPECT_FALSE(layer->isCurrent()); + master.updateMap(0, 0, 0); + EXPECT_TRUE(layer->isCurrent()); + } +} + TEST_F(LayerTest, InvalidMaxDeviationThrows) { for (double degrees : {0.0, 180.0, -5.0, 200.0})