15#include "auto_apms_behavior_tree_core/node/ros_node_context.hpp"
20#include "auto_apms_behavior_tree_core/exceptions.hpp"
21#include "auto_apms_util/logging.hpp"
26 rclcpp::Node::SharedPtr ros_node, rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group,
27 rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor,
29: ros_node_name_(ros_node ? ros_node->get_name() :
""),
30 fully_qualified_ros_node_name_(ros_node ? ros_node->get_fully_qualified_name() :
""),
31 base_logger_(ros_node ? ros_node->get_logger() : rclcpp::get_logger(
"")),
33 cb_group_(tree_node_waitables_callback_group),
34 executor_(tree_node_waitables_executor),
35 registration_options_(options)
47 const rclcpp::Logger child_logger = base_logger_.get_child(name);
50 }
catch (
const auto_apms_util::exceptions::SetLoggingSeverityError & e) {
52 base_logger_,
"Failed to set the logging severity for the child logger using the node's registration options: %s",
60 if (
const rclcpp::Node::SharedPtr node = nh_.lock()) {
63 return rclcpp::Clock(RCL_ROS_TIME).now();
70 const std::string instance_name = node->name();
71 const std::string registration_name = node->registrationName();
72 if (!registration_name.empty() && instance_name != registration_name) {
73 if (with_class_name) {
74 return instance_name +
" (" + registration_name +
" : " + registration_options_.class_name +
")";
76 return instance_name +
" (" + registration_name +
")";
78 return with_class_name ? (instance_name +
" (" + registration_options_.class_name +
")") : instance_name;
81BT::Expected<std::string> RosNodeContext::getCommunicationPortName(
const BT::TreeNode * node)
const
83 std::string res = registration_options_.
port;
84 BT::PortsRemapping input_ports = node->config().input_ports;
89 const std::regex pattern(R
"(\(input:([^)\s]+)\))");
90 const std::sregex_iterator replace_begin(res.begin(), res.end(), pattern);
91 const std::sregex_iterator replace_end = std::sregex_iterator();
95 for (std::sregex_iterator it = replace_begin; it != replace_end; ++it) {
96 const std::smatch match = *it;
97 const std::string input_port_key = match[1].str();
100 if (input_ports.find(input_port_key) != input_ports.end()) {
104 if (input_ports.at(input_port_key).empty()) {
105 return nonstd::make_unexpected(
107 " - Cannot get the name of the node's ROS 2 communication port: Input port '" + input_port_key +
108 "' required by substring '" + match.str() +
"' must not be empty.");
114 const BT::Expected<std::string> expected = node->getInput<std::string>(input_port_key);
117 res.replace(match.position(), match.length(), expected.value());
123 return nonstd::make_unexpected(
125 " - Cannot get the name of the node's ROS 2 communication port: Node input port '" + input_port_key +
126 "' required by substring '" + match.str() +
"' doesn't exist.");
std::string getROSNodeName() const
Get the name of the ROS 2 node passed to the constructor.
rclcpp::Logger getChildLogger(const std::string &name)
Get a child logger created using the associated ROS 2 node.
rclcpp::Time getCurrentTime() const
Get the current time using the associated ROS 2 node.
RosNodeContext(rclcpp::Node::SharedPtr ros_node, rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group, rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor, const NodeRegistrationOptions &options)
Constructor.
std::string getFullyQualifiedTreeNodeName(const BT::TreeNode *node, bool with_class_name=true) const
Create a string representing the detailed name of a behavior tree node.
std::string getFullyQualifiedRosNodeName() const
Get the fully qualified name of the ROS 2 node passed to the constructor.
rclcpp::Logger getBaseLogger() const
Get the logger of the associated ROS 2 node.
void setLoggingSeverity(const rclcpp::Logger &logger, const std::string &severity)
Set the logging severity of a ROS 2 logger.
Core API for AutoAPMS's behavior tree implementation.
Parameters for loading and registering a behavior tree node class from a shared library using e....
std::string port
Default port name of the corresponding ROS 2 communication interface.