AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
node_registration_options.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 <chrono>
18#include <map>
19#include <string>
20
21#include "auto_apms_util/exceptions.hpp"
22#include "auto_apms_util/yaml.hpp"
23
25{
27}
28
30namespace YAML
31{
32template <>
33struct convert<auto_apms_behavior_tree::core::NodeRegistrationOptions>
34{
35 using Options = auto_apms_behavior_tree::core::NodeRegistrationOptions;
36 static Node encode(const Options & rhs);
37 static bool decode(const Node & node, Options & lhs);
38};
39} // namespace YAML
41
43{
44
50{
51 static const std::string PARAM_NAME_CLASS;
52 static const std::string PARAM_NAME_PORT;
53 static const std::string PARAM_NAME_WAIT_TIMEOUT;
54 static const std::string PARAM_NAME_REQUEST_TIMEOUT;
55 static const std::string PARAM_NAME_ALLOW_UNREACHABLE;
56 static const std::string PARAM_NAME_LOGGER_LEVEL;
57
62
64
65
66 std::string class_name;
82 std::string port = "(input:port)";
84 std::chrono::duration<double> wait_timeout = std::chrono::duration<double>(3);
86 std::chrono::duration<double> request_timeout = std::chrono::duration<double>(2);
89 bool allow_unreachable = false;
91 std::string logger_level = "INFO";
92
97 bool valid() const;
98};
99
100} // namespace auto_apms_behavior_tree::core
101
102// #####################################################################################################################
103// ################################ DEFINITIONS ##############################################
104// #####################################################################################################################
105
107namespace YAML
108{
109inline Node convert<auto_apms_behavior_tree::core::NodeRegistrationOptions>::encode(const Options & rhs)
110{
111 Node node(NodeType::Map);
112 node[Options::PARAM_NAME_CLASS] = rhs.class_name;
113 node[Options::PARAM_NAME_PORT] = rhs.port;
114 node[Options::PARAM_NAME_WAIT_TIMEOUT] = rhs.wait_timeout.count();
115 node[Options::PARAM_NAME_REQUEST_TIMEOUT] = rhs.request_timeout.count();
116 node[Options::PARAM_NAME_ALLOW_UNREACHABLE] = rhs.allow_unreachable;
117 node[Options::PARAM_NAME_LOGGER_LEVEL] = rhs.logger_level;
118 return node;
119}
120inline bool convert<auto_apms_behavior_tree::core::NodeRegistrationOptions>::decode(const Node & node, Options & rhs)
121{
122 if (!node.IsMap())
123 throw auto_apms_util::exceptions::YAMLFormatError(
124 "YAML::Node for auto_apms_behavior_tree::core::NodeRegistrationOptions must be map but is type " +
125 std::to_string(node.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
126
127 for (auto it = node.begin(); it != node.end(); ++it) {
128 const std::string key = it->first.as<std::string>();
129 const Node & val = it->second;
130 if (!val.IsScalar())
131 throw auto_apms_util::exceptions::YAMLFormatError(
132 "Value for key '" + key + "' must be scalar but is type " + std::to_string(val.Type()) +
133 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
134
135 if (key == Options::PARAM_NAME_CLASS) {
136 rhs.class_name = val.as<std::string>();
137 continue;
138 }
139 if (key == Options::PARAM_NAME_PORT) {
140 rhs.port = val.as<std::string>();
141 continue;
142 }
143 if (key == Options::PARAM_NAME_WAIT_TIMEOUT) {
144 rhs.wait_timeout = std::chrono::duration<double>(val.as<double>());
145 continue;
146 }
147 if (key == Options::PARAM_NAME_REQUEST_TIMEOUT) {
148 rhs.request_timeout = std::chrono::duration<double>(val.as<double>());
149 continue;
150 }
151 if (key == Options::PARAM_NAME_ALLOW_UNREACHABLE) {
152 rhs.allow_unreachable = val.as<bool>();
153 continue;
154 }
155 if (key == Options::PARAM_NAME_LOGGER_LEVEL) {
156 rhs.logger_level = val.as<std::string>();
157 continue;
158 }
159 // Unkown parameter
160 throw auto_apms_util::exceptions::YAMLFormatError("Unkown parameter name '" + key + "'.");
161 }
162 return true;
163}
164} // namespace YAML
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
Macro for defining YAML encode/decode methods for a class.
Definition yaml.hpp:33
Core API for AutoAPMS's behavior tree implementation.
Definition builder.hpp:30
Parameters for loading and registering a behavior tree node class from a shared library using e....
std::string port
Default port name of the corresponding ROS 2 communication interface.
std::chrono::duration< double > wait_timeout
Period [s] (measured from tree construction) after the server is considered unreachable.
bool valid() const
Verify that the options are valid (e.g. all required values are set).
NodeRegistrationOptions()=default
Create the default node registration options.
std::chrono::duration< double > request_timeout
Period [s] (measured from sending a goal request) after the node aborts waiting for a server response...
std::string class_name
Fully qualified name of the behavior tree node plugin class.
std::string logger_level
Minimum severity level enabled for logging using the ROS 2 Logger API.