Understanding ROS 2 publisher size, subscriber size, and topic size
(I asked this question in robotics.stackexchange.com but did not received any answer! That is why asking in here with some modifications.) I have the following ROS 2 scripts for a publisher node:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import time
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 1)
self.timer1 = self.create_timer(1, self.timer1_callback)
self.timer2 = self.create_timer(2, self.timer2_callback)
self.timer3 = self.create_timer(3, self.timer3_callback)
self.i = 1
self.j = 1
self.k = 1
def timer3_callback(self):
msg = String()
msg.data = 'timer3: %d' % self.k
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.k += 1
def timer2_callback(self):
msg = String()
msg.data = 'timer2: %d' % self.j
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.j += 1
def timer1_callback(self):
msg = String()
msg.data = 'timer1: %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1
# self.create_rate(4).sleep()
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
time.sleep(2)
msg = String()
msg.data = 'Publishing garbage 1'
minimal_publisher.publisher_.publish(msg)
msg = String()
msg.data = 'Publishing garbage 2'
minimal_publisher.publisher_.publish(msg)
msg = String()
msg.data = 'Publishing garbage 3'
minimal_publisher.publisher_.publish(msg)
msg = String()
msg.data = 'Publishing garbage 4'
minimal_publisher.publisher_.publish(msg)
msg = String()
msg.data = 'Publishing garbage 5'
minimal_publisher.publisher_.publish(msg)
msg = String()
msg.data = 'Publishing garbage 6'
minimal_publisher.publisher_.publish(msg)
time.sleep(3)
# rate = minimal_publisher.create_rate(5)
# rate.sleep()
try:
rclpy.spin(minimal_publisher)
except KeyboardInterrupt:
pass
# minimal_publisher.destroy_node()
# rclpy.shutdown()
if __name__ == '__main__':
main()
I used timer to hold after publishing some garbage messages, and then started spinning using 3 timers. I have a simple subscriber node as follows:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import time
class MinimalSubscriber(Node):
def __init__(self):
super().__init__('minimal_subscriber')
self.subscription = self.create_subscription(String,'topic',self.listener_callback, 4)
self.subscription # prevent unused variable warning
def listener_callback(self, msg):
self.get_logger().info('I heard: "%s"' % msg.data)
def main(args=None):
rclpy.init(args=args)
minimal_subscriber = MinimalSubscriber()
# delay = minimal_subscriber.create_rate(2.5)
rclpy.spin(minimal_subscriber)
if __name__ == '__main__':
main()
I used a launch file to run both publisher and subscriber at once. I did some experiments by varying the publisher and subscriber queue size in the above code in order to understand topic size, publisher queue, subscriber queue as follows:
Experiment 1: pub queue size 1, sub queue size 1. Results: Only the 6th garbage message is shown. After that every message is received from all 3 timers (5 garbage messages lost)
Experiment 2: pub queue size 100, sub queue size 1. Results: EXACTLY SAME AS EXPERIMENT 1 (also checked for pub size 10, 5, 6 and results are same)
Experiment 3: pub queue size 1, sub queue size 100. Results: All 6 garbage messages are shown (nothing lost). After that every message is received from ...