show_grasps_rviz_server.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 import rospy, roslib
19 
20 import actionlib
21 import std_msgs.msg
22 import geometry_msgs.msg
23 import sensor_msgs.msg
24 import visualization_msgs.msg
25 import cob_grasp_generation.msg
26 from cob_grasp_generation import grasp_query_utils
27 
28 import tf
29 import tf2_ros
30 from urdf_parser_py.urdf import URDF
31 
32 class ShowGraspsRvizServer(object):
33 
34  def __init__(self):
35  self.joint_names = []
36  self.joint_positions = []
37  self.joint_mimic = []
38  for joint in URDF.from_parameter_server().joints:
39  if joint.mimic:
40  self.joint_mimic.append(joint)
41  elif joint.type == 'revolute':
42  self.joint_names.append(joint.name)
43  if joint.limit.lower < 0.0 and 0.0 < joint.limit.upper:
44  self.joint_positions.append(0.0)
45  else:
46  self.joint_positions.append((joint.limit.upper + joint.limit.lower)/2.0)
47 
48  self.js = sensor_msgs.msg.JointState()
49  self.js.name = self.joint_names
50  self.js.position = self.joint_positions
51 
52  self.t = geometry_msgs.msg.TransformStamped()
53  self.t.header.frame_id = "object_link"
54  self.t.child_frame_id = "grasp_link"
55  self.t.transform.translation.x = 0.0
56  self.t.transform.translation.y = 0.0
57  self.t.transform.translation.z = 0.0
58  q = tf.transformations.quaternion_from_euler(0, 0, 0)
59  self.t.transform.rotation.x = q[0]
60  self.t.transform.rotation.y = q[1]
61  self.t.transform.rotation.z = q[2]
62  self.t.transform.rotation.w = q[3]
63 
64  self.marker = visualization_msgs.msg.Marker()
65  self.marker.header.frame_id = "object_link"
66  self.marker.type = visualization_msgs.msg.Marker.MESH_RESOURCE
67  self.marker.pose.orientation.w = 1.0
68  self.marker.scale = geometry_msgs.msg.Vector3(1,1,1)
69  self.marker.color = std_msgs.msg.ColorRGBA(1,1,1,1)
70 
72  self._js_pub = rospy.Publisher('/joint_states', sensor_msgs.msg.JointState, queue_size=1)
73  self._marker_pub = rospy.Publisher('/object_marker', visualization_msgs.msg.Marker, queue_size=1)
74  self._timer = rospy.Timer(rospy.Duration(0.1), self.timer_cb)
75 
76  self._as_show = actionlib.SimpleActionServer('show_grasps_rviz', cob_grasp_generation.msg.ShowGraspsAction, execute_cb=self.show_cb, auto_start = False)
77  self._as_show.start()
78 
79  print("ShowGraspsRvizServer: action started...")
80 
81  def show_cb(self, goal):
82  success = False
83 
84  rospy.loginfo('Show grasp %i for object %s using gripper_type %s' % (goal.grasp_id, goal.object_name, goal.gripper_type))
85 
86  if grasp_query_utils.check_database(goal.object_name, goal.gripper_type):
87  self.marker.mesh_resource = "package://cob_grasp_generation/files/meshes/"+goal.object_name+".stl"
88  grasp_list = grasp_query_utils.get_grasps(goal.object_name, goal.gripper_type, goal.gripper_side, goal.grasp_id, 1)
89  if grasp_list:
90  #print grasp_list
91  self.js.name = grasp_list[0].pre_grasp_posture.joint_names #TODO: joint names in urdf are "side-independend"
92  self.js.position = grasp_list[0].grasp_posture.points[0].positions
93  for joint in self.joint_mimic:
94  idx = grasp_list[0].pre_grasp_posture.joint_names.index(joint.mimic.joint)
95  self.js.name.append(joint.name)
96  self.js.position.append(grasp_list[0].grasp_posture.points[0].positions[idx])
97  self.t.transform.translation = grasp_list[0].grasp_pose.pose.position
98  self.t.transform.rotation = grasp_list[0].grasp_pose.pose.orientation
99  success = True
100  else:
101  success = False
102  else:
103  rospy.logerr('GraspTable for Object %s does not exist!' % (goal.object_name))
104 
105  result = cob_grasp_generation.msg.ShowGraspsResult()
106  result.success = success
107 
108  if success:
109  rospy.loginfo('Show: Succeeded')
110  self._as_show.set_succeeded(result)
111  else:
112  rospy.logwarn('Show: Failed')
113  self._as_show.set_aborted(result)
114 
115  def timer_cb(self, event):
116  self.t.header.stamp = event.current_real
117  self.js.header.stamp = event.current_real
118  self.marker.header.stamp = event.current_real
119 
120  self._br.sendTransform(self.t)
121  self._js_pub.publish(self.js)
122  self._marker_pub.publish(self.marker)
123 
124 
125 if __name__ == '__main__':
126  rospy.init_node('show_grasps_rviz_server')
128  rospy.spin()


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