grasp_query_utils.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 #from itertools import groupby
19 #import numpy, time, analyzegrasp3d, scipy
20 import csv, os
21 
22 #ROS
23 import rospy, roslib
24 from moveit_msgs.msg import *
25 from geometry_msgs.msg import *
26 from trajectory_msgs.msg import *
27 
28 
29 #check if a database with the object_id exists
30 def check_database(object_name, gripper_type):
31  #Begins here to read the grasp .csv-Files
32  path_in = roslib.packages.get_pkg_dir('cob_grasp_generation')+'/files/database/'+object_name+'/'+gripper_type+'_'+object_name+'.csv'
33 
34  #Check if path exists
35  if os.path.exists(path_in):
36  return True
37  else:
38  return False
39 
40 def get_grasp_list(object_name, gripper_type, sort_by_quality=False):
41  #Begins here to read the grasp .csv-Files
42  path_in = roslib.packages.get_pkg_dir('cob_grasp_generation')+'/files/database/'+object_name+'/'+gripper_type+'_'+object_name+'.csv'
43 
44  #Check if path exists
45  try:
46  with open(path_in) as f: pass
47  except IOError as e:
48  rospy.logerr("The path or file does not exist: "+path_in)
49 
50  #If exists open with dictreader
51  reader = csv.DictReader( open(path_in, "rb"), delimiter=',')
52 
53  if sort_by_quality:
54  #sorting for threshold
55  return sorted(reader, key=lambda d: float(d['eps_l1']), reverse=True)
56  else:
57  return sorted(reader, key=lambda d: float(d['id']), reverse=False)
58 
59 #get the grasps
60 def get_grasps(object_name, gripper_type, gripper_side="", grasp_id=0, num_grasps=0, threshold=0):
61  #open database
62  grasp_list = get_grasp_list(object_name, gripper_type)
63 
64  #check for grasp_id and return
65  if grasp_id > 0:
66  if grasp_id < len(grasp_list):
67  #print _fill_grasp_msg(gripper_type, gripper_side, grasp_list[grasp_id])
68  return [_fill_grasp_msg(gripper_type, gripper_side, grasp_list[grasp_id])]
69  else:
70  print("Grasp not available")
71  return []
72 
73  #sorting for threshold
74  sorted_list = sorted(grasp_list, key=lambda d: float(d['eps_l1']), reverse=True)
75 
76  #calculate max_grasps
77  if num_grasps > 0:
78  max_grasps=min(len(sorted_list),num_grasps)
79  else:
80  max_grasps=len(sorted_list)
81 
82  #grasp output
83  selected_grasp_list = []
84  for i in range(0,max_grasps):
85  if threshold > 0 and float(sorted_list[i]['eps_l1']) >= threshold:
86  selected_grasp_list.append(_fill_grasp_msg(gripper_type, gripper_side, sorted_list[i]))
87  elif threshold == 0:
88  selected_grasp_list.append(_fill_grasp_msg(gripper_type, gripper_side, sorted_list[i]))
89  else:
90  pass
91 
92  #print len(selected_grasp_list)
93  return selected_grasp_list
94 
95 #fill the grasp message of ROS
96 def _fill_grasp_msg(gripper_type, gripper_side, grasp):
97 
98  #grasp posture
99  joint_config = JointTrajectory()
100  #joint_config.header.stamp = rospy.Time.now()
101  #joint_config.header.frame_id = ""
102 
103  #entries in the grasp table are side-independend
104  if gripper_type == "sdh":
105  joint_config.joint_names = ['sdh_knuckle_joint', 'sdh_finger_12_joint', 'sdh_finger_13_joint', 'sdh_finger_22_joint', 'sdh_finger_23_joint', 'sdh_thumb_2_joint', 'sdh_thumb_3_joint']
106  elif gripper_type == "sdhx":
107  joint_config.joint_names = ['gripper_finger_1_joint', 'gripper_finger_2_joint']
108  else:
109  rospy.logerr("Gripper not supported")
110  return Grasp()
111 
112  #get grasp joint configuration
113  point = JointTrajectoryPoint()
114  for joint_name in joint_config.joint_names:
115  point.positions.append(float(grasp[joint_name]))
116  point.velocities.append(0.0)
117  point.accelerations.append(0.0)
118  point.effort.append(0.0)
119  point.time_from_start = rospy.Duration(3.0)
120  joint_config.points.append(point)
121 
122  #side-specific joint_name correction
123  #ToDo: get rid of this hardcoded-joint names
124  if gripper_type == "sdh":
125  if gripper_side == "left":
126  joint_config.joint_names = ['sdh_left_knuckle_joint', 'sdh_left_finger_12_joint', 'sdh_left_finger_13_joint', 'sdh_left_finger_22_joint', 'sdh_left_finger_23_joint', 'sdh_left_thumb_2_joint', 'sdh_left_thumb_3_joint']
127  elif gripper_side == "right":
128  joint_config.joint_names = ['sdh_right_knuckle_joint', 'sdh_right_finger_12_joint', 'sdh_right_finger_13_joint', 'sdh_right_finger_22_joint', 'sdh_right_finger_23_joint', 'sdh_right_thumb_2_joint', 'sdh_right_thumb_3_joint']
129  else:
130  joint_config.joint_names = ['sdh_knuckle_joint', 'sdh_finger_12_joint', 'sdh_finger_13_joint', 'sdh_finger_22_joint', 'sdh_finger_23_joint', 'sdh_thumb_2_joint', 'sdh_thumb_3_joint']
131  elif gripper_type == "sdhx":
132  if gripper_side == "left":
133  joint_config.joint_names = ['gripper_left_finger_1_joint', 'gripper_left_finger_2_joint']
134  elif gripper_side == "right":
135  joint_config.joint_names = ['gripper_right_finger_1_joint', 'gripper_right_finger_2_joint']
136  else:
137  joint_config.joint_names = ['gripper_finger_1_joint', 'gripper_finger_2_joint']
138  else:
139  rospy.logerr("Gripper not supported")
140  return Grasp()
141 
142  #print joint_config
143 
144  #pregrasp posture
145  pre_joint_config = JointTrajectory()
146  #pre_joint_config.header.stamp = rospy.Time.now()
147  #pre_joint_config.header.frame_id = ""
148 
149  #ToDo: get rid of this hardcoded-joint names and open_config
150  if gripper_type == "sdh":
151  if gripper_side == "left":
152  pre_joint_config.joint_names = ['sdh_left_knuckle_joint', 'sdh_left_finger_12_joint', 'sdh_left_finger_13_joint', 'sdh_left_finger_22_joint', 'sdh_left_finger_23_joint', 'sdh_left_thumb_2_joint', 'sdh_left_thumb_3_joint']
153  elif gripper_side == "right":
154  pre_joint_config.joint_names = ['sdh_right_knuckle_joint', 'sdh_right_finger_12_joint', 'sdh_right_finger_13_joint', 'sdh_right_finger_22_joint', 'sdh_right_finger_23_joint', 'sdh_right_thumb_2_joint', 'sdh_right_thumb_3_joint']
155  else:
156  pre_joint_config.joint_names = ['sdh_knuckle_joint', 'sdh_finger_12_joint', 'sdh_finger_13_joint', 'sdh_finger_22_joint', 'sdh_finger_23_joint', 'sdh_thumb_2_joint', 'sdh_thumb_3_joint']
157  open_config = [0.0, -0.9854, 0.9472, -0.9854, 0.9472, -0.9854, 0.9472]
158  elif gripper_type == "sdhx":
159  if gripper_side == "left":
160  pre_joint_config.joint_names = ['gripper_left_finger_1_joint', 'gripper_left_finger_2_joint']
161  elif gripper_side == "right":
162  pre_joint_config.joint_names = ['gripper_right_finger_1_joint', 'gripper_right_finger_2_joint']
163  else:
164  pre_joint_config.joint_names = ['gripper_finger_1_joint', 'gripper_finger_2_joint']
165  open_config = [-0.85, -0.5]
166 
167  else:
168  rospy.logerr("Gripper not supported")
169  return Grasp()
170 
171  point = JointTrajectoryPoint()
172  for i in range(len(pre_joint_config.joint_names)):
173  point.positions.append(open_config[i])
174  point.velocities.append(0.0)
175  point.accelerations.append(0.0)
176  point.effort.append(0.0)
177  point.time_from_start = rospy.Duration(3.0)
178  pre_joint_config.points.append(point)
179  #print pre_joint_config
180 
181  #grasp pose
182  grasp_pose = PoseStamped()
183  grasp_pose.header.stamp = rospy.Time.now()
184  #grasp_pose.header.frame_id = ""
185  grasp_pose.pose.position.x = float(grasp['pos-x'])*0.001 #mm to m
186  grasp_pose.pose.position.y = float(grasp['pos-y'])*0.001 #mm to m
187  grasp_pose.pose.position.z = float(grasp['pos-z'])*0.001 #mm to m
188  grasp_pose.pose.orientation.x = float(grasp['qx'])
189  grasp_pose.pose.orientation.y = float(grasp['qy'])
190  grasp_pose.pose.orientation.z = float(grasp['qz'])
191  grasp_pose.pose.orientation.w = float(grasp['qw'])
192 
193  #grasp
194  grasp_out = Grasp()
195  grasp_out.id = grasp['id']
196  grasp_out.pre_grasp_posture = pre_joint_config
197  grasp_out.grasp_posture = joint_config
198  grasp_out.grasp_pose = grasp_pose
199  grasp_out.grasp_quality = float(grasp['eps_l1'])
200  grasp_out.max_contact_force = 0
201 
202  return grasp_out
def _fill_grasp_msg(gripper_type, gripper_side, grasp)
def get_grasps(object_name, gripper_type, gripper_side="", grasp_id=0, num_grasps=0, threshold=0)
def get_grasp_list(object_name, gripper_type, sort_by_quality=False)
def check_database(object_name, gripper_type)


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