epc.py
Go to the documentation of this file.
00001 
00002 import roslib; roslib.load_manifest('hrl_pr2_kinematics_tutorials')
00003 import rospy
00004 
00005 import numpy as np, math
00006 
00007 ## Class defining the core EPC function and a few simple examples.
00008 # More complex behaviors that use EPC should have their own ROS
00009 # packages.
00010 class EPC():
00011     def __init__(self, robot):
00012         self.robot = robot
00013 
00014     ##
00015     # @param equi_pt_generator: function that returns stop, ea  where ea: equilibrium angles and  stop: string which is '' for epc motion to continue
00016     # @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
00017     # @param time_step: time between successive calls to equi_pt_generator
00018     # @param arg_list - list of arguments to be passed to the equi_pt_generator
00019     # @return stop (the string which has the reason why the epc
00020     # motion stopped.), ea (last commanded equilibrium angles)
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             #self.robot.set_jointangles(arm, ea)
00029             #import pdb; pdb.set_trace()
00030             control_function(arm, *ea)
00031 
00032             # self.robot.step() this should be within the rapid_call_func for the meka arms.
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                 # self.robot.step() this should be within the rapid_call_func for the meka arms.
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     ## Pull back along a straight line (-ve x direction)
00051     # @param arm - 'right_arm' or 'left_arm'
00052     # @param ea - starting equilibrium angle.
00053     # @param rot_mat - rotation matrix defining end effector pose
00054     # @param distance - how far back to pull.
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     ## Pull back along a straight line (-ve x direction)
00078     # @param arm - 'right_arm' or 'left_arm'
00079     # @param ea - starting cep.
00080     # @param rot_mat - rotation matrix defining end effector pose
00081     # @param distance - how far back to pull.
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     #    epc.robot.open_gripper(arm)
00130         epc.robot.set_cartesian(arm, p, rot)
00131     #    raw_input('Hit ENTER to close the gripper')
00132     #    epc.robot.close_gripper(arm)
00133         raw_input('Hit ENTER to pull')
00134         epc.pull_back_cartesian_control(arm, p, rot, 0.4)
00135 
00136 
00137 
00138 


hrl_pr2_kinematics_tutorials
Author(s): Advait Jain (Healthcare Robotics Lab)
autogenerated on Wed Nov 27 2013 12:09:35