AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
has_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 <optional>
16#include <type_traits>
17
18#include "auto_apms_behavior_tree_core/node.hpp"
19#include "auto_apms_util/container.hpp"
20#include "rcl_interfaces/srv/list_parameters.hpp"
21
22#define INPUT_KEY_PARAM_NAME "parameter"
23#define INPUT_KEY_PARAM_VALUE "value"
24#define INPUT_KEY_NODE_NAME "node"
25
27{
28
29class HasParameter : public core::RosServiceNode<rcl_interfaces::srv::ListParameters>
30{
31public:
32 HasParameter(const std::string & instance_name, const Config & config, const Context & context)
33 : RosServiceNode(instance_name, config, context)
34 {
35 if (
36 config.input_ports.find(INPUT_KEY_NODE_NAME) == config.input_ports.end() ||
37 config.input_ports.at(INPUT_KEY_NODE_NAME).empty()) {
38 // Refer to this ROS 2 node as the target if respective input port is empty
39 createClient(context_.getFullyQualifiedRosNodeName() + "/list_parameters");
40 }
41 }
42
43 static BT::PortsList providedPorts()
44 {
45 // We do not use the default port for the service name
46 return {
47 BT::InputPort<std::string>(
48 INPUT_KEY_NODE_NAME, "Name of the targeted ROS 2 node. Leave empty to target this executor's node."),
49 BT::InputPort<std::string>(INPUT_KEY_PARAM_NAME, "Name of the parameter.")};
50 }
51
52 bool setRequest(Request::SharedPtr & request) override final
53 {
54 const BT::Expected<std::string> expected_name = getInput<std::string>(INPUT_KEY_PARAM_NAME);
55 if (!expected_name || expected_name.value().empty()) {
56 RCLCPP_ERROR(
57 logger_, "%s - Parameter name must not be empty.", context_.getFullyQualifiedTreeNodeName(this).c_str());
58 RCLCPP_DEBUG_EXPRESSION(
59 logger_, !expected_name, "%s - Error message: %s", context_.getFullyQualifiedTreeNodeName(this).c_str(),
60 expected_name.error().c_str());
61 return false;
62 }
63 has_parameter_name_ = expected_name.value();
64 request->depth = Request::DEPTH_RECURSIVE;
65 request->prefixes = {};
66 return true;
67 }
68
69 BT::NodeStatus onResponseReceived(const Response::SharedPtr & response) override final
70 {
71 if (auto_apms_util::contains(response->result.names, has_parameter_name_)) return BT::NodeStatus::SUCCESS;
72 return BT::NodeStatus::FAILURE;
73 }
74
75private:
76 std::string has_parameter_name_;
77};
78
79} // namespace auto_apms_behavior_tree
80
81AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE(auto_apms_behavior_tree::HasParameter)
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
bool contains(const ContainerT< ValueT, AllocatorT > &c, const ValueT &val)
Check whether a particular container structure contains a value.
Definition container.hpp:36
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30