Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
progress_checker_selector_node.cpp
1 // Copyright (c) 2024 Open Navigation LLC
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 <string>
16 #include <memory>
17 
18 #include "std_msgs/msg/string.hpp"
19 
20 #include "nav2_behavior_tree/plugins/action/progress_checker_selector_node.hpp"
21 
22 #include "rclcpp/rclcpp.hpp"
23 
24 namespace nav2_behavior_tree
25 {
26 
27 using std::placeholders::_1;
28 
30  const std::string & name,
31  const BT::NodeConfiguration & conf)
32 : BT::SyncActionNode(name, conf)
33 {
34  initialize();
35 }
36 
37 void ProgressCheckerSelector::initialize()
38 {
39  createROSInterfaces();
40 }
41 
42 void ProgressCheckerSelector::createROSInterfaces()
43 {
44  std::string topic_new;
45  getInput("topic_name", topic_new);
46  if (topic_new != topic_name_ || !progress_checker_selector_sub_) {
47  topic_name_ = topic_new;
48  node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
49 
50  rclcpp::QoS qos(rclcpp::KeepLast(1));
51  qos.transient_local().reliable();
52 
53  progress_checker_selector_sub_ = node_->create_subscription<std_msgs::msg::String>(
54  topic_name_, qos,
55  std::bind(&ProgressCheckerSelector::callbackProgressCheckerSelect, this, _1));
56  }
57 }
58 
59 BT::NodeStatus ProgressCheckerSelector::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 progress checker received from the topic input.
68  // When no input is specified it uses the default goaprogressl checker.
69  // If the default progress checker is not specified then we work in
70  // "required progress checker mode": In this mode, the behavior returns failure if the progress
71  // checker selection is not received from the topic input.
72  if (last_selected_progress_checker_.empty()) {
73  std::string default_progress_checker;
74  getInput("default_progress_checker", default_progress_checker);
75  if (default_progress_checker.empty()) {
76  return BT::NodeStatus::FAILURE;
77  } else {
78  last_selected_progress_checker_ = default_progress_checker;
79  }
80  }
81 
82  setOutput("selected_progress_checker", last_selected_progress_checker_);
83 
84  return BT::NodeStatus::SUCCESS;
85 }
86 
87 void
88 ProgressCheckerSelector::callbackProgressCheckerSelect(const std_msgs::msg::String::SharedPtr msg)
89 {
90  last_selected_progress_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::ProgressCheckerSelector>("ProgressCheckerSelector");
99 }
The ProgressCheckerSelector behavior is used to switch the progress checker of the controller server....
ProgressCheckerSelector(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ProgressCheckerSelector.