NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
astrobee_recorder.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #ifndef DATA_BAGGER_ASTROBEE_RECORDER_H_
36 #define DATA_BAGGER_ASTROBEE_RECORDER_H_
37 
38 #include <sys/stat.h>
39 #if !defined(_MSC_VER)
40  #include <termios.h>
41  #include <unistd.h>
42 #endif
43 #include <time.h>
44 
45 #include <boost/thread/condition.hpp>
46 #include <boost/thread/mutex.hpp>
47 #include <boost/regex.hpp>
48 
49 #include <ros/ros.h>
50 #include <ros/time.h>
51 #include <ros/callback_queue.h>
52 
53 #include <std_msgs/Empty.h>
54 #include <topic_tools/shape_shifter.h>
55 
56 #include <queue>
57 #include <set>
58 #include <string>
59 #include <vector>
60 #include <list>
61 #include <mutex>
62 #include <condition_variable> // NOLINT
63 
64 #include "rosbag/bag.h"
65 #include "rosbag/stream.h"
66 #include "rosbag/macros.h"
67 #include "rosbag/recorder.h"
68 
69 namespace astrobee_rosbag {
70 
71 using rosbag::OutgoingMessage;
72 using rosbag::OutgoingQueue;
73 using rosbag::RecorderOptions;
74 using rosbag::Bag;
75 
76 class ROSBAG_DECL Recorder {
77  public:
78  explicit Recorder(RecorderOptions const& options);
79 
80  void doTrigger();
81 
82  bool isSubscribed(std::string const& topic) const;
83 
84  boost::shared_ptr<ros::Subscriber> subscribe(std::string const& topic);
85 
86  int run();
87 
88  void stop();
89 
90  private:
91  void printUsage();
92 
93  void updateFilenames();
94  void startWriting();
95  void stopWriting();
96 
97  bool checkLogging();
98  bool scheduledCheckDisk();
99  bool checkDisk();
100 
101  void snapshotTrigger(std_msgs::Empty::ConstPtr trigger);
102  void doQueue(const ros::MessageEvent<topic_tools::ShapeShifter const>& msg_event,
103  std::string const& topic,
104  boost::shared_ptr<ros::Subscriber> subscriber,
105  boost::shared_ptr<int> count);
106  void doRecord();
107  void checkNumSplits();
108  bool checkSize();
109  bool checkDuration(const ros::Time&);
110  void doRecordSnapshotter();
111  void doCheckMaster(ros::TimerEvent const& e, ros::NodeHandle& node_handle);
112 
113  bool shouldSubscribeToTopic(std::string const& topic, bool from_node = false);
114 
115  template<class T>
116  static std::string timeToStr(T ros_t);
117 
118  private:
119  RecorderOptions options_;
120 
121  Bag bag_;
122 
123  std::string target_filename_;
124  std::string write_filename_;
125  std::list<std::string> current_files_;
126 
127  std::set<std::string> currently_recording_;
128  int num_subscribers_;
129 
130  int exit_code_;
131 
132  boost::condition_variable_any queue_condition_;
133  boost::mutex queue_mutex_;
134  std::queue<OutgoingMessage>* queue_;
135  uint64_t queue_size_;
136  uint64_t max_queue_size_;
137 
138  uint64_t split_count_;
139 
140  std::queue<OutgoingQueue> queue_queue_;
141 
142  ros::Time last_buffer_warn_;
143 
144  ros::Time start_time_;
145 
146  bool writing_enabled_;
147  boost::mutex check_disk_mutex_;
148  ros::WallTime check_disk_next_;
149  ros::WallTime warn_next_;
150  ros::NodeHandle node_handle_;
151  ros::CallbackQueue callback_queue_;
152  std::mutex mutex_;
153  bool should_stop_;
154  std::condition_variable condition_variable_;
155 };
156 
157 } // namespace astrobee_rosbag
158 
159 #endif // DATA_BAGGER_ASTROBEE_RECORDER_H_
160 
astrobee_rosbag::Recorder
Definition: astrobee_recorder.h:108
time.h
astrobee_rosbag
Definition: astrobee_recorder.h:69
mutex_
std::mutex mutex_
Definition: smart_dock_service.cc:37
localization_common::Time
double Time
Definition: time.h:23