00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2013, Open Source Robotics Foundation 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #ifndef ROSBAG_BAG_PLAYER_H 00036 #define ROSBAG_BAG_PLAYER_H 00037 00038 #include <boost/foreach.hpp> 00039 00040 #include "rosbag/bag.h" 00041 #include "rosbag/view.h" 00042 00043 namespace rosbag 00044 { 00045 00046 00047 // A helper struct 00048 struct BagCallback 00049 { 00050 virtual ~BagCallback() {}; 00051 virtual void call(MessageInstance m) = 0; 00052 }; 00053 00054 // A helper class for the callbacks 00055 template<class T> 00056 class BagCallbackT : public BagCallback 00057 { 00058 public: 00059 typedef boost::function<void (const boost::shared_ptr<const T>&)> Callback; 00060 00061 BagCallbackT(Callback cb) : 00062 cb_(cb) 00063 {} 00064 00065 void call(MessageInstance m) { 00066 cb_(m.instantiate<T>()); 00067 } 00068 00069 private: 00070 Callback cb_; 00071 }; 00072 00073 00074 /* A class for playing back bag files at an API level. It supports 00075 relatime, as well as accelerated and slowed playback. */ 00076 class BagPlayer 00077 { 00078 public: 00079 /* Constructor expecting the filename of a bag */ 00080 BagPlayer(const std::string &filename) throw(BagException); 00081 00082 /* Register a callback for a specific topic and type */ 00083 template<class T> 00084 void register_callback(const std::string &topic, 00085 typename BagCallbackT<T>::Callback f); 00086 00087 /* Unregister a callback for a topic already registered */ 00088 void unregister_callback(const std::string &topic); 00089 00090 /* Set the time in the bag to start. 00091 * Default is the first message */ 00092 void set_start(const ros::Time &start); 00093 00094 /* Set the time in the bag to stop. 00095 * Default is the last message */ 00096 void set_end(const ros::Time &end); 00097 00098 /* Set the speed to playback. 1.0 is the default. 00099 * 2.0 would be twice as fast, 0.5 is half realtime. */ 00100 void set_playback_speed(double scale); 00101 00102 /* Start playback of the bag file using the parameters previously 00103 set */ 00104 void start_play(); 00105 00106 /* Get the current time of the playback */ 00107 ros::Time get_time(); 00108 00109 // Destructor 00110 virtual ~BagPlayer(); 00111 00112 00113 // The bag file interface loaded in the constructor. 00114 Bag bag; 00115 00116 private: 00117 ros::Time real_time(const ros::Time &msg_time); 00118 00119 std::map<std::string, BagCallback *> cbs_; 00120 ros::Time bag_start_; 00121 ros::Time bag_end_; 00122 ros::Time last_message_time_; 00123 double playback_speed_; 00124 ros::Time play_start_; 00125 }; 00126 00127 template<class T> 00128 void BagPlayer::register_callback(const std::string &topic, 00129 typename BagCallbackT<T>::Callback cb) { 00130 cbs_[topic] = new BagCallbackT<T>(cb); 00131 } 00132 00133 } 00134 00135 #endif