AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
parameter.cpp
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#include "auto_apms_behavior_tree/util/parameter.hpp"
16
18{
19
20BT::Expected<BT::Any> createAnyFromParameterValue(const rclcpp::ParameterValue & val)
21{
22 switch (val.get_type()) {
23 case rclcpp::PARAMETER_BOOL:
24 return BT::GetAnyFromStringFunctor<bool>()(BT::toStr(val.get<bool>()));
25 case rclcpp::PARAMETER_INTEGER:
26 return BT::GetAnyFromStringFunctor<int64_t>()(BT::toStr(val.get<int64_t>()));
27 case rclcpp::PARAMETER_DOUBLE:
28 return BT::GetAnyFromStringFunctor<double>()(BT::toStr(val.get<double>()));
29 case rclcpp::PARAMETER_STRING:
30 return BT::GetAnyFromStringFunctor<std::string>()(BT::toStr(val.get<std::string>()));
31 case rclcpp::PARAMETER_BYTE_ARRAY:
32 return BT::GetAnyFromStringFunctor<std::vector<uint8_t>>()(BT::toStr(val.get<std::vector<uint8_t>>()));
33 case rclcpp::PARAMETER_BOOL_ARRAY:
34 return BT::GetAnyFromStringFunctor<std::vector<bool>>()(BT::toStr(val.get<std::vector<bool>>()));
35 case rclcpp::PARAMETER_INTEGER_ARRAY:
36 return BT::GetAnyFromStringFunctor<std::vector<int64_t>>()(BT::toStr(val.get<std::vector<int64_t>>()));
37 case rclcpp::PARAMETER_DOUBLE_ARRAY:
38 return BT::GetAnyFromStringFunctor<std::vector<double>>()(BT::toStr(val.get<std::vector<double>>()));
39 case rclcpp::PARAMETER_STRING_ARRAY:
40 return BT::GetAnyFromStringFunctor<std::vector<std::string>>()(BT::toStr(val.get<std::vector<std::string>>()));
41 default:
42 break;
43 }
44
45 // Cannot convert
46 return nonstd::make_unexpected(
47 "Conversion of rclcpp::ParameterValue with type '" + rclcpp::to_string(val.get_type()) +
48 "' to BT::Any is undefined.");
49}
50
51BT::Expected<rclcpp::ParameterValue> createParameterValueFromAny(const BT::Any & any, rclcpp::ParameterType type)
52{
53 std::string error;
54 switch (type) {
55 case rclcpp::ParameterType::PARAMETER_NOT_SET:
56 // If type is not set, try different casts and use the first one
57 if (any.isType<bool>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_BOOL);
58 if (any.isType<int64_t>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_INTEGER);
59 if (any.isType<double>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_DOUBLE);
60 if (any.isType<std::string>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_STRING);
61 if (any.isType<std::vector<uint8_t>>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_BYTE_ARRAY);
62 if (any.isType<std::vector<bool>>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_BOOL_ARRAY);
63 if (any.isType<std::vector<int64_t>>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_INTEGER_ARRAY);
64 if (any.isType<std::vector<double>>()) return createParameterValueFromAny(any, rclcpp::PARAMETER_DOUBLE_ARRAY);
65 if (any.isType<std::vector<std::string>>())
66 return createParameterValueFromAny(any, rclcpp::PARAMETER_STRING_ARRAY);
67
68 // If none of the expected parameter types, try internal conversions
69 if (any.isIntegral()) return createParameterValueFromAny(any, rclcpp::PARAMETER_INTEGER);
70 if (any.isNumber()) return createParameterValueFromAny(any, rclcpp::PARAMETER_DOUBLE);
71 if (any.isString()) return createParameterValueFromAny(any, rclcpp::PARAMETER_STRING);
72
73 error = "Cannot infer parameter type.";
74 break;
75 case rclcpp::PARAMETER_BOOL: {
76 const auto casted = any.tryCast<bool>();
77 if (casted) return rclcpp::ParameterValue(casted.value());
78 error = casted.error();
79 break;
80 }
81 case rclcpp::PARAMETER_INTEGER: {
82 const auto casted = any.tryCast<int64_t>();
83 if (casted) return rclcpp::ParameterValue(casted.value());
84 error = casted.error();
85 break;
86 }
87 case rclcpp::PARAMETER_DOUBLE: {
88 const auto casted = any.tryCast<double>();
89 if (casted) return rclcpp::ParameterValue(casted.value());
90 error = casted.error();
91 break;
92 }
93 case rclcpp::PARAMETER_STRING: {
94 const auto casted = any.tryCast<std::string>();
95 if (casted) return rclcpp::ParameterValue(casted.value());
96 error = casted.error();
97 break;
98 }
99 case rclcpp::PARAMETER_BYTE_ARRAY: {
100 const auto casted = any.tryCast<std::vector<uint8_t>>();
101 if (casted) return rclcpp::ParameterValue(casted.value());
102 error = casted.error();
103 break;
104 }
105 case rclcpp::PARAMETER_BOOL_ARRAY: {
106 const auto casted = any.tryCast<std::vector<bool>>();
107 if (casted) return rclcpp::ParameterValue(casted.value());
108 error = casted.error();
109 break;
110 }
111 case rclcpp::PARAMETER_INTEGER_ARRAY: {
112 const auto casted = any.tryCast<std::vector<int64_t>>();
113 if (casted) return rclcpp::ParameterValue(casted.value());
114 error = casted.error();
115 break;
116 }
117 case rclcpp::PARAMETER_DOUBLE_ARRAY: {
118 const auto casted = any.tryCast<std::vector<double>>();
119 if (casted) return rclcpp::ParameterValue(casted.value());
120 error = casted.error();
121 break;
122 }
123 case rclcpp::PARAMETER_STRING_ARRAY: {
124 const auto casted = any.tryCast<std::vector<std::string>>();
125 if (casted) return rclcpp::ParameterValue(casted.value());
126 error = casted.error();
127 break;
128 }
129 }
130
131 // Cannot convert
132 return nonstd::make_unexpected(
133 "Conversion of BT::Any (Internal type: " + BT::demangle(any.type()) + ") to rclcpp::ParameterValue with type '" +
134 rclcpp::to_string(type) + "' failed: " + error);
135}
136
137} // namespace auto_apms_behavior_tree
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30
BT::Expected< BT::Any > createAnyFromParameterValue(const rclcpp::ParameterValue &val)
Convert a ROS 2 parameter value to a BT::Any object.
Definition parameter.cpp:20
BT::Expected< rclcpp::ParameterValue > createParameterValueFromAny(const BT::Any &any, rclcpp::ParameterType type)
Convert a BT::Any object to a ROS 2 parameter value.
Definition parameter.cpp:51