Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
polygon.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__POLYGON_HPP_
16 #define NAV2_COLLISION_MONITOR__POLYGON_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <unordered_map>
22 
23 #include "rclcpp/clock.hpp"
24 #include "rclcpp/logger.hpp"
25 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
26 #include "rclcpp/parameter.hpp"
27 #include "geometry_msgs/msg/polygon_stamped.hpp"
28 
29 #include "tf2/time.hpp"
30 #include "nav2_ros_common/tf2_factories.hpp"
31 
32 #include "nav2_ros_common/lifecycle_node.hpp"
33 #include "nav2_costmap_2d/footprint_subscriber.hpp"
34 
35 #include "nav2_collision_monitor/types.hpp"
36 
37 using rcl_interfaces::msg::ParameterType;
38 
39 namespace nav2_collision_monitor
40 {
41 
47 class Polygon
48 {
49 public:
58  Polygon(
59  const nav2::LifecycleNode::WeakPtr & node,
60  const std::string & polygon_name,
61  const nav2::TransformBuffer::SharedPtr tf_buffer,
62  const std::string & base_frame_id,
63  const tf2::Duration & transform_tolerance);
67  virtual ~Polygon();
68 
74  bool configure();
78  void activate();
82  void deactivate();
83 
88  std::string getName() const;
93  ActionType getActionType() const;
98  bool getEnabled() const;
103  int getMinPoints() const;
104 
111  bool isTriggered(
112  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
113  std::vector<Point> & out_triggering_points);
114 
118  void resetTriggerState();
124  double getSlowdownRatio() const;
130  double getLinearLimit() const;
136  double getAngularLimit() const;
142  double getTimeBeforeCollision() const;
143 
148  virtual void getPolygon(std::vector<Point> & poly) const;
149 
154  std::vector<std::string> getSourcesNames() const;
155 
160  virtual bool isShapeSet();
161 
165  virtual void updatePolygon(const Velocity & /*cmd_vel_in*/);
166 
174  virtual int getPointsInside(
175  const std::vector<Point> & points,
176  std::vector<Point> & out_triggering_points) const;
177 
185  virtual int getPointsInside(
186  const std::vector<Point> & points,
187  std::vector<std::size_t> & out_triggering_indices) const;
188 
198  virtual int getPointsInside(
199  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
200  std::vector<Point> & out_triggering_points) const;
201 
202 
213  double getCollisionTime(
214  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
215  const Velocity & velocity,
216  std::vector<Point> & out_triggering_points) const;
217 
221  void publish();
222 
223 private:
224  bool isTriggeredInternal(int points_inside);
225 
226 protected:
238  bool getCommonParameters(
239  std::string & polygon_sub_topic,
240  std::string & polygon_pub_topic,
241  std::string & footprint_topic,
242  bool use_dynamic_sub = false);
243 
253  virtual bool getParameters(
254  std::string & polygon_sub_topic,
255  std::string & polygon_pub_topic,
256  std::string & footprint_topic);
257 
263  virtual void createSubscription(std::string & polygon_sub_topic);
264 
269  void updatePolygon(geometry_msgs::msg::PolygonStamped::ConstSharedPtr msg);
270 
275  void polygonCallback(geometry_msgs::msg::PolygonStamped::ConstSharedPtr msg);
276 
283  void
284  updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
285 
294  rcl_interfaces::msg::SetParametersResult
295  validateParameterUpdatesCallback(const std::vector<rclcpp::Parameter> & parameters);
296 
303  bool getPolygonFromString(std::string & poly_string, std::vector<Point> & polygon);
304 
305  // ----- Variables -----
306 
308  nav2::LifecycleNode::WeakPtr node_;
310  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
312  mutable std::mutex mutex_;
313  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
314  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
315 
316  // Basic parameters
318  std::string polygon_name_;
320  ActionType action_type_;
344  bool enabled_;
348  nav2::Subscription<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_sub_;
350  std::unique_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
352  std::vector<std::string> sources_names_;
353 
354  // Global variables
356  nav2::TransformBuffer::SharedPtr tf_buffer_;
358  std::string base_frame_id_;
360  tf2::Duration transform_tolerance_;
362  rclcpp::Clock::SharedPtr node_clock_;
363 
364  // Visualization
368  geometry_msgs::msg::PolygonStamped polygon_;
370  nav2::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_pub_;
371 
373  std::vector<Point> poly_;
374 }; // class Polygon
375 
376 } // namespace nav2_collision_monitor
377 
378 #endif // NAV2_COLLISION_MONITOR__POLYGON_HPP_
Basic polygon shape class. For STOP/SLOWDOWN/LIMIT model it represents zone around the robot while fo...
Definition: polygon.hpp:48
virtual bool getParameters(std::string &polygon_sub_topic, std::string &polygon_pub_topic, std::string &footprint_topic)
Supporting routine obtaining polygon-specific ROS-parameters.
Definition: polygon.cpp:535
int release_consecutive_points_
Number of consecutive misses required to release action.
Definition: polygon.hpp:326
int trigger_consecutive_points_
Number of consecutive hits required to trigger action.
Definition: polygon.hpp:324
int getMinPoints() const
Obtains polygon minimum points to enter inside polygon causing the action.
Definition: polygon.cpp:157
nav2::Publisher< geometry_msgs::msg::PolygonStamped >::SharedPtr polygon_pub_
Polygon publisher for visualization purposes.
Definition: polygon.hpp:370
double getTimeBeforeCollision() const
Obtains required time before collision for current polygon. Applicable for APPROACH model.
Definition: polygon.cpp:218
rclcpp::Clock::SharedPtr node_clock_
Collision monitor node's clock.
Definition: polygon.hpp:362
virtual void updatePolygon(const Velocity &)
Updates polygon from footprint subscriber (if any)
Definition: polygon.cpp:246
bool getCommonParameters(std::string &polygon_sub_topic, std::string &polygon_pub_topic, std::string &footprint_topic, bool use_dynamic_sub=false)
Supporting routine obtaining ROS-parameters common for all shapes.
Definition: polygon.cpp:407
std::mutex mutex_
Dynamic parameters handler.
Definition: polygon.hpp:312
double time_before_collision_
Time before collision in seconds.
Definition: polygon.hpp:340
double getCollisionTime(const std::unordered_map< std::string, std::vector< Point >> &sources_collision_points_map, const Velocity &velocity, std::vector< Point > &out_triggering_points) const
Obtains estimated (simulated) time before a collision. Applicable for APPROACH model.
Definition: polygon.cpp:339
bool enabled_
Whether polygon is enabled.
Definition: polygon.hpp:344
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them. This callback is triggered when one or more...
Definition: polygon.cpp:642
rclcpp::Logger logger_
Collision monitor node logger stored for further usage.
Definition: polygon.hpp:310
ActionType action_type_
Action type for the polygon.
Definition: polygon.hpp:320
geometry_msgs::msg::PolygonStamped polygon_
Polygon, used for: 1. visualization; 2. storing latest dynamic polygon message.
Definition: polygon.hpp:368
int trigger_hits_
Current consecutive hit counter.
Definition: polygon.hpp:328
virtual void createSubscription(std::string &polygon_sub_topic)
Creates polygon or radius topic subscription.
Definition: polygon.cpp:579
nav2::Subscription< geometry_msgs::msg::PolygonStamped >::SharedPtr polygon_sub_
Polygon subscription.
Definition: polygon.hpp:348
bool polygon_subscribe_transient_local_
Whether the subscription to polygon topic has transient local QoS durability.
Definition: polygon.hpp:346
virtual bool isShapeSet()
Returns true if polygon points were set. Otherwise, prints a warning and returns false.
Definition: polygon.cpp:237
double simulation_time_step_
Time step for robot movement simulation.
Definition: polygon.hpp:342
bool trigger_active_
Latched trigger state after temporal debounce.
Definition: polygon.hpp:332
void activate()
Activates polygon lifecycle publisher.
Definition: polygon.cpp:125
bool configure()
Shape configuration routine. Obtains ROS-parameters related to shape object and creates polygon lifec...
Definition: polygon.cpp:69
double getLinearLimit() const
Obtains speed linear limit for current polygon. Applicable for LIMIT model.
Definition: polygon.cpp:208
std::string getName() const
Returns the name of polygon.
Definition: polygon.cpp:141
bool isTriggered(const std::unordered_map< std::string, std::vector< Point >> &sources_collision_points_map, std::vector< Point > &out_triggering_points)
Temporal debounce for min_points trigger.
Definition: polygon.cpp:162
virtual void getPolygon(std::vector< Point > &poly) const
Gets polygon points.
Definition: polygon.cpp:228
tf2::Duration transform_tolerance_
Transform tolerance.
Definition: polygon.hpp:360
void resetTriggerState()
Reset temporal debounce state.
Definition: polygon.cpp:196
ActionType getActionType() const
Obtains polygon action type.
Definition: polygon.cpp:146
int min_points_
Minimum number of data readings within a zone to trigger the action.
Definition: polygon.hpp:322
std::vector< Point > poly_
Polygon points (vertices) in a base_frame_id_.
Definition: polygon.hpp:373
std::vector< std::string > sources_names_
Name of the observation sources to check for polygon.
Definition: polygon.hpp:352
void deactivate()
Deactivates polygon lifecycle publisher.
Definition: polygon.cpp:134
std::unique_ptr< nav2_costmap_2d::FootprintSubscriber > footprint_sub_
Footprint subscriber.
Definition: polygon.hpp:350
void polygonCallback(geometry_msgs::msg::PolygonStamped::ConstSharedPtr msg)
Dynamic polygon callback.
Definition: polygon.cpp:693
double getSlowdownRatio() const
Obtains speed slowdown ratio for current polygon. Applicable for SLOWDOWN model.
Definition: polygon.cpp:203
double getAngularLimit() const
Obtains speed angular z limit for current polygon. Applicable for LIMIT model.
Definition: polygon.cpp:213
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation This callback is executed when parameters have been successf...
Definition: polygon.cpp:650
std::string polygon_name_
Name of polygon.
Definition: polygon.hpp:318
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: polygon.hpp:308
virtual int getPointsInside(const std::vector< Point > &points, std::vector< Point > &out_triggering_points) const
Gets number of points inside given polygon.
Definition: polygon.cpp:293
double linear_limit_
Robot linear limit.
Definition: polygon.hpp:336
virtual ~Polygon()
Polygon destructor.
Definition: polygon.cpp:51
int release_hits_
Current consecutive miss counter.
Definition: polygon.hpp:330
bool getEnabled() const
Obtains polygon enabled state.
Definition: polygon.cpp:151
double angular_limit_
Robot angular limit.
Definition: polygon.hpp:338
bool getPolygonFromString(std::string &poly_string, std::vector< Point > &polygon)
Extracts Polygon points from a string with of the form [[x1,y1],[x2,y2],[x3,y3]......
Definition: polygon.cpp:704
bool visualize_
Whether to publish the polygon.
Definition: polygon.hpp:366
std::vector< std::string > getSourcesNames() const
Obtains the name of the observation sources for current polygon.
Definition: polygon.cpp:223
nav2::TransformBuffer::SharedPtr tf_buffer_
TF buffer.
Definition: polygon.hpp:356
std::string base_frame_id_
Base frame ID.
Definition: polygon.hpp:358
Polygon(const nav2::LifecycleNode::WeakPtr &node, const std::string &polygon_name, const nav2::TransformBuffer::SharedPtr tf_buffer, const std::string &base_frame_id, const tf2::Duration &transform_tolerance)
Polygon constructor.
Definition: polygon.cpp:36
double slowdown_ratio_
Robot slowdown (share of its actual speed)
Definition: polygon.hpp:334
void publish()
Publishes polygon message into a its own topic.
Definition: polygon.cpp:390
Velocity for 2D model of motion.
Definition: types.hpp:26