topic_handler.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import rospy
4 import time
5 import threading
6 
7 from rospy.exceptions import ROSTimeMovedBackwardsException
8 from tf2_msgs.msg import TFMessage
9 
11 
12  def __init__(self):
13  rospy.init_node('vizanti_topic_handler')
14 
15  self.updated = False
16  self.lock = threading.Lock()
17  self.transforms = {}
18 
21 
22  self.tf_sub = rospy.Subscriber('/tf', TFMessage, self.tf_callback)
23  self.tf_pub = rospy.Publisher('/vizanti/tf_consolidated', TFMessage, queue_size=1, latch=True)
24 
25  rospy.loginfo("TF handler ready.")
26 
27  def clear_old_tfs(self):
28  with self.lock:
29  for key in list(self.transforms.keys()):
30  if time.time() - self.transform_timeout[key] > 10.0:
31  parent = self.transforms[key].header.frame_id
32  del self.transforms[key]
33  del self.transform_timeout[key]
34  self.updated = True
35  rospy.logwarn("Deleted old TF link: "+str(parent)+" -> "+str(key))
36 
37  def publish(self):
38  # 150 /30 hz = once per 5 seconds
39  self.timeout_prescaler = (self.timeout_prescaler + 1) % 150
40  if self.timeout_prescaler == 0:
41  self.clear_old_tfs()
42 
43  if not self.updated:
44  return
45 
46  msg = TFMessage()
47  with self.lock:
48  msg.transforms = list(self.transforms.values())
49  self.updated = False
50 
51  self.tf_pub.publish(msg)
52 
53  def tf_callback(self, msg):
54  with self.lock:
55  for transform in msg.transforms:
56  self.transforms[transform.child_frame_id] = transform
57  self.transform_timeout[transform.child_frame_id] = time.time()
58  self.updated = True
59 
60 
61 odr = TopicHandler()
62 rate = rospy.Rate(30)
63 while not rospy.is_shutdown():
64  try:
65  odr.publish()
66  rate.sleep()
67  except ROSTimeMovedBackwardsException as e:
68  print(e)
topic_handler.TopicHandler.__init__
def __init__(self)
Definition: topic_handler.py:12
topic_handler.TopicHandler.clear_old_tfs
def clear_old_tfs(self)
Definition: topic_handler.py:27
topic_handler.TopicHandler
Definition: topic_handler.py:10
topic_handler.TopicHandler.tf_sub
tf_sub
Definition: topic_handler.py:22
topic_handler.TopicHandler.tf_callback
def tf_callback(self, msg)
Definition: topic_handler.py:53
topic_handler.TopicHandler.tf_pub
tf_pub
Definition: topic_handler.py:23
topic_handler.TopicHandler.transforms
transforms
Definition: topic_handler.py:17
topic_handler.TopicHandler.lock
lock
Definition: topic_handler.py:16
topic_handler.TopicHandler.updated
updated
Definition: topic_handler.py:15
topic_handler.TopicHandler.timeout_prescaler
timeout_prescaler
Definition: topic_handler.py:20
topic_handler.TopicHandler.transform_timeout
transform_timeout
Definition: topic_handler.py:19
topic_handler.TopicHandler.publish
def publish(self)
Definition: topic_handler.py:37


vizanti
Author(s): MoffKalast
autogenerated on Wed May 21 2025 02:34:06