@@ -0,0 +1,2434 @@
+#!/usr/bin/env python%0Afrom __future__ import division%0Aimport rospy%0Afrom optparse import OptionParser%0Aimport tf%0Aimport sys%0Aimport time, os, subprocess%0Aimport threading%0Aimport numpy as np%0A%0Afrom sensor_msgs.msg import Image%0Aimport cv%0Afrom cv_bridge import CvBridge, CvBridgeError%0A%0A%0A###############################################################################%0A#%0Aclass SaveBag:%0A    def __init__(self, basename, directory, topics):%0A        filename = basename + '_' time.strftime('%25Y_%25m_%25d_%25H_%25M_%25S') + '.bag'%0A%0A        # Make sure path exists.%0A        try:%0A            os.makedirs(directory)%0A        except OSError:%0A            pass%0A%0A        self.filenameBag = os.path.join(directory, filename)%0A        self.topics = topics%0A%0A        rospy.on_shutdown(self.OnShutdown_callback)%0A%0A    def OnShutdown_callback(self):%0A        self.StopRecordingBag()%0A        %0A    def StartRecordingBag(self):%0A        rospy.logwarn('Saving bag file: %25s' %25 (self.dirBag+'/'+self.filenameBag))%0A        cmdline = %5B'rosbag', 'record','-O', self.dirBag+'/'+self.filenameBag%5D%0A        cmdline.extend(%5Bself.topics%5D)%0A        self.processRosbag = subprocess.Popen(cmdline, preexec_fn=subprocess.os.setpgrp)%0A    %0A    def StopRecordingBag(self):%0A        subprocess.os.killpg(self.processRosbag.pid, subprocess.signal.SIGINT)%0A        rospy.logwarn('Closed bag file.')%0A                %0A    def Main(self):%0A        savebag.StartRecordingBag()%0A        while (not rospy.is_shutdown()):%0A            rospy.spin()            %0A        %0A%0Aif __name__ == '__main__':%0A    parser = OptionParser()%0A    parser.add_option(%22--directory_parameter_name%22, type=%22str%22, dest=%22directory_parameter_name%22, default='',%0A                        help=%22name of ros parameter where directory in which to save can be found%22)%0A                        %0A    parser.add_option(%22--topics_parameter_name%22, type=%22str%22, dest=%22topics_parameter_name%22, default='',%0A                        help=%22name of ros parameter where list of topics to save can be found%22)%0A    %0A    parser.add_option(%22--basename%22, type=%22str%22, dest=%22basename%22, default='',%0A                        help=%22basename for bagfile%22)%0A    (options, args) = parser.parse_args()%0A%0A    directory = rospy.get_param(options.directory_parameter_name)%0A    topics = rospy.get_param(options.topics_parameter_name)%0A    %0A    rospy.init_node('SaveBag', log_level=rospy.INFO)%0A    rospy.sleep(1)%0A    savebag = SaveBag(basename, directory, topics)%0A    savebag.Main()%0A    %0A

save bag