Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
are_poses_near_condition.cpp
1 // Copyright (c) 2025 Open Navigation LLC
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 <string>
16 #include <memory>
17 
18 #include "nav2_util/robot_utils.hpp"
19 #include "geometry_msgs/msg/pose_stamped.hpp"
20 #include "nav2_ros_common/node_utils.hpp"
21 #include "nav2_ros_common/tf2_factories.hpp"
22 
23 #include "nav2_behavior_tree/plugins/condition/are_poses_near_condition.hpp"
24 
25 namespace nav2_behavior_tree
26 {
27 
29  const std::string & condition_name,
30  const BT::NodeConfiguration & conf)
31 : BT::ConditionNode(condition_name, conf)
32 {
33  auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
34  global_frame_ = BT::deconflictPortAndParamFrame<std::string>(
35  node, "global_frame", this);
36 }
37 
39 {
40  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
41  tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>("tf_buffer");
42  node_->get_parameter("transform_tolerance", transform_tolerance_);
43 }
44 
46 {
47  if (!BT::isStatusActive(status())) {
48  initialize();
49  }
50 
51  if (arePosesNearby()) {
52  return BT::NodeStatus::SUCCESS;
53  }
54  return BT::NodeStatus::FAILURE;
55 }
56 
58 {
59  geometry_msgs::msg::PoseStamped pose1, pose2;
60  double tol;
61  getInput("ref_pose", pose1);
62  getInput("target_pose", pose2);
63  getInput("tolerance", tol);
64 
65  if (pose1.header.frame_id != pose2.header.frame_id) {
66  if (!nav2_util::transformPoseInTargetFrame(
67  pose1, pose1, *tf_, global_frame_, transform_tolerance_) ||
68  !nav2_util::transformPoseInTargetFrame(
69  pose2, pose2, *tf_, global_frame_, transform_tolerance_))
70  {
71  RCLCPP_ERROR(node_->get_logger(), "Failed to transform poses to the same frame");
72  return false;
73  }
74  }
75 
76  double dx = pose1.pose.position.x - pose2.pose.position.x;
77  double dy = pose1.pose.position.y - pose2.pose.position.y;
78  return (dx * dx + dy * dy) <= (tol * tol);
79 }
80 
81 } // namespace nav2_behavior_tree
82 
83 #include "behaviortree_cpp/bt_factory.h"
84 BT_REGISTER_NODES(factory)
85 {
86  factory.registerNodeType<nav2_behavior_tree::ArePosesNearCondition>("ArePosesNear");
87 }
A BT::ConditionNode that returns SUCCESS when a specified goal is reached and FAILURE otherwise.
ArePosesNearCondition(const std::string &condition_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ArePosesNearCondition.
BT::NodeStatus tick() override
The main override required by a BT action.
bool arePosesNearby()
Checks if the current robot pose lies within a given distance from the goal.
void initialize()
Function to read parameters and initialize class variables.