playable_bag.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_PLAYABLE_BAG_H
00018 #define CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_PLAYABLE_BAG_H
00019 #include <cartographer_ros_msgs/BagfileProgress.h>
00020 #include <ros/node_handle.h>
00021 
00022 #include <functional>
00023 #include <queue>
00024 
00025 #include "rosbag/bag.h"
00026 #include "rosbag/view.h"
00027 #include "tf2_ros/buffer.h"
00028 
00029 namespace cartographer_ros {
00030 
00031 class PlayableBag {
00032  public:
00033   // Handles messages early, i.e. when they are about to enter the buffer.
00034   // Returns a boolean indicating whether the message should enter the buffer.
00035   using FilteringEarlyMessageHandler =
00036       std::function<bool /* forward_message_to_buffer */ (
00037           const rosbag::MessageInstance&)>;
00038 
00039   PlayableBag(const std::string& bag_filename, int bag_id, ros::Time start_time,
00040               ros::Time end_time, ros::Duration buffer_delay,
00041               FilteringEarlyMessageHandler filtering_early_message_handler);
00042 
00043   ros::Time PeekMessageTime() const;
00044   rosbag::MessageInstance GetNextMessage(
00045       cartographer_ros_msgs::BagfileProgress* progress);
00046   bool IsMessageAvailable() const;
00047   std::tuple<ros::Time, ros::Time> GetBeginEndTime() const;
00048 
00049   int bag_id() const;
00050   std::set<std::string> topics() const { return topics_; }
00051   double duration_in_seconds() const { return duration_in_seconds_; }
00052   bool finished() const { return finished_; }
00053 
00054  private:
00055   void AdvanceOneMessage();
00056   void AdvanceUntilMessageAvailable();
00057 
00058   std::unique_ptr<rosbag::Bag> bag_;
00059   std::unique_ptr<rosbag::View> view_;
00060   rosbag::View::const_iterator view_iterator_;
00061   bool finished_;
00062   const int bag_id_;
00063   const std::string bag_filename_;
00064   const double duration_in_seconds_;
00065   int message_counter_;
00066   std::deque<rosbag::MessageInstance> buffered_messages_;
00067   const ::ros::Duration buffer_delay_;
00068   FilteringEarlyMessageHandler filtering_early_message_handler_;
00069   std::set<std::string> topics_;
00070 };
00071 
00072 class PlayableBagMultiplexer {
00073  public:
00074   PlayableBagMultiplexer();
00075   void AddPlayableBag(PlayableBag playable_bag);
00076 
00077   // Returns the next message from the multiplexed (merge-sorted) message
00078   // stream, along with the bag id corresponding to the message, and whether
00079   // this was the last message in that bag.
00080   std::tuple<rosbag::MessageInstance, int /* bag_id */,
00081              bool /* is_last_message_in_bag */>
00082   GetNextMessage();
00083 
00084   bool IsMessageAvailable() const;
00085   ros::Time PeekMessageTime() const;
00086 
00087   std::set<std::string> topics() const { return topics_; }
00088 
00089  private:
00090   struct BagMessageItem {
00091     ros::Time message_timestamp;
00092     int bag_index;
00093     struct TimestampIsGreater {
00094       bool operator()(const BagMessageItem& l, const BagMessageItem& r) {
00095         return l.message_timestamp > r.message_timestamp;
00096       }
00097     };
00098   };
00099 
00100   ros::NodeHandle pnh_;
00101   // Publishes information about the bag-file(s) processing and its progress
00102   ros::Publisher bag_progress_pub_;
00103   // Map between bagfile id and the last time when its progress was published
00104   std::map<int, ros::Time> bag_progress_time_map_;
00105   // The time interval of publishing bag-file(s) processing in seconds
00106   double progress_pub_interval_;
00107 
00108   std::vector<PlayableBag> playable_bags_;
00109   std::priority_queue<BagMessageItem, std::vector<BagMessageItem>,
00110                       BagMessageItem::TimestampIsGreater>
00111       next_message_queue_;
00112   std::set<std::string> topics_;
00113 };
00114 
00115 }  // namespace cartographer_ros
00116 
00117 #endif  // CARTOGRAPHER_ROS_CARTOGRAPHER_ROS_PLAYABLE_BAG_H


cartographer_ros
Author(s): The Cartographer Authors
autogenerated on Wed Jul 10 2019 04:10:28