#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace robot_costmap_2d { namespace { constexpr auto kPendingErrorDelay = std::chrono::seconds(5); constexpr auto kPendingErrorPeriod = std::chrono::seconds(5); bool topicMatches(const std::string& topic, const std::string& configured) { return topic == configured || topic == "/" + configured; } const char* stateName(DirectionFieldState state) { switch (state) { case DirectionFieldState::kDisabled: return "DISABLED"; case DirectionFieldState::kPending: return "PENDING"; case DirectionFieldState::kReady: return "READY"; } return "unknown"; } } // namespace DirectionalLayer::DirectionalLayer() : Layer(), mutex_(), snapshot_(), seen_map_(false), debug_state_(true), rolling_(false), map_topic_("map"), direction_topic_("direction_map"), pending_since_(), last_pending_log_() { } void DirectionalLayer::onInitialize() { robot::NodeHandle nh("~"); robot::NodeHandle priv_nh(nh, name_); const std::string filename = "directional_layer_params.yaml"; try { const char* env = std::getenv("PNKX_NAV_CORE_CONFIG_DIR"); const std::string folder = env && std::filesystem::exists(env) ? env : ""; const YAML::Node layer = YAML::LoadFile(getSourceFile(folder, filename))["directional_layer"]; enabled_ = loadParam(layer, "enabled", false); map_topic_ = loadParam(layer, "map_topic", std::string("map")); direction_topic_ = loadParam(layer, "direction_topic", std::string("direction_map")); debug_state_ = loadParam(layer, "debug_state", true); if (priv_nh.hasParam("enabled")) priv_nh.getParam("enabled", enabled_, enabled_); if (priv_nh.hasParam("map_topic")) priv_nh.getParam("map_topic", map_topic_); if (priv_nh.hasParam("direction_topic")) priv_nh.getParam("direction_topic", direction_topic_); if (priv_nh.hasParam("debug_state")) priv_nh.getParam("debug_state", debug_state_); } catch (const YAML::BadFile&) { throw std::runtime_error("DirectionalLayer " + name_ + ": cannot open the default file " + filename); } rolling_ = layered_costmap_->isRolling(); if (enabled_ && rolling_) robot::log_error("DirectionalLayer %s: rolling costmap is unsupported", name_.c_str()); if (enabled_ && (map_topic_.empty() || direction_topic_.empty() || topicMatches(map_topic_, direction_topic_) || topicMatches(direction_topic_, map_topic_))) throw std::runtime_error("DirectionalLayer " + name_ + ": topics must be nonempty and distinct"); robot::log_info("DirectionalLayer %s: enabled=%d map_topic=%s direction_topic=%s debug_state=%d", name_.c_str(), enabled_, map_topic_.c_str(), direction_topic_.c_str(), debug_state_); std::lock_guard lock(mutex_); publishLocked(enabled_ ? DirectionFieldState::kPending : DirectionFieldState::kDisabled); } void DirectionalLayer::publishLocked(DirectionFieldState state, std::shared_ptr field) { snapshot_.state = state; snapshot_.field = std::move(field); ++snapshot_.generation; // Layer::isCurrent() reads this non-atomic bool without this mutex (legacy API limitation). current_ = state != DirectionFieldState::kPending; if (state == DirectionFieldState::kPending) { pending_since_ = std::chrono::steady_clock::now(); last_pending_log_ = {}; } if (debug_state_) robot::log_info("[debug] DirectionalLayer %s: %s generation=%llu", name_.c_str(), stateName(state), static_cast(snapshot_.generation)); } DirectionFieldSnapshot DirectionalLayer::directionSnapshot() const { std::lock_guard lock(mutex_); return snapshot_; } DirectionGeometry DirectionalLayer::geometry() const { const auto* master = layered_costmap_->getCostmap(); return {master->getSizeInCellsX(), master->getSizeInCellsY(), master->getResolution(), master->getOriginX(), master->getOriginY(), layered_costmap_->getGlobalFrameID()}; } void DirectionalLayer::handleImpl(const void* data, const std::type_info& type, const std::string& topic) { if (!enabled_ || type != typeid(robot_nav_msgs::OccupancyGrid) || (!topicMatches(topic, map_topic_) && !topicMatches(topic, direction_topic_))) return; if (topicMatches(topic, map_topic_)) { std::lock_guard lock(mutex_); seen_map_ = true; publishLocked(DirectionFieldState::kPending); return; } DirectionGeometry expected; uint64_t generation; { boost::unique_lock master_lock(*layered_costmap_->getCostmap()->getMutex()); std::lock_guard lock(mutex_); if (rolling_) return; if (!seen_map_) { robot::log_error("DirectionalLayer %s: direction grid arrived before static map", name_.c_str()); return; } expected = geometry(); generation = snapshot_.generation; } DirectionValidationResult validation; auto field = DirectionField::create(*static_cast(data), expected, &validation); if (!field) { robot::log_error("DirectionalLayer %s: invalid direction grid: %s", name_.c_str(), validation.message.c_str()); return; } // resizeMap holds the master mutex through matchSize(), which increments generation. std::lock_guard lock(mutex_); if (snapshot_.generation != generation) { robot::log_error("DirectionalLayer %s: grid discarded because map state changed during validation", name_.c_str()); return; } publishLocked(DirectionFieldState::kReady, std::move(field)); } void DirectionalLayer::matchSize() { std::lock_guard lock(mutex_); if (enabled_) publishLocked(DirectionFieldState::kPending); } void DirectionalLayer::reset() { boost::unique_lock master_lock(*layered_costmap_->getCostmap()->getMutex()); std::lock_guard lock(mutex_); if (!enabled_ || snapshot_.state != DirectionFieldState::kReady) return; const auto now = geometry(); const auto& old = snapshot_.field->geometry(); if (old.width != now.width || old.height != now.height || old.frame_id != now.frame_id || !std::isfinite(now.resolution) || !std::isfinite(now.origin_x) || !std::isfinite(now.origin_y) || std::abs(old.resolution - now.resolution) > 1e-6 || std::abs(old.origin_x - now.origin_x) > 1e-6 || std::abs(old.origin_y - now.origin_y) > 1e-6) publishLocked(DirectionFieldState::kPending); } void DirectionalLayer::updateBounds(double, double, double, double*, double*, double*, double*) { std::lock_guard lock(mutex_); if (snapshot_.state != DirectionFieldState::kPending) return; const auto now = std::chrono::steady_clock::now(); if (now - pending_since_ >= kPendingErrorDelay && (last_pending_log_ == std::chrono::steady_clock::time_point() || now - last_pending_log_ >= kPendingErrorPeriod)) { robot::log_error("DirectionalLayer %s: PENDING for %.1f seconds", name_.c_str(), std::chrono::duration(now - pending_since_).count()); last_pending_log_ = now; } } } // namespace robot_costmap_2d boost::shared_ptr create_directional_plugin() { return boost::shared_ptr(new robot_costmap_2d::DirectionalLayer()); } BOOST_DLL_ALIAS(create_directional_plugin, DirectionalLayer)