Nav2 Navigation Stack - humble  humble
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  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
36 
37  getInput("topic_name", topic_name_);
38 
39  rclcpp::QoS qos(rclcpp::KeepLast(1));
40  qos.transient_local().reliable();
41 
42  goal_checker_selector_sub_ = node_->create_subscription<std_msgs::msg::String>(
43  topic_name_, qos, std::bind(&GoalCheckerSelector::callbackGoalCheckerSelect, this, _1));
44 }
45 
46 BT::NodeStatus GoalCheckerSelector::tick()
47 {
48  rclcpp::spin_some(node_);
49 
50  // This behavior always use the last selected goal checker received from the topic input.
51  // When no input is specified it uses the default goal checker.
52  // If the default goal checker is not specified then we work in "required goal checker mode":
53  // In this mode, the behavior returns failure if the goal checker selection is not received from
54  // the topic input.
55  if (last_selected_goal_checker_.empty()) {
56  std::string default_goal_checker;
57  getInput("default_goal_checker", default_goal_checker);
58  if (default_goal_checker.empty()) {
59  return BT::NodeStatus::FAILURE;
60  } else {
61  last_selected_goal_checker_ = default_goal_checker;
62  }
63  }
64 
65  setOutput("selected_goal_checker", last_selected_goal_checker_);
66 
67  return BT::NodeStatus::SUCCESS;
68 }
69 
70 void
71 GoalCheckerSelector::callbackGoalCheckerSelect(const std_msgs::msg::String::SharedPtr msg)
72 {
73  last_selected_goal_checker_ = msg->data;
74 }
75 
76 } // namespace nav2_behavior_tree
77 
78 #include "behaviortree_cpp_v3/bt_factory.h"
79 BT_REGISTER_NODES(factory)
80 {
81  factory.registerNodeType<nav2_behavior_tree::GoalCheckerSelector>("GoalCheckerSelector");
82 }
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.