10 import roslib; roslib.load_manifest(
'dynamic_tf_publisher')
15 from std_srvs.srv
import Empty, EmptyResponse
19 from threading
import Lock
25 rospy.wait_for_service(name, 1.0)
26 rospy.logfatal(
"%s is already exists" % (name))
28 except rospy.ROSException, e:
29 rospy.Service(name, srv, callback)
31 self.
pub_tf = rospy.Publisher(
"/tf", tf.msg.tfMessage, queue_size=1)
32 self.
pub_tf_mine = rospy.Publisher(
"~tf", tf.msg.tfMessage, queue_size=1)
40 self.
use_cache = rospy.get_param(
'~use_cache',
True)
45 if self.
use_cache and rospy.has_param(
'dynamic_tf_publisher'+rospy.get_name()) :
46 tfm = tf.msg.tfMessage()
47 roslib.message.fill_message_args(tfm,[yaml.load(rospy.get_param(
'dynamic_tf_publisher'+rospy.get_name()))])
48 for pose
in tfm.transforms :
49 self.
cur_tf[pose.child_frame_id] = pose
58 time = rospy.Time.now()
59 tfm = tf.msg.tfMessage()
67 for frame_id
in self.cur_tf.keys():
69 pose = self.
cur_tf[frame_id]
70 pose.header.stamp = time
71 tfm.transforms.append(pose)
74 if len(tfm.transforms) > 0:
75 self.pub_tf.publish(tfm)
76 self.pub_tf_mine.publish(tfm)
77 return EmptyResponse()
80 if (
not self.cur_tf.has_key(req.child_frame))
or self.
cur_tf[req.child_frame] == req.parent_frame:
81 rospy.logwarn(
"unkown key %s" % (req.child_frame))
82 return AssocTFResponse()
83 rospy.loginfo(
"assoc %s -> %s"%(req.parent_frame, req.child_frame))
84 self.listener.waitForTransform(req.parent_frame,
86 req.header.stamp, rospy.Duration(1.0))
87 ts = TransformStamped()
88 (trans,rot) = self.listener.lookupTransform(req.parent_frame, req.child_frame, req.header.stamp)
89 ts.transform.translation = Vector3(*trans)
90 ts.transform.rotation = Quaternion(*rot)
91 ts.header.stamp = req.header.stamp
92 ts.header.frame_id = req.parent_frame
93 ts.child_frame_id = req.child_frame
96 self.
cur_tf[req.child_frame] = ts
99 return AssocTFResponse()
103 rospy.loginfo(
"dissoc TF %s" % (req.frame_id))
105 if self.original_parent.has_key(req.frame_id):
106 areq = AssocTFRequest()
107 areq.header = req.header
108 areq.child_frame = req.frame_id
112 self.original_parent.pop(req.frame_id)
113 return DissocTFResponse()
116 rospy.loginfo(
"delete TF %s"%(req.header.frame_id))
118 if self.original_parent.has_key(req.header.frame_id):
120 if self.cur_tf.has_key(req.header.frame_id):
121 del self.
cur_tf[req.header.frame_id]
122 if self.update_tf.has_key(req.header.frame_id):
124 return DeleteTFResponse()
127 rospy.loginfo(
"%s => %s" % (req.cur_tf.header.frame_id, req.cur_tf.child_frame_id))
130 if not self.original_parent.has_key(req.cur_tf.child_frame_id):
132 self.
cur_tf[req.cur_tf.child_frame_id] = req.cur_tf
133 self.
update_tf[req.cur_tf.child_frame_id] =
True 134 rospy.loginfo(
"Latch [%s]/[%shz]"%(req.cur_tf.child_frame_id,req.freq))
137 time = rospy.Time.now()
138 tfm = tf.msg.tfMessage()
139 for frame_id
in self.cur_tf.keys():
140 pose = self.
cur_tf[frame_id]
141 pose.header.stamp = time
142 tfm.transforms.append(pose)
143 rospy.set_param(
'dynamic_tf_publisher'+rospy.get_name(),tfm.__str__())
144 return SetDynamicTFResponse()
151 if __name__ ==
"__main__":
153 rospy.init_node(
'tf_publish_server')
155 while not rospy.is_shutdown():
156 pub.publish_and_sleep()
157 except rospy.ROSInterruptException:
def advertiseServiceUnlessFound(self, name, srv, callback)
def publish_tf(self, req=None)
def publish_and_sleep(self)