create_IK_guess_dict.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 # 
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 # 
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 # Author: Advait Jain
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 #    print 'good configuration:', [math.degrees(qi) for qi in c]
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 


hrl_cody_arms
Author(s): Advait Jain, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab
autogenerated on Wed Nov 27 2013 12:11:49