process_bags.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import rospy
00004 import rosbag
00005 import time
00006 
00007 
00008 def callback(topic, msg):
00009         global current_time
00010         try:
00011                 header = getattr(msg, "header")
00012                 if header.stamp < current_time:
00013                         print "received old message, skipping '%s'" % topic
00014                         return
00015         except AttributeError:
00016                 pass
00017         global result
00018         result[topic] = msg
00019 
00020 
00021 def wait_for_result(timeout, out_topics):
00022         #print "  waiting for result"
00023         t = 0
00024         while len(result) != len(out_topics) and not rospy.is_shutdown() and timeout > t:
00025                 rospy.sleep(0.1)
00026                 t += 0.1
00027 #       print "  timing: %f"%t
00028 
00029         return len(result) == len(out_topics)
00030 
00031 def process_bags(infile, in_topics, in_types,
00032                                                                 outfile, out_topics, out_types,
00033                                                                 timeout):
00034         publishers = dict([ (topic, rospy.Publisher(topic, t)) 
00035                         for topic, t in zip(in_topics, in_types) ])
00036 
00037 #       subscribers=[
00038 #               rospy.Subscriber(out_topic,out_type,lambda x:callback(out_topic,x))
00039 #               for out_topic,out_type in zip(out_topics,out_types) ]
00040 
00041         if(len(out_topics) > 0):
00042                 rospy.Subscriber(out_topics[0], out_types[0], lambda x:callback(out_topics[0], x))
00043         if(len(out_topics) > 1):
00044                 rospy.Subscriber(out_topics[1], out_types[1], lambda x:callback(out_topics[1], x))
00045         if(len(out_topics) > 2):
00046                 rospy.Subscriber(out_topics[2], out_types[2], lambda x:callback(out_topics[2], x))
00047         if(len(out_topics) > 3):
00048                 rospy.Subscriber(out_topics[3], out_types[3], lambda x:callback(out_topics[3], x))
00049         if(len(out_topics) > 4):        
00050                 print "lambda trouble, add more lines.."
00051                 return
00052         
00053 
00054         global result
00055         result = {}
00056 
00057         outbag = rosbag.Bag(outfile, "w")
00058         global current_time
00059         current_time = rospy.get_rostime()
00060         for topic, msg, t in rosbag.Bag(infile).read_messages(topics=in_topics):
00061                 if(t != current_time):
00062                         print "frame: ", current_time
00063                         current_time = t
00064                         in_msgs = {}
00065                 
00066                 in_msgs[topic] = msg
00067 
00068                 if len(in_msgs) == len(in_topics):
00069                         result = {}
00070                         print "  in topics:"
00071                         for topic, msg in in_msgs.iteritems():
00072                                 print "    %s" % topic
00073                                 publishers[topic].publish(msg)
00074 
00075                         t0 = time.time()                                
00076                         complete = wait_for_result(timeout, out_topics)
00077 
00078                         print "  out topics (time=%f):" % (time.time() - t0)
00079                         for topic, msg in result.iteritems():
00080                                 print "    %s" % topic
00081 
00082                         if complete:
00083                                 if(time.time() - t0 > timeout * 0.5):
00084                                         timeout = (time.time() - t0) * 2
00085                                         print "  increasing timeout to %f" % timeout
00086                                 for topic, msg in in_msgs.iteritems():
00087                                         outbag.write(topic, msg, t)
00088                                 for topic, msg in result.iteritems():
00089                                         outbag.write(topic, msg, t)
00090                                 print "  saving frame"
00091                         else:
00092                                 print "  skipping frame"
00093 
00094                 if rospy.is_shutdown():
00095                         break;
00096                 
00097         outbag.close()
00098 
00099 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


articulation_structure
Author(s): sturm
autogenerated on Wed Dec 26 2012 15:37:59