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 
00031 
00032 import sys, time, os, optparse
00033 import math, numpy as np
00034 import copy
00035 
00036 import mekabot.coord_frames as mcf
00037 import compliant_trajectories as ct
00038 import m3.toolbox as m3t
00039 
00040 import roslib; roslib.load_manifest('2010_icra_epc_pull')
00041 import hrl_lib.util as ut
00042 import hrl_lib.transforms as tr
00043 
00044 if __name__=='__main__':
00045     p = optparse.OptionParser()
00046     p.add_option('--ha', action='store', dest='ha',type='float',
00047                  default=None,help='hook angle (degrees).')
00048     p.add_option('--ft', action='store', dest='ft',type='float',
00049                  default=80.,help='force threshold (Newtons). [default 80.]')
00050     p.add_option('--info', action='store', type='string', dest='info_string',
00051                  help='string to save in the pkl log.', default='')
00052     p.add_option('--pull_fixed', action='store_true', dest='pull_fixed',
00053                  help='pull with the segway stationary')
00054     p.add_option('--lead', action='store_true', dest='lead',
00055                  help='move the segway while pulling')
00056     p.add_option('--lpi', action='store_true', dest='lpi',
00057                  help='use the laser pointer interface to designate hooking location')
00058     p.add_option('-p', action='store', dest='p',type='int',
00059                  default=2,help='position number')
00060     p.add_option('-z', action='store', dest='z',type='float',
00061                  default=1.0,help='zenither height')
00062     p.add_option('--sa', action='store', dest='sa',type='float',
00063                  default=0.0,help='servo angle at which to take camera image (DEGREES)')
00064     p.add_option('--sliding_left', action='store_true',
00065                  dest='sliding_left',
00066                  help='defining the initial motion of the hook.')
00067 
00068     p.add_option('--use_jacobian', action='store_true',
00069                  dest='use_jacobian',
00070                  help='assume that kinematics estimation gives a jacobian')
00071 
00072     opt, args = p.parse_args()
00073     ha = opt.ha
00074     z = opt.z
00075     sa = opt.sa
00076     ft = opt.ft
00077     info_string = opt.info_string
00078     lead_flag = opt.lead
00079     lpi_flag = opt.lpi
00080     pull_fixed_flag = opt.pull_fixed
00081     move_segway_flag  = not pull_fixed_flag
00082     pnum = opt.p
00083 
00084     arm = 'right_arm'
00085 
00086     try:
00087         if ha == None:
00088             print 'please specify hook angle (--ha)'
00089             print 'Exiting...'
00090             sys.exit()
00091 
00092         cmg = ct.CompliantMotionGenerator(move_segway = move_segway_flag,
00093                                           use_right_arm = True,
00094                                           use_left_arm = False)
00095 
00096         if lead_flag:
00097             sys.path.append(os.environ['HRLBASEPATH']+'/src/projects/lead')
00098             import lead
00099 
00100             print 'hit a key to start leading.'
00101             k=m3t.get_keystroke()
00102             cmg.firenze.power_on()
00103 
00104             if move_segway_flag:
00105                 mec = cmg.segway_command_node
00106             else:
00107                 import segway_omni.segway as segway
00108                 mec = segway.Mecanum()
00109 
00110             zed = cmg.z
00111             import mekabot.hrl_robot as hr
00112             settings_lead = hr.MekaArmSettings(stiffness_list=[0.2,0.3,0.3,0.5,0.8])
00113             cmg.firenze.set_arm_settings(settings_lead,None)
00114 
00115             follower = lead.Lead(cmg.firenze,'right_arm',mec,zed,
00116                                  max_allowable_height=zed.calib['max_height'],
00117                                  init_height = zed.get_position_meters())
00118             qr = [0, 0, 0, math.pi/2, -(math.radians(ha)-ct.hook_3dprint_angle), 0, 0]
00119             follower.start_lead_thread(qr=qr)
00120 
00121             print 'hit a key to start hook and pull.'
00122             k=m3t.get_keystroke()
00123             follower.stop()
00124 
00125             cmg.firenze.set_arm_settings(cmg.settings_stiff,None)
00126             cmg.firenze.step()
00127             cmg.firenze.pose_robot('right_arm')
00128 
00129             print 'Hit ENTER to reposition the robot'
00130             k=m3t.get_keystroke()
00131             if k!='\r':
00132                 print 'You did not press ENTER.'
00133                 print 'Exiting ...'
00134                 sys.exit()
00135 
00136             hook_location = cmg.firenze.end_effector_pos('right_arm')
00137             pull_loc = cmg.reposition_robot(hook_location)
00138 
00139         elif lpi_flag:
00140             import lpi
00141 
00142             import hrl_tilting_hokuyo.processing_3d as p3d
00143 
00144 
00145             cmg.z.torque_move_position(1.0)
00146             cmg.firenze.power_on()
00147             cmg.movement_posture()
00148             if z<0.5:
00149                 print 'desired zenither height of %.2f is unsafe.'%(z)
00150                 print 'Exiting...'
00151                 sys.exit()
00152 
00153             hook_location = None
00154             
00155             z_list = [opt.z]
00156             i = 0
00157             while hook_location == None:
00158                 if i == len(z_list):
00159                     print 'Did not get a click. Exiting...'
00160                     sys.exit()
00161                 z = z_list[i]
00162                 cmg.z.torque_move_position(z)
00163                 hook_location = lpi.select_location(cmg.cam,cmg.thok,math.radians(sa))
00164                 i += 1
00165 
00166             hl_thok0 = mcf.thok0Tglobal(hook_location)
00167             hl_torso = mcf.torsoTglobal(hook_location)
00168 
00169             t_begin = time.time()
00170             angle = 0.
00171             pull_loc,residual_angle = cmg.reposition_robot(hl_torso,angle,math.radians(ha),
00172                                                            position_number=pnum)
00173             print 'pull_loc:',pull_loc.A1.tolist()
00174 
00175             starting_location = copy.copy(hl_torso)
00176             starting_angle = -angle
00177             pose_dict = {}
00178             pose_dict['loc'] = starting_location
00179             pose_dict['angle'] = angle
00180             pose_dict['residual_angle'] = residual_angle
00181             pose_dict['position_number'] = pnum
00182 
00183             if opt.sliding_left:
00184                 thresh = 2.
00185             else:
00186                 thresh = 5.
00187             res, jep = cmg.search_and_hook(arm, math.radians(ha),
00188                                     pull_loc, residual_angle, thresh)
00189             print 'result of search and hook:', res
00190             if res != 'got a hook':
00191                 print 'Did not get a hook.'
00192                 print 'Exiting...'
00193                 sys.exit()
00194 
00195         elif pull_fixed_flag:
00196             print 'hit a key to power up the arms.'
00197             k=m3t.get_keystroke()
00198             cmg.firenze.power_on()
00199 
00200             t_begin = time.time()
00201             pull_loc = np.matrix([0.55, -0.2, -.23]).T
00202             if opt.sliding_left:
00203                 thresh = 2.
00204             else:
00205                 thresh = 5.
00206             res, jep = cmg.search_and_hook(arm, math.radians(ha),
00207                                            pull_loc, 0., thresh)
00208             print 'result of search and hook:', res
00209             if res != 'got a hook':
00210                 print 'Did not get a hook.'
00211                 print 'Exiting...'
00212                 sys.exit()
00213             residual_angle = 0.
00214             pose_dict = {}
00215 
00216         else:
00217             raise RuntimeError('Unsupported. Advait Jan 02, 2010.')
00218 
00219         if opt.use_jacobian:
00220             kinematics_estimation = 'jacobian'
00221         else:
00222             kinematics_estimation = 'rotation_center'
00223 
00224         t_pull = time.time()
00225         cmg.pull(arm, math.radians(ha), residual_angle, ft, jep,
00226                  strategy = 'control_radial_force',
00227                  info_string=info_string, cep_vel = 0.10,
00228                  kinematics_estimation=kinematics_estimation,
00229                  pull_left = opt.sliding_left)
00230         t_end = time.time()
00231 
00232         pose_dict['t0'] = t_begin
00233         pose_dict['t1'] = t_pull
00234         pose_dict['t2'] = t_end
00235         ut.save_pickle(pose_dict,'pose_dict_'+ut.formatted_time()+'.pkl')
00236 
00237         print 'hit  a key to end everything'
00238         k=m3t.get_keystroke()
00239         cmg.stop()
00240 
00241     except: 
00242         print 'Hello, Mr. Exception'
00243         cmg.stop()
00244         raise
00245 
00246