Go to the documentation of this file.00001
00002 import roslib
00003 roslib.load_manifest("biotac_log_parser")
00004 import rospy
00005 import rosbag
00006 import numpy
00007 import glob
00008 import sys
00009 import cPickle
00010 from optparse import OptionParser
00011 import itertools
00012 import rosjson_time.rosjson_time
00013 from biotac_sensors.msg import BioTacHand
00014
00015 def main():
00016 """ Usage
00017 First log the data using rosbag:
00018 rosbag record /biotac_pub [other topics] -o file_prefix
00019
00020 Many topics can be recorded, but this file only parses biotac msgs.
00021 The call rosrun rosrun biotac_log_parser parse_log_json.py -i bag_file -o output.json
00022
00023 Multiple bag files can be specified on the command line using wildcards, but they need to be
00024 enclosed in quotes:
00025 The call rosrun rosrun biotac_log_parser parse_log_json.py -i "*.bag" -o output.json
00026 """
00027 parser = OptionParser()
00028 parser.add_option("-i", "--input_file", dest="input_file",
00029 help="input bag FILEs. Use wildcards for multiple files",
00030 metavar="FILE", action="store")
00031 parser.add_option("-o", "--output_file", dest="output_file",
00032 help="output FILE", metavar="FILE")
00033
00034 (options, _) = parser.parse_args()
00035
00036 if options.input_file is None:
00037 rospy.logerr("The input bag has to be specified")
00038 sys.exit()
00039 if options.output_file is None:
00040 rospy.logerr("The output file has to be specified")
00041 sys.exit()
00042
00043
00044 output_file = open(options.output_file, "w")
00045
00046
00047 gen = (glob.glob(f) for f in options.input_file.split())
00048 frame_count = 0
00049 elements = []
00050 for filename in itertools.chain.from_iterable(gen):
00051
00052 rospy.loginfo("Opening bag %s"% filename)
00053 bag = rosbag.Bag(filename)
00054
00055 for _, msg, _ in bag.read_messages(topics="/biotac_pub"):
00056 isinstance(msg, BioTacHand)
00057
00058 msg.header.frame_id = frame_count
00059 toWrite = rosjson_time.ros_message_to_json(msg) + '\n'
00060 elements.append(toWrite)
00061
00062 frame_count +=1
00063
00064 output_file.write("[\n")
00065 output_file.write( ",".join(elements))
00066 output_file.write("]\n")
00067 output_file.close()
00068
00069
00070 if __name__ == "__main__":
00071 main()