38 from moveit_msgs.msg
import CollisionObject, AttachedCollisionObject
39 from moveit_ros_planning_interface
import _moveit_planning_scene_interface
40 from geometry_msgs.msg
import PoseStamped, Pose, Point
41 from shape_msgs.msg
import SolidPrimitive, Plane, Mesh, MeshTriangle
42 from exception
import MoveItCommanderException
45 from pyassimp
import pyassimp
52 print(
"Failed to import pyassimp, see https://github.com/ros-planning/moveit/issues/86 for more info")
58 """ Simple interface to making updates to a planning scene """ 61 """ Create a planning scene interface; it uses both C++ wrapped methods and scene manipulation topics. """ 62 self.
_psi = _moveit_planning_scene_interface.PlanningSceneInterface(ns)
64 self.
_pub_co = rospy.Publisher(
'/collision_object', CollisionObject, queue_size=100)
65 self.
_pub_aco = rospy.Publisher(
'/attached_collision_object', AttachedCollisionObject, queue_size=100)
68 co = CollisionObject()
69 co.operation = CollisionObject.ADD
71 co.header = pose.header
72 sphere = SolidPrimitive()
73 sphere.type = SolidPrimitive.SPHERE
74 sphere.dimensions = [radius]
75 co.primitives = [sphere]
76 co.primitive_poses = [pose.pose]
81 Add a sphere to the planning scene 86 co = CollisionObject()
87 co.operation = CollisionObject.ADD
89 co.header = pose.header
90 box = SolidPrimitive()
91 box.type = SolidPrimitive.BOX
92 box.dimensions = list(size)
94 co.primitive_poses = [pose.pose]
97 def __make_mesh(self, name, pose, filename, scale = (1, 1, 1)):
98 co = CollisionObject()
100 raise MoveItCommanderException(
"Pyassimp needs patch https://launchpadlibrarian.net/319496602/patchPyassim.txt")
101 scene = pyassimp.load(filename)
102 if not scene.meshes
or len(scene.meshes) == 0:
103 raise MoveItCommanderException(
"There are no meshes in the file")
104 if len(scene.meshes[0].faces) == 0:
105 raise MoveItCommanderException(
"There are no faces in the mesh")
106 co.operation = CollisionObject.ADD
108 co.header = pose.header
111 first_face = scene.meshes[0].faces[0]
112 if hasattr(first_face,
'__len__'):
113 for face
in scene.meshes[0].faces:
115 triangle = MeshTriangle()
116 triangle.vertex_indices = [face[0], face[1], face[2]]
117 mesh.triangles.append(triangle)
118 elif hasattr(first_face,
'indices'):
119 for face
in scene.meshes[0].faces:
120 if len(face.indices) == 3:
121 triangle = MeshTriangle()
122 triangle.vertex_indices = [face.indices[0],
125 mesh.triangles.append(triangle)
127 raise MoveItCommanderException(
"Unable to build triangles from mesh due to mesh object structure")
128 for vertex
in scene.meshes[0].vertices:
130 point.x = vertex[0]*scale[0]
131 point.y = vertex[1]*scale[1]
132 point.z = vertex[2]*scale[2]
133 mesh.vertices.append(point)
135 co.mesh_poses = [pose.pose]
136 pyassimp.release(scene)
141 Create an empty Collision Object, used when the object already exists 143 co = CollisionObject()
147 def add_mesh(self, name, pose, filename, size = (1, 1, 1)):
149 Add a mesh to the planning scene 151 self._pub_co.publish(self.
__make_mesh(name, pose, filename, size))
153 def add_box(self, name, pose, size = (1, 1, 1)):
155 Add a box to the planning scene 157 self._pub_co.publish(self.
__make_box(name, pose, size))
159 def add_plane(self, name, pose, normal = (0, 0, 1), offset = 0):
160 """ Add a plane to the planning scene """ 161 co = CollisionObject()
162 co.operation = CollisionObject.ADD
164 co.header = pose.header
166 p.coef = list(normal)
167 p.coef.append(offset)
169 co.plane_poses = [pose.pose]
170 self._pub_co.publish(co)
172 def attach_mesh(self, link, name, pose = None, filename = '', size = (1, 1, 1), touch_links = []):
173 aco = AttachedCollisionObject()
174 if pose!=
None and filename:
175 aco.object = self.
__make_mesh(name, pose, filename, size)
179 aco.touch_links = [link]
180 if len(touch_links) > 0:
181 aco.touch_links = touch_links
182 self._pub_aco.publish(aco)
184 def attach_box(self, link, name, pose = None, size = (1, 1, 1), touch_links = []):
185 aco = AttachedCollisionObject()
187 aco.object = self.
__make_box(name, pose, size)
191 if len(touch_links) > 0:
192 aco.touch_links = touch_links
194 aco.touch_links = [link]
195 self._pub_aco.publish(aco)
199 Remove an object from planning scene, or all if no name is provided 201 co = CollisionObject()
202 co.operation = CollisionObject.REMOVE
205 self._pub_co.publish(co)
209 Remove an attached object from planning scene, or all objects attached to this link if no name is provided 211 aco = AttachedCollisionObject()
212 aco.object.operation = CollisionObject.REMOVE
216 self._pub_aco.publish(aco)
220 Get the names of all known objects in the world. If with_type is set to true, only return objects that have a known type. 222 return self._psi.get_known_object_names(with_type)
226 Get the names of known objects in the world that are located within a bounding region (specified in the frame reported by 227 get_planning_frame()). If with_type is set to true, only return objects that have a known type. 229 return self._psi.get_known_object_names_in_roi(minx, miny, minz, maxx, maxy, maxz, with_type)
233 Get the poses from the objects identified by the given object ids list. 235 ser_ops = self._psi.get_object_poses(object_ids)
239 conversions.msg_from_string(msg, ser_ops[key])
245 Get the objects identified by the given object ids list. If no ids are provided, return all the known objects. 247 ser_objs = self._psi.get_objects(object_ids)
250 msg = CollisionObject()
251 conversions.msg_from_string(msg, ser_objs[key])
257 Get the attached objects identified by the given object ids list. If no ids are provided, return all the attached objects. 259 ser_aobjs = self._psi.get_attached_objects(object_ids)
261 for key
in ser_aobjs:
262 msg = AttachedCollisionObject()
263 conversions.msg_from_string(msg, ser_aobjs[key])
def add_box(self, name, pose, size=(1, 1, 1))
def get_attached_objects(self, object_ids=[])
def add_mesh(self, name, pose, filename, size=(1, 1, 1))
def __make_sphere(self, name, pose, radius)
def __make_box(self, name, pose, size)
def attach_mesh(self, link, name, pose=None, filename='', size=(1, 1, 1), touch_links=[])
def get_known_object_names_in_roi(self, minx, miny, minz, maxx, maxy, maxz, with_type=False)
def add_plane(self, name, pose, normal=(0, 0, 1), offset=0)
def __init__(self, ns='')
def get_object_poses(self, object_ids)
def __make_existing(self, name)
def __make_mesh(self, name, pose, filename, scale=(1, 1, 1))
def attach_box(self, link, name, pose=None, size=(1, 1, 1), touch_links=[])
def get_known_object_names(self, with_type=False)
def remove_attached_object(self, link, name=None)
def add_sphere(self, name, pose, radius=1)
def remove_world_object(self, name=None)
def get_objects(self, object_ids=[])