00001
00002
00003
00004
00005
00006
00007 import numpy as np, math
00008 import copy
00009
00010 import roslib; roslib.load_manifest('hrl_pr2_lib')
00011 import rospy
00012
00013 import hrl_lib.util as ut
00014
00015
00016
00017
00018 class EPC():
00019 def __init__(self, robot):
00020 self.robot = robot
00021 self.f_list = []
00022 self.ee_list = []
00023 self.cep_list = []
00024
00025
00026
00027
00028
00029
00030
00031
00032 def epc_motion(self, equi_pt_generator, time_step, arm, arg_list,
00033 rapid_call_func=None, control_function=None):
00034
00035 stop, ea = equi_pt_generator(*arg_list)
00036 t_end = rospy.get_time()
00037 while stop == '':
00038 if rospy.is_shutdown():
00039 stop = 'rospy shutdown'
00040 continue
00041 t_end += time_step
00042
00043
00044 control_function(arm, *ea)
00045
00046
00047 t1 = rospy.get_time()
00048 while t1<t_end:
00049 if rapid_call_func != None:
00050 stop = rapid_call_func(arm)
00051 if stop != '':
00052 break
00053
00054 rospy.sleep(0.01)
00055 t1 = rospy.get_time()
00056
00057 if stop == '':
00058 stop, ea = equi_pt_generator(*arg_list)
00059 if stop == 'reset timing':
00060 stop = ''
00061 t_end = rospy.get_time()
00062
00063 return stop, ea
00064
00065
00066
00067
00068
00069 def pull_back_cartesian_control(self, arm, distance, logging_fn):
00070 cep, _ = self.robot.get_cep_jtt(arm)
00071 cep_end = cep + distance * np.matrix([-1., 0., 0.]).T
00072 self.dist_left = distance
00073
00074 def eq_gen_pull_back(cep):
00075 logging_fn(arm)
00076 if self.dist_left <= 0.:
00077 return 'done', None
00078 step_size = 0.01
00079 cep[0,0] -= step_size
00080 self.dist_left -= step_size
00081 if cep[0,0] < 0.4:
00082 return 'very close to the body: %.3f'%cep[0,0], None
00083 return '', (cep, None)
00084
00085 arg_list = [cep]
00086 s = self.epc_motion(eq_gen_pull_back, 0.1, arm, arg_list,
00087 control_function = self.robot.set_cep_jtt)
00088 print s
00089
00090 def move_till_hit(self, arm, vec=np.matrix([0.3,0.,0.]).T, force_threshold=3.0,
00091 speed=0.1, bias_FT=True):
00092 unit_vec = vec/np.linalg.norm(vec)
00093 time_step = 0.1
00094 dist = np.linalg.norm(vec)
00095 step_size = speed * time_step
00096 cep_start, _ = self.robot.get_cep_jtt(arm)
00097 cep = copy.copy(cep_start)
00098 def eq_gen(cep):
00099 force = self.robot.get_wrist_force(arm, base_frame = True)
00100 force_projection = force.T*unit_vec *-1
00101 print 'force_projection:', force_projection
00102 if force_projection>force_threshold:
00103 return 'done', None
00104 elif np.linalg.norm(force)>45.:
00105 return 'large force', None
00106 elif np.linalg.norm(cep_start-cep) >= dist:
00107 return 'reached without contact', None
00108 else:
00109 cep_t = cep + unit_vec * step_size
00110 cep[0,0] = cep_t[0,0]
00111 cep[1,0] = cep_t[1,0]
00112 cep[2,0] = cep_t[2,0]
00113 return '', (cep, None)
00114
00115 if bias_FT:
00116 self.robot.bias_wrist_ft(arm)
00117 rospy.sleep(0.5)
00118 return self.epc_motion(eq_gen, time_step, arm, [cep],
00119 control_function = self.robot.set_cep_jtt)
00120
00121 def cep_gen_surface_follow(self, arm, move_dir, force_threshold,
00122 cep, cep_start):
00123 wrist_force = self.robot.get_wrist_force(arm, base_frame=True)
00124 if wrist_force[0,0] < -3.:
00125 cep[0,0] -= 0.002
00126 if wrist_force[0,0] > -1.:
00127 cep[0,0] += 0.003
00128
00129 if cep[0,0] > (cep_start[0,0]+0.05):
00130 cep[0,0] = cep_start[0,0]+0.05
00131
00132 step_size = 0.002
00133 cep_t = cep + move_dir * step_size
00134 cep[0,0] = cep_t[0,0]
00135 cep[1,0] = cep_t[1,0]
00136 cep[2,0] = cep_t[2,0]
00137
00138 v = cep - cep_start
00139 if (wrist_force.T * move_dir)[0,0] < -force_threshold:
00140 stop = 'got a hook'
00141 elif np.linalg.norm(wrist_force) > 50.:
00142 stop = 'force is large %f'%(np.linalg.norm(wrist_force))
00143 elif (v.T * move_dir)[0,0] > 0.20:
00144 stop = 'moved a lot without a hook'
00145 else:
00146 stop = ''
00147 return stop, (cep, None)
00148
00149 if __name__ == '__main__':
00150 import pr2_arms as pa
00151 rospy.init_node('epc_pr2', anonymous = True)
00152 rospy.logout('epc_pr2: ready')
00153
00154 pr2_arms = pa.PR2Arms()
00155 epc = EPC(pr2_arms)
00156
00157 r_arm, l_arm = 0, 1
00158 arm = r_arm
00159
00160
00161
00162
00163
00164
00165 raw_input('Hit ENTER to close')
00166 pr2_arms.close_gripper(arm)
00167 raw_input('Hit ENTER to search_and_hook')
00168 p1 = np.matrix([0.8, -0.22, -0.05]).T
00169 epc.search_and_hook(arm, p1)
00170 epc.pull_back_cartesian_control(arm, 0.3)
00171
00172 d = {
00173 'f_list': epc.f_list, 'ee_list': epc.ee_list,
00174 'cep_list': epc.cep_list
00175 }
00176
00177 ut.save_pickle(d, 'pr2_pull_'+ut.formatted_time()+'.pkl')
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204