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