Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
goal_reached_condition.cpp
1 // Copyright (c) 2019 Intel Corporation
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/goal_reached_condition.hpp"
24 
25 namespace nav2_behavior_tree
26 {
27 
28 GoalReachedCondition::GoalReachedCondition(
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 
35  robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
36  node, "robot_base_frame", this);
37 }
38 
40 {
41  cleanup();
42 }
43 
45 {
46  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
47 
48  goal_reached_tol_ = node_->declare_or_get_parameter("goal_reached_tol", 0.25);
49  tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>("tf_buffer");
50 
51  node_->get_parameter("transform_tolerance", transform_tolerance_);
52 }
53 
55 {
56  if (!BT::isStatusActive(status())) {
57  initialize();
58  }
59 
60  if (isGoalReached()) {
61  return BT::NodeStatus::SUCCESS;
62  }
63  return BT::NodeStatus::FAILURE;
64 }
65 
67 {
68  geometry_msgs::msg::PoseStamped goal;
69  getInput("goal", goal);
70 
71  geometry_msgs::msg::PoseStamped current_pose;
72  if (!nav2_util::getCurrentPose(
73  current_pose, *tf_, goal.header.frame_id, robot_base_frame_, transform_tolerance_))
74  {
75  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
76  return false;
77  }
78 
79  double dx = goal.pose.position.x - current_pose.pose.position.x;
80  double dy = goal.pose.position.y - current_pose.pose.position.y;
81 
82  return (dx * dx + dy * dy) <= (goal_reached_tol_ * goal_reached_tol_);
83 }
84 
85 } // namespace nav2_behavior_tree
86 
87 #include "behaviortree_cpp/bt_factory.h"
88 BT_REGISTER_NODES(factory)
89 {
90  factory.registerNodeType<nav2_behavior_tree::GoalReachedCondition>("GoalReached");
91 }
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.
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.