record.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('hrl_trajectory_playback')
00004 import rospy
00005 
00006 import hrl_lib.util as hrl_util
00007 from sensor_msgs.msg import JointState
00008 
00009 from collections import deque # fast appends
00010 import numpy as np, math
00011 
00012 # Hack (this code currently only supports the right arm!)
00013 JOINT_NAMES = ['%s_shoulder_pan_joint', '%s_shoulder_lift_joint', '%s_upper_arm_roll_joint',
00014                '%s_elbow_flex_joint', '%s_forearm_roll_joint', '%s_wrist_flex_joint',
00015                '%s_wrist_roll_joint']
00016 
00017 class Listener:
00018     def __init__(self, arm = 'right'):
00019         self.q = deque() # np.zeros(( n_samples, 7))
00020         self.t = deque() # np.zeros((n_samples, 1))
00021         self.arm = arm
00022         # self.tind = 0
00023         # self.ind = 0
00024         self.initialized = False
00025         self.starttime = rospy.Time(0)
00026         self.r_jt_idx_lis = None
00027         self.l_jt_idx_lis = None
00028 
00029     def callback( self, msg ):
00030         currtime = msg.header.stamp.to_sec()
00031         
00032         if not self.initialized:
00033             self.initialized = True
00034             self.starttime = currtime
00035             self.lasttime = currtime
00036             self.r_jt_idx_lis = [msg.name.index(joint_name % 'r') for joint_name in JOINT_NAMES]
00037             self.l_jt_idx_lis = [msg.name.index(joint_name % 'l') for joint_name in JOINT_NAMES]
00038             
00039         if currtime - self.lasttime > 1.0:
00040             self.lasttime = currtime
00041             print 'Still Capturing (%d sec)' % (currtime - self.starttime)
00042 
00043 
00044         if self.arm == 'right':
00045             q_ja = [ msg.position[idx] for idx in self.r_jt_idx_lis ]
00046         else:
00047             q_ja = [ msg.position[idx] for idx in self.l_jt_idx_lis ]
00048         self.q.append( q_ja )
00049         
00050         # for i,idx in enumerate( r_jt_idx_lis ):  # only dealing with right arm!
00051         #     self.q[self.ind, i] = msg.position[idx]
00052         #     print self.q[self.ind, i],
00053         # print ""
00054         # self.t[self.ind] = msg.header.stamp.to_sec()
00055         # self.ind += 1
00056         self.t.append([ currtime ])
00057         
00058 
00059 if __name__ == '__main__':
00060     import optparse
00061     p = optparse.OptionParser()
00062     p.add_option('--pkl', action='store', type='string', dest='pkl',
00063                  help='Output file [default=\'out.pkl\']', default='out.pkl')
00064     p.add_option('--left', action='store_true', dest='left_arm',
00065                  help='Use the left arm? [Right is default]')
00066     opt, args = p.parse_args()
00067     
00068     rospy.init_node('quick_data')
00069 
00070     if opt.left_arm:
00071         lis = Listener( arm = 'left' )
00072     else:
00073         lis = Listener( arm = 'right' )
00074     sub = rospy.Subscriber( '/joint_states', JointState, lis.callback )
00075 
00076     print 'Recording...  <Ctrl-C> to stop.'
00077     rospy.spin()
00078     sub.unregister()
00079 
00080     
00081     #hrl_util.save_pickle([lis.q[0:lis.ind,:], lis.t[0:lis.ind,:]], 'untuck_traj_6.pickle')
00082     print 'Outputting to: %s' % opt.pkl
00083     hrl_util.save_pickle([ np.array(lis.q),   # Nx7 numpy array
00084                            np.array(lis.t) ], # Nx1 numpy array
00085                          opt.pkl )


hrl_trajectory_playback
Author(s): Travis Deyle and Kelsey Hawkins
autogenerated on Wed Nov 27 2013 11:44:23