show_grasps_rviz_server.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
00016 
00017 
00018 import rospy, roslib
00019 
00020 import actionlib
00021 import std_msgs.msg
00022 import geometry_msgs.msg
00023 import sensor_msgs.msg
00024 import visualization_msgs.msg
00025 import cob_grasp_generation.msg
00026 from cob_grasp_generation import grasp_query_utils
00027 
00028 import tf
00029 import tf2_ros
00030 from urdf_parser_py.urdf import URDF
00031 
00032 class ShowGraspsRvizServer(object):
00033 
00034   def __init__(self):
00035     self.joint_names = []
00036     self.joint_positions = []
00037     self.joint_mimic = []
00038     for joint in URDF.from_parameter_server().joints:
00039       if joint.mimic:
00040         self.joint_mimic.append(joint)
00041       elif joint.type == 'revolute':
00042         self.joint_names.append(joint.name)
00043         if joint.limit.lower < 0.0 and 0.0 < joint.limit.upper:
00044           self.joint_positions.append(0.0)
00045         else:
00046           self.joint_positions.append((joint.limit.upper + joint.limit.lower)/2.0)
00047 
00048     self.js = sensor_msgs.msg.JointState()
00049     self.js.name = self.joint_names
00050     self.js.position = self.joint_positions
00051 
00052     self.t = geometry_msgs.msg.TransformStamped()
00053     self.t.header.frame_id = "object_link"
00054     self.t.child_frame_id = "grasp_link"
00055     self.t.transform.translation.x = 0.0
00056     self.t.transform.translation.y = 0.0
00057     self.t.transform.translation.z = 0.0
00058     q = tf.transformations.quaternion_from_euler(0, 0, 0)
00059     self.t.transform.rotation.x = q[0]
00060     self.t.transform.rotation.y = q[1]
00061     self.t.transform.rotation.z = q[2]
00062     self.t.transform.rotation.w = q[3]
00063 
00064     self.marker = visualization_msgs.msg.Marker()
00065     self.marker.header.frame_id = "object_link"
00066     self.marker.type = visualization_msgs.msg.Marker.MESH_RESOURCE
00067     self.marker.pose.orientation.w = 1.0
00068     self.marker.scale = geometry_msgs.msg.Vector3(1,1,1)
00069     self.marker.color = std_msgs.msg.ColorRGBA(1,1,1,1)
00070 
00071     self._br = tf2_ros.TransformBroadcaster()
00072     self._js_pub = rospy.Publisher('/joint_states', sensor_msgs.msg.JointState, queue_size=1)
00073     self._marker_pub = rospy.Publisher('/object_marker', visualization_msgs.msg.Marker, queue_size=1)
00074     self._timer = rospy.Timer(rospy.Duration(0.1), self.timer_cb)
00075 
00076     self._as_show = actionlib.SimpleActionServer('show_grasps_rviz', cob_grasp_generation.msg.ShowGraspsAction, execute_cb=self.show_cb, auto_start = False)
00077     self._as_show.start()
00078 
00079     print("ShowGraspsRvizServer: action started...")
00080 
00081   def show_cb(self, goal):
00082     success = False
00083 
00084     rospy.loginfo('Show grasp %i for object %s using gripper_type %s' % (goal.grasp_id, goal.object_name, goal.gripper_type))
00085 
00086     if grasp_query_utils.check_database(goal.object_name, goal.gripper_type):
00087       self.marker.mesh_resource = "package://cob_grasp_generation/files/meshes/"+goal.object_name+".stl"
00088       grasp_list = grasp_query_utils.get_grasps(goal.object_name, goal.gripper_type, goal.gripper_side, goal.grasp_id, 1)
00089       if grasp_list:
00090         #print grasp_list
00091         self.js.name = grasp_list[0].pre_grasp_posture.joint_names              #TODO: joint names in urdf are "side-independend"
00092         self.js.position = grasp_list[0].grasp_posture.points[0].positions
00093         for joint in self.joint_mimic:
00094           idx = grasp_list[0].pre_grasp_posture.joint_names.index(joint.mimic.joint)
00095           self.js.name.append(joint.name)
00096           self.js.position.append(grasp_list[0].grasp_posture.points[0].positions[idx])
00097         self.t.transform.translation = grasp_list[0].grasp_pose.pose.position
00098         self.t.transform.rotation = grasp_list[0].grasp_pose.pose.orientation
00099         success = True
00100       else:
00101         success = False
00102     else:
00103       rospy.logerr('GraspTable for Object %s does not exist!' % (goal.object_name))
00104 
00105     result   = cob_grasp_generation.msg.ShowGraspsResult()
00106     result.success = success
00107 
00108     if success:
00109       rospy.loginfo('Show: Succeeded')
00110       self._as_show.set_succeeded(result)
00111     else:
00112       rospy.logwarn('Show: Failed')
00113       self._as_show.set_aborted(result)
00114 
00115   def timer_cb(self, event):
00116     self.t.header.stamp = event.current_real    
00117     self.js.header.stamp = event.current_real
00118     self.marker.header.stamp = event.current_real
00119 
00120     self._br.sendTransform(self.t)
00121     self._js_pub.publish(self.js)
00122     self._marker_pub.publish(self.marker)
00123 
00124 
00125 if __name__ == '__main__':
00126   rospy.init_node('show_grasps_rviz_server')
00127   ShowGraspsRvizServer()
00128   rospy.spin()


cob_grasp_generation
Author(s): Witalij Siebert, Felix Messmer
autogenerated on Thu Jun 6 2019 21:22:47