epc.py
Go to the documentation of this file.
00001 
00002 #
00003 # Temoprarily in this package. Advait needs to move it to a better
00004 # location.
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 ## Class defining the core EPC function and a few simple examples.
00016 # More complex behaviors that use EPC should have their own ROS
00017 # packages.
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     # @param equi_pt_generator: function that returns stop, ea  where ea: equilibrium angles and  stop: string which is '' for epc motion to continue
00027     # @param rapid_call_func: called in the time between calls to the equi_pt_generator can be used for logging, safety etc.  returns string which is '' for epc motion to continue
00028     # @param time_step: time between successive calls to equi_pt_generator
00029     # @param arg_list - list of arguments to be passed to the equi_pt_generator
00030     # @return stop (the string which has the reason why the epc
00031     # motion stopped.), ea (last commanded equilibrium angles)
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             #self.robot.set_jointangles(arm, ea)
00043             #import pdb; pdb.set_trace()
00044             control_function(arm, *ea)
00045 
00046             # self.robot.step() this should be within the rapid_call_func for the meka arms.
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                 #self.robot.step() this should be within the rapid_call_func for the meka arms
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     ## Pull back along a straight line (-ve x direction)
00067     # @param arm - 'right_arm' or 'left_arm'
00068     # @param distance - how far back to pull.
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 # projection in direction opposite to motion.
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 #    #----- testing move_till_hit ------
00161 #    p1 = np.matrix([0.6, -0.22, -0.05]).T
00162 #    epc.robot.go_cep_jtt(arm, p1)
00163 #    epc.move_till_hit(arm)
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 #    if False:
00182 #        ea = [0, 0, 0, 0, 0, 0, 0]
00183 #        ea = epc.robot.get_joint_angles(arm)
00184 #        rospy.logout('Going to starting position')
00185 #        epc.robot.set_jointangles(arm, ea, duration=4.0)
00186 #        raw_input('Hit ENTER to pull')
00187 #        epc.pull_back(arm, ea, tr.Rx(0), 0.2)
00188 #
00189 #    if False:
00190 #        p = np.matrix([0.9, -0.3, -0.15]).T
00191 #        rot = tr.Rx(0.)
00192 #        rot = tr.Rx(math.radians(90.))
00193 #
00194 #        rospy.logout('Going to starting position')
00195 #    #    epc.robot.open_gripper(arm)
00196 #        epc.robot.set_cartesian(arm, p, rot)
00197 #    #    raw_input('Hit ENTER to close the gripper')
00198 #    #    epc.robot.close_gripper(arm)
00199 #        raw_input('Hit ENTER to pull')
00200 #        epc.pull_back_cartesian_control(arm, p, rot, 0.4)
00201 
00202 
00203 
00204 


hrl_pr2_lib
Author(s): haidai
autogenerated on Wed Nov 27 2013 11:40:34