Nav2 Navigation Stack - jazzy  jazzy
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/decorator_node.h"
21 #include "nav2_behavior_tree/plugins/decorator/goal_updated_controller.hpp"
22 
23 namespace nav2_behavior_tree
24 {
25 
27  const std::string & name,
28  const BT::NodeConfiguration & conf)
29 : BT::DecoratorNode(name, conf)
30 {
31 }
32 
33 BT::NodeStatus GoalUpdatedController::tick()
34 {
35  if (!BT::isStatusActive(status())) {
36  // Reset since we're starting a new iteration of
37  // the goal updated controller (moving from IDLE to RUNNING)
38 
39  BT::getInputOrBlackboard("goals", goals_);
40  BT::getInputOrBlackboard("goal", goal_);
41 
42  goal_was_updated_ = true;
43  }
44 
45  setStatus(BT::NodeStatus::RUNNING);
46 
47  std::vector<geometry_msgs::msg::PoseStamped> current_goals;
48  BT::getInputOrBlackboard("goals", current_goals);
49  geometry_msgs::msg::PoseStamped current_goal;
50  BT::getInputOrBlackboard("goal", current_goal);
51 
52  if (goal_ != current_goal || goals_ != current_goals) {
53  goal_ = current_goal;
54  goals_ = current_goals;
55  goal_was_updated_ = true;
56  }
57 
58  // The child gets ticked the first time through and any time the goal has
59  // changed or preempted. In addition, once the child begins to run, it is ticked each time
60  // 'til completion
61  if ((child_node_->status() == BT::NodeStatus::RUNNING) || goal_was_updated_) {
62  goal_was_updated_ = false;
63  return child_node_->executeTick();
64  }
65 
66  return status();
67 }
68 
69 } // namespace nav2_behavior_tree
70 
71 #include "behaviortree_cpp/bt_factory.h"
72 BT_REGISTER_NODES(factory)
73 {
74  factory.registerNodeType<nav2_behavior_tree::GoalUpdatedController>("GoalUpdatedController");
75 }
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.