00001
00002 import roslib
00003 roslib.load_manifest('smach_ros')
00004 roslib.load_manifest('actionlib')
00005 roslib.load_manifest('rfid_datacapture')
00006 roslib.load_manifest('rfid_demos')
00007 roslib.load_manifest('rfid_behaviors')
00008 roslib.load_manifest('hrl_lib')
00009 roslib.load_manifest('tf')
00010 roslib.load_manifest('sensor_msgs')
00011 roslib.load_manifest('rosbag')
00012 roslib.load_manifest('visualization_msgs')
00013 import rospy
00014
00015 import cPickle as pkl
00016 import hrl_lib.rutils as ru
00017 import hrl_lib.viz as viz
00018 import sensor_msgs.msg as sm
00019 import numpy as np, math
00020 import sm_aware_home_explore as ahe
00021 import rosbag
00022 import visualization_msgs.msg as vm
00023 from hrl_lib.cmd_process import CmdProcess
00024 import glob
00025 import json
00026 import yaml
00027 import time
00028 import os
00029
00030 def publish_robotpose( trial, obj, pub_time = 30e3, screen_cap = False ):
00031 print 'Change screen to RVIZ. Starting %d, %d' % (trial, obj)
00032 loc = ( trial + obj ) % 9
00033
00034 obj_name = ahe.tdb[obj][0]
00035 tname = obj_name.replace( ' ', '' )
00036
00037
00038 fname = 'search_aware_home/woot_150_'+str(trial)+'_tag_'+tname+'_headpost.bag'
00039
00040
00041 bp = bagplay( fname )
00042 bp.run()
00043
00044 while not bp.is_finished():
00045 try:
00046 rospy.init_node( 'markpub' )
00047 pub_mark = rospy.Publisher( '/robot_pose', vm.Marker )
00048 except:
00049 print 'Init Failure.'
00050
00051
00052
00053
00054 vsm = viz.single_marker
00055 mark = [ vsm( np.matrix([ 0.0, 0.0, 0.0 ]).T,
00056 np.matrix([ 0.0, 0.0, 0.0, 1.0 ]).T,
00057 'cube', '/base_link',
00058 scale = [0.65, 0.65, 0.001],
00059 color = [158./255, 86./255, 192./255, 0.9],
00060 duration = 30.0,
00061 m_id = 20000 )]
00062
00063 [ pub_mark.publish( x ) for x in mark ]
00064
00065
00066 sim_safe_sleep( 1.0 )
00067
00068
00069
00070 if screen_cap:
00071 os.system( 'scrot -d 2 -u Obj%d_Trial%d_RobotView.png' % ( obj, trial ))
00072
00073
00074 print 'Waiting for markers and points to expire'
00075 t0 = time.time()
00076 t_sleep = 60.0
00077 while time.time() - t0 < t_sleep and not rospy.is_shutdown():
00078 if int(time.time() - t0) % 5 == 0:
00079 print 'Time left: %d' % (t_sleep - int(time.time() - t0))
00080 time.sleep( 1.0 )
00081
00082 return
00083
00084
00085 def sim_safe_sleep( dur, real_time_sleep = 0.05 ):
00086 t0 = rospy.Time.now().to_sec()
00087 ct = rospy.Time.now().to_sec()
00088 while True:
00089 if ct - t0 >= dur:
00090 break
00091
00092 time.sleep( real_time_sleep )
00093 nt = rospy.Time.now().to_sec()
00094
00095 if nt == ct:
00096 break
00097
00098 ct = nt
00099 return
00100
00101 def bagplay( fname ):
00102
00103
00104
00105
00106
00107
00108 cmd = 'rosbag play --clock ' + fname + ' -r 2.0 -q'
00109 rospy.logout( 'Launching bag file: %s' % fname )
00110 return CmdProcess( cmd.split() )
00111
00112
00113 def order_by_rostime( dat ):
00114
00115
00116
00117
00118 def build_fname( t,o ):
00119
00120 fname = 'search_aware_home/woot_150_'+str(t)+'_tag_'+ahe.tdb[o][0].replace( ' ', '' )+'_headpost.bag'
00121 return fname
00122
00123
00124 dat = [ [t,o] + [ build_fname(t,o) ] for t,o in dat ]
00125 dat = [ d for d in dat if glob.glob( d[-1] ) != [] ]
00126
00127 rospy.logout( 'Ordering the bagfiles in increasing order of start time.' )
00128 def gettime( fname ):
00129 print fname
00130
00131 b = rosbag.Bag( fname )
00132 msg = b.read_messages().next()
00133 tt = msg[-1].to_sec()
00134 b.close()
00135 return tt
00136
00137 start_times = [ gettime( d[-1] ) for d in dat ]
00138 rospy.logout( 'Done ordering.' )
00139
00140 return [ [dat[ind][0],dat[ind][1]] for ind in np.argsort( start_times ) ]
00141
00142
00143 if __name__ == '__main__':
00144 import optparse
00145 p = optparse.OptionParser()
00146
00147
00148 p.add_option('--trial', action='store', type='int', dest='trial',
00149 help='trial number (0-8)')
00150 p.add_option('--obj', action='store', type='int', dest='obj',
00151 help='object number (0-8)')
00152 p.add_option('--sc', action='store_true', dest='sc',
00153 help='Take screenshot', default=False)
00154
00155
00156
00157 opt, args = p.parse_args()
00158
00159 if opt.trial < 9:
00160 publish_robotpose( opt.trial, opt.obj, screen_cap = opt.sc )
00161 else:
00162 print 'Click on RVIZ!'
00163 time.sleep( 3 )
00164
00165
00166 X,Y = np.meshgrid( range(0,9), range(0,9) )
00167 trial_obj = zip( Y.flatten(), X.flatten() )
00168 [ publish_robotpose( trial, obj, 15, screen_cap = True )
00169 for trial, obj in order_by_rostime(trial_obj) ]
00170
00171