fast_grasp.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import numpy
4 from copy import deepcopy
5 
6 import rospy
7 from shape_msgs.msg import SolidPrimitive
8 from visualization_msgs.msg import Marker
9 from sr_robot_msgs.srv import GetFastGraspFromBoundingBox
10 from moveit_msgs.msg import Grasp
11 from moveit_commander import MoveGroupCommander
12 from moveit_msgs.srv import GetRobotStateFromWarehouse as GetState
13 from moveit_msgs.srv import GetPositionIK
14 
15 
16 def quaternion_from_matrix(matrix, isprecise=False):
17  M = numpy.array(matrix, dtype=numpy.float64, copy=False)[:4, :4]
18  if isprecise:
19  q = numpy.empty((4, ))
20  t = numpy.trace(M)
21  if t > M[3, 3]:
22  q[0] = t
23  q[3] = M[1, 0] - M[0, 1]
24  q[2] = M[0, 2] - M[2, 0]
25  q[1] = M[2, 1] - M[1, 2]
26  else:
27  i, j, k = 1, 2, 3
28  if M[1, 1] > M[0, 0]:
29  i, j, k = 2, 3, 1
30  if M[2, 2] > M[i, i]:
31  i, j, k = 3, 1, 2
32  t = M[i, i] - (M[j, j] + M[k, k]) + M[3, 3]
33  q[i] = t
34  q[j] = M[i, j] + M[j, i]
35  q[k] = M[k, i] + M[i, k]
36  q[3] = M[k, j] - M[j, k]
37  q *= 0.5 / math.sqrt(t * M[3, 3])
38  else:
39  m00 = M[0, 0]
40  m01 = M[0, 1]
41  m02 = M[0, 2]
42  m10 = M[1, 0]
43  m11 = M[1, 1]
44  m12 = M[1, 2]
45  m20 = M[2, 0]
46  m21 = M[2, 1]
47  m22 = M[2, 2]
48  # symmetric matrix K
49  K = numpy.array([[m00-m11-m22, 0.0, 0.0, 0.0],
50  [m01+m10, m11-m00-m22, 0.0, 0.0],
51  [m02+m20, m12+m21, m22-m00-m11, 0.0],
52  [m21-m12, m02-m20, m10-m01, m00+m11+m22]])
53  K /= 3.0
54  # quaternion is eigenvector of K that corresponds to largest eigenvalue
55  w, V = numpy.linalg.eigh(K)
56  q = V[[3, 0, 1, 2], numpy.argmax(w)]
57  if q[0] < 0.0:
58  numpy.negative(q, q)
59  return q
60 
61 
63  def __init__(self):
64  self.__marker_pub = rospy.Publisher("visualization_marker",
65  Marker, queue_size=1)
66  self.__grasp_server = rospy.Service("grasp_from_bounding_box",
67  GetFastGraspFromBoundingBox,
68  self.__bounding_box_cb)
69  self.__default_grasp = 'super_amazing_grasp'
70  self.__get_state = rospy.ServiceProxy(
71  '/grasp_warehouse/get_robot_state', GetState)
72 
73  hand_group = rospy.get_param("~hand_group", "right_hand")
74  arm_group = rospy.get_param("~arm_group", "right_arm")
75 
76  self.__group = MoveGroupCommander(hand_group)
77  self.__arm_g = MoveGroupCommander(arm_group)
78  self.__ik = rospy.ServiceProxy("compute_ik", GetPositionIK)
79 
80  def __modify_grasp_pose(self, grasp, pose):
81  """
82  Aligns grasp with axis from origin to center of object.
83  A crude way to make a vaguely sane orientation for the hand
84  that seems to more or less work.
85  """
86 
87  v1 = numpy.array([pose.pose.position.x,
88  pose.pose.position.y,
89  pose.pose.position.z])
90  v1_length = numpy.linalg.norm(v1)
91 
92  v1 = v1/v1_length
93 
94  v2 = [1, 0, -v1[0]/v1[2]]
95  v2 = v2/numpy.linalg.norm(v2)
96 
97  v3 = numpy.cross(v1, v2)
98  v3 = v3/numpy.linalg.norm(v3)
99 
100  m = [
101  [v3[0], v1[0], v2[0]],
102  [v3[1], v1[1], v2[1]],
103  [v3[2], v1[2], v2[2]]
104  ]
105 
107 
108  grasp.grasp_pose = deepcopy(pose)
109 
110  grasp.grasp_pose.pose.orientation.x = q[0]
111  grasp.grasp_pose.pose.orientation.y = q[1]
112  grasp.grasp_pose.pose.orientation.z = q[2]
113  grasp.grasp_pose.pose.orientation.w = q[3]
114 
115  def __bounding_box_cb(self, request):
116  box = request.bounding_box
117  pose = request.pose
118  if SolidPrimitive.BOX != box.type:
119  rospy.logerr("Bounding volume must be a BOX.")
120  return None
121  self.__send_marker_to_rviz(box, pose)
122  grasp_name = self.__select_grasp()
123  grasp = self.__get_grasp(grasp_name)
124 
125  self.__modify_grasp_pose(grasp, pose)
126 
127  return grasp
128 
129  def __select_grasp(self):
130  return self.__default_grasp
131 
132  def __get_grasp(self, name):
133  try:
134  open_state = self.__get_state(name + "_open", "").state
135  closed_state = self.__get_state(name + "_closed", "").state
136  except:
137  rospy.logfatal("Couldn't get grasp pose from db.")
138  return Grasp()
139 
140  try:
141  self.__group.set_start_state_to_current_state()
142  pre_pose = self.__group.plan(open_state.joint_state)
143  self.__group.set_start_state(open_state)
144  pose = self.__group.plan(closed_state.joint_state)
145  except:
146  rospy.logfatal("Couldn't plan grasp trajectories.")
147  return Grasp()
148 
149  grasp = Grasp()
150  grasp.id = name
151  grasp.pre_grasp_posture = pre_pose.joint_trajectory
152  grasp.grasp_posture = pose.joint_trajectory
153 
154  grasp.pre_grasp_approach.desired_distance = 0.2
155  grasp.pre_grasp_approach.min_distance = 0.1
156  grasp.pre_grasp_approach.direction.vector.x = 0
157  grasp.pre_grasp_approach.direction.vector.y = -1
158  grasp.pre_grasp_approach.direction.vector.z = 0
159 
160  return grasp
161 
162  def __get_major_axis(self, box):
163  m = max(box.dimensions)
164  max_index = [i for i, j in enumerate(box.dimensions) if j == m]
165  return max_index[-1] # Get the LAST axis with max val.
166 
167  def __send_marker_to_rviz(self, box, pose):
168  marker = self.__get_marker_from_box(box, pose)
169  self.__marker_pub.publish(marker)
170 
171  def __get_marker_from_box(self, box, pose):
172  marker = Marker()
173  marker.pose = pose.pose
174  marker.header.frame_id = pose.header.frame_id
175 
176  marker.scale.x = box.dimensions[SolidPrimitive.BOX_X]
177  marker.scale.y = box.dimensions[SolidPrimitive.BOX_Y]
178  marker.scale.z = box.dimensions[SolidPrimitive.BOX_Z]
179  marker.color.r = 1.0
180  marker.color.g = 0.0
181  marker.color.b = 0.0
182  marker.color.a = 0.5
183 
184  marker.lifetime = rospy.rostime.Duration()
185  marker.type = Marker.CUBE
186  marker.ns = "sr_fast_grasp_target"
187  marker.id = 0
188  marker.action = Marker.ADD
189  return marker
190 
191 
192 if "__main__" == __name__:
193  rospy.init_node('sr_fast_grasp')
194  grasp = SrFastGrasp()
195  rospy.spin()
def __get_marker_from_box(self, box, pose)
Definition: fast_grasp.py:171
def __select_grasp(self)
Definition: fast_grasp.py:129
def quaternion_from_matrix(matrix, isprecise=False)
Definition: fast_grasp.py:16
def __get_grasp(self, name)
Definition: fast_grasp.py:132
def __bounding_box_cb(self, request)
Definition: fast_grasp.py:115
def __modify_grasp_pose(self, grasp, pose)
Definition: fast_grasp.py:80
def __send_marker_to_rviz(self, box, pose)
Definition: fast_grasp.py:167
def __get_major_axis(self, box)
Definition: fast_grasp.py:162


sr_grasp_fast_planner
Author(s):
autogenerated on Tue Oct 13 2020 03:50:44