Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
goal_updated_controller.cpp
1 // Copyright (c) 2018 Intel Corporation
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 <string>
17 
18 #include "rclcpp/rclcpp.hpp"
19 #include "geometry_msgs/msg/pose_stamped.hpp"
20 #include "behaviortree_cpp_v3/decorator_node.h"
21 #include "nav2_behavior_tree/plugins/decorator/goal_updated_controller.hpp"
22 
23 
24 namespace nav2_behavior_tree
25 {
26 
28  const std::string & name,
29  const BT::NodeConfiguration & conf)
30 : BT::DecoratorNode(name, conf)
31 {
32 }
33 
34 BT::NodeStatus GoalUpdatedController::tick()
35 {
36  if (status() == BT::NodeStatus::IDLE) {
37  // Reset since we're starting a new iteration of
38  // the goal updated controller (moving from IDLE to RUNNING)
39 
40  config().blackboard->get<std::vector<geometry_msgs::msg::PoseStamped>>("goals", goals_);
41  config().blackboard->get<geometry_msgs::msg::PoseStamped>("goal", goal_);
42 
43  goal_was_updated_ = true;
44  }
45 
46  setStatus(BT::NodeStatus::RUNNING);
47 
48  std::vector<geometry_msgs::msg::PoseStamped> current_goals;
49  config().blackboard->get<std::vector<geometry_msgs::msg::PoseStamped>>("goals", current_goals);
50  geometry_msgs::msg::PoseStamped current_goal;
51  config().blackboard->get<geometry_msgs::msg::PoseStamped>("goal", current_goal);
52 
53  if (goal_ != current_goal || goals_ != current_goals) {
54  goal_ = current_goal;
55  goals_ = current_goals;
56  goal_was_updated_ = true;
57  }
58 
59  // The child gets ticked the first time through and any time the goal has
60  // changed or preempted. In addition, once the child begins to run, it is ticked each time
61  // 'til completion
62  if ((child_node_->status() == BT::NodeStatus::RUNNING) || goal_was_updated_) {
63  goal_was_updated_ = false;
64  const BT::NodeStatus child_state = child_node_->executeTick();
65 
66  switch (child_state) {
67  case BT::NodeStatus::RUNNING:
68  return BT::NodeStatus::RUNNING;
69 
70  case BT::NodeStatus::SUCCESS:
71  return BT::NodeStatus::SUCCESS;
72 
73  case BT::NodeStatus::FAILURE:
74  default:
75  return BT::NodeStatus::FAILURE;
76  }
77  }
78 
79  return status();
80 }
81 
82 } // namespace nav2_behavior_tree
83 
84 #include "behaviortree_cpp_v3/bt_factory.h"
85 BT_REGISTER_NODES(factory)
86 {
87  factory.registerNodeType<nav2_behavior_tree::GoalUpdatedController>("GoalUpdatedController");
88 }
A BT::DecoratorNode that ticks its child if the goal was updated.
GoalUpdatedController(const std::string &name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::GoalUpdatedController.