@@ -24,7 +24,7 @@ class SyncStatus(enum.Enum):
2424
2525class Comm_node :
2626 def __init__ (self , this_node , client_node , robot_configs ,
27- client_callback , server_callback , client_timeout , node = None ):
27+ client_callback , server_callback , client_timeout , ros_node ):
2828 # Check input arguments
2929 assert isinstance (this_node , str )
3030 assert isinstance (client_node , str )
@@ -33,13 +33,11 @@ def __init__(self, this_node, client_node, robot_configs,
3333 assert callable (server_callback )
3434 assert isinstance (client_timeout , (int , float ))
3535
36- # Store or create the ROS2 node
37- self .node = node
38- if self .node is None :
39- # Create a minimal logger if no node provided
40- self .logger = rclpy .logging .get_logger ('zmq_comm_node' )
41- else :
42- self .logger = self .node .get_logger ()
36+ # Confirm that we are providing a ROS node
37+ assert ros_node is not None
38+ self .ros_node = ros_node
39+ self .logger = self .ros_node .get_logger ()
40+ self .ros_node_name = self .ros_node .get_fully_qualified_name ()
4341
4442 # Check that this_node and client_node exist in the config file
4543 if this_node not in robot_configs :
@@ -50,7 +48,6 @@ def __init__(self, this_node, client_node, robot_configs,
5048 if client_node not in robot_configs [self .this_node ]["clients" ]:
5149 self .logger .error (f"{ this_node } - Node: client_node not in config file" )
5250 raise ValueError ("client_node not in config file" )
53-
5451 self .client_node = client_node
5552
5653 self .robot_configs = robot_configs
@@ -72,14 +69,11 @@ def __init__(self, this_node, client_node, robot_configs,
7269 self .client_timeout = client_timeout
7370
7471 # Create a publisher for the client bandwidth
75- if self .node is not None :
76- self .pub_client_stats = self .node .create_publisher (
77- mocha_core .msg .ClientStats ,
78- f"ddb/client_stats/{ self .client_node } " ,
79- 10
80- )
81- else :
82- self .pub_client_stats = None
72+ self .pub_client_stats = self .ros_node .create_publisher (
73+ mocha_core .msg .ClientStats ,
74+ f"{ self .ros_node_name } /client_stats/{ self .client_node } " ,
75+ 10
76+ )
8377 self .pub_client_count = 0
8478
8579 self .syncStatus = SyncStatus .IDLE
@@ -134,11 +128,8 @@ def connect_send_message(self, msg):
134128 self .logger .debug (f"{ self .this_node } - Node - SENDMSG: " +
135129 f"Sending ({ full_msg } )" )
136130 client .send (full_msg )
137- if self .node is not None :
138- start_ts = self .node .get_clock ().now ()
139- else :
140- import time
141- start_ts = time .time ()
131+ start_ts = self .ros_node .get_clock ().now ()
132+
142133 # Wait at most self.client_timeout * 1000 ms
143134 socks = dict (poll .poll (self .client_timeout * 1000 ))
144135 if socks .get (client ) == zmq .POLLIN :
@@ -157,29 +148,22 @@ def connect_send_message(self, msg):
157148 f"{ self .this_node } - Node - SENDMSG: Server replied " +
158149 f"({ len (reply )} bytes)"
159150 )
160- if self .node is not None :
161- stop_ts = self .node .get_clock ().now ()
162- time_d = (stop_ts - start_ts ).nanoseconds
163- time_s = float (time_d / 1e9 )
164- else :
165- import time
166- stop_ts = time .time ()
167- time_s = stop_ts - start_ts
168-
151+ stop_ts = self .ros_node .get_clock ().now ()
152+ time_d = (stop_ts - start_ts ).nanoseconds
153+ time_s = float (time_d / 1e9 )
154+
169155 bw = len (reply )/ time_s / 1024 / 1024
170-
171- if self .pub_client_stats is not None :
172- stats = mocha_core .msg .ClientStats ()
173- if self .node is not None :
174- stats .header .stamp = self .node .get_clock ().now ().to_msg ()
175- stats .header .frame_id = self .this_node
176- # Note: ROS2 doesn't have seq field in header
177- stats .msg = msg [:5 ].decode ("utf-8" )
178- stats .rtt = time_s
179- stats .bw = bw
180- stats .answ_len = len (reply )
181- self .pub_client_stats .publish (stats )
182- self .pub_client_count += 1
156+
157+ stats = mocha_core .msg .ClientStats ()
158+ stats .header .stamp = self .ros_node .get_clock ().now ().to_msg ()
159+ stats .header .frame_id = self .this_node
160+ # Note: ROS2 doesn't have seq field in header
161+ stats .msg = msg [:5 ].decode ("utf-8" )
162+ stats .rtt = time_s
163+ stats .bw = bw
164+ stats .answ_len = len (reply )
165+ self .pub_client_stats .publish (stats )
166+ self .pub_client_count += 1
183167 if len (reply ) > 10 * 1024 and SHOW_BANDWIDTH :
184168 self .logger .info (f"{ self .this_node } - Node - " +
185169 f"SENDMSG: Data RTT: { time_s } " )
0 commit comments