Nav2 Navigation Stack - lyrical  lyrical
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/duration.hpp"
24 #include "rclcpp/node_options.hpp"
25 #include "rclcpp/time.hpp"
26 #include "geometry_msgs/msg/twist.hpp"
27 #include "visualization_msgs/msg/marker_array.hpp"
28 #include "geometry_msgs/msg/twist_stamped.hpp"
29 
30 #include "tf2/time.hpp"
31 
32 #include "nav2_ros_common/lifecycle_node.hpp"
33 #include "nav2_ros_common/tf2_factories.hpp"
34 #include "nav2_util/twist_publisher.hpp"
35 #include "nav2_util/twist_subscriber.hpp"
36 #include "nav2_msgs/msg/collision_monitor_state.hpp"
37 #include "nav2_msgs/srv/toggle.hpp"
38 
39 #include "nav2_collision_monitor/types.hpp"
40 #include "nav2_collision_monitor/polygon.hpp"
41 #include "nav2_collision_monitor/circle.hpp"
42 #include "nav2_collision_monitor/velocity_polygon.hpp"
43 #include "nav2_collision_monitor/source.hpp"
44 #include "nav2_collision_monitor/scan.hpp"
45 #include "nav2_collision_monitor/pointcloud.hpp"
46 #include "nav2_collision_monitor/range.hpp"
48 #include "nav2_collision_monitor/polygon_source.hpp"
49 
50 namespace nav2_collision_monitor
51 {
52 
57 {
58 public:
63  explicit CollisionMonitor(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
68 
69 protected:
76  nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State & state) override;
82  nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State & state) override;
88  nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State & state) override;
94  nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State & state) override;
100  nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
101 
102 protected:
107  void cmdVelInCallbackStamped(const geometry_msgs::msg::TwistStamped::ConstSharedPtr & msg);
108  void cmdVelInCallbackUnstamped(const geometry_msgs::msg::Twist::ConstSharedPtr & msg);
115  void publishVelocity(const Action & robot_action, const std_msgs::msg::Header & header);
116 
125  bool getParameters(
126  std::string & cmd_vel_in_topic,
127  std::string & cmd_vel_out_topic,
128  std::string & state_topic);
135  bool configurePolygons(
136  const std::string & base_frame_id,
137  const tf2::Duration & transform_tolerance);
149  bool configureSources(
150  const std::string & base_frame_id,
151  const std::string & odom_frame_id,
152  const tf2::Duration & transform_tolerance,
153  const rclcpp::Duration & source_timeout,
154  const bool base_shift_correction);
155 
161  void process(const Velocity & cmd_vel_in, const std_msgs::msg::Header & header);
162 
173  const std::shared_ptr<Polygon> polygon,
174  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
175  const Velocity & velocity,
176  Action & robot_action) const;
177 
187  bool processApproach(
188  const std::shared_ptr<Polygon> polygon,
189  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
190  const Velocity & velocity,
191  Action & robot_action) const;
192 
198  void notifyActionState(
199  const Action & robot_action, const std::shared_ptr<Polygon> action_polygon) const;
200 
204  void publishVisualizations() const;
205 
210  void publishTriggeringPoints(const Action & action);
211 
218  const std::shared_ptr<rmw_request_id_t> request_header,
219  const std::shared_ptr<nav2_msgs::srv::Toggle::Request> request,
220  std::shared_ptr<nav2_msgs::srv::Toggle::Response> response);
221 
222  // ----- Variables -----
223 
225  nav2::TransformBuffer::SharedPtr tf_buffer_;
227  nav2::TransformListener::SharedPtr tf_listener_;
228 
230  std::vector<std::shared_ptr<Polygon>> polygons_;
231 
233  std::vector<std::shared_ptr<Source>> sources_;
234 
235  // Input/output speed controls
237  std::unique_ptr<nav2_util::TwistSubscriber> cmd_vel_in_sub_;
239  std::unique_ptr<nav2_util::TwistPublisher> cmd_vel_out_pub_;
240 
242  nav2::Publisher<nav2_msgs::msg::CollisionMonitorState>::SharedPtr
244 
246  nav2::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr
248 
250  nav2::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr
252 
254  nav2::ServiceServer<nav2_msgs::srv::Toggle>::SharedPtr toggle_cm_service_;
255 
258 
260  std::string base_frame_id_;
261 
263  bool enabled_;
264 
267 
271  rclcpp::Time stop_stamp_;
273  rclcpp::Duration stop_pub_timeout_;
274 }; // class CollisionMonitor
275 
276 } // namespace nav2_collision_monitor
277 
278 #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.
bool collision_points_marker_3d_
Whether to include z in the collision_points_marker.
std::unique_ptr< nav2_util::TwistPublisher > cmd_vel_out_pub_
Output cmd_vel publisher.
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,...
nav2::Publisher< visualization_msgs::msg::MarkerArray >::SharedPtr triggering_points_pub_
Triggering points marker publisher (points inside the active triggering zone)
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.
~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.
nav2::TransformListener::SharedPtr tf_listener_
TF listener.
void publishTriggeringPoints(const Action &action)
Publishes action.triggering_points as markers, colour-coded by action type.
nav2::ServiceServer< nav2_msgs::srv::Toggle >::SharedPtr toggle_cm_service_
Enable/disable collision monitor service.
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
std::string base_frame_id_
Robot base frame ID.
void toggleCMServiceCallback(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< nav2_msgs::srv::Toggle::Request > request, std::shared_ptr< nav2_msgs::srv::Toggle::Response > response)
Enable/disable collision monitor service callback.
nav2::TransformBuffer::SharedPtr tf_buffer_
TF buffer.
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.
void publishVisualizations() const
Publishes all visualization topics (polygons and exclusion zones).
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 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 enabled_
Whether collision monitor is enabled.
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.
void cmdVelInCallbackStamped(const geometry_msgs::msg::TwistStamped::ConstSharedPtr &msg)
Callback for input cmd_vel.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
: Activates LifecyclePublishers, polygons and main processor, creates bond connection
Observation source that converts a Nav2 costmap topic into 2D points for Collision Monitor.
Action for robot.
Definition: types.hpp:79
Velocity for 2D model of motion.
Definition: types.hpp:26