tf2_ros  master
This package contains the ROS bindings for the tf2 library, for both Python and C++.
buffer_server.h
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2009, Willow Garage, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of Willow Garage, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Author: Eitan Marder-Eppstein
36 *********************************************************************/
37 
38 #ifndef TF2_ROS__BUFFER_SERVER_H_
39 #define TF2_ROS__BUFFER_SERVER_H_
40 
41 #include <tf2/time.h>
42 #include <tf2/buffer_core_interface.h>
44 
45 #include <geometry_msgs/msg/transform_stamped.hpp>
46 #include <rclcpp/create_timer.hpp>
47 #include <rclcpp/rclcpp.hpp>
48 #include <rclcpp_action/rclcpp_action.hpp>
49 #include <tf2_msgs/action/lookup_transform.hpp>
50 
51 #include <functional>
52 #include <list>
53 #include <memory>
54 #include <mutex>
55 #include <string>
56 
57 namespace tf2_ros
58 {
64 class BufferServer
65 {
66  using LookupTransformAction = tf2_msgs::action::LookupTransform;
68 
69 public:
76  template<typename NodePtr>
78  const tf2::BufferCoreInterface & buffer,
79  NodePtr node,
80  const std::string & ns,
81  tf2::Duration check_period = tf2::durationFromSec(0.01))
82  : buffer_(buffer),
83  logger_(node->get_logger())
84  {
85  server_ = rclcpp_action::create_server<LookupTransformAction>(
86  node,
87  ns,
88  std::bind(&BufferServer::goalCB, this, std::placeholders::_1, std::placeholders::_2),
89  std::bind(&BufferServer::cancelCB, this, std::placeholders::_1),
90  std::bind(&BufferServer::acceptedCB, this, std::placeholders::_1));
91 
92  check_timer_ = rclcpp::create_timer(
93  node, node->get_clock(), check_period, std::bind(&BufferServer::checkTransforms, this));
94  RCLCPP_DEBUG(logger_, "Buffer server started");
95  }
96 
97 private:
98  struct GoalInfo
99  {
100  GoalHandle handle;
101  tf2::TimePoint end_time;
102  };
103 
105  rclcpp_action::GoalResponse goalCB(
106  const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const LookupTransformAction::Goal> goal);
107 
109  void acceptedCB(GoalHandle gh);
110 
112  rclcpp_action::CancelResponse cancelCB(GoalHandle gh);
113 
115  void checkTransforms();
116 
118  bool canTransform(GoalHandle gh);
119 
121  geometry_msgs::msg::TransformStamped lookupTransform(GoalHandle gh);
122 
123  const tf2::BufferCoreInterface & buffer_;
124  rclcpp::Logger logger_;
125  rclcpp_action::Server<LookupTransformAction>::SharedPtr server_;
126  std::list<GoalInfo> active_goals_;
127  std::mutex mutex_;
128  rclcpp::TimerBase::SharedPtr check_timer_;
129 };
130 
131 } // namespace tf2_ros
132 
133 #endif // TF2_ROS__BUFFER_SERVER_H_
std::bind
T bind(T... args)
std::string
std::shared_ptr
std::list< GoalInfo >
tf2_ros
Definition: async_buffer_interface.h:43
TF2_ROS_PUBLIC
#define TF2_ROS_PUBLIC
Definition: visibility_control.h:58
visibility_control.h
std::mutex
tf2_ros::BufferServer::BufferServer
BufferServer(const tf2::BufferCoreInterface &buffer, NodePtr node, const std::string &ns, tf2::Duration check_period=tf2::durationFromSec(0.01))
Constructor.
Definition: buffer_server.h:147