planning_scene_interface.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2008, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 #
00033 # Author: Ioan Sucan, Felix Messmer
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 # This is going to have more functionality; (feel free to add some!)
00043 # This class will include simple Python code for publishing messages for a planning scene
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)


moveit_commander
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 02:24:45