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"
23 #include "nav2_behavior_tree/plugins/condition/goal_reached_condition.hpp"
25 namespace nav2_behavior_tree
28 GoalReachedCondition::GoalReachedCondition(
29 const std::string & condition_name,
30 const BT::NodeConfiguration & conf)
31 : BT::ConditionNode(condition_name, conf)
33 auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>(
"node");
35 robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
36 node,
"robot_base_frame",
this);
46 node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>(
"node");
47 clock_ = node_->get_clock();
49 goal_reached_tol_ = node_->declare_or_get_parameter(
"goal_reached_tol", 0.25);
50 tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>(
"tf_buffer");
52 node_->get_parameter(
"transform_tolerance", transform_tolerance_);
53 transform_staleness_threshold_ = node_->declare_or_get_parameter(
54 "transform_staleness_threshold", 0.0);
59 if (!BT::isStatusActive(status())) {
64 return BT::NodeStatus::SUCCESS;
66 return BT::NodeStatus::FAILURE;
71 geometry_msgs::msg::PoseStamped goal;
72 getInput(
"goal", goal);
74 geometry_msgs::msg::PoseStamped current_pose;
75 if (!nav2_util::getFreshPose(
76 *tf_, goal.header.frame_id, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
79 RCLCPP_DEBUG(node_->get_logger(),
"Current robot pose is not available.");
83 double dx = goal.pose.position.x - current_pose.pose.position.x;
84 double dy = goal.pose.position.y - current_pose.pose.position.y;
86 return (dx * dx + dy * dy) <= (goal_reached_tol_ * goal_reached_tol_);
91 #include "behaviortree_cpp/bt_factory.h"
92 BT_REGISTER_NODES(factory)
A BT::ConditionNode that returns SUCCESS when a specified goal is reached and FAILURE otherwise.
void initialize()
Function to read parameters and initialize class variables.
void cleanup()
Cleanup function.
BT::NodeStatus tick() override
The main override required by a BT action.
~GoalReachedCondition() override
A destructor for nav2_behavior_tree::GoalReachedCondition.
bool isGoalReached()
Checks if the current robot pose lies within a given distance from the goal.