Skip to content

Commit cda1b48

Browse files
committed
feat: enable to wait while topic published
Signed-off-by: yoshiri <[email protected]>
1 parent 25a769f commit cda1b48

File tree

1 file changed

+34
-19
lines changed
  • common/autoware_debug_tools/autoware_debug_tools/processing_time_visualizer

1 file changed

+34
-19
lines changed

common/autoware_debug_tools/autoware_debug_tools/processing_time_visualizer/node.py

+34-19
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020

2121
class ProcessingTimeVisualizer(Node):
22-
def __init__(self, topic_name=None):
22+
def __init__(self, topic_waiting_sec=1.0, topic_name=None):
2323
super().__init__("processing_time_visualizer" + str(uuid.uuid4()).replace("-", "_"))
2424
self.topic_name = topic_name
25+
self.topic_waiting_sec = topic_waiting_sec
2526
self.subscriber = self.subscribe_processing_time_tree()
2627
self.quit_option = None
2728
self.trees: Dict[str, ProcessingTimeTree] = {}
@@ -34,16 +35,25 @@ def __init__(self, topic_name=None):
3435

3536
self.create_timer(0.1, self.update_screen)
3637

38+
def search_for_topic(self, topic_name: str) -> bool:
39+
for name, topic_types in self.get_topic_names_and_types():
40+
if name == topic_name:
41+
return True
42+
return False
43+
44+
def wait_for_topics(self, condition_func, wait_sec: float) -> bool:
45+
s = time.time()
46+
while time.time() - s < wait_sec:
47+
if condition_func():
48+
return True
49+
return False
50+
3751
def subscribe_processing_time_tree(self):
52+
# if topic name is specified with -t option
3853
if self.topic_name:
39-
topic_found = False
40-
for topic_name, topic_types in self.get_topic_names_and_types():
41-
if (
42-
topic_name == self.topic_name
43-
and "tier4_debug_msgs/msg/ProcessingTimeTree" in topic_types
44-
):
45-
topic_found = True
46-
break
54+
topic_found = self.wait_for_topics(
55+
lambda: self.search_for_topic(self.topic_name), self.topic_waiting_sec
56+
)
4757

4858
if not topic_found:
4959
self.get_logger().info(f"Specified topic '{self.topic_name}' not found.")
@@ -56,22 +66,22 @@ def subscribe_processing_time_tree(self):
5666
self.callback,
5767
10,
5868
)
59-
else:
69+
else: # if topic name is not specified
6070
topics = []
61-
s = time.time()
62-
while True:
63-
for topic_name, topic_types in self.get_topic_names_and_types():
71+
72+
def condition_func():
73+
for name, topic_types in self.get_topic_names_and_types():
6474
for topic_type in topic_types:
6575
if (
6676
topic_type == "tier4_debug_msgs/msg/ProcessingTimeTree"
67-
and topic_name not in topics
77+
and name not in topics
6878
):
69-
topics.append(topic_name)
79+
topics.append(name)
80+
return len(topics) > 0
7081

71-
if time.time() - s > 1.0:
72-
break
82+
topic_found = self.wait_for_topics(condition_func, self.topic_waiting_sec)
7383

74-
if len(topics) == 0:
84+
if not topic_found:
7585
self.get_logger().info("No ProcessingTimeTree topic found")
7686
self.get_logger().info("Exiting...")
7787
exit(1)
@@ -132,11 +142,16 @@ def callback(self, msg: ProcessingTimeTreeMsg):
132142
def main(args=None):
133143
parser = argparse.ArgumentParser(description="Processing Time Visualizer")
134144
parser.add_argument("-t", "--topic", type=str, help="Specify the topic name to subscribe to")
145+
parser.add_argument(
146+
"-w", "--waiting-sec", type=float, default=1.0, help="Waiting time for topic"
147+
)
135148
parsed_args = parser.parse_args(args)
136149

137150
rclpy.init(args=args)
138151
try:
139-
node = ProcessingTimeVisualizer(topic_name=parsed_args.topic)
152+
node = ProcessingTimeVisualizer(
153+
topic_name=parsed_args.topic, topic_waiting_sec=parsed_args.waiting_sec
154+
)
140155
except KeyboardInterrupt:
141156
exit_curses()
142157
return

0 commit comments

Comments
 (0)