19
19
20
20
21
21
class ProcessingTimeVisualizer (Node ):
22
- def __init__ (self , topic_name = None ):
22
+ def __init__ (self , topic_waiting_sec = 1.0 , topic_name = None ):
23
23
super ().__init__ ("processing_time_visualizer" + str (uuid .uuid4 ()).replace ("-" , "_" ))
24
24
self .topic_name = topic_name
25
+ self .topic_waiting_sec = topic_waiting_sec
25
26
self .subscriber = self .subscribe_processing_time_tree ()
26
27
self .quit_option = None
27
28
self .trees : Dict [str , ProcessingTimeTree ] = {}
@@ -34,16 +35,25 @@ def __init__(self, topic_name=None):
34
35
35
36
self .create_timer (0.1 , self .update_screen )
36
37
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
+
37
51
def subscribe_processing_time_tree (self ):
52
+ # if topic name is specified with -t option
38
53
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
+ )
47
57
48
58
if not topic_found :
49
59
self .get_logger ().info (f"Specified topic '{ self .topic_name } ' not found." )
@@ -56,22 +66,22 @@ def subscribe_processing_time_tree(self):
56
66
self .callback ,
57
67
10 ,
58
68
)
59
- else :
69
+ else : # if topic name is not specified
60
70
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 ():
64
74
for topic_type in topic_types :
65
75
if (
66
76
topic_type == "tier4_debug_msgs/msg/ProcessingTimeTree"
67
- and topic_name not in topics
77
+ and name not in topics
68
78
):
69
- topics .append (topic_name )
79
+ topics .append (name )
80
+ return len (topics ) > 0
70
81
71
- if time .time () - s > 1.0 :
72
- break
82
+ topic_found = self .wait_for_topics (condition_func , self .topic_waiting_sec )
73
83
74
- if len ( topics ) == 0 :
84
+ if not topic_found :
75
85
self .get_logger ().info ("No ProcessingTimeTree topic found" )
76
86
self .get_logger ().info ("Exiting..." )
77
87
exit (1 )
@@ -132,11 +142,16 @@ def callback(self, msg: ProcessingTimeTreeMsg):
132
142
def main (args = None ):
133
143
parser = argparse .ArgumentParser (description = "Processing Time Visualizer" )
134
144
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
+ )
135
148
parsed_args = parser .parse_args (args )
136
149
137
150
rclpy .init (args = args )
138
151
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
+ )
140
155
except KeyboardInterrupt :
141
156
exit_curses ()
142
157
return
0 commit comments