28 import threading, copy
32 from pyassimp
import pyassimp
50 from moveit_msgs.msg
import CollisionObject, AttachedCollisionObject
51 from moveit_msgs.msg
import PlanningScene, PlanningSceneComponents, ObjectColor
52 from moveit_msgs.srv
import GetPlanningScene, ApplyPlanningScene
53 from shape_msgs.msg
import MeshTriangle, Mesh, SolidPrimitive, Plane
62 def __init__(self, frame, ns='', init_from_service=True):
64 if not isinstance(ns, basestring):
65 rospy.logerr(
'Namespace must be a string!')
67 elif not ns.endswith(
'/'):
75 self.
_apply_service = rospy.ServiceProxy(ns +
'apply_planning_scene', ApplyPlanningScene)
90 rospy.loginfo(
'Waiting for get_planning_scene')
91 rospy.wait_for_service(ns +
'get_planning_scene')
92 self.
_service = rospy.ServiceProxy(ns +
'get_planning_scene',
95 req = PlanningSceneComponents()
96 req.components = sum([
97 PlanningSceneComponents.WORLD_OBJECT_NAMES,
98 PlanningSceneComponents.WORLD_OBJECT_GEOMETRY,
99 PlanningSceneComponents.ROBOT_STATE_ATTACHED_OBJECTS])
101 self.
sceneCb(scene.scene, initial=
True)
102 except rospy.ServiceException
as e:
103 rospy.logerr(
'Failed to get initial planning scene, results may be wonky: %s', e)
106 rospy.Subscriber(ns +
'move_group/monitored_planning_scene',
114 def sendUpdate(self, collision_object, attached_collision_object, use_service=True):
117 ps.robot_state.is_diff =
True 119 ps.world.collision_objects.append(collision_object)
121 if attached_collision_object:
122 ps.robot_state.attached_collision_objects.append(attached_collision_object)
125 resp = self._apply_service.call(ps)
127 rospy.logerr(
"Could not apply planning scene diff.")
129 self._scene_pub.publish(ps)
145 rospy.logerr(
'pyassimp is broken on your platform, cannot load meshes')
147 scene = pyassimp.load(filename)
149 rospy.logerr(
'Unable to load mesh')
153 for face
in scene.meshes[0].faces:
154 triangle = MeshTriangle()
155 if hasattr(face,
'indices'):
156 if len(face.indices) == 3:
157 triangle.vertex_indices = [face.indices[0],
162 triangle.vertex_indices = [face[0],
165 mesh.triangles.append(triangle)
166 for vertex
in scene.meshes[0].vertices:
171 mesh.vertices.append(point)
172 pyassimp.release(scene)
174 o = CollisionObject()
175 o.header.stamp = rospy.Time.now()
178 o.meshes.append(mesh)
179 o.mesh_poses.append(pose)
189 o = CollisionObject()
190 o.header.stamp = rospy.Time.now()
192 o.header.frame_id = frame_id
196 o.primitives.append(solid)
197 o.primitive_poses.append(pose)
202 def makeAttached(self, link_name, obj, touch_links, detach_posture,
204 o = AttachedCollisionObject()
205 o.link_name = link_name
208 o.touch_links = touch_links
210 o.detach_posture = detach_posture
219 def addMesh(self, name, pose, filename, use_service=True):
220 o = self.
makeMesh(name, pose, filename)
229 def attachMesh(self, name, pose, filename, link_name, touch_links=None,
230 detach_posture=
None, weight=0.0, use_service=
True):
231 o = self.
makeMesh(name, pose, filename)
232 o.header.frame_id = link_name
233 a = self.
makeAttached(link_name, o, touch_links, detach_posture,
257 def addCylinder(self, name, height, radius, x, y, z, use_service=True, frame_id=None):
259 s.dimensions = [height, radius]
264 ps.pose.position.x = x
265 ps.pose.position.y = y
266 ps.pose.position.z = z
267 ps.pose.orientation.w = 1.0
278 def addSphere(self, name, radius, x, y, z, use_service=True, frame_id=None):
280 s.dimensions = [radius]
285 ps.pose.position.x = x
286 ps.pose.position.y = y
287 ps.pose.position.z = z
288 ps.pose.orientation.w = 1.0
302 def addBox(self, name, size_x, size_y, size_z, x, y, z, use_service=True, frame_id=None):
304 s.dimensions = [size_x, size_y, size_z]
309 ps.pose.position.x = x
310 ps.pose.position.y = y
311 ps.pose.position.z = z
312 ps.pose.orientation.w = 1.0
327 def attachBox(self, name, size_x, size_y, size_z, x, y, z, link_name,
328 touch_links=
None, detach_posture=
None, weight=0.0,
331 s.dimensions = [size_x, size_y, size_z]
338 p.orientation.w = 1.0
340 o.header.frame_id = link_name
341 a = self.
makeAttached(link_name, o, touch_links, detach_posture, weight)
348 def addCube(self, name, size, x, y, z, use_service=True, frame_id=None):
349 self.
addBox(name, size, size, size, x, y, z, use_service, frame_id)
354 """ Remove an object. """ 355 o = CollisionObject()
356 o.header.stamp = rospy.Time.now()
359 o.operation = o.REMOVE
372 """ Remove an attached object. """ 373 o = AttachedCollisionObject()
374 o.object.operation = CollisionObject.REMOVE
387 """ Recieve updates from move_group. """ 388 self._mutex.acquire()
389 for obj
in msg.world.collision_objects:
391 if obj.operation == obj.ADD:
392 self._collision.append(obj.id)
393 rospy.logdebug(
'ObjectManager: Added Collision Obj "%s"',
398 elif obj.operation == obj.REMOVE:
399 self._collision.remove(obj.id)
400 self._removed.pop(obj.id,
None)
401 rospy.logdebug(
'ObjectManager: Removed Collision Obj "%s"',
406 for obj
in msg.robot_state.attached_collision_objects:
407 rospy.logdebug(
'ObjectManager: attached collision Obj "%s"',
409 self._attached.append(obj.object.id)
413 self._mutex.release()
417 self._mutex.acquire()
419 self._mutex.release()
424 self._mutex.acquire()
426 self._mutex.release()
437 if name
in self._removed.keys():
439 rospy.logwarn(
'ObjectManager: %s not removed yet', name)
443 if name
in self._attached_removed.keys():
445 rospy.logwarn(
'ObjectManager: Attached object name: %s not removed yet', name)
449 for name
in self._objects.keys():
451 rospy.logwarn(
'ObjectManager: %s not added yet', name)
454 for name
in self._attached_objects.keys():
456 rospy.logwarn(
'ObjectManager: %s not attached yet', name)
460 if rospy.Time.now() - t > rospy.Duration(max_time):
461 rospy.logerr(
'ObjectManager: sync timed out.')
463 rospy.logdebug(
'ObjectManager: waiting for sync.')
469 color = ObjectColor()
482 for color
in self._colors.values():
483 p.object_colors.append(color)
484 resp = self._apply_service.call(p)
486 rospy.logerr(
"Could not update colors through service, using topic instead.")
487 self._scene_pub.publish(p)
def sendUpdate(self, collision_object, attached_collision_object, use_service=True)
Send new update to planning scene.
def makeSolidPrimitive(self, name, solid, pose, frame_id=None)
Make a solid primitive collision object.
def removeCollisionObject(self, name, use_service=True)
Send message to remove object.
def addCylinder(self, name, height, radius, x, y, z, use_service=True, frame_id=None)
Insert new cylinder into planning scene.
def sceneCb(self, msg, initial=False)
Update the object lists from a PlanningScene message.
def addSolidPrimitive(self, name, solid, pose, use_service=True, frame_id=None)
Insert a solid primitive into planning scene.
def getKnownCollisionObjects(self)
Get a list of names of collision objects.
def attachMesh(self, name, pose, filename, link_name, touch_links=None, detach_posture=None, weight=0.0, use_service=True)
Attach a mesh into the planning scene.
def sendColors(self)
Actually send the colors to MoveIt!
def waitForSync(self, max_time=2.0)
Wait for sync.
def addCube(self, name, size, x, y, z, use_service=True, frame_id=None)
Insert new cube to planning scene.
def clear(self)
Clear the planning scene of all objects.
def setColor(self, name, r, g, b, a=0.9)
Set the color of an object.
def addSphere(self, name, radius, x, y, z, use_service=True, frame_id=None)
Insert new sphere into planning scene.
def getKnownAttachedObjects(self)
Get a list of names of attached objects.
def makeMesh(self, name, pose, filename)
Make a mesh collision object.
def attachBox(self, name, size_x, size_y, size_z, x, y, z, link_name, touch_links=None, detach_posture=None, weight=0.0, use_service=True)
Attach a box into the planning scene.
def makeAttached(self, link_name, obj, touch_links, detach_posture, weight)
Make an attachedCollisionObject.
def addMesh(self, name, pose, filename, use_service=True)
Insert a mesh into the planning scene.
def addBox(self, name, size_x, size_y, size_z, x, y, z, use_service=True, frame_id=None)
Insert new box into planning scene.
def __init__(self, frame, ns='', init_from_service=True)
A class for managing the state of the planning scene.
def removeAttachedObject(self, name, use_service=True)
Send message to remove object.