Nav2 Navigation Stack - rolling  main
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 <chrono>
16 #include <memory>
17 #include <string>
18 
19 #include "rclcpp/rclcpp.hpp"
20 #include "tf2/time.hpp"
21 #include "tf2_ros/buffer.hpp"
22 
23 #include "nav2_behavior_tree/plugins/condition/transform_available_condition.hpp"
24 
25 using namespace std::chrono_literals; // NOLINT
26 
27 namespace nav2_behavior_tree
28 {
29 
30 TransformAvailableCondition::TransformAvailableCondition(
31  const std::string & condition_name,
32  const BT::NodeConfiguration & conf)
33 : BT::ConditionNode(condition_name, conf),
34  was_found_(false)
35 {
36  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
37  tf_ = config().blackboard->get<std::shared_ptr<tf2_ros::Buffer>>("tf_buffer");
38 }
39 
41 {
42  RCLCPP_DEBUG(node_->get_logger(), "Shutting down TransformAvailableCondition BT node");
43 }
44 
46 {
47  getInput("child", child_frame_);
48  getInput("parent", parent_frame_);
49 
50  if (child_frame_.empty() || parent_frame_.empty()) {
51  RCLCPP_FATAL(
52  node_->get_logger(), "Child frame (%s) or parent frame (%s) were empty.",
53  child_frame_.c_str(), parent_frame_.c_str());
54  throw std::runtime_error("TransformAvailableCondition: Child or parent frames not provided!");
55  }
56 
57  RCLCPP_DEBUG(node_->get_logger(), "Initialized an TransformAvailableCondition BT node");
58 }
59 
61 {
62  if (!BT::isStatusActive(status())) {
63  initialize();
64  }
65 
66  if (was_found_) {
67  return BT::NodeStatus::SUCCESS;
68  }
69 
70  std::string tf_error;
71  bool found = tf_->canTransform(
72  child_frame_, parent_frame_, tf2::TimePointZero, &tf_error);
73 
74  if (found) {
75  was_found_ = true;
76  return BT::NodeStatus::SUCCESS;
77  }
78 
79  RCLCPP_INFO(
80  node_->get_logger(), "Transform from %s to %s was not found, tf error: %s",
81  child_frame_.c_str(), parent_frame_.c_str(), tf_error.c_str());
82 
83  return BT::NodeStatus::FAILURE;
84 }
85 
86 } // namespace nav2_behavior_tree
87 
88 #include "behaviortree_cpp/bt_factory.h"
89 BT_REGISTER_NODES(factory)
90 {
91  factory.registerNodeType<nav2_behavior_tree::TransformAvailableCondition>("TransformAvailable");
92 }
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.
void initialize()
Function to read parameters and initialize class variables.
BT::NodeStatus tick() override
The main override required by a BT action.