|
| 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 | + last_error_(0.0) |
| 25 | +{ |
| 26 | + node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node"); |
| 27 | + callback_group_ = node_->create_callback_group( |
| 28 | + rclcpp::CallbackGroupType::MutuallyExclusive, |
| 29 | + false); |
| 30 | + callback_group_executor_.add_callback_group(callback_group_, node_->get_node_base_interface()); |
| 31 | + |
| 32 | + tracking_feedback_sub_ = node_->create_subscription<nav2_msgs::msg::TrackingFeedback>( |
| 33 | + "/tracking_feedback", |
| 34 | + std::bind(&IsWithinPathTrackingBoundsCondition::trackingFeedbackCallback, this, |
| 35 | + std::placeholders::_1), |
| 36 | + rclcpp::SystemDefaultsQoS(), |
| 37 | + callback_group_); |
| 38 | + |
| 39 | + bt_loop_duration_ = |
| 40 | + config().blackboard->template get<std::chrono::milliseconds>("bt_loop_duration"); |
| 41 | + |
| 42 | + RCLCPP_DEBUG(node_->get_logger(), "Initialized IsWithinPathTrackingBoundsCondition BT node"); |
| 43 | + RCLCPP_INFO_ONCE(node_->get_logger(), "Waiting for tracking error"); |
| 44 | + initialize(); |
| 45 | +} |
| 46 | + |
| 47 | +void IsWithinPathTrackingBoundsCondition::trackingFeedbackCallback( |
| 48 | + const nav2_msgs::msg::TrackingFeedback::SharedPtr msg) |
| 49 | +{ |
| 50 | + last_error_ = msg->tracking_error; |
| 51 | +} |
| 52 | + |
| 53 | +void IsWithinPathTrackingBoundsCondition::initialize() |
| 54 | +{ |
| 55 | + getInput("max_error", max_error_); |
| 56 | +} |
| 57 | + |
| 58 | +BT::NodeStatus IsWithinPathTrackingBoundsCondition::tick() |
| 59 | +{ |
| 60 | + if (!BT::isStatusActive(status())) { |
| 61 | + initialize(); |
| 62 | + } |
| 63 | + |
| 64 | + callback_group_executor_.spin_all(bt_loop_duration_); |
| 65 | + |
| 66 | + if (!getInput("max_error", max_error_)) { |
| 67 | + RCLCPP_ERROR(node_->get_logger(), "max_error parameter not provided"); |
| 68 | + max_error_ = 1.0; // Default fallback |
| 69 | + } |
| 70 | + |
| 71 | + if (max_error_ < 0.0) { |
| 72 | + RCLCPP_WARN(node_->get_logger(), "max_error should be positive, using absolute value"); |
| 73 | + max_error_ = std::abs(max_error_); |
| 74 | + } |
| 75 | + |
| 76 | + if (last_error_ <= max_error_) { |
| 77 | + return BT::NodeStatus::SUCCESS; |
| 78 | + } |
| 79 | + return BT::NodeStatus::FAILURE; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace nav2_behavior_tree |
| 83 | + |
| 84 | +#include "behaviortree_cpp/bt_factory.h" |
| 85 | +BT_REGISTER_NODES(factory) |
| 86 | +{ |
| 87 | + factory.registerNodeType<nav2_behavior_tree::IsWithinPathTrackingBoundsCondition>( |
| 88 | + "IsWithinPathTrackingBounds"); |
| 89 | +} |
0 commit comments