|
| 1 | +// Copyright (c) 2022 Samsung R&D Institute Russia |
| 2 | +// Copyright (c) 2023 Pixel Robotics GmbH |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#ifndef NAV2_COLLISION_MONITOR__COLLISION_DETECTOR_NODE_HPP_ |
| 17 | +#define NAV2_COLLISION_MONITOR__COLLISION_DETECTOR_NODE_HPP_ |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +#include "rclcpp/rclcpp.hpp" |
| 24 | + |
| 25 | +#include "tf2/time.h" |
| 26 | +#include "tf2_ros/buffer.h" |
| 27 | +#include "tf2_ros/transform_listener.h" |
| 28 | + |
| 29 | +#include "nav2_util/lifecycle_node.hpp" |
| 30 | +#include "nav2_msgs/msg/collision_detector_state.hpp" |
| 31 | + |
| 32 | +#include "nav2_collision_monitor/types.hpp" |
| 33 | +#include "nav2_collision_monitor/polygon.hpp" |
| 34 | +#include "nav2_collision_monitor/circle.hpp" |
| 35 | +#include "nav2_collision_monitor/source.hpp" |
| 36 | +#include "nav2_collision_monitor/scan.hpp" |
| 37 | +#include "nav2_collision_monitor/pointcloud.hpp" |
| 38 | +#include "nav2_collision_monitor/range.hpp" |
| 39 | + |
| 40 | +namespace nav2_collision_monitor |
| 41 | +{ |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Collision Monitor ROS2 node |
| 45 | + */ |
| 46 | +class CollisionDetector : public nav2_util::LifecycleNode |
| 47 | +{ |
| 48 | +public: |
| 49 | + /** |
| 50 | + * @brief Constructor for the nav2_collision_monitor::CollisionDetector |
| 51 | + * @param options Additional options to control creation of the node. |
| 52 | + */ |
| 53 | + explicit CollisionDetector(const rclcpp::NodeOptions & options = rclcpp::NodeOptions()); |
| 54 | + /** |
| 55 | + * @brief Destructor for the nav2_collision_monitor::CollisionDetector |
| 56 | + */ |
| 57 | + ~CollisionDetector(); |
| 58 | + |
| 59 | +protected: |
| 60 | + /** |
| 61 | + * @brief: Initializes and obtains ROS-parameters, creates main subscribers and publishers, |
| 62 | + * creates polygons and data sources objects |
| 63 | + * @param state Lifecycle Node's state |
| 64 | + * @return Success or Failure |
| 65 | + */ |
| 66 | + nav2_util::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override; |
| 67 | + /** |
| 68 | + * @brief: Activates LifecyclePublishers, polygons and main processor, creates bond connection |
| 69 | + * @param state Lifecycle Node's state |
| 70 | + * @return Success or Failure |
| 71 | + */ |
| 72 | + nav2_util::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override; |
| 73 | + /** |
| 74 | + * @brief: Deactivates LifecyclePublishers, polygons and main processor, destroys bond connection |
| 75 | + * @param state Lifecycle Node's state |
| 76 | + * @return Success or Failure |
| 77 | + */ |
| 78 | + nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override; |
| 79 | + /** |
| 80 | + * @brief: Resets all subscribers/publishers, polygons/data sources arrays |
| 81 | + * @param state Lifecycle Node's state |
| 82 | + * @return Success or Failure |
| 83 | + */ |
| 84 | + nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override; |
| 85 | + /** |
| 86 | + * @brief Called in shutdown state |
| 87 | + * @param state Lifecycle Node's state |
| 88 | + * @return Success or Failure |
| 89 | + */ |
| 90 | + nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override; |
| 91 | + |
| 92 | +protected: |
| 93 | + /** |
| 94 | + * @brief Supporting routine obtaining all ROS-parameters |
| 95 | + * @return True if all parameters were obtained or false in failure case |
| 96 | + */ |
| 97 | + bool getParameters(); |
| 98 | + /** |
| 99 | + * @brief Supporting routine creating and configuring all polygons |
| 100 | + * @param base_frame_id Robot base frame ID |
| 101 | + * @param transform_tolerance Transform tolerance |
| 102 | + * @return True if all polygons were configured successfully or false in failure case |
| 103 | + */ |
| 104 | + bool configurePolygons( |
| 105 | + const std::string & base_frame_id, |
| 106 | + const tf2::Duration & transform_tolerance); |
| 107 | + /** |
| 108 | + * @brief Supporting routine creating and configuring all data sources |
| 109 | + * @param base_frame_id Robot base frame ID |
| 110 | + * @param odom_frame_id Odometry frame ID. Used as global frame to get |
| 111 | + * source->base time interpolated transform. |
| 112 | + * @param transform_tolerance Transform tolerance |
| 113 | + * @param source_timeout Maximum time interval in which data is considered valid |
| 114 | + * @param base_shift_correction Whether to correct source data towards to base frame movement, |
| 115 | + * considering the difference between current time and latest source time |
| 116 | + * @return True if all sources were configured successfully or false in failure case |
| 117 | + */ |
| 118 | + bool configureSources( |
| 119 | + const std::string & base_frame_id, |
| 120 | + const std::string & odom_frame_id, |
| 121 | + const tf2::Duration & transform_tolerance, |
| 122 | + const rclcpp::Duration & source_timeout, |
| 123 | + const bool base_shift_correction); |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Main processing routine |
| 127 | + */ |
| 128 | + void process(); |
| 129 | + |
| 130 | + /** |
| 131 | + * @brief Polygons publishing routine. Made for visualization. |
| 132 | + */ |
| 133 | + void publishPolygons() const; |
| 134 | + |
| 135 | + // ----- Variables ----- |
| 136 | + |
| 137 | + /// @brief TF buffer |
| 138 | + std::shared_ptr<tf2_ros::Buffer> tf_buffer_; |
| 139 | + /// @brief TF listener |
| 140 | + std::shared_ptr<tf2_ros::TransformListener> tf_listener_; |
| 141 | + |
| 142 | + /// @brief Polygons array |
| 143 | + std::vector<std::shared_ptr<Polygon>> polygons_; |
| 144 | + /// @brief Data sources array |
| 145 | + std::vector<std::shared_ptr<Source>> sources_; |
| 146 | + |
| 147 | + /// @brief collision monitor state publisher |
| 148 | + rclcpp_lifecycle::LifecyclePublisher<nav2_msgs::msg::CollisionDetectorState>::SharedPtr |
| 149 | + state_pub_; |
| 150 | + /// @brief timer that runs actions |
| 151 | + rclcpp::TimerBase::SharedPtr timer_; |
| 152 | + |
| 153 | + /// @brief main loop frequency |
| 154 | + double frequency_; |
| 155 | +}; // class CollisionDetector |
| 156 | + |
| 157 | +} // namespace nav2_collision_monitor |
| 158 | + |
| 159 | +#endif // NAV2_COLLISION_MONITOR__COLLISION_DETECTOR_NODE_HPP_ |
0 commit comments