Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
is_within_path_tracking_bounds_condition.cpp
1 // Copyright (c) 2025 Berkan Tali
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 #include "nav2_behavior_tree/plugins/condition/is_within_path_tracking_bounds_condition.hpp"
16 
17 namespace nav2_behavior_tree
18 {
19 
20 IsWithinPathTrackingBoundsCondition::IsWithinPathTrackingBoundsCondition(
21  const std::string & condition_name,
22  const BT::NodeConfiguration & conf)
23 : BT::ConditionNode(condition_name, conf)
24 {
25  auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
26  logger_ = node->get_logger();
27  clock_ = node->get_clock();
28 }
29 
31 {
32  getInput<double>("max_error_left", max_error_left_);
33  getInput<double>("max_error_right", max_error_right_);
34  getInput<double>("max_error_heading", max_error_heading_);
35  if (max_error_right_ < 0.0) {
36  RCLCPP_WARN_ONCE(logger_, "max_error_right should be positive, using absolute value");
37  max_error_right_ = std::abs(max_error_right_);
38  }
39  if (max_error_left_ < 0.0) {
40  RCLCPP_WARN_ONCE(logger_, "max_error_left should be positive, using absolute value");
41  max_error_left_ = std::abs(max_error_left_);
42  }
43  if (max_error_heading_ < 0.0) {
44  RCLCPP_WARN_ONCE(logger_, "max_error_heading should be positive, using absolute value");
45  max_error_heading_ = std::abs(max_error_heading_);
46  }
47 }
48 
50 {
51  if (!BT::isStatusActive(status())) {
52  initialize();
53  }
54 
55  // If not set yet (like on starting to track)
56  // pass as initialization should always be within tracking bounds
57  if (!getInput<nav2_msgs::msg::TrackingFeedback>("tracking_feedback", tracking_feedback_)) {
58  return BT::NodeStatus::SUCCESS;
59  }
60  double position_tracking_error = tracking_feedback_.position_tracking_error;
61  double heading_tracking_error = tracking_feedback_.heading_tracking_error;
62  bool is_within_position_bounds;
63  if (position_tracking_error >= 0.0) { // Positive or zero = left side (or on path)
64  is_within_position_bounds = (position_tracking_error <= max_error_left_);
65  } else { // Negative = right side
66  is_within_position_bounds = (std::abs(position_tracking_error) <= max_error_right_);
67  }
68 
69  bool is_within_heading_bounds = std::abs(heading_tracking_error) <= max_error_heading_;
70 
71  if (is_within_position_bounds && is_within_heading_bounds) {
72  return BT::NodeStatus::SUCCESS;
73  } else {
74  RCLCPP_WARN_THROTTLE(
75  logger_,
76  *clock_,
77  1000,
78  "Robot is out of path tracking bounds! Position error: %.2f, Heading error: %.2f",
79  position_tracking_error, heading_tracking_error);
80  return BT::NodeStatus::FAILURE;
81  }
82 }
83 
84 } // namespace nav2_behavior_tree
85 
86 #include "behaviortree_cpp/bt_factory.h"
87 BT_REGISTER_NODES(factory)
88 {
90  "IsWithinPathTrackingBounds");
91 }
A BT::ConditionNode that returns SUCCESS when the current path tracking error is within specified bou...
BT::NodeStatus tick() override
The main override required by a BT action.
void initialize()
Function to read parameters and initialize class variables.