tf2_ros  master
This package contains the ROS bindings for the tf2 library, for both Python and C++.
transform_listener.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
32 #ifndef TF2_ROS__TRANSFORM_LISTENER_H_
33 #define TF2_ROS__TRANSFORM_LISTENER_H_
34 
35 #include <tf2/buffer_core.h>
36 #include <tf2/time.h>
38 
39 #include <tf2_msgs/msg/tf_message.hpp>
40 #include <rclcpp/rclcpp.hpp>
41 
42 #include <tf2_ros/qos.hpp>
43 
44 #include <functional>
45 #include <memory>
46 #include <thread>
47 #include <utility>
48 
49 
50 namespace tf2_ros
51 {
52 
53 namespace detail
54 {
55 template<class AllocatorT = std::allocator<void>>
56 rclcpp::SubscriptionOptionsWithAllocator<AllocatorT>
58 {
59  rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> options;
60  options.qos_overriding_options = rclcpp::QosOverridingOptions{
61  rclcpp::QosPolicyKind::Depth,
62  rclcpp::QosPolicyKind::Durability,
63  rclcpp::QosPolicyKind::History,
64  rclcpp::QosPolicyKind::Reliability};
65  return options;
66 }
67 
68 template<class AllocatorT = std::allocator<void>>
69 rclcpp::SubscriptionOptionsWithAllocator<AllocatorT>
71 {
72  rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> options;
73  options.qos_overriding_options = rclcpp::QosOverridingOptions{
74  rclcpp::QosPolicyKind::Depth,
75  rclcpp::QosPolicyKind::History,
76  rclcpp::QosPolicyKind::Reliability};
77  return options;
78 }
79 } // namespace detail
80 
84 {
85 public:
88  explicit TransformListener(tf2::BufferCore & buffer, bool spin_thread = true);
89 
90  template<class NodeT, class AllocatorT = std::allocator<void>>
92  tf2::BufferCore & buffer,
93  NodeT && node,
94  bool spin_thread = true,
95  const rclcpp::QoS & qos = DynamicListenerQoS(),
96  const rclcpp::QoS & static_qos = StaticListenerQoS(),
97  const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options =
98  detail::get_default_transform_listener_sub_options<AllocatorT>(),
99  const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & static_options =
100  detail::get_default_transform_listener_static_sub_options<AllocatorT>())
101  : buffer_(buffer)
102  {
103  init(node, spin_thread, qos, static_qos, options, static_options);
104  node_logging_interface_ = node->get_node_logging_interface();
105  }
106 
108  virtual ~TransformListener();
109 
110 private:
111  template<class NodeT, class AllocatorT = std::allocator<void>>
112  void init(
113  NodeT && node,
114  bool spin_thread,
115  const rclcpp::QoS & qos,
116  const rclcpp::QoS & static_qos,
117  const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & options,
118  const rclcpp::SubscriptionOptionsWithAllocator<AllocatorT> & static_options)
119  {
120  node_logging_interface_ = node->get_node_logging_interface();
121 
122  using callback_t = std::function<void (tf2_msgs::msg::TFMessage::SharedPtr)>;
123  callback_t cb = std::bind(
124  &TransformListener::subscription_callback, this, std::placeholders::_1, false);
125  callback_t static_cb = std::bind(
126  &TransformListener::subscription_callback, this, std::placeholders::_1, true);
127 
128  message_subscription_tf_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
129  node,
130  "/tf",
131  qos,
132  std::move(cb),
133  options);
134  message_subscription_tf_static_ = rclcpp::create_subscription<tf2_msgs::msg::TFMessage>(
135  node,
136  "/tf_static",
137  static_qos,
138  std::move(static_cb),
139  static_options);
140 
141  if (spin_thread) {
142  initThread(node->get_node_base_interface());
143  }
144  }
145 
147  void initThread(
148  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface);
149 
152  void subscription_callback(tf2_msgs::msg::TFMessage::SharedPtr msg, bool is_static);
153 
154  // ros::CallbackQueue tf_message_callback_queue_;
155  using thread_ptr =
157  thread_ptr dedicated_listener_thread_;
158 
159  rclcpp::Node::SharedPtr optional_default_node_ = nullptr;
160  rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr message_subscription_tf_;
161  rclcpp::Subscription<tf2_msgs::msg::TFMessage>::SharedPtr message_subscription_tf_static_;
162  tf2::BufferCore & buffer_;
163  tf2::TimePoint last_update_;
164  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface_;
165 };
166 } // namespace tf2_ros
167 
168 #endif // TF2_ROS__TRANSFORM_LISTENER_H_
std::bind
T bind(T... args)
std::move
T move(T... args)
std::function
tf2_ros::detail::get_default_transform_listener_static_sub_options
rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > get_default_transform_listener_static_sub_options()
Definition: transform_listener.h:70
qos.hpp
tf2_ros::TransformListener
This class provides an easy way to request and receive coordinate frame transform information.
Definition: transform_listener.h:83
std::thread
tf2_ros
Definition: async_buffer_interface.h:43
tf2_ros::TransformListener::TransformListener
TF2_ROS_PUBLIC TransformListener(tf2::BufferCore &buffer, bool spin_thread=true)
Constructor for transform listener.
tf2_ros::detail::get_default_transform_listener_sub_options
rclcpp::SubscriptionOptionsWithAllocator< AllocatorT > get_default_transform_listener_sub_options()
Definition: transform_listener.h:57
tf2_ros::DynamicListenerQoS
Definition: qos.hpp:39
TF2_ROS_PUBLIC
#define TF2_ROS_PUBLIC
Definition: visibility_control.h:58
tf2_ros::TransformListener::~TransformListener
virtual TF2_ROS_PUBLIC ~TransformListener()
visibility_control.h
tf2_ros::TransformListener::TransformListener
TransformListener(tf2::BufferCore &buffer, NodeT &&node, 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 >())
Definition: transform_listener.h:91
tf2_ros::StaticListenerQoS
Definition: qos.hpp:53
std::unique_ptr