AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
mission_configuration.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 <utility>
18#include <vector>
19
20#include "auto_apms_behavior_tree_core/tree/tree_resource.hpp"
21#include "auto_apms_util/exceptions.hpp"
22#include "auto_apms_util/yaml.hpp"
23
24namespace auto_apms_mission
25{
27}
28
30namespace YAML
31{
32template <>
33struct convert<auto_apms_mission::MissionConfiguration>
34{
35 using Config = auto_apms_mission::MissionConfiguration;
36 static Node encode(const Config & rhs);
37 static bool decode(const Node & node, Config & rhs);
38};
39} // namespace YAML
41
42namespace auto_apms_mission
43{
44
49struct MissionConfiguration
50{
51 using TreeResourceIdentity = auto_apms_behavior_tree::core::TreeResourceIdentity;
52
53 static const std::string YAML_KEY_BRINGUP;
54 static const std::string YAML_KEY_MISSION;
55 static const std::string YAML_KEY_CONTINGENCY;
56 static const std::string YAML_KEY_EMERGENCY;
57 static const std::string YAML_KEY_SHUTDOWN;
58
59 MissionConfiguration() = default;
60
62
63
73 static MissionConfiguration fromResourceIdentity(const std::string identity);
74
75 std::vector<TreeResourceIdentity> bringup;
76 std::vector<TreeResourceIdentity> mission;
77 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> contingency;
78 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> emergency;
79 std::vector<TreeResourceIdentity> shutdown;
80};
81
82} // namespace auto_apms_mission
83
84// #####################################################################################################################
85// ################################ DEFINITIONS ##############################################
86// #####################################################################################################################
87
89namespace YAML
90{
91inline Node convert<auto_apms_mission::MissionConfiguration>::encode(const Config & rhs)
92{
93 Node node(NodeType::Map);
94 node[Config::YAML_KEY_BRINGUP] = rhs.bringup;
95 node[Config::YAML_KEY_MISSION] = rhs.mission;
96 Node contingency_node = node[Config::YAML_KEY_CONTINGENCY];
97 for (const auto & [key, val] : rhs.contingency) {
98 contingency_node[key.str()] = val;
99 }
100 Node emergency_node = node[Config::YAML_KEY_EMERGENCY];
101 for (const auto & [key, val] : rhs.emergency) {
102 emergency_node[key.str()] = val;
103 }
104 node[Config::YAML_KEY_SHUTDOWN] = rhs.shutdown;
105 return node;
106}
107inline bool convert<auto_apms_mission::MissionConfiguration>::decode(const Node & node, Config & rhs)
108{
109 if (!node.IsMap())
110 throw auto_apms_util::exceptions::YAMLFormatError(
111 "YAML::Node for auto_apms_mission::MissionConfiguration must be map but is type " + std::to_string(node.Type()) +
112 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
113
114 for (auto it = node.begin(); it != node.end(); ++it) {
115 const std::string key = it->first.as<std::string>();
116 const Node val = it->second;
117
118 if (key == Config::YAML_KEY_BRINGUP) {
119 if (val.IsNull()) continue;
120 if (!val.IsSequence()) {
121 throw auto_apms_util::exceptions::YAMLFormatError(
122 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
123 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
124 }
125 rhs.bringup = val.as<std::vector<Config::TreeResourceIdentity>>();
126 continue;
127 }
128
129 if (key == Config::YAML_KEY_MISSION) {
130 if (!val.IsSequence()) {
131 throw auto_apms_util::exceptions::YAMLFormatError(
132 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
133 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
134 }
135 rhs.mission = val.as<std::vector<Config::TreeResourceIdentity>>();
136 continue;
137 }
138
139 if (key == Config::YAML_KEY_CONTINGENCY) {
140 if (val.IsNull()) continue;
141 if (!val.IsMap()) {
142 throw auto_apms_util::exceptions::YAMLFormatError(
143 "Value for key '" + key + "' must be a map but is type " + std::to_string(val.Type()) +
144 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
145 }
146 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
147 const std::string monitor_id = it2->first.as<std::string>();
148 if (!it2->second.IsScalar()) {
149 throw auto_apms_util::exceptions::YAMLFormatError(
150 "Value for key '" + monitor_id + "' in the CONTINGENCY group must be scalar but is type " +
151 std::to_string(val.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
152 }
153 rhs.contingency.push_back(
154 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
155 }
156 continue;
157 }
158
159 if (key == Config::YAML_KEY_EMERGENCY) {
160 if (val.IsNull()) continue;
161 if (!val.IsMap()) {
162 throw auto_apms_util::exceptions::YAMLFormatError(
163 "Value for key '" + key + "' must be a map but is type " + std::to_string(val.Type()) +
164 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
165 }
166 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
167 const std::string monitor_id = it2->first.as<std::string>();
168 if (!it2->second.IsScalar()) {
169 throw auto_apms_util::exceptions::YAMLFormatError(
170 "Value for key '" + monitor_id + "' in the EMERGENCY group must be scalar but is type " +
171 std::to_string(val.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
172 }
173 rhs.emergency.push_back(
174 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
175 }
176 continue;
177 }
178
179 if (key == Config::YAML_KEY_SHUTDOWN) {
180 if (val.IsNull()) continue;
181 if (!val.IsSequence()) {
182 throw auto_apms_util::exceptions::YAMLFormatError(
183 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
184 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
185 }
186 rhs.shutdown = val.as<std::vector<Config::TreeResourceIdentity>>();
187 continue;
188 }
189
190 // Unkown parameter
191 throw auto_apms_util::exceptions::YAMLFormatError("Unkown parameter name '" + key + "'.");
192 }
193 return true;
194}
195} // 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
Mission design utilities incorporating behavior trees to model the complexity of arbitrary operations...
Struct that encapsulates the identity string for a declared behavior tree.
Configuration parameters for standard missions supported by AutoAPMS.
static MissionConfiguration fromResourceIdentity(const std::string identity)
Create a mission configuration from an installed resource.