From 5e807e6e080e34df2c681fa723f360b280e1e75a Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Mon, 19 Apr 2021 16:04:19 +0900 Subject: [PATCH] `ros2 topic pub` starts publishing right away. Signed-off-by: Tomoya Fujita --- ros2topic/ros2topic/verb/pub.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/ros2topic/ros2topic/verb/pub.py b/ros2topic/ros2topic/verb/pub.py index 9a46b632c..346c9d0c9 100644 --- a/ros2topic/ros2topic/verb/pub.py +++ b/ros2topic/ros2topic/verb/pub.py @@ -188,11 +188,16 @@ def timer_callback(): print('publishing #%d: %r\n' % (count, msg)) pub.publish(msg) - timer = node.create_timer(period, timer_callback) - while times == 0 or count < times: - rclpy.spin_once(node) - - # give some time for the messages to reach the wire before exiting - time.sleep(keep_alive) - - node.destroy_timer(timer) + # give some time for discovery process + time.sleep(0.1) + timer_callback() + if times != 1: + timer = node.create_timer(period, timer_callback) + while times == 0 or count < times: + rclpy.spin_once(node) + # give some time for the messages to reach the wire before exiting + time.sleep(keep_alive) + node.destroy_timer(timer) + else: + # give some time for the messages to reach the wire before exiting + time.sleep(keep_alive)