17#include "auto_apms_behavior_tree/util/parameter.hpp"
18#include "auto_apms_behavior_tree_core/node.hpp"
19#include "rcl_interfaces/msg/parameter_value.hpp"
20#include "rcl_interfaces/srv/get_parameters.hpp"
21#include "rclcpp/parameter_value.hpp"
23#define INPUT_KEY_PARAM_NAME "parameter"
24#define OUTPUT_KEY_PARAM_VALUE "value"
25#define INPUT_KEY_NODE_NAME "node"
34 GetParameterTemplate(
const std::string & instance_name,
const Config & config,
const Context & context)
38 std::string node_name = context_.getFullyQualifiedRosNodeName();
40 config.input_ports.find(INPUT_KEY_NODE_NAME) != config.input_ports.end() &&
41 !config.input_ports.at(INPUT_KEY_NODE_NAME).empty()) {
42 node_name = config.input_ports.at(INPUT_KEY_NODE_NAME);
47 static BT::PortsList providedPorts()
54 using AnyType =
typename std::conditional_t<std::is_same_v<BT::Any, T>, BT::AnyTypeAllowed, T>;
56 BT::InputPort<std::string>(
57 INPUT_KEY_NODE_NAME,
"Name of the targeted ROS 2 node. Leave empty to target this executor's node."),
58 BT::OutputPort<AnyType>(OUTPUT_KEY_PARAM_VALUE,
"Output port for the parameter's value."),
59 BT::InputPort<std::string>(INPUT_KEY_PARAM_NAME,
"Name of the parameter to get.")};
62 bool setRequest(Request::SharedPtr & request)
override final
64 const BT::Expected<std::string> expected_name = getInput<std::string>(INPUT_KEY_PARAM_NAME);
65 if (!expected_name || expected_name.value().empty()) {
67 logger_,
"%s - Parameter name must not be empty.", context_.getFullyQualifiedTreeNodeName(
this).c_str());
68 RCLCPP_DEBUG_EXPRESSION(
69 logger_, !expected_name,
"%s - Error message: %s", context_.getFullyQualifiedTreeNodeName(
this).c_str(),
70 expected_name.error().c_str());
73 requested_parameter_name_ = expected_name.value();
74 request->names.push_back(requested_parameter_name_);
78 BT::NodeStatus onResponseReceived(
const Response::SharedPtr & response)
override final
80 if (response->values.empty()) {
81 throw std::logic_error(
82 context_.getFullyQualifiedTreeNodeName(
this) +
" - Response vector doesn't contain any values.");
84 rclcpp::ParameterValue val(response->values[0]);
85 if (val.get_type() == rclcpp::PARAMETER_NOT_SET) {
87 logger_,
"%s - Tried to get undeclared parameter '%s'.", context_.getFullyQualifiedTreeNodeName(
this).c_str(),
88 requested_parameter_name_.c_str());
89 return BT::NodeStatus::FAILURE;
92 BT::Result set_ouput_result;
93 if constexpr (std::is_same_v<BT::Any, T>) {
99 logger_,
"%s - %s", context_.getFullyQualifiedTreeNodeName(
this).c_str(), expected.error().c_str());
100 return BT::NodeStatus::FAILURE;
102 set_ouput_result = setOutput(OUTPUT_KEY_PARAM_VALUE, expected.value());
104 set_ouput_result = setOutput(OUTPUT_KEY_PARAM_VALUE, val.get<T>());
107 if (!set_ouput_result) {
109 logger_,
"%s - %s", context_.getFullyQualifiedTreeNodeName(
this).c_str(), set_ouput_result.error().c_str());
110 return BT::NodeStatus::FAILURE;
113 return BT::NodeStatus::SUCCESS;
117 std::string requested_parameter_name_;
122class GetParameter :
public GetParameterTemplate<BT::Any>
125 using GetParameterTemplate::GetParameterTemplate;
128class GetParameterBool :
public GetParameterTemplate<bool>
131 using GetParameterTemplate::GetParameterTemplate;
134class GetParameterInt :
public GetParameterTemplate<int64_t>
137 using GetParameterTemplate::GetParameterTemplate;
140class GetParameterDouble :
public GetParameterTemplate<double>
143 using GetParameterTemplate::GetParameterTemplate;
146class GetParameterString :
public GetParameterTemplate<std::string>
149 using GetParameterTemplate::GetParameterTemplate;
152class GetParameterByteVec :
public GetParameterTemplate<std::vector<uint8_t>>
155 using GetParameterTemplate::GetParameterTemplate;
158class GetParameterBoolVec :
public GetParameterTemplate<std::vector<bool>>
161 using GetParameterTemplate::GetParameterTemplate;
164class GetParameterIntVec :
public GetParameterTemplate<std::vector<int64_t>>
167 using GetParameterTemplate::GetParameterTemplate;
170class GetParameterDoubleVec :
public GetParameterTemplate<std::vector<double>>
173 using GetParameterTemplate::GetParameterTemplate;
176class GetParameterStringVec :
public GetParameterTemplate<std::vector<std::string>>
179 using GetParameterTemplate::GetParameterTemplate;
Generic behavior tree node wrapper for a ROS 2 service client.
bool createClient(const std::string &service_name)
RosServiceNode(const std::string &instance_name, const Config &config, Context context)
#define AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE(type)
Macro for registering a behavior tree node plugin.
Powerful tooling for incorporating behavior trees for task development.
BT::Expected< BT::Any > createAnyFromParameterValue(const rclcpp::ParameterValue &val)
Convert a ROS 2 parameter value to a BT::Any object.