Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
transform_available_condition.cpp
1 // Copyright (c) 2020 Samsung Research America
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 <chrono>
17 #include <memory>
18 
19 #include "nav2_behavior_tree/plugins/condition/transform_available_condition.hpp"
20 
21 using namespace std::chrono_literals; // NOLINT
22 
23 namespace nav2_behavior_tree
24 {
25 
26 TransformAvailableCondition::TransformAvailableCondition(
27  const std::string & condition_name,
28  const BT::NodeConfiguration & conf)
29 : BT::ConditionNode(condition_name, conf),
30  was_found_(false)
31 {
32  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
33  tf_ = config().blackboard->get<std::shared_ptr<tf2_ros::Buffer>>("tf_buffer");
34 
35  getInput("child", child_frame_);
36  getInput("parent", parent_frame_);
37 
38  if (child_frame_.empty() || parent_frame_.empty()) {
39  RCLCPP_FATAL(
40  node_->get_logger(), "Child frame (%s) or parent frame (%s) were empty.",
41  child_frame_.c_str(), parent_frame_.c_str());
42  exit(-1);
43  }
44 
45  RCLCPP_DEBUG(node_->get_logger(), "Initialized an TransformAvailableCondition BT node");
46 }
47 
49 {
50  RCLCPP_DEBUG(node_->get_logger(), "Shutting down TransformAvailableCondition BT node");
51 }
52 
54 {
55  if (was_found_) {
56  return BT::NodeStatus::SUCCESS;
57  }
58 
59  std::string tf_error;
60  bool found = tf_->canTransform(
61  child_frame_, parent_frame_, tf2::TimePointZero, &tf_error);
62 
63  if (found) {
64  was_found_ = true;
65  return BT::NodeStatus::SUCCESS;
66  }
67 
68  RCLCPP_INFO(
69  node_->get_logger(), "Transform from %s to %s was not found, tf error: %s",
70  child_frame_.c_str(), parent_frame_.c_str(), tf_error.c_str());
71 
72  return BT::NodeStatus::FAILURE;
73 }
74 
75 } // namespace nav2_behavior_tree
76 
77 #include "behaviortree_cpp_v3/bt_factory.h"
78 BT_REGISTER_NODES(factory)
79 {
80  factory.registerNodeType<nav2_behavior_tree::TransformAvailableCondition>("TransformAvailable");
81 }
A BT::ConditionNode that returns SUCCESS if there is a valid transform between two specified frames a...
~TransformAvailableCondition()
A destructor for nav2_behavior_tree::TransformAvailableCondition.
BT::NodeStatus tick() override
The main override required by a BT action.