4 from copy
import deepcopy
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
17 M = numpy.array(matrix, dtype=numpy.float64, copy=
False)[:4, :4]
19 q = numpy.empty((4, ))
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]
32 t = M[i, i] - (M[j, j] + M[k, k]) + M[3, 3]
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])
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]])
55 w, V = numpy.linalg.eigh(K)
56 q = V[[3, 0, 1, 2], numpy.argmax(w)]
67 GetFastGraspFromBoundingBox,
71 '/grasp_warehouse/get_robot_state', GetState)
73 hand_group = rospy.get_param(
"~hand_group",
"right_hand")
74 arm_group = rospy.get_param(
"~arm_group",
"right_arm")
76 self.
__group = MoveGroupCommander(hand_group)
77 self.
__arm_g = MoveGroupCommander(arm_group)
78 self.
__ik = rospy.ServiceProxy(
"compute_ik", GetPositionIK)
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. 87 v1 = numpy.array([pose.pose.position.x,
89 pose.pose.position.z])
90 v1_length = numpy.linalg.norm(v1)
94 v2 = [1, 0, -v1[0]/v1[2]]
95 v2 = v2/numpy.linalg.norm(v2)
97 v3 = numpy.cross(v1, v2)
98 v3 = v3/numpy.linalg.norm(v3)
101 [v3[0], v1[0], v2[0]],
102 [v3[1], v1[1], v2[1]],
103 [v3[2], v1[2], v2[2]]
108 grasp.grasp_pose = deepcopy(pose)
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]
116 box = request.bounding_box
118 if SolidPrimitive.BOX != box.type:
119 rospy.logerr(
"Bounding volume must be a BOX.")
134 open_state = self.
__get_state(name +
"_open",
"").state
135 closed_state = self.
__get_state(name +
"_closed",
"").state
137 rospy.logfatal(
"Couldn't get grasp pose from db.")
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)
146 rospy.logfatal(
"Couldn't plan grasp trajectories.")
151 grasp.pre_grasp_posture = pre_pose.joint_trajectory
152 grasp.grasp_posture = pose.joint_trajectory
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
163 m = max(box.dimensions)
164 max_index = [i
for i, j
in enumerate(box.dimensions)
if j == m]
169 self.__marker_pub.publish(marker)
173 marker.pose = pose.pose
174 marker.header.frame_id = pose.header.frame_id
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]
184 marker.lifetime = rospy.rostime.Duration()
185 marker.type = Marker.CUBE
186 marker.ns =
"sr_fast_grasp_target" 188 marker.action = Marker.ADD
192 if "__main__" == __name__:
193 rospy.init_node(
'sr_fast_grasp')
def __get_marker_from_box(self, box, pose)
def quaternion_from_matrix(matrix, isprecise=False)
def __get_grasp(self, name)
def __bounding_box_cb(self, request)
def __modify_grasp_pose(self, grasp, pose)
def __send_marker_to_rviz(self, box, pose)
def __get_major_axis(self, box)