00001
00002
00003 import numpy as np, math
00004 import sys
00005 from openravepy import *
00006
00007 import roslib; roslib.load_manifest('hrl_motion_planners_darpa_m3')
00008 import rospy
00009 import hrl_lib.util as ut
00010 import hrl_motion_planners_darpa_m3.write_xml_openrave as w_xml
00011
00012
00013 def move_bodies_to_position(bodies, positions):
00014 for i in range(len(positions)):
00015 pos = positions[i]
00016 bod = bodies[i]
00017 t = bod.GetTransform()
00018 if np.max(np.abs(pos)) > 1.5:
00019 continue
00020 t[0,3] = pos[0]
00021 t[1,3] = pos[1]
00022 t[2,3] = pos[2]
00023 bod.SetTransform(t)
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 def write_openrave_xml_files(reach_problem_dict, joints, links):
00039 w_xml.write_linkage_xml_file(joints, links, 'sim.robot.xml')
00040 w_xml.write_environment_xml_file(reach_problem_dict, 'planar.env.xml', 'sim.robot.xml')
00041
00042
00043 def position_obstacles(env, movable_positions, fixed_positions, ignore_movable):
00044
00045 bodies_list = env.GetBodies()
00046
00047 if not ignore_movable:
00048 movable_bodies = filter(lambda x: 'movable' in x.GetName(), bodies_list)
00049 move_bodies_to_position(movable_bodies, movable_positions)
00050
00051 fixed_bodies = filter(lambda x: 'fixed' in x.GetName(), bodies_list)
00052 move_bodies_to_position(fixed_bodies, fixed_positions)
00053
00054
00055
00056
00057
00058 def plan_path_to_goal(goal, env, init_arm_config, quiet,
00059 planner_name):
00060 robot = env.GetRobots()[0]
00061 ikmodel=databases.inversekinematics.InverseKinematicsModel(robot,iktype=IkParameterization.Type.TranslationXY2D)
00062 if not ikmodel.load():
00063 ikmodel.autogenerate()
00064
00065 robot.SetDOFValues(init_arm_config, range(len(init_arm_config)))
00066 manipprob = interfaces.BaseManipulation(robot, plannername=planner_name)
00067
00068 if not quiet:
00069 bodies_list = env.GetBodies()
00070 goal[2] = 0.1
00071 t_body = filter(lambda x: 'target' in x.GetName(), bodies_list)[0]
00072 move_bodies_to_position([t_body], [goal])
00073 raw_input('Hit ENTER to continue.')
00074
00075 solutions = ikmodel.manip.FindIKSolutions(IkParameterization(goal[0:2], IkParameterization.Type.TranslationXY2D), IkFilterOptions.CheckEnvCollisions)
00076
00077 if solutions is None:
00078 solutions = []
00079
00080 print 'Number of Solutions:', len(solutions)
00081 reached = False
00082
00083 for s in solutions:
00084 try:
00085 res = manipprob.MoveManipulator(goal = s)
00086 robot.WaitForController(0)
00087 reached = True
00088 break
00089 except planning_error, e:
00090 print e
00091
00092 if not quiet:
00093 raw_input('Hit ENTER to continue.')
00094
00095 q = robot.GetDOFValues().tolist()
00096 result_dict = {}
00097 result_dict['final_q'] = q
00098 if reached == True:
00099 result_dict['result'] = "Reached"
00100 else:
00101 result_dict['result'] = "Failed"
00102
00103 return result_dict
00104
00105
00106 def setup_openrave(reach_problem_dict, robot_defn, ignore_movable, quiet):
00107 write_openrave_xml_files(reach_problem_dict, robot_defn.b_jts, robot_defn.bodies)
00108
00109 env = Environment()
00110 env.Load('planar.env.xml')
00111 if not quiet:
00112 env.SetViewer('qtcoin')
00113
00114 d = reach_problem_dict
00115 movable_positions = d['moveable_position'][0:d['num_move_used']]
00116 fixed_positions = d['fixed_position'][0:d['num_fixed_used']]
00117 position_obstacles(env, movable_positions, fixed_positions, ignore_movable)
00118 return env
00119
00120
00121 def plan_with_stopping_dist_from_goal(dist, goal, env,
00122 init_arm_config, quiet,
00123 planner_name, n_rad, n_theta):
00124
00125 d_dist = dist / n_rad
00126
00127 d_theta = np.radians(360./n_theta)
00128
00129 for r in range(n_rad):
00130 for i in range(n_theta):
00131 dx = (dist - d_dist * r) * math.cos(i * d_theta)
00132 dy = dist * math.sin(i * d_theta)
00133 g = [goal[0]+dx, goal[1]+dy, goal[2]]
00134
00135 res_dict = plan_path_to_goal(g, env, init_arm_config, quiet,
00136 planner_name)
00137
00138 if res_dict['result'] == 'Reached':
00139 break
00140
00141 return res_dict
00142
00143
00144
00145 if __name__ == '__main__':
00146 import optparse
00147 p = optparse.OptionParser()
00148
00149 p.add_option('--reach_problem_dict', '--rpd', action='store',
00150 dest='rpd', type='string', default=None,
00151 help='reach problem dict pkl')
00152
00153 p.add_option('--linkage', action='store', dest='linkage',
00154 default='', help='string that specifies the linkage to use')
00155
00156 p.add_option('--quiet', '-q', action='store_true', dest='quiet',
00157 help='do not bring up the gui.')
00158
00159 p.add_option('--ignore_movable', '--im', action='store_true', dest='im',
00160 help='do not use movable obstacles.')
00161
00162 p.add_option('--planner', action='store', dest='planner',
00163 help='BasicRRT, BiRRT etc.')
00164
00165 p.add_option('--safety_margin', action='store',
00166 dest='safety_margin', type='float', default=0.,
00167 help='Safety margin added to radius of all obstacles')
00168
00169 opt, args = p.parse_args()
00170
00171
00172 if opt.linkage == '':
00173 raise RuntimeError('Please specify a --linkage')
00174
00175 elif opt.linkage == 'three_link_planar':
00176 import hrl_common_code_darpa_m3.robot_config.three_link_planar_capsule as robot_defn
00177
00178 elif opt.linkage == 'six_link_planar':
00179 import hrl_common_code_darpa_m3.robot_config.six_link_planar as robot_defn
00180
00181 else:
00182 print 'Unrecognized linkage: %s'%opt.linkage
00183 print 'Options are: three_link_planar, six_link_planar'
00184 print "Exiting ..."
00185 sys.exit()
00186
00187 rpd = ut.load_pickle(opt.rpd)
00188
00189 for dim in rpd['moveable_dimen']:
00190 dim[0] += opt.safety_margin
00191 dim[1] += opt.safety_margin
00192
00193 for dim in rpd['fixed_dimen']:
00194 dim[0] += opt.safety_margin
00195 dim[1] += opt.safety_margin
00196
00197 env = setup_openrave(rpd, robot_defn, opt.im, opt.quiet)
00198 res_dict = plan_path_to_goal(rpd['goal'], env, robot_defn.b_jt_start,
00199 opt.quiet, opt.planner)
00200
00201
00202
00203
00204
00205 env.Destroy()
00206
00207 print 'Result:', res_dict['result']
00208
00209
00210
00211