process_bags_utils.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import roslib
00004 roslib.load_manifest( 'tf' )
00005 roslib.load_manifest( 'hrl_lib' )
00006 import rospy
00007 
00008 import tf
00009 from hrl_lib.cmd_process import CmdProcess
00010 
00011 from threading import Thread
00012 import time
00013 
00014 
00015 def bagplay( fname ):
00016     # to use:
00017     #   bp = bagplay( my_file_name )
00018     #   bp.run() # starts the execution
00019     #   while not bp.is_finished():
00020     #       rospy.sleep( 0.5 )
00021     #   bp.kill() # not necessary
00022     cmd = 'rosbag play --clock ' + fname + ' -r 2.0 -q'
00023     rospy.logout( 'Launching bag file: %s' % fname )
00024     return CmdProcess( cmd.split() )
00025 
00026 
00027 def sim_safe_sleep( dur, real_time_sleep = 0.05 ):
00028     t0 = rospy.Time.now().to_sec()
00029     ct = rospy.Time.now().to_sec()
00030     while True:
00031         if ct - t0 >= dur:
00032             break
00033 
00034         time.sleep( real_time_sleep )
00035         nt = rospy.Time.now().to_sec()
00036 
00037         if nt == ct: # rostime will stop when bag not playing -- exit immediately.
00038             break  
00039         
00040         ct = nt
00041     return
00042 
00043 class TFthread( Thread ):
00044     # Takes in a (PoseStamped-like) dictionary and publishes a new transform from frame_id -> frame_name
00045     # For example: (YAML syntax)
00046     #    child_frame: '/datacap'
00047     #    parent_frame: '/map'
00048     #    x_pos: 6.480
00049     #    y_pos: 2.865
00050     #    z_pos: 1.291
00051     #    x_orient: 0.0
00052     #    y_orient: 0.0
00053     #    z_orient: 0.0
00054     #    w_orient: 1.0
00055     
00056     def __init__( self, d ):
00057         self.d = d
00058         Thread.__init__( self )
00059         
00060         # cf = self.d[ 'child_frame' ]
00061         # rospy.init_node( 'tf_thread_' + cf.strip('/') )
00062 
00063         self.bc = tf.TransformBroadcaster()
00064 
00065         self.should_run = True
00066         self.start()
00067 
00068     def run( self ):
00069         rospy.logout( 'TFthread: Starting %s ' % self.d[ 'child_frame' ] )
00070         while self.should_run and not rospy.is_shutdown():
00071             self.bc.sendTransform( ( self.d[ 'x_pos' ],
00072                                      self.d[ 'y_pos' ],
00073                                      self.d[ 'z_pos' ] ),
00074                                    ( self.d[ 'x_orient' ],
00075                                      self.d[ 'y_orient' ],
00076                                      self.d[ 'z_orient' ],
00077                                      self.d[ 'w_orient' ] ),
00078                                    rospy.Time.now(),
00079                                    self.d[ 'child_frame' ],
00080                                    self.d[ 'parent_frame' ] )
00081 
00082             try:
00083                 sim_safe_sleep( 0.10 )  # 10 Hz
00084             except:
00085                 pass # ROS exception (eg. Ctrl-C).
00086         rospy.logout( 'TFthread: Stopping %s ' % self.d[ 'child_frame' ] )
00087         
00088     def stop( self ):
00089         # Kill off the poller thread.
00090         self.should_run = False
00091         self.join(5)
00092         if (self.isAlive()):
00093             raise RuntimeError('TFthread: Unable to stop thread %s ' % self.d[ 'child_frame' ] )


rfid_datacapture
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 12:11:16