#ifndef ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_ #define ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_ #include #include #include #include #include namespace robot_costmap_2d { /** @brief Publish immutable direction snapshots without modifying master costs. * The host thread feeds map and direction messages through dataCallBack(); the * costmap update thread calls updateBounds()/updateCosts(); planner threads use * directionSnapshot() and may retain a snapshot for an entire search. * When both mutexes are required, acquire the master costmap mutex before the * layer mutex, never the reverse. Snapshot readers hold only the layer mutex * briefly. Layer::isCurrent() still reads the legacy non-atomic current_ flag. */ class DirectionalLayer : public Layer, public DirectionFieldProvider { public: DirectionalLayer(); ~DirectionalLayer() override = default; /** @brief Identify this plugin as a directional layer. */ LayerType getType() const override { return LayerType::DIRECTIONAL_LAYER; } /** @brief Copy a consistent immutable snapshot for a planner thread. */ DirectionFieldSnapshot directionSnapshot() const override; /** @brief Update thread: leave bounds unchanged and report a stuck pending state. * @param robot_x Robot x [m] in the global frame. * @param robot_y Robot y [m] in the global frame. * @param robot_yaw Robot yaw [rad] in the global frame. * @param min_x Lower x bound [m] in the global frame. * @param min_y Lower y bound [m] in the global frame. * @param max_x Upper x bound [m] in the global frame. * @param max_y Upper y bound [m] in the global frame. */ void updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x, double* min_y, double* max_x, double* max_y) override; /** @brief Update thread: leave master costs unchanged. * @param master Master costmap in the global frame. * @param min_i Lower x cell index relative to the grid origin. * @param min_j Lower y cell index relative to the grid origin. * @param max_i Upper x cell index relative to the grid origin. * @param max_j Upper y cell index relative to the grid origin. */ void updateCosts(Costmap2D& /*master*/, int /*min_i*/, int /*min_j*/, int /*max_i*/, int /*max_j*/) override {} /** @brief Resize path: invalidate an enabled layer's snapshot. */ void matchSize() override; /** @brief Retain a ready snapshot if master geometry [m, cells, frame] is unchanged. */ void reset() override; protected: /** @brief Load plugin configuration and establish its initial state. */ void onInitialize() override; /** @brief Host thread: process static-map and direction-grid callbacks. */ void handleImpl(const void* data, const std::type_info& type, const std::string& topic) override; private: DirectionGeometry geometry() const; void publishLocked(DirectionFieldState state, std::shared_ptr field = {}); mutable std::mutex mutex_; DirectionFieldSnapshot snapshot_; bool seen_map_; bool debug_state_; bool rolling_; double max_deviation_rad_; ///< Maximum angular deviation [rad]. std::string map_topic_, direction_topic_; std::chrono::steady_clock::time_point pending_since_, last_pending_log_; }; } // namespace robot_costmap_2d #endif // ROBOT_COSTMAP_2D_DIRECTIONAL_LAYER_H_