Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
distance_controller.cpp
1 // Copyright (c) 2018 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 <chrono>
17 #include <string>
18 #include <memory>
19 #include <cmath>
20 
21 #include "nav2_util/robot_utils.hpp"
22 #include "nav2_util/geometry_utils.hpp"
23 #include "geometry_msgs/msg/pose_stamped.hpp"
24 #include "nav2_ros_common/tf2_factories.hpp"
25 
26 #include "behaviortree_cpp/decorator_node.h"
27 
28 #include "nav2_behavior_tree/plugins/decorator/distance_controller.hpp"
29 
30 namespace nav2_behavior_tree
31 {
32 
34  const std::string & name,
35  const BT::NodeConfiguration & conf)
36 : BT::DecoratorNode(name, conf),
37  distance_(1.0),
38  first_time_(false)
39 {
40  getInput("distance", distance_);
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 
54 inline BT::NodeStatus DistanceController::tick()
55 {
56  if (!BT::isStatusActive(status())) {
57  // Reset the starting position since we're starting a new iteration of
58  // the distance controller (moving from IDLE to RUNNING)
59  if (!nav2_util::getFreshPose(
60  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
61  start_pose_))
62  {
63  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
64  return BT::NodeStatus::FAILURE;
65  }
66  first_time_ = true;
67  }
68 
69  setStatus(BT::NodeStatus::RUNNING);
70 
71  // Determine distance travelled since we've started this iteration
72  geometry_msgs::msg::PoseStamped current_pose;
73  if (!nav2_util::getFreshPose(
74  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
75  current_pose))
76  {
77  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
78  return BT::NodeStatus::FAILURE;
79  }
80 
81  // Get euclidean distance
82  auto travelled = nav2_util::geometry_utils::euclidean_distance(
83  start_pose_.pose, current_pose.pose);
84 
85  // The child gets ticked the first time through and every time the threshold
86  // distance is crossed. In addition, once the child begins to run, it is
87  // ticked each time 'til completion
88  if (first_time_ || (child_node_->status() == BT::NodeStatus::RUNNING) ||
89  travelled >= distance_)
90  {
91  first_time_ = false;
92  const BT::NodeStatus child_state = child_node_->executeTick();
93 
94  switch (child_state) {
95  case BT::NodeStatus::SKIPPED:
96  case BT::NodeStatus::RUNNING:
97  return child_state;
98 
99  case BT::NodeStatus::SUCCESS:
100  if (!nav2_util::getFreshPose(
101  *tf_, global_frame_, robot_base_frame_, clock_->now(), transform_staleness_threshold_,
102  start_pose_))
103  {
104  RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
105  return BT::NodeStatus::FAILURE;
106  }
107  return BT::NodeStatus::SUCCESS;
108 
109  case BT::NodeStatus::FAILURE:
110  default:
111  return BT::NodeStatus::FAILURE;
112  }
113  }
114 
115  return status();
116 }
117 
118 } // namespace nav2_behavior_tree
119 
120 #include "behaviortree_cpp/bt_factory.h"
121 BT_REGISTER_NODES(factory)
122 {
123  factory.registerNodeType<nav2_behavior_tree::DistanceController>("DistanceController");
124 }
A BT::DecoratorNode that ticks its child every time the robot travels a specified distance.
DistanceController(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::DistanceController.