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)
127 rospy.logerr(
"Could not apply planning scene diff.")
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
300 def addCone(self, name, height, radius, x, y, z, use_service=True, frame_id=None):
302 s.dimensions = [height, radius]
307 ps.pose.position.x = x
308 ps.pose.position.y = y
309 ps.pose.position.z = z
310 ps.pose.orientation.w = 1.0
324 def addBox(self, name, size_x, size_y, size_z, x, y, z, use_service=True, frame_id=None):
326 s.dimensions = [size_x, size_y, size_z]
331 ps.pose.position.x = x
332 ps.pose.position.y = y
333 ps.pose.position.z = z
334 ps.pose.orientation.w = 1.0
349 def attachBox(self, name, size_x, size_y, size_z, x, y, z, link_name,
350 touch_links=None, detach_posture=None, weight=0.0,
353 s.dimensions = [size_x, size_y, size_z]
360 p.orientation.w = 1.0
362 o.header.frame_id = link_name
363 a = self.
makeAttached(link_name, o, touch_links, detach_posture, weight)
370 def addCube(self, name, size, x, y, z, use_service=True, frame_id=None):
371 self.
addBox(name, size, size, size, x, y, z, use_service, frame_id)
376 """ Remove an object. """ 377 o = CollisionObject()
378 o.header.stamp = rospy.Time.now()
381 o.operation = o.REMOVE
394 """ Remove an attached object. """ 395 o = AttachedCollisionObject()
396 o.object.operation = CollisionObject.REMOVE
409 """ Recieve updates from move_group. """ 411 for obj
in msg.world.collision_objects:
413 if obj.operation == obj.ADD:
415 rospy.logdebug(
'ObjectManager: Added Collision Obj "%s"',
420 elif obj.operation == obj.REMOVE:
423 rospy.logdebug(
'ObjectManager: Removed Collision Obj "%s"',
428 for obj
in msg.robot_state.attached_collision_objects:
429 rospy.logdebug(
'ObjectManager: attached collision Obj "%s"',
461 rospy.logwarn(
'ObjectManager: %s not removed yet', name)
467 rospy.logwarn(
'ObjectManager: Attached object name: %s not removed yet', name)
473 rospy.logwarn(
'ObjectManager: %s not added yet', name)
478 rospy.logwarn(
'ObjectManager: %s not attached yet', name)
482 if rospy.Time.now() - t > rospy.Duration(max_time):
483 rospy.logerr(
'ObjectManager: sync timed out.')
485 rospy.logdebug(
'ObjectManager: waiting for sync.')
491 color = ObjectColor()
504 for color
in self.
_colors.values():
505 p.object_colors.append(color)
508 rospy.logerr(
"Could not update colors through service, using topic instead.")
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 addCone(self, name, height, radius, x, y, z, use_service=True, frame_id=None)
Insert new cone into 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.