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/rclcpp.hpp"
24 #include "geometry_msgs/msg/polygon_stamped.hpp"
25 
26 #include "tf2/time.hpp"
27 #include "tf2/LinearMath/Transform.hpp"
28 
29 #include "nav2_ros_common/lifecycle_node.hpp"
30 #include "nav2_ros_common/tf2_factories.hpp"
31 
32 #include "nav2_msgs/msg/exclusion_zone_description.hpp"
33 
34 #include "nav2_collision_monitor/types.hpp"
35 
36 namespace nav2_collision_monitor
37 {
38 
52 {
53 public:
65  const nav2::LifecycleNode::WeakPtr & node,
66  const std::string & zone_name,
67  const nav2::TransformBuffer::SharedPtr tf_buffer,
68  const std::string & base_frame_id,
69  const std::string & global_frame_id,
70  const tf2::Duration & transform_tolerance,
71  const bool base_shift_correction);
72 
77 
82  bool configure();
83 
89  bool configure(const nav2_msgs::msg::ExclusionZoneDescription & desc);
90 
94  void activate();
95 
99  void deactivate();
100 
108  void apply(const rclcpp::Time & curr_time, std::vector<Point> & data) const;
109 
114  std::string getName() const;
115 
120  bool getEnabled() const;
121 
125  void publish() const;
126 
127 protected:
132  bool getParameters();
133 
138  void updateParametersCallback(const std::vector<rclcpp::Parameter> & parameters);
139 
145  rcl_interfaces::msg::SetParametersResult validateParameterUpdatesCallback(
146  const std::vector<rclcpp::Parameter> & parameters);
147 
155  const rclcpp::Time & curr_time, tf2::Transform & tf_zone_to_base) const;
156 
157  // ----- Variables -----
158 
160  nav2::LifecycleNode::WeakPtr node_;
162  rclcpp::Logger logger_{rclcpp::get_logger("collision_monitor")};
164  rclcpp::Clock::SharedPtr node_clock_;
166  mutable std::mutex mutex_;
167  rclcpp::node_interfaces::PostSetParametersCallbackHandle::SharedPtr post_set_params_handler_;
168  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr on_set_params_handler_;
169 
171  std::string zone_name_;
172 
174  nav2::TransformBuffer::SharedPtr tf_buffer_;
176  std::string base_frame_id_;
178  std::string global_frame_id_;
180  tf2::Duration transform_tolerance_;
183 
186  double frame_hold_timeout_{0.0};
187 
189  std::string frame_id_;
191  bool is_circle_{false};
193  std::vector<Point> poly_;
195  double radius_{0.0};
197  double radius_squared_{0.0};
199  double min_height_;
201  double max_height_;
203  bool enabled_{false};
204 
206  bool visualize_{false};
208  nav2::Publisher<geometry_msgs::msg::PolygonStamped>::SharedPtr zone_pub_;
209 }; // class ExclusionZone
210 
211 } // namespace nav2_collision_monitor
212 
213 #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.
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)
double frame_hold_timeout_
Extra time (s) beyond the transform tolerance that a stale zone-frame pose may keep being used before...
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.
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.