Go to the documentation of this file.00001
00002 import roslib
00003 roslib.load_manifest('pr2_playpen')
00004 import rospy
00005 import math
00006 import tf
00007 import numpy as np
00008 import os
00009 import sys
00010 import cPickle as pkl
00011
00012 def is_topic_pub(topic):
00013 flag = False
00014 for item in rospy.get_published_topics():
00015 for thing in item:
00016 if topic in thing:
00017 flag = True
00018 else:
00019 pass
00020 return flag
00021
00022 def get_data(listener, rate):
00023 right = False
00024 left = False
00025
00026 dist_ls = []
00027 time_ls = []
00028
00029 while is_topic_pub('/r_overhead_grasp/feedback') == False and is_topic_pub('/l_overhead_grasp/feedback') == False:
00030 print "waiting for bag file"
00031 rate.sleep()
00032
00033 if is_topic_pub('/r_overhead_grasp/feedback'):
00034 right = True
00035 elif is_topic_pub('/l_overhead_grasp/feedback'):
00036 left = True
00037
00038 if left == True:
00039 prefix = 'l_'
00040 elif right == True:
00041 prefix = 'r_'
00042
00043 frame1 = prefix+'gripper_l_finger_tip_link'
00044 frame2 = prefix+'gripper_r_finger_tip_link'
00045
00046
00047 while is_topic_pub('/clock') == True:
00048 try:
00049 (trans,rot) = listener.lookupTransform(frame1, frame2, rospy.Time(0))
00050 dist = math.sqrt((np.matrix(trans)*np.matrix(trans).T)[0,0])
00051 time = rospy.get_time()
00052 dist_ls.append(dist)
00053 time_ls.append(time)
00054
00055 except (tf.LookupException, tf.ConnectivityException):
00056
00057 continue
00058
00059
00060 rate.sleep()
00061
00062 return dist_ls, time_ls
00063
00064 if __name__ == '__main__':
00065 rospy.init_node('get_gripper_position')
00066 listener = tf.TransformListener()
00067 rate = rospy.Rate(100.0)
00068
00069 path = sys.argv[1]
00070 print "path is :", path
00071
00072 for i in xrange(9):
00073 j = 0
00074 dist_dict = {}
00075 f_hand = open(path+'/object'+str(i).zfill(3)+'_gripper_dist.pkl', 'w')
00076 while os.path.isfile(path + '/object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'.bag') == True:
00077 f_path = path + '/object'+str(i).zfill(3)+'_try'+str(j).zfill(3)+'.bag'
00078 os.system('rosbag play -r 2 '+ f_path + ' &')
00079 dist_ls, time_ls = get_data(listener, rate)
00080 dist_dict['try'+str(j).zfill(3)] = {'dist':dist_ls, 'time':time_ls}
00081 j = j+1
00082 pkl.dump(dist_dict, f_hand)
00083 f_hand.close()