AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
set_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 <type_traits>
16
17#include "auto_apms_behavior_tree/util/parameter.hpp"
18#include "auto_apms_behavior_tree_core/node.hpp"
19#include "rcl_interfaces/msg/parameter.hpp"
20#include "rcl_interfaces/msg/parameter_type.hpp"
21#include "rcl_interfaces/msg/parameter_value.hpp"
22#include "rcl_interfaces/srv/set_parameters.hpp"
23
24#define INPUT_KEY_PARAM_NAME "parameter"
25#define INPUT_KEY_PARAM_VALUE "value"
26#define INPUT_KEY_NODE_NAME "node"
27
29{
30
31template <typename T>
32class SetParameterTemplate : public core::RosServiceNode<rcl_interfaces::srv::SetParameters>
33{
34public:
35 SetParameterTemplate(const std::string & instance_name, const Config & config, const Context & context)
36 : RosServiceNode(instance_name, config, context)
37 {
38 if (
39 config.input_ports.find(INPUT_KEY_NODE_NAME) == config.input_ports.end() ||
40 config.input_ports.at(INPUT_KEY_NODE_NAME).empty()) {
41 // Refer to this ROS 2 node as the target if respective input port is empty
42 createClient(context_.getFullyQualifiedRosNodeName() + "/set_parameters");
43 }
44 }
45
46 static BT::PortsList providedPorts()
47 {
48 // We do not use the default port for the service name
49
50 // There is no string conversion function for variables that are type initialized using the value port if the
51 // BT::Any version is used. To prevent errors when using these variables in e.g. the scripting language we have to
52 // set the type to BT::AnyTypeAllowed to truly indicate that the type is not set by this port
53 using AnyType = typename std::conditional_t<std::is_same_v<BT::Any, T>, BT::AnyTypeAllowed, T>;
54 return {
55 BT::InputPort<std::string>(
56 INPUT_KEY_NODE_NAME, "Name of the targeted ROS 2 node. Leave empty to target this executor's node."),
57 BT::InputPort<AnyType>(INPUT_KEY_PARAM_VALUE, "Value of the parameter to be set."),
58 BT::InputPort<std::string>(INPUT_KEY_PARAM_NAME, "Name of the parameter to be set."),
59 };
60 }
61
62 bool setRequest(Request::SharedPtr & request) override final
63 {
64 rcl_interfaces::msg::Parameter parameter;
65 const BT::Expected<std::string> expected_name = getInput<std::string>(INPUT_KEY_PARAM_NAME);
66 if (!expected_name || expected_name.value().empty()) {
67 RCLCPP_ERROR(
68 logger_, "%s - Parameter name must not be empty.", context_.getFullyQualifiedTreeNodeName(this).c_str());
69 RCLCPP_DEBUG_EXPRESSION(
70 logger_, !expected_name, "%s - Error message: %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
71 expected_name.error().c_str());
72 return false;
73 }
74 parameter.name = expected_name.value();
75
76 rclcpp::ParameterType inferred_param_type = rclcpp::PARAMETER_NOT_SET;
77 if constexpr (!std::is_same_v<BT::Any, T>) {
78 inferred_param_type = rclcpp::ParameterValue(T()).get_type();
79 }
80
81 rcl_interfaces::msg::ParameterValue param_val;
82 const BT::PortsRemapping::iterator it = config().input_ports.find(INPUT_KEY_PARAM_VALUE);
83 if (it == config().input_ports.end() || it->second.empty()) {
84 if constexpr (std::is_same_v<BT::Any, T>) {
85 throw exceptions::RosNodeError(
86 context_.getFullyQualifiedTreeNodeName(this) + " - Parameter value must not be empty.");
87 } else {
88 // Use default value of rcl_interfaces::msg::Parameter if input is not specified (Don't set param_val.value)
89 param_val.type = inferred_param_type;
90 }
91 } else {
92 // If input port is a literal, the user must use one of the statically typed versions of SetParameter, since
93 // it's impossible to know what type the parameter to be set should have
94 if (!isBlackboardPointer(it->second) && std::is_same_v<BT::Any, T>) {
95 throw exceptions::RosNodeError(
96 context_.getFullyQualifiedTreeNodeName(this) +
97 " - Cannot infer type from literal string. Use one of the type specialized versions of SetParameter "
98 "instead.");
99 }
100
101 const BT::Expected<T> expected_entry = getInput<T>(INPUT_KEY_PARAM_VALUE);
102 if (!expected_entry) {
103 RCLCPP_ERROR(
104 logger_, "%s - %s", context_.getFullyQualifiedTreeNodeName(this).c_str(), expected_entry.error().c_str());
105 return false;
106 }
107 const BT::Expected<rclcpp::ParameterValue> expected_param_val =
108 createParameterValueFromAny(BT::Any(expected_entry.value()), inferred_param_type);
109 if (!expected_param_val) {
110 // Conversion might not be possible. In this case, log error message and reject to set parameter.
111 RCLCPP_ERROR(
112 logger_, "%s - %s", context_.getFullyQualifiedTreeNodeName(this).c_str(), expected_param_val.error().c_str());
113 return false;
114 }
115 param_val = expected_param_val.value().to_value_msg();
116 }
117
118 parameter.value = param_val;
119 requested_parameter_ = rclcpp::Parameter(parameter.name, parameter.value);
120 request->parameters.push_back(parameter);
121 return true;
122 }
123
124 BT::NodeStatus onResponseReceived(const Response::SharedPtr & response) override final
125 {
126 const rcl_interfaces::msg::SetParametersResult & result = response->results[0];
127 if (!result.successful) {
128 RCLCPP_ERROR(
129 logger_, "Failed to set parameter %s = %s (Type: %s) via service '%s': %s",
130 requested_parameter_.get_name().c_str(), requested_parameter_.value_to_string().c_str(),
131 requested_parameter_.get_type_name().c_str(), getServiceName().c_str(), result.reason.c_str());
132 return BT::NodeStatus::FAILURE;
133 }
134 return BT::NodeStatus::SUCCESS;
135 }
136
137private:
138 rclcpp::Parameter requested_parameter_;
139};
140
141// Automatically infer the parameter type from BT::Any
142class SetParameter : public SetParameterTemplate<BT::Any>
143{
144public:
145 using SetParameterTemplate::SetParameterTemplate;
146};
147
148class SetParameterBool : public SetParameterTemplate<bool>
149{
150public:
151 using SetParameterTemplate::SetParameterTemplate;
152};
153
154class SetParameterInt : public SetParameterTemplate<int64_t>
155{
156public:
157 using SetParameterTemplate::SetParameterTemplate;
158};
159
160class SetParameterDouble : public SetParameterTemplate<double>
161{
162public:
163 using SetParameterTemplate::SetParameterTemplate;
164};
165
166class SetParameterString : public SetParameterTemplate<std::string>
167{
168public:
169 using SetParameterTemplate::SetParameterTemplate;
170};
171
172class SetParameterByteVec : public SetParameterTemplate<std::vector<uint8_t>>
173{
174public:
175 using SetParameterTemplate::SetParameterTemplate;
176};
177
178class SetParameterBoolVec : public SetParameterTemplate<std::vector<bool>>
179{
180public:
181 using SetParameterTemplate::SetParameterTemplate;
182};
183
184class SetParameterIntVec : public SetParameterTemplate<std::vector<int64_t>>
185{
186public:
187 using SetParameterTemplate::SetParameterTemplate;
188};
189
190class SetParameterDoubleVec : public SetParameterTemplate<std::vector<double>>
191{
192public:
193 using SetParameterTemplate::SetParameterTemplate;
194};
195
196class SetParameterStringVec : public SetParameterTemplate<std::vector<std::string>>
197{
198public:
199 using SetParameterTemplate::SetParameterTemplate;
200};
201
202} // namespace auto_apms_behavior_tree
203
204AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameter)
205AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterBool)
206AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterInt)
207AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterDouble)
208AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterString)
209AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterByteVec)
210AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterBoolVec)
211AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterIntVec)
212AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterDoubleVec)
213AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::SetParameterStringVec)
Generic behavior tree node wrapper for a ROS 2 service client.
RosServiceNode(const std::string &instance_name, const Config &config, Context context)
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(type)
Macro for registering a behavior tree node plugin.
Definition node.hpp:40
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30
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