Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
exclusion_zone.hpp
1 // Copyright (c) 2026 Dexory
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__EXCLUSION_ZONE_HPP_
16 #define NAV2_COLLISION_MONITOR__EXCLUSION_ZONE_HPP_
17 
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "rclcpp/clock.hpp"
24 #include "rclcpp/logger.hpp"
25 #include "rclcpp/parameter.hpp"
26 #include "rclcpp/time.hpp"
27 #include "rclcpp/node_interfaces/node_parameters_interface.hpp"
28 #include "geometry_msgs/msg/polygon_stamped.hpp"
29 
30 #include "tf2/time.hpp"
31 #include "tf2/LinearMath/Transform.hpp"
32 
33 #include "nav2_ros_common/lifecycle_node.hpp"
34 #include "nav2_ros_common/tf2_factories.hpp"
35 
36 #include "nav2_msgs/msg/exclusion_zone_description.hpp"
37 
38 #include "nav2_collision_monitor/types.hpp"
39 
40 namespace nav2_collision_monitor
41 {
42 
56 {
57 public:
69  const nav2::LifecycleNode::WeakPtr & node,
70  const std::string & zone_name,
71  const nav2::TransformBuffer::SharedPtr tf_buffer,
72  const std::string & base_frame_id,
73  const std::string & global_frame_id,
74  const tf2::Duration & transform_tolerance,
75  const bool base_shift_correction);
76 
81 
86  bool configure();
87 
93  bool configure(const nav2_msgs::msg::ExclusionZoneDescription & desc);
94 
98  void activate();
99 
103  void deactivate();
104 
112  void apply(const rclcpp::Time & curr_time, std::vector<Point> & data) const;
113 
118  std::string getName() const;
119 
124  bool getEnabled() const;
125 
129  void publish() const;
130 
131 protected:
136  bool getParameters();
137 
142  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
143 
149  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
150  const std::vector<rclcpp::Parameter> & parameters);
151 
159  const rclcpp::Time & curr_time, tf2::Transform & tf_zone_to_base) const;
160 
161  // ----- Variables -----
162 
164  nav2::LifecycleNode::WeakPtr node_;
166  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
168  rclcpp::Clock::SharedPtr node_clock_;
170  mutable std::mutex mutex_;
171  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
172  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
173 
175  std::string zone_name_;
176 
178  nav2::TransformBuffer::SharedPtr tf_buffer_;
180  std::string base_frame_id_;
182  std::string global_frame_id_;
184  tf2::Duration transform_tolerance_;
191 
193  std::string frame_id_;
195  bool is_circle_{false};
197  std::vector<Point> poly_;
199  double radius_{0.0};
201  double radius_squared_{0.0};
203  double min_height_;
205  double max_height_;
207  bool enabled_{false};
208 
210  bool visualize_{false};
212  nav2::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr zone_pub_;
213 }; // class ExclusionZone
214 
215 } // namespace nav2_collision_monitor
216 
217 #endif // NAV2_COLLISION_MONITOR__EXCLUSION_ZONE_HPP_
Region that removes (masks out) source points falling inside it.
bool base_shift_correction_
Whether to correct the zone transform for base movement between data and current time.
void deactivate()
Deactivates the visualization publisher (if any)
nav2::TransformBuffer::SharedPtr tf_buffer_
TF buffer.
double min_height_
Lower bound of the height band (base-frame z) a point must be within to be excluded.
void apply(const rclcpp::Time &curr_time, std::vector< Point > &data) const
Removes from data all points that fall inside the (enabled) zone. No-op when the zone is disabled....
rclcpp::Logger logger_
Collision monitor node logger.
double base_transform_staleness_threshold_
Maximum age in seconds for the robot's global-frame transform.
bool enabled_
Whether the zone is currently active.
double radius_
Circle radius (for circle type)
double radius_squared_
radius squared, cached
~ExclusionZone()
ExclusionZone destructor.
bool getParameters()
Reads ROS parameters for the zone.
ExclusionZone(const nav2::LifecycleNode::WeakPtr &node, const std::string &zone_name, const nav2::TransformBuffer::SharedPtr tf_buffer, const std::string &base_frame_id, const std::string &global_frame_id, const tf2::Duration &transform_tolerance, const bool base_shift_correction)
ExclusionZone constructor.
bool is_circle_
Whether the zone shape is a circle (otherwise polygon)
bool getZoneToBaseTransform(const rclcpp::Time &curr_time, tf2::Transform &tf_zone_to_base) const
Resolve the zone-frame -> base-frame transform for this cycle.
std::vector< Point > poly_
Zone polygon vertices, expressed in frame_id_ (for polygon type)
bool visualize_
Whether to publish the zone footprint for visualization.
std::string zone_name_
Name of the zone.
bool configure()
Reads ROS parameters and configures the zone.
std::string global_frame_id_
Global (fixed) frame ID, used to bridge the zone lookup in time.
std::string frame_id_
Frame the zone shape is anchored to (tracked via TF). Defaults to base_frame_id_.
std::string base_frame_id_
Robot base frame ID.
void publish() const
Publishes the zone footprint for visualization (if enabled)
void activate()
Activates the visualization publisher (if any)
nav2::LifecycleNode::WeakPtr node_
Collision Monitor node.
double transform_staleness_threshold_
Maximum age in seconds for the zone-frame transform.
bool getEnabled() const
Obtains the enabled state of the zone.
rclcpp::Clock::SharedPtr node_clock_
Node clock (for throttled logging and message stamps)
void updateParametersCallback(const std::vector< rclcpp::Parameter > &parameters)
Apply parameter updates after validation (dynamic reconfigure)
std::string getName() const
Obtains the name of the zone.
std::mutex mutex_
Dynamic parameters handlers.
double max_height_
Upper bound of the height band (base-frame z) a point must be within to be excluded.
rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(const std::vector< rclcpp::Parameter > &parameters)
Validate incoming parameter updates before applying them.
nav2::Publisher< geometry_msgs::msg::PolygonStamped >::SharedPtr zone_pub_
Zone footprint publisher.
tf2::Duration transform_tolerance_
Transform tolerance.