Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
are_error_codes_present_condition.hpp
1 // Copyright (c) 2022 Joshua Wallace
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 #ifndef NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__ARE_ERROR_CODES_PRESENT_CONDITION_HPP_
16 #define NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__ARE_ERROR_CODES_PRESENT_CONDITION_HPP_
17 
18 #include <string>
19 #include <memory>
20 #include <vector>
21 #include <set>
22 
23 #include "rclcpp/rclcpp.hpp"
24 #include "behaviortree_cpp/condition_node.h"
25 
26 namespace nav2_behavior_tree
27 {
28 
29 class AreErrorCodesPresent : public BT::ConditionNode
30 {
31 public:
33  const std::string & condition_name,
34  const BT::NodeConfiguration & conf)
35  : BT::ConditionNode(condition_name, conf)
36  {
37  std::vector<int> error_codes_to_check_vector;
38  getInput("error_codes_to_check", error_codes_to_check_vector); //NOLINT
39 
40  error_codes_to_check_ = std::set<uint16_t>(
41  error_codes_to_check_vector.begin(),
42  error_codes_to_check_vector.end());
43  }
44 
45  AreErrorCodesPresent() = delete;
46 
47  BT::NodeStatus tick()
48  {
49  getInput<uint16_t>("error_code", error_code_); //NOLINT
50 
51  if (error_codes_to_check_.find(error_code_) != error_codes_to_check_.end()) {
52  return BT::NodeStatus::SUCCESS;
53  }
54 
55  return BT::NodeStatus::FAILURE;
56  }
57 
58  static BT::PortsList providedPorts()
59  {
60  return
61  {
62  BT::InputPort<uint16_t>("error_code", "The active error codes"), //NOLINT
63  BT::InputPort<std::vector<int>>("error_codes_to_check", "Error codes to check")//NOLINT
64  };
65  }
66 
67 protected:
68  uint16_t error_code_; //NOLINT
69  std::set<uint16_t> error_codes_to_check_; //NOLINT
70 };
71 
72 } // namespace nav2_behavior_tree
73 
74 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__ARE_ERROR_CODES_PRESENT_CONDITION_HPP_