Nav2 Navigation Stack - jazzy  jazzy
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 
37 {
38  RCLCPP_DEBUG(node_->get_logger(), "Shutting down TransformAvailableCondition BT node");
39 }
40 
42 {
43  getInput("child", child_frame_);
44  getInput("parent", parent_frame_);
45 
46  if (child_frame_.empty() || parent_frame_.empty()) {
47  RCLCPP_FATAL(
48  node_->get_logger(), "Child frame (%s) or parent frame (%s) were empty.",
49  child_frame_.c_str(), parent_frame_.c_str());
50  throw std::runtime_error("TransformAvailableCondition: Child or parent frames not provided!");
51  }
52 
53  RCLCPP_DEBUG(node_->get_logger(), "Initialized an TransformAvailableCondition BT node");
54 }
55 
57 {
58  if (!BT::isStatusActive(status())) {
59  initialize();
60  }
61 
62  if (was_found_) {
63  return BT::NodeStatus::SUCCESS;
64  }
65 
66  std::string tf_error;
67  bool found = tf_->canTransform(
68  child_frame_, parent_frame_, tf2::TimePointZero, &tf_error);
69 
70  if (found) {
71  was_found_ = true;
72  return BT::NodeStatus::SUCCESS;
73  }
74 
75  RCLCPP_INFO(
76  node_->get_logger(), "Transform from %s to %s was not found, tf error: %s",
77  child_frame_.c_str(), parent_frame_.c_str(), tf_error.c_str());
78 
79  return BT::NodeStatus::FAILURE;
80 }
81 
82 } // namespace nav2_behavior_tree
83 
84 #include "behaviortree_cpp/bt_factory.h"
85 BT_REGISTER_NODES(factory)
86 {
87  factory.registerNodeType<nav2_behavior_tree::TransformAvailableCondition>("TransformAvailable");
88 }
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.