Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
collision_monitor_node.hpp
1 // Copyright (c) 2022 Samsung R&D Institute Russia
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef NAV2_COLLISION_MONITOR__COLLISION_MONITOR_NODE_HPP_
16 #define NAV2_COLLISION_MONITOR__COLLISION_MONITOR_NODE_HPP_
17 
18 #include <string>
19 #include <vector>
20 #include <memory>
21 #include <unordered_map>
22 
23 #include "rclcpp/rclcpp.hpp"
24 #include "geometry_msgs/msg/twist.hpp"
25 #include "visualization_msgs/msg/marker_array.hpp"
26 #include "geometry_msgs/msg/twist_stamped.hpp"
27 
28 #include "tf2/time.hpp"
29 #include "tf2_ros/buffer.hpp"
30 #include "tf2_ros/transform_listener.hpp"
31 
32 #include "nav2_ros_common/lifecycle_node.hpp"
33 #include "nav2_util/twist_publisher.hpp"
34 #include "nav2_util/twist_subscriber.hpp"
35 #include "nav2_msgs/msg/collision_monitor_state.hpp"
36 
37 #include "nav2_collision_monitor/types.hpp"
38 #include "nav2_collision_monitor/polygon.hpp"
39 #include "nav2_collision_monitor/circle.hpp"
40 #include "nav2_collision_monitor/velocity_polygon.hpp"
41 #include "nav2_collision_monitor/source.hpp"
42 #include "nav2_collision_monitor/scan.hpp"
43 #include "nav2_collision_monitor/pointcloud.hpp"
44 #include "nav2_collision_monitor/range.hpp"
45 #include "nav2_collision_monitor/polygon_source.hpp"
46 
47 namespace nav2_collision_monitor
48 {
49 
54 {
55 public:
60  explicit CollisionMonitor(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
65 
66 protected:
73  nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
79  nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
85  nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
91  nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
97  nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
98 
99 protected:
104  void cmdVelInCallbackStamped(geometry_msgs::msg::TwistStamped::SharedPtr msg);
105  void cmdVelInCallbackUnstamped(geometry_msgs::msg::Twist::SharedPtr msg);
112  void publishVelocity(const Action & robot_action, const std_msgs::msg::Header & header);
113 
122  bool getParameters(
123  std::string & cmd_vel_in_topic,
124  std::string & cmd_vel_out_topic,
125  std::string & state_topic);
132  bool configurePolygons(
133  const std::string & base_frame_id,
134  const tf2::Duration & transform_tolerance);
146  bool configureSources(
147  const std::string & base_frame_id,
148  const std::string & odom_frame_id,
149  const tf2::Duration & transform_tolerance,
150  const rclcpp::Duration & source_timeout,
151  const bool base_shift_correction);
152 
158  void process(const Velocity & cmd_vel_in, const std_msgs::msg::Header & header);
159 
170  const std::shared_ptr<Polygon> polygon,
171  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
172  const Velocity & velocity,
173  Action & robot_action) const;
174 
184  bool processApproach(
185  const std::shared_ptr<Polygon> polygon,
186  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
187  const Velocity & velocity,
188  Action & robot_action) const;
189 
195  void notifyActionState(
196  const Action & robot_action, const std::shared_ptr<Polygon> action_polygon) const;
197 
201  void publishPolygons() const;
202 
203  // ----- Variables -----
204 
206  std::shared_ptr<tf2_ros::Buffer> tf_buffer_;
208  std::shared_ptr<tf2_ros::TransformListener> tf_listener_;
209 
211  std::vector<std::shared_ptr<Polygon>> polygons_;
212 
214  std::vector<std::shared_ptr<Source>> sources_;
215 
216  // Input/output speed controls
218  std::unique_ptr<nav2_util::TwistSubscriber> cmd_vel_in_sub_;
220  std::unique_ptr<nav2_util::TwistPublisher> cmd_vel_out_pub_;
221 
223  nav2::Publisher<nav2_msgs::msg::CollisionMonitorState>::SharedPtr
225 
227  nav2::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr
229 
232 
236  rclcpp::Time stop_stamp_;
238  rclcpp::Duration stop_pub_timeout_;
239 }; // class CollisionMonitor
240 
241 } // namespace nav2_collision_monitor
242 
243 #endif // NAV2_COLLISION_MONITOR__COLLISION_MONITOR_NODE_HPP_
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
std::unique_ptr< nav2_util::TwistSubscriber > cmd_vel_in_sub_
Input cmd_vel subscriber.
void notifyActionState(const Action &robot_action, const std::shared_ptr< Polygon > action_polygon) const
Log and publish current robot action and polygon.
std::unique_ptr< nav2_util::TwistPublisher > cmd_vel_out_pub_
Output cmd_vel publisher.
std::shared_ptr< tf2_ros::TransformListener > tf_listener_
TF listener.
nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
: Resets all subscribers/publishers, polygons/data sources arrays
rclcpp::Duration stop_pub_timeout_
Timeout after which 0-velocity ceases to be published.
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
: Initializes and obtains ROS-parameters, creates main subscribers and publishers,...
bool process_active_
Whether main routine is active.
bool configurePolygons(const std::string &base_frame_id, const tf2::Duration &transform_tolerance)
Supporting routine creating and configuring all polygons.
std::vector< std::shared_ptr< Polygon > > polygons_
Polygons array.
std::shared_ptr< tf2_ros::Buffer > tf_buffer_
TF buffer.
~CollisionMonitor()
Destructor for the nav2_collision_monitor::CollisionMonitor.
bool processApproach(const std::shared_ptr< Polygon > polygon, const std::unordered_map< std::string, std::vector< Point >> &sources_collision_points_map, const Velocity &velocity, Action &robot_action) const
Processes APPROACH action type.
rclcpp::Time stop_stamp_
Latest timestamp when robot has 0-velocity.
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called in shutdown state.
std::vector< std::shared_ptr< Source > > sources_
Data sources array.
bool getParameters(std::string &cmd_vel_in_topic, std::string &cmd_vel_out_topic, std::string &state_topic)
Supporting routine obtaining all ROS-parameters.
nav2::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr collision_points_marker_pub_
Collision points marker publisher.
CollisionMonitor(const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
Constructor for the nav2_collision_monitor::CollisionMonitor.
nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
: Deactivates LifecyclePublishers, polygons and main processor, destroys bond connection
void cmdVelInCallbackStamped(geometry_msgs::msg::TwistStamped::SharedPtr msg)
Callback for input cmd_vel.
bool configureSources(const std::string &base_frame_id, const std::string &odom_frame_id, const tf2::Duration &transform_tolerance, const rclcpp::Duration &source_timeout, const bool base_shift_correction)
Supporting routine creating and configuring all data sources.
nav2::Publisher< nav2_msgs::msg::CollisionMonitorState >::SharedPtr state_pub_
CollisionMonitor state publisher.
void process(const Velocity &cmd_vel_in, const std_msgs::msg::Header &header)
Main processing routine.
void publishPolygons() const
Polygons publishing routine. Made for visualization.
void publishVelocity(const Action &robot_action, const std_msgs::msg::Header &header)
Publishes output cmd_vel. If robot was stopped more than stop_pub_timeout_ seconds,...
bool processStopSlowdownLimit(const std::shared_ptr< Polygon > polygon, const std::unordered_map< std::string, std::vector< Point >> &sources_collision_points_map, const Velocity &velocity, Action &robot_action) const
Processes the polygon of STOP, SLOWDOWN and LIMIT action type.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
: Activates LifecyclePublishers, polygons and main processor, creates bond connection
Action for robot.
Definition: types.hpp:76
Velocity for 2D model of motion.
Definition: types.hpp:25