Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
distance_traveled_condition.cpp
1 // Copyright (c) 2019 Intel Corporation
2 // Copyright (c) 2020 Sarthak Mittal
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <string>
17 #include <memory>
18 
19 #include "nav2_util/robot_utils.hpp"
20 #include "nav2_util/geometry_utils.hpp"
21 #include "nav2_ros_common/tf2_factories.hpp"
22 
23 #include "nav2_behavior_tree/plugins/condition/distance_traveled_condition.hpp"
24 
25 namespace nav2_behavior_tree
26 {
27 
28 DistanceTraveledCondition::DistanceTraveledCondition(
29  const std::string & condition_name,
30  const BT::NodeConfiguration & conf)
31 : BT::ConditionNode(condition_name, conf),
32  distance_(1.0),
33  transform_tolerance_(0.1)
34 {
35 }
36 
38 {
39  getInput("distance", distance_);
40 
41  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
42  tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>("tf_buffer");
43  node_->get_parameter("transform_tolerance", transform_tolerance_);
44 
45  global_frame_ = BT::deconflictPortAndParamFrame<std::string>(
46  node_, "global_frame", this);
47  robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
48  node_, "robot_base_frame", this);
49 }
50 
52 {
53  if (!BT::isStatusActive(status())) {
54  initialize();
55  if (!nav2_util::getCurrentPose(
56  start_pose_, *tf_, global_frame_, robot_base_frame_,
57  transform_tolerance_))
58  {
59  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
60  }
61  return BT::NodeStatus::FAILURE;
62  }
63 
64  // Determine distance travelled since we've started this iteration
65  geometry_msgs::msg::PoseStamped current_pose;
66  if (!nav2_util::getCurrentPose(
67  current_pose, *tf_, global_frame_, robot_base_frame_,
68  transform_tolerance_))
69  {
70  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
71  return BT::NodeStatus::FAILURE;
72  }
73 
74  // Get euclidean distance
75  auto travelled = nav2_util::geometry_utils::euclidean_distance(
76  start_pose_.pose, current_pose.pose);
77 
78  if (travelled < distance_) {
79  return BT::NodeStatus::FAILURE;
80  }
81 
82  // Update start pose
83  start_pose_ = current_pose;
84 
85  return BT::NodeStatus::SUCCESS;
86 }
87 
88 } // namespace nav2_behavior_tree
89 
90 #include "behaviortree_cpp/bt_factory.h"
91 BT_REGISTER_NODES(factory)
92 {
93  factory.registerNodeType<nav2_behavior_tree::DistanceTraveledCondition>("DistanceTraveled");
94 }
A BT::ConditionNode that returns SUCCESS every time the robot travels a specified distance and FAILUR...
BT::NodeStatus tick() override
The main override required by a BT action.
void initialize()
Function to read parameters and initialize class variables.