Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions tf2_ros/include/tf2_ros/transform_broadcaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace tf2_ros
class TransformBroadcaster
{
public:
/** \brief Node interface constructor */
/** \brief Node constructor */
template<class NodeT, class AllocatorT = std::allocator<void>>
TransformBroadcaster(
NodeT && node,
Expand All @@ -67,9 +67,31 @@ class TransformBroadcaster
rclcpp::QosPolicyKind::Reliability};
return options;
} ())
: TransformBroadcaster(
node->get_node_parameters_interface(),
node->get_node_topics_interface(),
qos,
options)
{}

/** \brief Node interfaces constructor */
template<class AllocatorT = std::allocator<void>>
TransformBroadcaster(
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
const rclcpp::QoS & qos = DynamicBroadcasterQoS(),
const rclcpp::PublisherOptionsWithAllocator<AllocatorT> & options = [] () {
rclcpp::PublisherOptionsWithAllocator<AllocatorT> options;
options.qos_overriding_options = rclcpp::QosOverridingOptions{
rclcpp::QosPolicyKind::Depth,
rclcpp::QosPolicyKind::Durability,
rclcpp::QosPolicyKind::History,
rclcpp::QosPolicyKind::Reliability};
return options;
} ())
{
publisher_ = rclcpp::create_publisher<tf2_msgs::msg::TFMessage>(
node, "/tf", qos, options);
node_parameters, node_topics, "/tf", qos, options);
}

/** \brief Send a TransformStamped message
Expand Down
93 changes: 75 additions & 18 deletions tf2_ros/include/tf2_ros/transform_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ get_default_transform_listener_static_sub_options()
class TransformListener
{
public:
/**@brief Constructor for transform listener */
/** \brief Simplified constructor for transform listener.
*
* This constructor will create a new ROS 2 node under the hood.
* If you already have access to a ROS 2 node and you want to associate the TransformListener
* to it, then it's recommended to use one of the other constructors.
*/
TF2_ROS_PUBLIC
explicit TransformListener(tf2::BufferCore & buffer, bool spin_thread = true);

/** \brief Node constructor */
template<class NodeT, class AllocatorT = std::allocator<void>>
TransformListener(
tf2::BufferCore & buffer,
Expand All @@ -97,27 +103,67 @@ class TransformListener
detail::get_default_transform_listener_sub_options<AllocatorT>(),
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & static_options =
detail::get_default_transform_listener_static_sub_options<AllocatorT>())
: TransformListener(
buffer,
node->get_node_base_interface(),
node->get_node_logging_interface(),
node->get_node_parameters_interface(),
node->get_node_topics_interface(),
spin_thread,
qos,
static_qos,
options,
static_options)
{}

/** \brief Node interface constructor */
template<class AllocatorT = std::allocator<void>>
TransformListener(
tf2::BufferCore & buffer,
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
bool spin_thread = true,
const rclcpp::QoS & qos = DynamicListenerQoS(),
const rclcpp::QoS & static_qos = StaticListenerQoS(),
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options =
detail::get_default_transform_listener_sub_options<AllocatorT>(),
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & static_options =
detail::get_default_transform_listener_static_sub_options<AllocatorT>())
: buffer_(buffer)
{
init(node, spin_thread, qos, static_qos, options, static_options);
init(
node_base,
node_logging,
node_parameters,
node_topics,
spin_thread,
qos,
static_qos,
options,
static_options);
}

TF2_ROS_PUBLIC
virtual ~TransformListener();

private:
template<class NodeT, class AllocatorT = std::allocator<void>>
template<class AllocatorT = std::allocator<void>>
void init(
NodeT && node,
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters,
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics,
bool spin_thread,
const rclcpp::QoS & qos,
const rclcpp::QoS & static_qos,
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options,
const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & static_options)
{
spin_thread_ = spin_thread;
node_base_interface_ = node->get_node_base_interface();
node_logging_interface_ = node->get_node_logging_interface();
node_base_interface_ = node_base;
node_logging_interface_ = node_logging;

using callback_t = std::function<void (tf2_msgs::msg::TFMessage::ConstSharedPtr)>;
callback_t cb = std::bind(
Expand All @@ -136,9 +182,14 @@ class TransformListener
tf_static_options.callback_group = callback_group_;

message_subscription_tf_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
node, "/tf", qos, std::move(cb), tf_options);
node_parameters, node_topics, "/tf", qos, std::move(cb), tf_options);
message_subscription_tf_static_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
node, "/tf_static", static_qos, std::move(static_cb), tf_static_options);
node_parameters,
node_topics,
"/tf_static",
static_qos,
std::move(static_cb),
tf_static_options);

// Create executor with dedicated thread to spin.
executor_ = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
Expand All @@ -148,28 +199,34 @@ class TransformListener
buffer_.setUsingDedicatedThread(true);
} else {
message_subscription_tf_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
node, "/tf", qos, std::move(cb), options);
node_parameters, node_topics, "/tf", qos, std::move(cb), options);
message_subscription_tf_static_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
node, "/tf_static", static_qos, std::move(static_cb), static_options);
node_parameters,
node_topics,
"/tf_static",
static_qos,
std::move(static_cb),
static_options);
}
}
/// Callback function for ros message subscriptoin
TF2_ROS_PUBLIC
void subscription_callback(tf2_msgs::msg::TFMessage::ConstSharedPtr msg, bool is_static);

// ros::CallbackQueue tf_message_callback_queue_;
bool spin_thread_{false};
std::unique_ptr<std::thread> dedicated_listener_thread_;
std::unique_ptr<std::thread> dedicated_listener_thread_ {nullptr};
rclcpp::CallbackGroup::SharedPtr callback_group_{nullptr};
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
rclcpp::Executor::SharedPtr executor_ {nullptr};

rclcpp::Node::SharedPtr optional_default_node_ = nullptr;
rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr message_subscription_tf_;
rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr message_subscription_tf_static_;
rclcpp::Node::SharedPtr optional_default_node_ {nullptr};
rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr
message_subscription_tf_ {nullptr};
rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr
message_subscription_tf_static_ {nullptr};
tf2::BufferCore & buffer_;
tf2::TimePoint last_update_;
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface_;
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface_;
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface_ {nullptr};
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface_ {nullptr};
};
} // namespace tf2_ros

Expand Down
7 changes: 5 additions & 2 deletions tf2_ros/src/transform_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ TransformListener::TransformListener(tf2::BufferCore & buffer, bool spin_thread)
options.start_parameter_services(false);
optional_default_node_ = rclcpp::Node::make_shared("_", options);
init(
optional_default_node_, spin_thread, DynamicListenerQoS(), StaticListenerQoS(),
optional_default_node_->get_node_base_interface(),
optional_default_node_->get_node_logging_interface(),
optional_default_node_->get_node_parameters_interface(),
optional_default_node_->get_node_topics_interface(),
spin_thread, DynamicListenerQoS(), StaticListenerQoS(),
detail::get_default_transform_listener_sub_options(),
detail::get_default_transform_listener_static_sub_options());
}
Expand All @@ -66,7 +70,6 @@ TransformListener::~TransformListener()
}
}


void TransformListener::subscription_callback(
const tf2_msgs::msg::TFMessage::ConstSharedPtr msg,
bool is_static)
Expand Down