00001
00002 import roslib
00003 roslib.load_manifest('hrl_trajectory_playback')
00004 import rospy
00005 import actionlib
00006
00007 import trajectory_msgs.msg as tm
00008 import pr2_controllers_msgs.msg as pm
00009 import hrl_lib.util as hrl_util
00010
00011 from hrl_trajectory_playback.srv import TrajPlaybackSrv, TrajPlaybackSrvRequest
00012
00013 import numpy as np, math
00014
00015
00016
00017 class PR2(object):
00018 def __init__(self):
00019 self.right = PR2Arm('r')
00020 self.left = PR2Arm('l')
00021
00022 class PR2Arm(object):
00023 def __init__(self, arm):
00024 joint_controller_name = arm + '_arm_controller'
00025 self.client = actionlib.SimpleActionClient('/%s/joint_trajectory_action' % joint_controller_name, pm.JointTrajectoryAction)
00026 rospy.loginfo('pr2arm: waiting for server %s' % joint_controller_name)
00027 self.client.wait_for_server()
00028 self.joint_names = rospy.get_param('/%s/joints' % joint_controller_name)
00029 self.zeros = [0 for j in range(len(self.joint_names))]
00030
00031 def create_trajectory(self, pos_mat, times, vel_mat=None):
00032
00033 points = [tm.JointTrajectoryPoint() for i in range(pos_mat.shape[1])]
00034 for i in range(pos_mat.shape[1]):
00035 points[i].positions = pos_mat[:,i].A1.tolist()
00036 points[i].accelerations = self.zeros
00037 if vel_mat == None:
00038 points[i].velocities = self.zeros
00039 else:
00040 points[i].velocities = vel_mat[:,i].A1.tolist()
00041
00042 for i in range(pos_mat.shape[1]):
00043 points[i].time_from_start = rospy.Duration(times[i])
00044
00045
00046 jt = tm.JointTrajectory()
00047 jt.joint_names = self.joint_names
00048 jt.points = points
00049 jt.header.stamp = rospy.get_rostime()
00050 return jt
00051
00052
00053
00054 class TrajPlayback():
00055 def __init__( self, name, fname, arm = 'right' ):
00056 self.arm = arm
00057 self.name = name
00058
00059 rospy.logout( 'TrajPlayback: Initializing (%s)' % self.name )
00060 try:
00061 rospy.init_node('traj_playback_'+self.name)
00062 except:
00063 pass
00064
00065 dat = hrl_util.load_pickle( fname )
00066 self.q = np.mat( dat[0].T )
00067
00068
00069 self.q = self.q[:,0::30]
00070
00071 self.t = np.linspace( 1.3, 1.3 + (self.q.shape[1]-1)*0.3, self.q.shape[1] )
00072
00073 self.vel = self.q.copy()
00074 self.vel[:,1:] -= self.q[:,0:-1]
00075 self.vel[:,0] = 0
00076 self.vel /= 0.3
00077
00078 self.pr2 = PR2()
00079
00080 self.__service = rospy.Service( 'traj_playback/' + name,
00081 TrajPlaybackSrv,
00082 self.process_service )
00083
00084 rospy.logout( 'TrajPlayback: Ready for service requests (%s)' % self.name )
00085
00086
00087 def process_service( self, req ):
00088 if req.play_backward:
00089 rospy.loginfo( 'TrajPlayback: Playing Reverse (%s)' % self.name )
00090 else:
00091 rospy.loginfo( 'TrajPlayback: Playing Forward (%s)' % self.name )
00092
00093 if req.play_backward:
00094 tq = self.q[:,::-1].copy()
00095 tvel = -1 * self.vel[:,::-1].copy()
00096 tvel[:,-1] = 0
00097 tt = self.t.copy()
00098 tt[0] = 0
00099 tt[1:] += 0.1
00100 else:
00101 tq = self.q.copy()
00102 tvel = self.vel.copy()
00103 tt = self.t.copy()
00104
00105 if self.arm == 'right':
00106 joint_traj = self.pr2.right.create_trajectory( tq, tt, tvel )
00107 else:
00108 joint_traj = self.pr2.left.create_trajectory( tq, tt, tvel )
00109
00110 dur = rospy.Duration.from_sec( tt[-1] + 5 )
00111
00112
00113
00114
00115
00116
00117 g = pm.JointTrajectoryGoal()
00118 g.trajectory = joint_traj
00119
00120 if self.arm == 'right':
00121 self.pr2.right.client.send_goal( g )
00122 self.pr2.right.client.wait_for_result()
00123 else:
00124 self.pr2.left.client.send_goal( g )
00125 self.pr2.left.client.wait_for_result()
00126
00127 return True
00128
00129 if __name__ == '__main__':
00130 import optparse
00131 p = optparse.OptionParser()
00132 p.add_option('--pkl', action='store', type='string', dest='pkl',
00133 help='Output file [default=\'out.pkl\']', default='out.pkl')
00134 p.add_option('--name', action='store', type='string', dest='name',
00135 help='Service "name": /traj_playback/name [default=\'test\']',
00136 default='test')
00137 p.add_option('--play', action='store_true', dest='play',
00138 help='Just play it once instead of building service [default=False]',
00139 default=False)
00140 p.add_option('--reverse', action='store_true', dest='rev',
00141 help='Just play it once in reverse [default=False]',
00142 default=False)
00143 p.add_option('--left', action='store_true', dest='left_arm',
00144 help='Use the left arm? [Right is default]')
00145 opt, args = p.parse_args()
00146
00147 if opt.left_arm:
00148 tp = TrajPlayback( opt.name, opt.pkl, arm = 'left' )
00149 else:
00150 tp = TrajPlayback( opt.name, opt.pkl, arm = 'right' )
00151
00152 if opt.play:
00153 req = TrajPlaybackSrvRequest()
00154 req.play_backward = opt.rev
00155 tp.process_service( req )
00156 else:
00157 rospy.spin()
00158