playable_bag.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
21 #include "glog/logging.h"
22 #include "tf2_msgs/TFMessage.h"
23 
24 namespace cartographer_ros {
25 
27  const std::string& bag_filename, const int bag_id,
28  const ros::Time start_time, const ros::Time end_time,
29  const ros::Duration buffer_delay,
30  FilteringEarlyMessageHandler filtering_early_message_handler)
31  : bag_(cartographer::common::make_unique<rosbag::Bag>(
32  bag_filename, rosbag::bagmode::Read)),
33  view_(cartographer::common::make_unique<rosbag::View>(*bag_, start_time,
34  end_time)),
35  view_iterator_(view_->begin()),
36  finished_(false),
37  bag_id_(bag_id),
38  bag_filename_(bag_filename),
39  duration_in_seconds_(
40  (view_->getEndTime() - view_->getBeginTime()).toSec()),
41  log_counter_(0),
42  buffer_delay_(buffer_delay),
43  filtering_early_message_handler_(
44  std::move(filtering_early_message_handler)) {
46 }
47 
49  CHECK(IsMessageAvailable());
50  return buffered_messages_.front().getTime();
51 }
52 
53 std::tuple<ros::Time, ros::Time> PlayableBag::GetBeginEndTime() const {
54  return std::make_tuple(view_->getBeginTime(), view_->getEndTime());
55 }
56 
58  CHECK(IsMessageAvailable());
59  const rosbag::MessageInstance msg = buffered_messages_.front();
60  buffered_messages_.pop_front();
62  if ((log_counter_++ % 10000) == 0) {
63  LOG(INFO) << "Processed " << (msg.getTime() - view_->getBeginTime()).toSec()
64  << " of " << duration_in_seconds_ << " seconds of bag "
65  << bag_filename_;
66  }
67  return msg;
68 }
69 
71  return !buffered_messages_.empty() &&
72  (buffered_messages_.front().getTime() <
73  buffered_messages_.back().getTime() - buffer_delay_);
74 }
75 
76 int PlayableBag::bag_id() const { return bag_id_; }
77 
79  CHECK(!finished_);
80  if (view_iterator_ == view_->end()) {
81  finished_ = true;
82  return;
83  }
87  buffered_messages_.push_back(msg);
88  }
90 }
91 
93  if (finished_) {
94  return;
95  }
96  do {
98  } while (!finished_ && !IsMessageAvailable());
99 }
100 
102  playable_bags_.push_back(std::move(playable_bag));
103  CHECK(playable_bags_.back().IsMessageAvailable());
104  next_message_queue_.emplace(
105  BagMessageItem{playable_bags_.back().PeekMessageTime(),
106  static_cast<int>(playable_bags_.size() - 1)});
107 }
108 
110  return !next_message_queue_.empty();
111 }
112 
113 std::tuple<rosbag::MessageInstance, int, bool>
115  CHECK(IsMessageAvailable());
116  const int current_bag_index = next_message_queue_.top().bag_index;
117  PlayableBag& current_bag = playable_bags_.at(current_bag_index);
118  rosbag::MessageInstance msg = current_bag.GetNextMessage();
119  CHECK_EQ(msg.getTime(), next_message_queue_.top().message_timestamp);
120  next_message_queue_.pop();
121  if (current_bag.IsMessageAvailable()) {
122  next_message_queue_.emplace(
123  BagMessageItem{current_bag.PeekMessageTime(), current_bag_index});
124  }
125  return std::make_tuple(std::move(msg), current_bag.bag_id(),
126  !current_bag.IsMessageAvailable());
127 }
128 
130  CHECK(IsMessageAvailable());
131  return next_message_queue_.top().message_timestamp;
132 }
133 
134 } // namespace cartographer_ros
const std::string bag_filename_
Definition: playable_bag.h:56
_Unique_if< T >::_Single_object make_unique(Args &&... args)
ros::Time const & getTime() const
std::unique_ptr< rosbag::View > view_
Definition: playable_bag.h:52
std::tuple< ros::Time, ros::Time > GetBeginEndTime() const
Definition: playable_bag.cc:53
const ::ros::Duration buffer_delay_
Definition: playable_bag.h:60
std::deque< rosbag::MessageInstance > buffered_messages_
Definition: playable_bag.h:59
FilteringEarlyMessageHandler filtering_early_message_handler_
Definition: playable_bag.h:61
std::tuple< rosbag::MessageInstance, int, bool > GetNextMessage()
PlayableBag(const std::string &bag_filename, int bag_id, ros::Time start_time, ros::Time end_time, ros::Duration buffer_delay, FilteringEarlyMessageHandler filtering_early_message_handler)
Definition: playable_bag.cc:26
void AddPlayableBag(PlayableBag playable_bag)
rosbag::View::const_iterator view_iterator_
Definition: playable_bag.h:53
rosbag::MessageInstance GetNextMessage()
Definition: playable_bag.cc:57
std::function< bool(const rosbag::MessageInstance &)> FilteringEarlyMessageHandler
Definition: playable_bag.h:34
Read
ros::Time PeekMessageTime() const
Definition: playable_bag.cc:48
void move(std::vector< T > &a, std::vector< T > &b)


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:06:05