topic_with_transform.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_ROS_TOPIC_WITH_TRANSFORM_H
2 #define SLAM_CTOR_ROS_TOPIC_WITH_TRANSFORM_H
3 
4 #include <string>
5 #include <vector>
6 #include <memory>
8 #include <tf/message_filter.h>
10 #include <boost/shared_ptr.hpp>
11 
12 // TODO: make this class inner
13 template <typename MType>
14 class TopicObserver { // iface
15 public: // methods
17  const tf::StampedTransform&) = 0;
18 };
19 
20 // TODO: add scan drop
21 template <typename MsgType>
23  /* NB: wasn't able to implement with TF2 (ROS jade),
24  probably because of deadlock
25  (https://github.com/ros/geometry2/pull/144)
26  Try TF2 with proposed patch.
27  */
28 public: // methods
29  // TODO: copy, move ctrs, dtor
31  const std::string& topic_name,
32  const std::string& target_frame,
33  const double buffer_duration = 5.0,
34  const uint32_t tf_filter_queue_size = 1000,
35  const uint32_t subscribers_queue_size = 1000):
36  _target_frame{target_frame},
37  _subscr{nh, topic_name, subscribers_queue_size},
38  _tf_lsnr{ros::Duration(buffer_duration)},
39  _msg_flt{new tf::MessageFilter<MsgType>{
40  _subscr, _tf_lsnr, _target_frame, tf_filter_queue_size}} {
42  this);
43  }
44 
45  void subscribe(std::shared_ptr<TopicObserver<MsgType>> obs) {
46  _observers.push_back(obs);
47  }
48 private: // methods
50  tf::StampedTransform transform;
51  std::string msg_frame_id =
53  ros::Time msg_time =
55 
56  try{
57  // NB: using last available msg (time = 0) causes issues
58  // on running with higher rate.
59  _tf_lsnr.lookupTransform(_target_frame, msg_frame_id,
60  msg_time, transform);
61  }
62  catch (tf::TransformException ex){
63  ROS_ERROR("Transform retrieval failed. %s",ex.what());
64  return;
65  }
66 
67  for (auto obs : _observers) {
68  if (auto obs_ptr = obs.lock()) {
69  obs_ptr->handle_transformed_msg(msg, transform);
70  }
71  }
72  }
73 private: // fields
74  std::string _target_frame;
77  std::unique_ptr<tf::MessageFilter<MsgType>> _msg_flt;
78  std::vector<std::weak_ptr<TopicObserver<MsgType>>> _observers;
79 };
80 
81 #endif // macro guard
std::vector< std::weak_ptr< TopicObserver< MsgType > > > _observers
message_filters::Subscriber< MsgType > _subscr
virtual void handle_transformed_msg(const boost::shared_ptr< MType >, const tf::StampedTransform &)=0
tf::TransformListener _tf_lsnr
TopicWithTransform(ros::NodeHandle nh, const std::string &topic_name, const std::string &target_frame, const double buffer_duration=5.0, const uint32_t tf_filter_queue_size=1000, const uint32_t subscribers_queue_size=1000)
void subscribe(std::shared_ptr< TopicObserver< MsgType >> obs)
std::unique_ptr< tf::MessageFilter< MsgType > > _msg_flt
void transformed_msg_cb(const boost::shared_ptr< MsgType > msg)
#define ROS_ERROR(...)
Connection registerCallback(const C &callback)


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25