Nav2 Navigation Stack - rolling  main
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  clock_ = node_->get_clock();
43  tf_ = config().blackboard->get<nav2::TransformBuffer::SharedPtr>("tf_buffer");
44  node_->get_parameter("transform_tolerance", transform_tolerance_);
45  transform_staleness_threshold_ = node_->declare_or_get_parameter(
46  "transform_staleness_threshold", 0.0);
47 
48  global_frame_ = BT::deconflictPortAndParamFrame<std::string>(
49  node_, "global_frame", this);
50  robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
51  node_, "robot_base_frame", this);
52 }
53 
55 {
56  if (!BT::isStatusActive(status())) {
57  initialize();
58  if (!nav2_util::getFreshPose(
59  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
60  start_pose_))
61  {
62  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
63  }
64  return BT::NodeStatus::FAILURE;
65  }
66 
67  // Determine distance travelled since we've started this iteration
68  geometry_msgs::msg::PoseStamped current_pose;
69  if (!nav2_util::getFreshPose(
70  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
71  current_pose))
72  {
73  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
74  return BT::NodeStatus::FAILURE;
75  }
76 
77  // Get euclidean distance
78  auto travelled = nav2_util::geometry_utils::euclidean_distance(
79  start_pose_.pose, current_pose.pose);
80 
81  if (travelled < distance_) {
82  return BT::NodeStatus::FAILURE;
83  }
84 
85  // Update start pose
86  start_pose_ = current_pose;
87 
88  return BT::NodeStatus::SUCCESS;
89 }
90 
91 } // namespace nav2_behavior_tree
92 
93 #include "behaviortree_cpp/bt_factory.h"
94 BT_REGISTER_NODES(factory)
95 {
96  factory.registerNodeType<nav2_behavior_tree::DistanceTraveledCondition>("DistanceTraveled");
97 }
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.