bag_topic_with_transform.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_ROS_BAG_TOPIC_WITH_TRANSFORM_H
2 #define SLAM_CTOR_ROS_BAG_TOPIC_WITH_TRANSFORM_H
3 
4 #include <vector>
5 #include <string>
6 #include <tuple>
7 #include <queue>
8 #include <unordered_map>
9 
10 #include <rosbag/bag.h>
11 #include <rosbag/view.h>
12 #include <tf/tf.h>
13 #include <tf/tfMessage.h>
14 #include <geometry_msgs/TransformStamped.h>
15 
16 #include <boost/shared_ptr.hpp>
17 
18 template <typename MsgType>
20 protected:
21  using vstr = std::vector<std::string>;
22  using Transforms = std::unordered_map<std::string, std::vector<std::string>>;
23 public:
24  BagTopicWithTransform(const std::string &bag_fname,
25  const std::string &topic_name,
26  const std::string &tf_topic_name,
27  const std::string &target_frame)
28  : _topic_name{topic_name}, _tf_topic_name{tf_topic_name}
29  , _target_frame{target_frame}
30  , _bag{bag_fname}
33  , _tf_cache{true, ros::Duration{1000.0}} {}
34 
36  _bag.close();
37  }
38 
43 
44  void set_tf_ignores(const Transforms &tf_ignores) {
45  _tf_ignores = tf_ignores;
46  }
47 
49  while (_view_iter != _view.end()) {
50  auto ros_msg = *(_view_iter++);
51  auto msg_topic = ros_msg.getTopic();
52  if (msg_topic == _topic_name) {
53  _msg_cache.push(ros_msg.instantiate<MsgType>());
54  } else if (msg_topic == _tf_topic_name) {
55  auto tf_msg = ros_msg.instantiate<tf::tfMessage>();
56  for(auto t : tf_msg->transforms) {
57  if (should_skip_tf_transform(t)) {
58  continue;
59  }
63  }
64  }
65  if (has_synced_msg()) { return true; }
66  }
67 
68  return has_synced_msg();
69  }
70 
71  const auto &msg() const { return _msg;}
72  const auto &transform() const { return _transform;}
73  auto timestamp() const {
75  }
76 
77 private:
78 
79  bool should_skip_tf_transform(const geometry_msgs::TransformStamped &t) {
80  auto from = t.header.frame_id, to = t.child_frame_id;
81  if (_tf_ignores.find(from) == _tf_ignores.end()) {
82  return false;
83  }
84  auto skip_tos = _tf_ignores.at(from);
85  return std::find(skip_tos.begin(), skip_tos.end(), to) != skip_tos.end();
86  }
87 
88  bool has_synced_msg() {
89  if (_msg_cache.empty()) { return false;}
90 
91  auto msg = _msg_cache.front();
92 
93  using namespace ros::message_traits;
94  auto frame_id = FrameId<MsgType>::value(*msg);
95  auto time = TimeStamp<MsgType>::value(*msg);
97  try {
98  _tf_cache.lookupTransform(_target_frame, frame_id, time, transform);
99  } catch (const tf::ExtrapolationException &e) {
100  auto reason = std::string{e.what()};
101  if (reason.find("extrapolation into the future") != std::string::npos) {
102  // ok, wait for tf transforms
103  return false;
104  }
105 
106  std::cout << "[WARN][BagTopic] ";
107  if (reason.find("past") != std::string::npos) {
108  _msg_cache.pop();
109  std::cout << "Scan dropped. ";
110  }
111  std::cout << reason << std::endl;;
112  return false;
113  }
114 
115  _msg_cache.pop();
116  _msg = std::move(msg);
117  _transform = std::move(transform);
118  return true;
119  }
120 
121 private:
126 
129  std::queue<boost::shared_ptr<MsgType>> _msg_cache;
130 
133 };
134 
135 #endif
void set_tf_ignores(const Transforms &tf_ignores)
bool setTransform(const StampedTransform &transform, const std::string &authority="default_authority")
void close()
BagTopicWithTransform & operator=(const BagTopicWithTransform &)=delete
std::vector< std::string > vstr
bool should_skip_tf_transform(const geometry_msgs::TransformStamped &t)
rosbag::View::const_iterator _view_iter
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
iterator const_iterator
BagTopicWithTransform(const std::string &bag_fname, const std::string &topic_name, const std::string &tf_topic_name, const std::string &target_frame)
std::unordered_map< std::string, std::vector< std::string >> Transforms
const auto & transform() const
iterator end()
const auto & msg() const
iterator begin()
boost::shared_ptr< MsgType > _msg
tf::StampedTransform _transform
static void transformStampedMsgToTF(const geometry_msgs::TransformStamped &msg, StampedTransform &bt)
std::queue< boost::shared_ptr< MsgType > > _msg_cache


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