tf_broadcast_server.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 import roslib
00003 roslib.load_manifest('tf_broadcast_server')
00004 import rospy
00005 import tf
00006 from tf_broadcast_server.srv import *
00007 
00008 ##
00009 # contains a list of default 'good' frames, loaded in from parameter file
00010 # (has just python code) 
00011 #
00012 # base_link
00013 # torso_lift_link
00014 # r_gripper_tool_frame
00015 # l_gripper_tool_frame
00016 # base_laser_link
00017 # laser_tilt_link
00018 
00019 class TFBroadcastServer:
00020 
00021     def __init__(self, user_frames):
00022         self.broadcaster = tf.TransformBroadcaster()
00023         self.tf_listener = tf.TransformListener()
00024         self.tfdict = {}
00025         self.should_run = True
00026         self.user_frames = user_frames
00027         
00028         self.broadcast_transform_srv = rospy.Service('broadcast_transform', 
00029                 BroadcastTransform, self.broadcast_transform_cb)
00030         self.remove_transform_srv = rospy.Service('stop_broadcast_transform',
00031                 RemoveTransform, self.remove_transform_cb)
00032         self.clear_all_transforms_srv = rospy.Service('clear_all_transforms', 
00033                 ClearTransforms, self.clear_all_transforms_cb)
00034         self.get_transforms_srv = rospy.Service('get_transforms',
00035                 GetTransforms, self.get_transforms_cb)
00036 
00037     def run(self):
00038         r = rospy.Rate(30)
00039         rospy.loginfo('TF broadcast server UP!')
00040         while not rospy.is_shutdown():
00041         #while self.should_run:
00042             for k in self.tfdict.keys():
00043                 posestamped = self.tfdict[k]['pose']
00044                 parent       = self.tfdict[k]['parent']
00045                 name         = k
00046                 p = posestamped.pose.position
00047                 o = posestamped.pose.orientation
00048                 self.broadcaster.sendTransform((p.x, p.y, p.z), 
00049                     (o.x, o.y, o.z, o.w), 
00050                     rospy.Time.now(), name, parent)
00051             #for posestamped, name, parent in self.tfdict:
00052             r.sleep()
00053 
00054     def broadcast_transform_cb(self, req):
00055         rospy.loginfo('Broadcasting frame %s with parent %s and pose %s' % (req.frame_name, req.pose.header.frame_id, str(req.pose)))
00056         self.broadcast_transform(req.pose, req.pose.header.frame_id, req.frame_name)
00057         return BroadcastTransformResponse()
00058 
00059     def remove_transform_cb(self, req):
00060         self.remove_transform(req.frame_name)
00061         return RemoveTransformResponse()
00062 
00063     def clear_all_transforms_cb(self, req):
00064         self.clear_all_transforms()
00065         return ClearTransformsResponse()
00066 
00067     def get_transforms_cb(self, req):
00068         return GetTransformsResponse(self.get_transforms())
00069 
00070     def broadcast_transform(self, pose_stamped, parent, frame_name):
00071         #print pose_stamped, parent, frame_name
00072         self.tfdict[frame_name] = {'parent': parent, 'pose': pose_stamped}
00073 
00074     def remove_transform(self, frame_name):
00075         self.tfdict.pop(frame_name)
00076 
00077     def clear_all_transforms(self):
00078         self.tfdict = {}
00079 
00080     ##
00081     # valid transforms are thse that are declared explicitly and in tfdict
00082     def get_transforms(self):
00083         all_frames = self.tf_listener.getFrameStrings()
00084         valid_transforms = []
00085         tfkeys = ['/' + n for n in self.tfdict.keys()]
00086         frames = set(self.user_frames + tfkeys)
00087         #print all_frames
00088         #print '\n'
00089         #print tfkeys
00090         for T in frames:
00091             if T in all_frames:
00092                 valid_transforms.append(T)
00093         return valid_transforms
00094 
00095 
00096 if __name__ == '__main__':
00097     rospy.init_node('tf_broadcast_server')
00098     user_frames = ['/base_link', '/torso_lift_link', '/r_gripper_tool_frame', 
00099                    '/l_gripper_tool_frame', '/base_laser_link', '/laser_tilt_link', '/map']
00100     server = TFBroadcastServer(user_frames)
00101     server.run()
00102 
00103 


tf_broadcast_server
Author(s): Hai Nguyen (haidai@gmail.com)
autogenerated on Thu Nov 28 2013 11:46:09