or_grasp_generation.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 """Generates a database of grasps
19 """
20 #libs and modules
21 from itertools import groupby
22 import numpy, time, scipy
23 import roslib
24 from openravepy import Environment # pylint: disable=import-error
25 from openravepy import databases, options, matrixFromPose # pylint: disable=import-error
26 from cob_grasp_generation import analyzegrasp3d, grasp_query_utils
27 
28 
30  def __init__(self, viewer=False):
31  self.env = None
32  self.target = None
33 
34  def setup_environment(self, object_name, gripper_type, viewer=False):
35  if self.env == None:
36  self.env = Environment()
37  self.env.Load(roslib.packages.get_pkg_dir('cob_grasp_generation')+'/files/env/'+str(gripper_type)+'.env.xml')
38 
39  if viewer:
40  if self.env.GetViewer() == None:
41  self.env.SetViewer('qtcoin')
42  else:
43  print("Viewer already loaded")
44  else:
45  print("Not using a viewer for OpenRAVE")
46 
47  if self.target == None:
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)
50 
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)
56 
57  print("Environment set up!")
58 
59  def generate_grasps(self, object_name, gripper_type, replan=False):
60  #robot
61  robot = self.env.GetRobots()[0]
62  manip = robot.GetManipulator(gripper_type) #make sure the manipulator in the collada file is named accordingly
63  gmodel = databases.grasping.GraspingModel(robot,self.target)
64 
65  #TCP - transformed to hand wrist
66  tool_trafo = manip.GetLocalToolTransform()
67  tool_trafo[2,3] = 0.0
68  manip.SetLocalToolTransform(tool_trafo)
69 
70  ### Options for GraspGeneration
71  #~ friction = None
72  #~ preshapes = None
73  #~ manipulatordirections = None
74  #~ approachrays = None
75  #~ standoffs = None
76  #~ rolls = None
77  #~ avoidlinks = None
78  #~ graspingnoise = None
79  #~ plannername = None
80  #~ normalanglerange = 0
81  #~ directiondelta=0
82  #~ translationstepmult=None
83  #~ finestep=None
84  ####
85 
86  options = self.configure_grasp_generation()
87 
88  now_plan = time.time()
89 
90  ### Do actual GraspGeneration
91  if gmodel.load():
92  if replan == True:
93  print("Replanning database")
94  gmodel.autogenerate(options)
95  print("Successfully loaded a database")
96  else:
97  print("No database available")
98  print("Generating a database")
99  gmodel.autogenerate(options)
100 
101  #time diff
102  end_plan = time.time()
103  time_difference = int(end_plan - now_plan)
104  print("GraspGeneration took: ", time_difference)
105 
106  ### GetValidGrasps now
107  #Return all validgrasps
108  #@OPTIONAL: We can return a desired number of grasps
109  #computeValidGrasps(startindex=0, checkcollision=True, checkik=True, checkgrasper=True, backupdist=0.0, returnnum=inf)
110  #Note: setting backupdist does somehow not effect the validgrasps list???
111 
112  validgrasps, validindicees = gmodel.computeValidGrasps(checkcollision=True, checkik=False)
113  #validgrasps, validindicees = gmodel.computeValidGrasps(backupdist=0.025, checkcollision=True, checkik=False) #opt1
114  #validgrasps, validindicees = gmodel.computeValidGrasps(backupdist=0.0, checkcollision=True, checkik=False) #opt2
115  print("TotalNumValidGrasps: ",len(validgrasps))
116 
117  #prevent from saving an empty file
118  if len(validgrasps) == 0:
119  print('No valid grasps generated')
120  return []
121  databases.grasping.RaveDestroy()
122 
123  #~ backupdist_array = [0.025, 0.05, 0.075, 0.1]
124  #~ num_grasps=numpy.inf
125  #~ #num_grasps = 20
126  #~
127  #~ validgrasps = []
128  #~ validindicees = []
129  #~
130  #~ for bd in backupdist_array:
131  #~ ###GRASP-PLANNING
132  #~ #Return all validgrasps
133  #~ #@OPTIONAL: We can return a desired number of grasps
134  #~ #computeValidGrasps(startindex=0, checkcollision=True, checkik=True, checkgrasper=True, backupdist=0.0, returnnum=inf)
135  #~ validgrasps_bd, validindicees_bd = gmodel.computeValidGrasps(checkcollision=True, checkik=False, backupdist=bd, returnnum=num_grasps)
136  #~
137  #~ print "Backupdist: ",bd
138  #~ print "NumValidGrasps: ",len(validgrasps_bd)
139  #~
140  #~ validgrasps.extend(validgrasps_bd)
141  #~ validindicees.extend(validindicees_bd)
142  #~
143  #~ print "TotalNumValidGrasps: ",len(validgrasps)
144 
145  ##Write all validgrasps to file
146  grasps_to_file = []
147  meta_info = []
148  meta_info.append(object_name)
149  meta_info.append(gripper_type)
150  meta_info.append(time)
151  grasps_to_file.append(meta_info)
152 
153  #@todo: for debug, remove
154  for graspnmb in range(0,len(validgrasps)):
155 
156  #for segmentation fault purposes
157  auto_fill_rest = False
158 
159  grasp_to_file = []
160  grasp_to_file.append(graspnmb) #to file grasp number for id
161  grasp_num = int(graspnmb)
162 
163  #calculate metric and final joint configuration
164  try:
165  print("Grasp: ",graspnmb)
166  contacts,finalconfig,mindist,volume = gmodel.runGrasp(grasp=validgrasps[grasp_num], forceclosure=True)
167  except:
168  print("something went wrong")
169  mindist = 0.0
170  volume = 0.0
171 
172  #show grasp before calculating the correct transformation
173  direction = gmodel.getGlobalApproachDir(validgrasps[graspnmb])
174  #gmodel.showgrasp(validgrasps[graspnmb],collisionfree=True)
175  transf = []
176 
177  with gmodel.GripperVisibility(manip):
178  with self.env:
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()
187  with self.target:
188  self.target.SetTransform(numpy.eye(4))
189  with gmodel.GripperVisibility(manip):
190  with self.env:
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()
197  # wait while environment is locked?
198  transf = manip.GetEndEffectorTransform()
199 
200  grasp_to_file.append(finalconfig[0][manip.GetGripperIndices()])
201  grasp_to_file.append(transf)
202  #gmodel.showgrasp(validgrasps[graspnmb],collisionfree=True)
203  grasp_to_file.append(mindist)
204  grasp_to_file.append(volume)
205 
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)
210  #print "GlobApproachDir: ",gmodel.getGlobalApproachDir(validgrasps[i])
211  #SDH Joint Values - Beginnend mit Daumen(1) und die Zwei Finger GUZS nummeriert(2)(3)
212  #[Fingerwinkel(2), Fingerknick(2), Fingerrotation(2)(3), Fingerwinkel(3), Fingerknick(3), Fingerwinkel(1), Fingerknick(1)]
213  grasps_to_file.append(grasp_to_file)
214 
215  gmodel.getGlobalApproachDir(validgrasps[graspnmb])
216 
217  #Create a csv file if needed
218  analyzegrasp3d.or_to_csv(grasps_to_file)
219 
220  print('Finished.')
221  databases.grasping.RaveDestroy()
222  return len(validgrasps)
223 
225  ### Options for GraspGeneration
226  #~ 'preshapes'
227  #~ 'manipulatordirections'
228  #~ 'boxdelta'
229  #~ 'spheredelta'
230  #~ 'standoffs'
231  #~ 'rolls'
232  #~ 'friction'
233  #~ 'avoidlinks'
234  #~ 'graspingnoise'
235  #~ 'plannername'
236  #~ 'normalanglerange'
237  #~ 'directiondelta'
238  #~ 'translationstepmult'
239  #~ 'finestep'
240  #~ 'numthreads'
241  ####
242 
243  #preshape --- default is 'something strange' ?
244  preshape_array = []
245  #preshape = '1.047 -0.785 1.047 -0.785 1.047 -0.785 1.047' # spheric_open
246  #preshape_array.append(preshape)
247  preshape = '0.0 -0.9854 0.9472 -0.9854 0.9472 -0.9854 0.9472' # cylindric_open
248  preshape_array.append(preshape)
249  preshape = '0.0 -0.9854 0.47 -0.9854 0.47 -0.9854 0.47' # fingers_half_straight_open
250  preshape_array.append(preshape)
251  #preshape = '0.0 -0.9854 0.0 -0.9854 0.0 -0.9854 0.0' # fingers_straight_open
252  #preshape_array.append(preshape)
253  options.preshapes = numpy.array(preshape_array)
254 
255  #manipulatordirections --- default is [0.0,1.0,0.0] ?
256  #~ md_array = []
257  #~ direction1 = '0.0 0.0 1.0' #along z-axis -- points 'out of sdh'
258  #~ md_array.append(direction1)
259  #~ options.manipulatordirections = numpy.array(md_array)
260 
261  #boxdelta --- default is 0.02
262  options.boxdelta = 0.05
263 
264  #spheredelta --- default is 0.1
265  #~ only used if boxdelta is not set
266 
267  #standoffs --- default is [0.0, 0.025] --- 0.0 is bad as we will hit the object!
268  standoff_array = [0.025, 0.075]
269  options.standoffs = numpy.array(standoff_array)
270 
271  #rolls --- default is [0.0, pi/2, pi, 3*pi/2, 2*pi]
272 
273  #fricion
274  options.friction = 1.0 #coefficient of static friction in graspit: rubber-<xobject> = 1
275 
276  #avoidlinks --- default is None ?
277 
278  #graspingnoise --- ?
279 
280  #plannername --- ?
281 
282  #normalanglerange --- default is 0.0 - used for computeXXXApproachRays
283  options.normalanglerange = 0.0
284  #options.normalanglerange = 15
285 
286  #directiondelta --- default is 0.4 - used for computeXXXApproachRays
287  options.directiondelta = 1
288  #options.directiondelta = 5
289 
290  #translationstepmult --- default is None ?
291 
292  #finestep
293 
294  #numthreads --- default is 1
295  #options.numthreads = 4 #--- multiple threads somehow not working
296 
297  return options
298 
299  def show_grasp(self, object_name, gripper_type, grasp_id, sort_by_quality=False):
300  self.setup_environment(object_name, gripper_type, viewer=True)
301 
302  grasp_list = grasp_query_utils.get_grasp_list(object_name, gripper_type, sort_by_quality)
303 
304  #robot
305  robot = self.env.GetRobots()[0]
306  manip = robot.GetManipulator(gripper_type) #make sure the manipulator in the collada file is named accordingly
307  gmodel = databases.grasping.GraspingModel(robot,self.target)
308 
309  #TCP - transformed to hand wrist
310  tool_trafo = manip.GetLocalToolTransform()
311  tool_trafo[2,3] = 0.0
312  manip.SetLocalToolTransform(tool_trafo)
313 
314  #Show the grasps
315  #SetDOFValues
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']))
320  #print grasp
321 
322  with gmodel.GripperVisibility(manip):
323  dof_array = [0]*27
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))
326 
327  #matrix from pose
328  mm_in_m = 0.001
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)
332 
333  #transform robot
334  Tdelta = numpy.dot(Tgrasp,numpy.linalg.inv(manip.GetEndEffectorTransform()))
335  for link in manip.GetChildLinks():
336  link.SetTransform(numpy.dot(Tdelta,link.GetTransform()))
337 
338  #publish update of scene
339  time.sleep(1)
340  self.env.UpdatePublishedBodies()
341 
342 
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 setup_environment(self, object_name, gripper_type, viewer=False)


cob_grasp_generation
Author(s): Witalij Siebert, Felix Messmer
autogenerated on Sun Dec 6 2020 03:25:46