ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Wait for subscriber to exist?

asked 2021-11-24 16:01:09 -0600

I would like for one node to wait for a subscriber to a topic to exist before publishing to it. Is there a straightforward way to do that in rclcpp?

I'm developing on rolling if that matters.

edit retag flag offensive close merge delete

Comments

I think @Geoff's answer suffices for your purpose, but FYI there are wait_for_[publishers/subscribers]methods in the RCL layer (refer to https://github.com/ros2/rcl/pull/907/). It would be nice if there were client library (rclcpp/rclpy) wrappers because that fits your use case exactly!

aprotyas gravatar image aprotyas  ( 2021-11-25 04:02:58 -0600 )edit

Some advice: in a robust distributed design, there are almost no good reasons a publishing node needs to know the number of subscribers. Remember you don't control who connects, and message delivery is unreliable. How does your node use this information?

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-11-25 08:09:27 -0600 )edit

The original reason for this was to recreate the behavior of latched topics. I've found another way to do that though.

tyler-picknik gravatar image tyler-picknik  ( 2022-03-02 09:11:08 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-11-24 16:58:58 -0600

Geoff gravatar image

The easiest method I can think of off the top of my head is to check the publisher's get_subscription_count() method before publishing, and skip publishing (or even preparing to publish) if the count is zero. You might also need to check get_intra_process_subscription_count() as well.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-11-24 16:01:09 -0600

Seen: 1,728 times

Last updated: Nov 24 '21