00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import rospy
00036 from moveit_msgs.msg import CollisionObject, AttachedCollisionObject
00037 from geometry_msgs.msg import PoseStamped, Point
00038 from shape_msgs.msg import SolidPrimitive, Plane, Mesh, MeshTriangle
00039 from exception import MoveItCommanderException
00040 from pyassimp import pyassimp
00041
00042
00043
00044
00045 class PlanningSceneInterface(object):
00046 """ Simple interface to making updates to a planning scene """
00047
00048 def __init__(self):
00049 self._pub_co = rospy.Publisher('/collision_object', CollisionObject)
00050 self._pub_aco = rospy.Publisher('/attached_collision_object', AttachedCollisionObject)
00051
00052 def __make_sphere(self, name, pose, radius):
00053 co = CollisionObject()
00054 co.operation = CollisionObject.ADD
00055 co.id = name
00056 co.header = pose.header
00057 sphere = SolidPrimitive()
00058 sphere.type = SolidPrimitive.SPHERE
00059 sphere.dimensions = [radius]
00060 co.primitives = [sphere]
00061 co.primitive_poses = [pose.pose]
00062 return co
00063
00064 def add_sphere(self, name, pose, radius = 1):
00065 """
00066 Add a sphere to the planning scene
00067 """
00068 self._pub_co.publish(self.__make_sphere(name, pose, radius))
00069
00070 def __make_box(self, name, pose, size):
00071 co = CollisionObject()
00072 co.operation = CollisionObject.ADD
00073 co.id = name
00074 co.header = pose.header
00075 box = SolidPrimitive()
00076 box.type = SolidPrimitive.BOX
00077 box.dimensions = list(size)
00078 co.primitives = [box]
00079 co.primitive_poses = [pose.pose]
00080 return co
00081
00082 def __make_mesh(self, name, pose, filename):
00083 co = CollisionObject()
00084 scene = pyassimp.load(filename)
00085 if not scene.meshes:
00086 raise MoveItCommanderException("There are no meshes in the file")
00087 co.operation = CollisionObject.ADD
00088 co.id = name
00089 co.header = pose.header
00090
00091 mesh = Mesh()
00092 for face in scene.meshes[0].faces:
00093 triangle = MeshTriangle()
00094 if len(face.indices) == 3:
00095 triangle.vertex_indices = [face.indices[0], face.indices[1], face.indices[2]]
00096 mesh.triangles.append(triangle)
00097 for vertex in scene.meshes[0].vertices:
00098 point = Point()
00099 point.x = vertex[0]
00100 point.y = vertex[1]
00101 point.z = vertex[2]
00102 mesh.vertices.append(point)
00103 co.meshes = [mesh]
00104 co.mesh_poses = [pose.pose]
00105 pyassimp.release(scene)
00106 return co
00107
00108 def add_mesh(self, name, pose, filename):
00109 """
00110 Add a mesh to the planning scene
00111 """
00112 self._pub_co.publish(self.__make_mesh(name, pose, filename))
00113
00114 def add_box(self, name, pose, size = (1, 1, 1)):
00115 """
00116 Add a box to the planning scene
00117 """
00118 self._pub_co.publish(self.__make_box(name, pose, size))
00119
00120 def add_plane(self, name, pose, normal = (0, 0, 1), offset = 0):
00121 """ Add a plane to the planning scene """
00122 co = CollisionObject()
00123 co.operation = CollisionObject.ADD
00124 co.id = name
00125 co.header = pose.header
00126 p = Plane()
00127 p.coef = list(normal)
00128 p.coef.append(offset)
00129 co.planes = [p]
00130 co.plane_poses = [pose.pose]
00131 self._pub_co.publish(co)
00132
00133 def attach_mesh(self, link, name, pose, filename, touch_links = []):
00134 aco = AttachedCollisionObject()
00135 aco.object = self.__make_mesh(name, pose, filename)
00136 aco.link_name = link
00137 aco.touch_links = [link]
00138 if len(touch_links) > 0:
00139 aco.touch_links = touch_links
00140 self._pub_aco.publish(aco)
00141
00142 def attach_box(self, link, name, pose, size = (1, 1, 1), touch_links = []):
00143 aco = AttachedCollisionObject()
00144 aco.object = self.__make_box(name, pose, size)
00145 aco.link_name = link
00146 if len(touch_links) > 0:
00147 aco.touch_links = touch_links
00148 else:
00149 aco.touch_links = [link]
00150 self._pub_aco.publish(aco)
00151
00152 def remove_world_object(self, name):
00153 """
00154 Remove object from planning scene
00155 """
00156 co = CollisionObject()
00157 co.operation = CollisionObject.REMOVE
00158 co.id = name
00159 self._pub_co.publish(co)
00160
00161 def remove_attached_object(self, link, name = ''):
00162 """
00163 Remove object from planning scene
00164 """
00165 aco = AttachedCollisionObject()
00166 aco.object.operation = CollisionObject.REMOVE
00167 aco.link_name = link
00168 aco.object.id = name
00169 self._pub_aco.publish(aco)