Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
goal_checker_selector_node.cpp
1 // Copyright (c) 2018 Intel Corporation
2 // Copyright (c) 2020 Pablo IƱigo Blasco
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 "std_msgs/msg/string.hpp"
20 
21 #include "nav2_behavior_tree/plugins/action/goal_checker_selector_node.hpp"
22 
23 #include "rclcpp/rclcpp.hpp"
24 
25 namespace nav2_behavior_tree
26 {
27 
28 using std::placeholders::_1;
29 
31  const std::string & name,
32  const BT::NodeConfiguration & conf)
33 : BT::SyncActionNode(name, conf)
34 {
35  initialize();
36 }
37 
38 void GoalCheckerSelector::initialize()
39 {
40  createROSInterfaces();
41 }
42 
43 void GoalCheckerSelector::createROSInterfaces()
44 {
45  std::string topic_new;
46  getInput("topic_name", topic_new);
47  if (topic_new != topic_name_ || !goal_checker_selector_sub_) {
48  topic_name_ = topic_new;
49  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
50 
51  rclcpp::QoS qos(rclcpp::KeepLast(1));
52  qos.transient_local().reliable();
53 
54  goal_checker_selector_sub_ = node_->create_subscription<std_msgs::msg::String>(
55  topic_name_, qos, std::bind(&GoalCheckerSelector::callbackGoalCheckerSelect, this, _1));
56  }
57 }
58 
59 BT::NodeStatus GoalCheckerSelector::tick()
60 {
61  if (!BT::isStatusActive(status())) {
62  initialize();
63  }
64 
65  rclcpp::spin_some(node_);
66 
67  // This behavior always use the last selected goal checker received from the topic input.
68  // When no input is specified it uses the default goal checker.
69  // If the default goal checker is not specified then we work in "required goal checker mode":
70  // In this mode, the behavior returns failure if the goal checker selection is not received from
71  // the topic input.
72  if (last_selected_goal_checker_.empty()) {
73  std::string default_goal_checker;
74  getInput("default_goal_checker", default_goal_checker);
75  if (default_goal_checker.empty()) {
76  return BT::NodeStatus::FAILURE;
77  } else {
78  last_selected_goal_checker_ = default_goal_checker;
79  }
80  }
81 
82  setOutput("selected_goal_checker", last_selected_goal_checker_);
83 
84  return BT::NodeStatus::SUCCESS;
85 }
86 
87 void
88 GoalCheckerSelector::callbackGoalCheckerSelect(const std_msgs::msg::String::SharedPtr msg)
89 {
90  last_selected_goal_checker_ = msg->data;
91 }
92 
93 } // namespace nav2_behavior_tree
94 
95 #include "behaviortree_cpp/bt_factory.h"
96 BT_REGISTER_NODES(factory)
97 {
98  factory.registerNodeType<nav2_behavior_tree::GoalCheckerSelector>("GoalCheckerSelector");
99 }
The GoalCheckerSelector behavior is used to switch the goal checker of the controller server....
GoalCheckerSelector(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::GoalCheckerSelector.