|
| 1 | +// Copyright 2025 Intelligent Robotics Lab |
| 2 | +// |
| 3 | +// This file is part of the project Easy Navigation (EasyNav in sh0rt) |
| 4 | +// licensed under the GNU General Public License v3.0. |
| 5 | +// See <http://www.gnu.org/licenses/> for details. |
| 6 | +// |
| 7 | +// Easy Navigation program is free software: you can redistribute it and/or modify |
| 8 | +// it under the terms of the GNU General Public License as published by |
| 9 | +// the Free Software Foundation, either version 3 of the License, or |
| 10 | +// (at your option) any later version. |
| 11 | +// |
| 12 | +// This program is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +// GNU General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU General Public License |
| 18 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +/// \file |
| 21 | +/// \brief Definition of the GridmapMapsBuilderNode class. |
| 22 | + |
| 23 | +#ifndef EASYNAV_GRIDMAP_MAPS_MANAGER__GRIDMAPMAPSBUILDERNODE_HPP_ |
| 24 | +#define EASYNAV_GRIDMAP_MAPS_MANAGER__GRIDMAPMAPSBUILDERNODE_HPP_ |
| 25 | + |
| 26 | +#include "rclcpp/rclcpp.hpp" |
| 27 | +#include "rclcpp/macros.hpp" |
| 28 | +#include "rclcpp_lifecycle/lifecycle_node.hpp" |
| 29 | + |
| 30 | +#include "sensor_msgs/msg/point_cloud2.hpp" |
| 31 | +#include "grid_map_msgs/msg/grid_map.hpp" |
| 32 | +#include "std_srvs/srv/trigger.hpp" |
| 33 | + |
| 34 | +#include "grid_map_ros/grid_map_ros.hpp" |
| 35 | + |
| 36 | +#include "easynav_common/types/Perceptions.hpp" |
| 37 | + |
| 38 | +namespace easynav |
| 39 | +{ |
| 40 | + |
| 41 | +/** |
| 42 | + * @class GridmapMapsBuilderNode |
| 43 | + * @brief Lifecycle node that subscribes to point cloud sensor data and builds a grid map. |
| 44 | + * |
| 45 | + * This node processes perception data (point clouds) to generate a single outdoor grid map. |
| 46 | + * It uses ROS 2 lifecycle management for clean startup, activation, deactivation, and cleanup. |
| 47 | + * The node publishes the resulting grid map for downstream use. |
| 48 | + */ |
| 49 | +class GridmapMapsBuilderNode : public rclcpp_lifecycle::LifecycleNode |
| 50 | +{ |
| 51 | +public: |
| 52 | + RCLCPP_SMART_PTR_DEFINITIONS(GridmapMapsBuilderNode) |
| 53 | + |
| 54 | + using CallbackReturnT = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn; |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Constructor. |
| 58 | + * @param options Options for node initialization. |
| 59 | + */ |
| 60 | + explicit GridmapMapsBuilderNode(const rclcpp::NodeOptions & options = rclcpp::NodeOptions()); |
| 61 | + |
| 62 | + /** |
| 63 | + * @brief Destructor. |
| 64 | + */ |
| 65 | + ~GridmapMapsBuilderNode(); |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Lifecycle configure callback. |
| 69 | + * @param state Current lifecycle state. |
| 70 | + * @return CallbackReturnT indicating success or failure. |
| 71 | + */ |
| 72 | + CallbackReturnT on_configure(const rclcpp_lifecycle::State & state) override; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Lifecycle activate callback. |
| 76 | + * @param state Current lifecycle state. |
| 77 | + * @return CallbackReturnT indicating success or failure. |
| 78 | + */ |
| 79 | + CallbackReturnT on_activate(const rclcpp_lifecycle::State & state) override; |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Lifecycle deactivate callback. |
| 83 | + * @param state Current lifecycle state. |
| 84 | + * @return CallbackReturnT indicating success or failure. |
| 85 | + */ |
| 86 | + CallbackReturnT on_deactivate(const rclcpp_lifecycle::State & state) override; |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief Lifecycle cleanup callback. |
| 90 | + * @param state Current lifecycle state. |
| 91 | + * @return CallbackReturnT indicating success or failure. |
| 92 | + */ |
| 93 | + CallbackReturnT on_cleanup(const rclcpp_lifecycle::State & state) override; |
| 94 | + |
| 95 | + /** |
| 96 | + * @brief Perform a processing cycle on the perception data and update the grid map. |
| 97 | + * |
| 98 | + * This method should be called periodically (e.g., in a timer or main loop) to process |
| 99 | + * incoming sensor data, update the internal grid map representation, and publish outputs. |
| 100 | + */ |
| 101 | + void cycle(); |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Registers a perception handler. |
| 105 | + * @param handler Shared pointer to a PerceptionHandler instance. |
| 106 | + */ |
| 107 | + void register_handler(std::shared_ptr<PerceptionHandler> handler); |
| 108 | + |
| 109 | + const grid_map::GridMap & get_map() const {return map_;} |
| 110 | + void set_map(const grid_map::GridMap & map) {map_ = map;} |
| 111 | + |
| 112 | +private: |
| 113 | + /// Name of the sensor topic to subscribe to (e.g., point clouds). |
| 114 | + std::string sensor_topic_; |
| 115 | + |
| 116 | + /// Map of perception data grouped by sensor name. |
| 117 | + std::map<std::string, std::vector<PerceptionPtr>> perceptions_; |
| 118 | + |
| 119 | + /// Callback group for concurrency management of subscriptions and timers. |
| 120 | + rclcpp::CallbackGroup::SharedPtr cbg_; |
| 121 | + |
| 122 | + /// Downsampling resolution applied to point cloud data. |
| 123 | + double downsample_resolution_; |
| 124 | + |
| 125 | + /// Default frame ID used for perception data and published messages. |
| 126 | + std::string perception_default_frame_; |
| 127 | + |
| 128 | + /// Publisher for the processed grid map. |
| 129 | + rclcpp_lifecycle::LifecyclePublisher<grid_map_msgs::msg::GridMap>::SharedPtr pub_; |
| 130 | + |
| 131 | + /// Registered perception handlers by sensor name. |
| 132 | + std::map<std::string, std::shared_ptr<PerceptionHandler>> handlers_; |
| 133 | + |
| 134 | + grid_map::GridMap map_; |
| 135 | +}; |
| 136 | + |
| 137 | +} // namespace easynav |
| 138 | + |
| 139 | +#endif // EASYNAV_GRIDMAP_MAPS_MANAGER__GRIDMAPMAPSBUILDERNODE_HPP_ |
0 commit comments