AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
node_registration_template.hpp
1// Copyright 2024 Robin Müller
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// https://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#pragma once
16
17#include <map>
18#include <regex>
19#include <stdexcept>
20#include <type_traits>
21
22#include "auto_apms_behavior_tree_core/exceptions.hpp"
23#include "auto_apms_behavior_tree_core/node/node_registration_interface.hpp"
24#include "behaviortree_cpp/basic_types.h"
25
27{
28
30
36template <
37 class T, typename = std::enable_if_t<std::is_base_of_v<BT::TreeNode, T>>,
38 bool requires_ros_node_params =
39 std::is_constructible_v<T, const std::string &, const BT::NodeConfig &, RosNodeContext>>
40class NodeRegistrationTemplate : public NodeRegistrationInterface
41{
42public:
43 NodeRegistrationTemplate() = default;
44 virtual ~NodeRegistrationTemplate() = default;
45
46 bool requiresRosNodeContext() const override { return requires_ros_node_params; }
47
48 void registerWithBehaviorTreeFactory(
49 BT::BehaviorTreeFactory & factory, const std::string & registration_name,
50 const RosNodeContext * const context_ptr = nullptr) const override
51 {
52 BT::PortsList ports_list = BT::getProvidedPorts<T>();
53 if (context_ptr) {
54 try {
55 context_ptr->modifyProvidedPortsListForRegistration(ports_list);
56 } catch (const std::exception & e) {
57 throw exceptions::NodeRegistrationError(
58 "[registerWithBehaviorTreeFactory] Error registering node '" + registration_name + "': " + e.what());
59 }
60 }
61
62 if constexpr (requires_ros_node_params) {
63 if (!context_ptr) {
64 throw std::invalid_argument(
65 "[registerWithBehaviorTreeFactory] Error registering node '" + registration_name +
66 "': You must pass a valid RosNodeContext object to be passed via argument "
67 "'context_ptr'.");
68 }
69 factory.registerNodeType<T>(registration_name, ports_list, *context_ptr);
70 } else {
71 if (context_ptr) {
72 // Warn the user that some features of RosNodeContext are not supported
73 if (context_ptr->registration_options_.port_alias.size() > 0) {
74 throw exceptions::NodeRegistrationError(
75 "[registerWithBehaviorTreeFactory] Error registering node '" + registration_name +
76 "': Port aliasing is not supported by this node, but " + NodeRegistrationOptions::PARAM_NAME_PORT_ALIAS +
77 " was provided in the registration options, please remove the field "
78 "from the node manifest to avoid confusion.");
79 }
80 }
81 factory.registerNodeType<T>(registration_name, ports_list);
82 }
83 }
84};
85
87
88} // namespace auto_apms_behavior_tree::core
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32