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;
90 std::string port = "(input:port)";
92 std::chrono::duration<double> wait_timeout = std::chrono::duration<double>(3);
94 std::chrono::duration<double> request_timeout = std::chrono::duration<double>(2);
97 bool allow_unreachable = false;
99 std::string logger_level = "INFO";
100
105 bool valid() const;
106};
107
108} // namespace auto_apms_behavior_tree::core
109
110// #####################################################################################################################
111// ################################ DEFINITIONS ##############################################
112// #####################################################################################################################
113
115namespace YAML
116{
117inline Node convert<auto_apms_behavior_tree::core::NodeRegistrationOptions>::encode(const Options & rhs)
118{
119 Node node(NodeType::Map);
120 node[Options::PARAM_NAME_CLASS] = rhs.class_name;
121 node[Options::PARAM_NAME_PORT] = rhs.port;
122 node[Options::PARAM_NAME_WAIT_TIMEOUT] = rhs.wait_timeout.count();
123 node[Options::PARAM_NAME_REQUEST_TIMEOUT] = rhs.request_timeout.count();
124 node[Options::PARAM_NAME_ALLOW_UNREACHABLE] = rhs.allow_unreachable;
125 node[Options::PARAM_NAME_LOGGER_LEVEL] = rhs.logger_level;
126 return node;
127}
128inline bool convert<auto_apms_behavior_tree::core::NodeRegistrationOptions>::decode(const Node & node, Options & rhs)
129{
130 if (!node.IsMap())
131 throw auto_apms_util::exceptions::YAMLFormatError(
132 "YAML::Node for auto_apms_behavior_tree::core::NodeRegistrationOptions must be map but is type " +
133 std::to_string(node.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
134
135 for (auto it = node.begin(); it != node.end(); ++it) {
136 const std::string key = it->first.as<std::string>();
137 const Node & val = it->second;
138 if (!val.IsScalar())
139 throw auto_apms_util::exceptions::YAMLFormatError(
140 "Value for key '" + key + "' must be scalar but is type " + std::to_string(val.Type()) +
141 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
142
143 if (key == Options::PARAM_NAME_CLASS) {
144 rhs.class_name = val.as<std::string>();
145 continue;
146 }
147 if (key == Options::PARAM_NAME_PORT) {
148 rhs.port = val.as<std::string>();
149 continue;
150 }
151 if (key == Options::PARAM_NAME_WAIT_TIMEOUT) {
152 rhs.wait_timeout = std::chrono::duration<double>(val.as<double>());
153 continue;
154 }
155 if (key == Options::PARAM_NAME_REQUEST_TIMEOUT) {
156 rhs.request_timeout = std::chrono::duration<double>(val.as<double>());
157 continue;
158 }
159 if (key == Options::PARAM_NAME_ALLOW_UNREACHABLE) {
160 rhs.allow_unreachable = val.as<bool>();
161 continue;
162 }
163 if (key == Options::PARAM_NAME_LOGGER_LEVEL) {
164 rhs.logger_level = val.as<std::string>();
165 continue;
166 }
167 // Unkown parameter
168 throw auto_apms_util::exceptions::YAMLFormatError("Unkown parameter name '" + key + "'.");
169 }
170 return true;
171}
172} // 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.