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/rclcpp.hpp"
24 #include "geometry_msgs/msg/polygon_stamped.hpp"
25 
26 #include "tf2/time.hpp"
27 #include "nav2_ros_common/tf2_factories.hpp"
28 
29 #include "nav2_ros_common/lifecycle_node.hpp"
30 #include "nav2_costmap_2d/footprint_subscriber.hpp"
31 
32 #include "nav2_collision_monitor/types.hpp"
33 
34 using rcl_interfaces::msg::ParameterType;
35 
36 namespace nav2_collision_monitor
37 {
38 
44 class Polygon
45 {
46 public:
55  Polygon(
56  const nav2::LifecycleNode::WeakPtr & node,
57  const std::string & polygon_name,
58  const nav2::TransformBuffer::SharedPtr tf_buffer,
59  const std::string & base_frame_id,
60  const tf2::Duration & transform_tolerance);
64  virtual ~Polygon();
65 
71  bool configure();
75  void activate();
79  void deactivate();
80 
85  std::string getName() const;
90  ActionType getActionType() const;
95  bool getEnabled() const;
100  int getMinPoints() const;
101 
108  bool isTriggered(
109  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
110  std::vector<Point> & out_triggering_points);
111 
115  void resetTriggerState();
121  double getSlowdownRatio() const;
127  double getLinearLimit() const;
133  double getAngularLimit() const;
139  double getTimeBeforeCollision() const;
140 
145  virtual void getPolygon(std::vector<Point> & poly) const;
146 
151  std::vector<std::string> getSourcesNames() const;
152 
157  virtual bool isShapeSet();
158 
162  virtual void updatePolygon(const Velocity & /*cmd_vel_in*/);
163 
171  virtual int getPointsInside(
172  const std::vector<Point> & points,
173  std::vector<Point> & out_triggering_points) const;
174 
182  virtual int getPointsInside(
183  const std::vector<Point> & points,
184  std::vector<std::size_t> & out_triggering_indices) const;
185 
195  virtual int getPointsInside(
196  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
197  std::vector<Point> & out_triggering_points) const;
198 
199 
210  double getCollisionTime(
211  const std::unordered_map<std::string, std::vector<Point>> & sources_collision_points_map,
212  const Velocity & velocity,
213  std::vector<Point> & out_triggering_points) const;
214 
218  void publish();
219 
220 private:
221  bool isTriggeredInternal(int points_inside);
222 
223 protected:
235  bool getCommonParameters(
236  std::string & polygon_sub_topic,
237  std::string & polygon_pub_topic,
238  std::string & footprint_topic,
239  bool use_dynamic_sub = false);
240 
250  virtual bool getParameters(
251  std::string & polygon_sub_topic,
252  std::string & polygon_pub_topic,
253  std::string & footprint_topic);
254 
260  virtual void createSubscription(std::string & polygon_sub_topic);
261 
266  void updatePolygon(geometry_msgs::msg::PolygonStamped::ConstSharedPtr msg);
267 
272  void polygonCallback(geometry_msgs::msg::PolygonStamped::ConstSharedPtr msg);
273 
280  void
281  updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
282 
291  rcl_interfaces::msg::SetParametersResult
292  validateParameterUpdatesCallback(const std::vector<rclcpp::Parameter> & parameters);
293 
300  bool getPolygonFromString(std::string & poly_string, std::vector<Point> & polygon);
301 
302  // ----- Variables -----
303 
305  nav2::LifecycleNode::WeakPtr node_;
307  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
309  mutable std::mutex mutex_;
310  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
311  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
312 
313  // Basic parameters
315  std::string polygon_name_;
317  ActionType action_type_;
341  bool enabled_;
345  nav2::Subscription<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_sub_;
347  std::unique_ptr<nav2_costmap_2d::FootprintSubscriber> footprint_sub_;
349  std::vector<std::string> sources_names_;
350 
351  // Global variables
353  nav2::TransformBuffer::SharedPtr tf_buffer_;
355  std::string base_frame_id_;
357  tf2::Duration transform_tolerance_;
359  rclcpp::Clock::SharedPtr node_clock_;
360 
361  // Visualization
365  geometry_msgs::msg::PolygonStamped polygon_;
367  nav2::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr polygon_pub_;
368 
370  std::vector<Point> poly_;
371 }; // class Polygon
372 
373 } // namespace nav2_collision_monitor
374 
375 #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:45
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:323
int trigger_consecutive_points_
Number of consecutive hits required to trigger action.
Definition: polygon.hpp:321
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:367
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:359
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:309
double time_before_collision_
Time before collision in seconds.
Definition: polygon.hpp:337
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:341
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:307
ActionType action_type_
Action type for the polygon.
Definition: polygon.hpp:317
geometry_msgs::msg::PolygonStamped polygon_
Polygon, used for: 1. visualization; 2. storing latest dynamic polygon message.
Definition: polygon.hpp:365
int trigger_hits_
Current consecutive hit counter.
Definition: polygon.hpp:325
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:345
bool polygon_subscribe_transient_local_
Whether the subscription to polygon topic has transient local QoS durability.
Definition: polygon.hpp:343
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:339
bool trigger_active_
Latched trigger state after temporal debounce.
Definition: polygon.hpp:329
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:357
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:319
std::vector< Point > poly_
Polygon points (vertices) in a base_frame_id_.
Definition: polygon.hpp:370
std::vector< std::string > sources_names_
Name of the observation sources to check for polygon.
Definition: polygon.hpp:349
void deactivate()
Deactivates polygon lifecycle publisher.
Definition: polygon.cpp:134
std::unique_ptr< nav2_costmap_2d::FootprintSubscriber > footprint_sub_
Footprint subscriber.
Definition: polygon.hpp:347
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:315
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
Definition: polygon.hpp:305
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:333
virtual ~Polygon()
Polygon destructor.
Definition: polygon.cpp:51
int release_hits_
Current consecutive miss counter.
Definition: polygon.hpp:327
bool getEnabled() const
Obtains polygon enabled state.
Definition: polygon.cpp:151
double angular_limit_
Robot angular limit.
Definition: polygon.hpp:335
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:363
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:353
std::string base_frame_id_
Base frame ID.
Definition: polygon.hpp:355
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:331
void publish()
Publishes polygon message into a its own topic.
Definition: polygon.cpp:390
Velocity for 2D model of motion.
Definition: types.hpp:26