00001
00002 import roslib; roslib.load_manifest('hrl_pr2_kinematics_tutorials')
00003 import rospy
00004
00005 import numpy as np, math
00006
00007
00008
00009
00010 class EPC():
00011 def __init__(self, robot):
00012 self.robot = robot
00013
00014
00015
00016
00017
00018
00019
00020
00021 def epc_motion(self, equi_pt_generator, time_step, arm, arg_list,
00022 rapid_call_func=None, control_function=None):
00023
00024 stop, ea = equi_pt_generator(*arg_list)
00025 t_end = rospy.get_time()
00026 while stop == '':
00027 t_end += time_step
00028
00029
00030 control_function(arm, *ea)
00031
00032
00033 t1 = rospy.get_time()
00034 while t1<t_end:
00035 if rapid_call_func != None:
00036 stop = rapid_call_func(arm)
00037 if stop != '':
00038 break
00039
00040 t1 = rospy.get_time()
00041
00042 stop, ea = equi_pt_generator(*arg_list)
00043
00044 if stop == 'reset timing':
00045 stop = ''
00046 t_end = rospy.get_time()
00047
00048 return stop, ea
00049
00050
00051
00052
00053
00054
00055 def pull_back(self, arm, ea, rot_mat, distance):
00056 self.cep = self.robot.FK(arm, ea)
00057 self.dist_left = distance
00058 self.ea = ea
00059
00060 def eq_gen_pull_back(robot, arm, rot_mat):
00061 if self.dist_left <= 0.:
00062 return 'done', None
00063 step_size = 0.01
00064 self.cep[0,0] -= step_size
00065 self.dist_left -= step_size
00066 ea = robot.IK(arm, self.cep, rot_mat, self.ea)
00067 self.ea = ea
00068 if ea == None:
00069 return 'IK fail', ea
00070 return '', [ea,]
00071
00072 arg_list = [self.robot, arm, rot_mat]
00073 stop, ea = self.epc_motion(eq_gen_pull_back, 0.1, arm, arg_list,
00074 control_function = self.robot.set_jointangles)
00075 print stop, ea
00076
00077
00078
00079
00080
00081
00082 def pull_back_cartesian_control(self, arm, cep, rot_mat, distance):
00083 self.cep = cep
00084 self.dist_left = distance
00085
00086 def eq_gen_pull_back(robot, arm, rot_mat):
00087 if self.dist_left <= 0.:
00088 return 'done', None
00089 step_size = 0.01
00090 self.cep[0,0] -= step_size
00091 self.dist_left -= step_size
00092 if self.cep[0,0] < 0.4:
00093 return 'very close to the body: %.3f'%self.cep[0,0], None
00094
00095 return '', (self.cep, rot_mat)
00096
00097 arg_list = [self.robot, arm, rot_mat]
00098 stop, ea = self.epc_motion(eq_gen_pull_back, 0.1, arm, arg_list,
00099 control_function = self.robot.set_cartesian)
00100 print stop, ea
00101
00102
00103 if __name__ == '__main__':
00104 import hrl_pr2
00105 import hrl_lib.transforms as tr
00106
00107 rospy.init_node('epc_pr2', anonymous = True)
00108 rospy.logout('epc_pr2: ready')
00109
00110 pr2 = hrl_pr2.HRL_PR2()
00111 epc = EPC(pr2)
00112
00113 arm = 'right_arm'
00114
00115 if False:
00116 ea = [0, 0, 0, 0, 0, 0, 0]
00117 ea = epc.robot.get_joint_angles(arm)
00118 rospy.logout('Going to starting position')
00119 epc.robot.set_jointangles(arm, ea, duration=4.0)
00120 raw_input('Hit ENTER to pull')
00121 epc.pull_back(arm, ea, tr.Rx(0), 0.2)
00122
00123 if True:
00124 p = np.matrix([0.9, -0.3, -0.15]).T
00125 rot = tr.Rx(0.)
00126 rot = tr.Rx(math.radians(90.))
00127
00128 rospy.logout('Going to starting position')
00129
00130 epc.robot.set_cartesian(arm, p, rot)
00131
00132
00133 raw_input('Hit ENTER to pull')
00134 epc.pull_back_cartesian_control(arm, p, rot, 0.4)
00135
00136
00137
00138