viz.py
Go to the documentation of this file.
00001 
00002 #
00003 # Functions to visualize a reach_problem_dict
00004 #
00005 #
00006 
00007 import sys
00008 import numpy as np, math
00009 import matplotlib.pyplot as pp
00010 
00011 import roslib; roslib.load_manifest('hrl_common_code_darpa_m3')
00012 import hrl_lib.util as ut
00013 import hrl_lib.matplotlib_util as mpu
00014 
00015 
00016 # X coordinate of obstacle is along the Y-axis of matplotlib and Y
00017 # coordinate of the obstacles increase to the left.
00018 def draw_obstacles(ctype_list, pos_list, dimen_list, color):
00019     if len(pos_list) == 0:
00020         return
00021     pos_arr = np.array(pos_list)
00022 
00023     for i in range(len(ctype_list)):
00024         cx, cy = -pos_arr[i,1], pos_arr[i,0]
00025 
00026         if ctype_list[i] == 'wall':
00027             # x,y is exchanged
00028             length = dimen_list[i][1]
00029             width  = dimen_list[i][0]
00030             slope  = pos_arr[i,3] 
00031 
00032             mpu.plot_rectangle(cx, cy, slope, width, length, color=color)
00033         else:
00034             radius = dimen_list[i][0]
00035             mpu.plot_circle(cx, cy, radius, 0., math.pi*2, color=color)    
00036             #mpu.plot_circle(cx, cy, rad_arr[i], 0., math.pi*2,
00037             #                color='k', linewidth=0.5)
00038 
00039 def draw_obstacles_from_reach_problem_dict(d):
00040     draw_obstacles(d['fixed_ctype'], d['fixed_position'], d['fixed_dimen'], color='r')
00041     draw_obstacles(d['moveable_ctype'], d['moveable_position'], d['moveable_dimen'], color='#A0A0A0')
00042             
00043     pp.xlabel('Negative Y-coordinate in the robot\'s frame')
00044     pp.ylabel('X-coordinate in the robot\'s frame')
00045 
00046 def draw_goal_from_reach_problem_dict(d):
00047     g = d['goal']
00048     pp.scatter([-g[1]], [g[0]], s=100, c='g', marker='x', lw=3,
00049             edgecolor='g')
00050 
00051 def viz_reach_problem_dict(d, kinematics):
00052     draw_obstacles_from_reach_problem_dict(d)
00053 
00054 #    q = np.radians([60, 0, 0])
00055 #    kinematics.plot_arm(q, color='b', alpha=0.7, flip_xy=True,
00056 #                        linewidth = 0.5)
00057 
00058     q = np.radians([90, 0, 0])
00059     kinematics.plot_arm(q, color='b', alpha=0.7, flip_xy=True,
00060                         linewidth = 0.5)
00061 
00062     #    q = np.radians([120, 0, 0])
00063     #    kinematics.plot_arm(q, color='b', alpha=0.7, flip_xy=True,
00064     #                        linewidth = 0.5)
00065 
00066     draw_goal_from_reach_problem_dict(d)
00067 
00068 
00069 
00070 if __name__ == '__main__':
00071     import optparse
00072 
00073     # if we are using this file, then we need to have
00074     # hrl_software_simulation_darpa_m3
00075     roslib.load_manifest('hrl_software_simulation_darpa_m3')
00076     import hrl_software_simulation_darpa_m3.gen_sim_arms as gsa
00077 
00078     p = optparse.OptionParser()
00079 
00080     p.add_option('--problem_dict_pkl', '--pdp', action='store', dest='pdp',
00081                  type='string', default = None,
00082                  help='reach problem dict pkl file')
00083     p.add_option('--quiet', '-q', action='store_true', dest='q',
00084                  help='do not bring up matplotlib window')
00085     p.add_option('--save_figure', '--sf', action='store_true', dest='sf',
00086                  help='save the figure')
00087     p.add_option('--sim3', action='store_true', dest='sim3',
00088                  help='three link planar (torso, upper arm, forearm)')
00089     p.add_option('--sim3_with_hand', action='store_true', dest='sim3_with_hand',
00090                  help='three link planar (upper arm, forearm, hand)')
00091 
00092     opt, args = p.parse_args()
00093 
00094     if opt.sim3:
00095         import hrl_common_code_darpa_m3.robot_config.three_link_planar_capsule as d_robot
00096     elif opt.sim3_with_hand:
00097         import hrl_common_code_darpa_m3.robot_config.three_link_with_hand as d_robot
00098 
00099     kinematics = gsa.RobotSimulatorKDL(d_robot)
00100 
00101     if not opt.pdp:
00102         raise RuntimeError('Please specify a reach problem dict file')
00103 
00104     d = ut.load_pickle(opt.pdp)
00105 
00106     mpu.figure()
00107     viz_reach_problem_dict(d, kinematics)
00108 
00109     if opt.sf:
00110         nm = '.'.join(opt.pdp.split('.pkl')[0:-1]) + '.png'
00111         pp.savefig(nm)
00112 
00113     if not opt.q:
00114         pp.show()
00115 
00116 
00117 
00118 
00119 


hrl_common_code_darpa_m3
Author(s): Advait Jain, Marc Killpack. Advisor: Prof. Charlie Kemp. Healthcare Robotics Lab, Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:42