Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 import cody_arm_kinematics as cak
00031 import math, numpy as np
00032 import sys, optparse
00033 
00034 import roslib; roslib.load_manifest('hrl_cody_arms')
00035 import hrl_lib.transforms as tr
00036 import hrl_lib.util as ut
00037 
00038 def find_good_config(pt,dic):
00039     ''' finds a good configuration for the 3x1 pt.
00040     '''
00041     m = dic['cart_pts_mat']
00042     min_idx = np.argmin(ut.norm(m-pt))
00043     c = dic['good_configs_list'][min_idx]
00044 
00045     return c
00046 
00047 def test_dict(fname):
00048     dic = ut.load_pickle(fname)
00049     firenze = cak.CodyArmKinematics('r')
00050 
00051     rot = tr.rotY(math.radians(-90))
00052     p = np.matrix([0.4,-0.42,-0.2]).T
00053 
00054     c = find_good_config(p, dic)
00055     res = firenze.IK(p, rot, q_guess=c)
00056     print 'IK soln: ', [math.degrees(qi) for qi in res]
00057 
00058 def create_dict(fname):
00059     firenze = cak.CodyArmKinematics('r')
00060     good_configs_list = ut.load_pickle(fname)
00061     cartesian_points_list = []
00062     for gc in good_configs_list:
00063         cartesian_points_list.append(firenze.FK(gc).A1.tolist())
00064 
00065     m = np.matrix(cartesian_points_list).T
00066     print 'm.shape:', m.shape
00067     dic = {'cart_pts_mat':m, 'good_configs_list':good_configs_list}
00068     ut.save_pickle(dic,ut.formatted_time()+'_goodconf_dict.pkl')
00069 
00070 def record_good_configs(use_left_arm):
00071     import m3.toolbox as m3t
00072     import cody_arm_client as cac
00073     if use_left_arm:
00074         arm = 'l'
00075     else:
00076         arm = 'r'
00077 
00078     firenze = cac.CodyArmClient(arm)
00079     print 'hit ENTER to start the recording process'
00080     k=m3t.get_keystroke()
00081 
00082     good_configs_list = []
00083 
00084     while k == '\r':
00085         print 'hit ENTER to record configuration, something else to exit'
00086         k=m3t.get_keystroke()
00087         q = firenze.get_joint_angles(arm)
00088         good_configs_list.append(np.matrix(q).A1.tolist())
00089 
00090     ut.save_pickle(good_configs_list,ut.formatted_time()+'_good_configs_list.pkl')
00091 
00092 if __name__=='__main__':
00093     p = optparse.OptionParser()
00094     p.add_option('-r', action='store_true', dest='record',
00095                  help='put robot in GC and record good configurations.')
00096     p.add_option('-c', action='store_true', dest='create',
00097                  help='create table to map points to good configs. (needs a good_configs pkl)')
00098     p.add_option('-t', action='store_true', dest='test',
00099                  help='find a good config for a cartesian point. (needs a dict pkl)')
00100     p.add_option('-f', action='store', type='string', dest='fname',
00101                  help='pkl file to use.', default='')
00102     p.add_option('--ra', action='store_true', dest='right',
00103                  help='choose the right arm')
00104     p.add_option('--la', action='store_true', dest='left',
00105                  help='choose the left arm')
00106 
00107     opt, args = p.parse_args()
00108     record = opt.record
00109     create = opt.create
00110     test = opt.test
00111     fname = opt.fname
00112     use_left_arm = opt.left
00113     use_right_arm = opt.right
00114     
00115     if test:
00116         if fname == '':
00117             print 'Specify a file name.'
00118             sys.exit()
00119         test_dict(fname)
00120     elif create:
00121         if fname == '':
00122             print 'Specify a file name.'
00123             sys.exit()
00124         create_dict(fname)
00125     elif record:
00126         if use_right_arm == None and use_left_arm == None:
00127             print 'Please specify either left or right arm. (--ra or --la)'
00128             print 'Exiting...'
00129             sys.exit()
00130 
00131         record_good_configs(use_left_arm)
00132     else:
00133         print 'Please specify either record or create.'
00134 
00135