18 """Generates a database of grasps 21 from itertools
import groupby
22 import numpy, time, scipy
24 from openravepy
import Environment
25 from openravepy
import databases, options, matrixFromPose
26 from cob_grasp_generation
import analyzegrasp3d, grasp_query_utils
36 self.
env = Environment()
37 self.env.Load(roslib.packages.get_pkg_dir(
'cob_grasp_generation')+
'/files/env/'+str(gripper_type)+
'.env.xml')
40 if self.env.GetViewer() ==
None:
41 self.env.SetViewer(
'qtcoin')
43 print(
"Viewer already loaded")
45 print(
"Not using a viewer for OpenRAVE")
48 self.
target = self.env.ReadKinBodyURI(roslib.packages.get_pkg_dir(
'cob_grasp_generation')+
'/files/meshes/'+str(object_name)+
'.stl')
49 self.env.Add(self.
target,
True)
51 if self.target.GetName() != object_name:
52 print(
"Changing the target object")
53 self.env.Remove(self.
target)
54 self.
target = self.env.ReadKinBodyURI(roslib.packages.get_pkg_dir(
'cob_grasp_generation')+
'/files/meshes/'+str(object_name)+
'.stl')
55 self.env.Add(self.
target,
True)
57 print(
"Environment set up!")
61 robot = self.env.GetRobots()[0]
62 manip = robot.GetManipulator(gripper_type)
63 gmodel = databases.grasping.GraspingModel(robot,self.
target)
66 tool_trafo = manip.GetLocalToolTransform()
68 manip.SetLocalToolTransform(tool_trafo)
88 now_plan = time.time()
93 print(
"Replanning database")
94 gmodel.autogenerate(options)
95 print(
"Successfully loaded a database")
97 print(
"No database available")
98 print(
"Generating a database")
99 gmodel.autogenerate(options)
102 end_plan = time.time()
103 time_difference = int(end_plan - now_plan)
104 print(
"GraspGeneration took: ", time_difference)
112 validgrasps, validindicees = gmodel.computeValidGrasps(checkcollision=
True, checkik=
False)
115 print(
"TotalNumValidGrasps: ",len(validgrasps))
118 if len(validgrasps) == 0:
119 print(
'No valid grasps generated')
121 databases.grasping.RaveDestroy()
148 meta_info.append(object_name)
149 meta_info.append(gripper_type)
150 meta_info.append(time)
151 grasps_to_file.append(meta_info)
154 for graspnmb
in range(0,len(validgrasps)):
157 auto_fill_rest =
False 160 grasp_to_file.append(graspnmb)
161 grasp_num = int(graspnmb)
165 print(
"Grasp: ",graspnmb)
166 contacts,finalconfig,mindist,volume = gmodel.runGrasp(grasp=validgrasps[grasp_num], forceclosure=
True)
168 print(
"something went wrong")
173 direction = gmodel.getGlobalApproachDir(validgrasps[graspnmb])
177 with gmodel.GripperVisibility(manip):
179 gmodel.setPreshape(validgrasps[graspnmb])
180 Tgrasp = gmodel.getGlobalGraspTransform(validgrasps[graspnmb],collisionfree=
True)
181 Tdelta = numpy.dot(Tgrasp,numpy.linalg.inv(manip.GetEndEffectorTransform()))
182 DOFValues = robot.GetActiveDOFValues()
183 DOFValues[manip.GetGripperIndices()] = finalconfig[0][manip.GetGripperIndices()]
184 robot.SetDOFValues(DOFValues)
185 robot.SetTransform(numpy.dot(Tdelta,robot.GetTransform()))
186 self.env.UpdatePublishedBodies()
188 self.target.SetTransform(numpy.eye(4))
189 with gmodel.GripperVisibility(manip):
191 gmodel.setPreshape(validgrasps[graspnmb])
192 Tgrasp = gmodel.getGlobalGraspTransform(validgrasps[graspnmb],collisionfree=
True)
193 Tdelta = numpy.dot(Tgrasp,numpy.linalg.inv(manip.GetEndEffectorTransform()))
194 for link
in manip.GetChildLinks():
195 link.SetTransform(numpy.dot(Tdelta,link.GetTransform()))
196 self.env.UpdatePublishedBodies()
198 transf = manip.GetEndEffectorTransform()
200 grasp_to_file.append(finalconfig[0][manip.GetGripperIndices()])
201 grasp_to_file.append(transf)
203 grasp_to_file.append(mindist)
204 grasp_to_file.append(volume)
206 forceclosure = validgrasps[graspnmb][gmodel.graspindices[
'forceclosure']]
207 grasp_to_file.append(forceclosure)
208 grasp_to_file.append(validindicees[graspnmb])
209 grasp_to_file.append(direction)
213 grasps_to_file.append(grasp_to_file)
215 gmodel.getGlobalApproachDir(validgrasps[graspnmb])
218 analyzegrasp3d.or_to_csv(grasps_to_file)
221 databases.grasping.RaveDestroy()
222 return len(validgrasps)
247 preshape =
'0.0 -0.9854 0.9472 -0.9854 0.9472 -0.9854 0.9472' 248 preshape_array.append(preshape)
249 preshape =
'0.0 -0.9854 0.47 -0.9854 0.47 -0.9854 0.47' 250 preshape_array.append(preshape)
253 options.preshapes = numpy.array(preshape_array)
262 options.boxdelta = 0.05
268 standoff_array = [0.025, 0.075]
269 options.standoffs = numpy.array(standoff_array)
274 options.friction = 1.0
283 options.normalanglerange = 0.0
287 options.directiondelta = 1
299 def show_grasp(self, object_name, gripper_type, grasp_id, sort_by_quality=False):
302 grasp_list = grasp_query_utils.get_grasp_list(object_name, gripper_type, sort_by_quality)
305 robot = self.env.GetRobots()[0]
306 manip = robot.GetManipulator(gripper_type)
307 gmodel = databases.grasping.GraspingModel(robot,self.
target)
310 tool_trafo = manip.GetLocalToolTransform()
311 tool_trafo[2,3] = 0.0
312 manip.SetLocalToolTransform(tool_trafo)
316 grasp = grasp_list[int(grasp_id)]
317 print(
'Grasp ID: '+str(grasp[
'id']))
318 print(
'Grasp Quality epsilon: '+str(grasp[
'eps_l1']))
319 print(
'Grasp Quality volume: '+str(grasp[
'vol_l1']))
322 with gmodel.GripperVisibility(manip):
324 dof_array[7:13] =[float(grasp[
'sdh_finger_22_joint']),float(grasp[
'sdh_finger_23_joint']),float(grasp[
'sdh_knuckle_joint']),float(grasp[
'sdh_finger_12_joint']),float(grasp[
'sdh_finger_13_joint']),float(grasp[
'sdh_thumb_2_joint']),float(grasp[
'sdh_thumb_3_joint'])]
325 robot.SetDOFValues(numpy.array(dof_array))
329 pos = [float(grasp[
'pos-x'])*mm_in_m, float(grasp[
'pos-y'])*mm_in_m, float(grasp[
'pos-z'])*mm_in_m]
330 quat = [float(grasp[
'qw']), float(grasp[
'qx']), float(grasp[
'qy']), float(grasp[
'qz'])]
331 Tgrasp = matrixFromPose(quat+pos)
334 Tdelta = numpy.dot(Tgrasp,numpy.linalg.inv(manip.GetEndEffectorTransform()))
335 for link
in manip.GetChildLinks():
336 link.SetTransform(numpy.dot(Tdelta,link.GetTransform()))
340 self.env.UpdatePublishedBodies()
def configure_grasp_generation(self)
def generate_grasps(self, object_name, gripper_type, replan=False)
def show_grasp(self, object_name, gripper_type, grasp_id, sort_by_quality=False)
def __init__(self, viewer=False)
def setup_environment(self, object_name, gripper_type, viewer=False)