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

Revision history [back]

Hi,

ROS takes some time to notify the publishers and subscribers of the topics. So, you have two options:

1 - Sleep some seconds before publish the first message

2 - Make sure that there are subscribers when you starts publishing.

I made a video (https://youtu.be/BGV6DI_PItA) that shows how to solve exactly this question by checking the subscribers.

Hi,Hi @j1337,

ROS takes some time to notify the publishers and subscribers of the topics. So, you have two options:

1 - Sleep some seconds before publish the first message

message. In the "main" part of your code could be done like:

if __name__ == "__main__":
    try:

        from time import sleep
        print 'Sleeping 10 seconds to publish'
        sleep(10)
        print 'Sleep finished.'

        m = PoseChange()
        m.run()
    except rospy.ROSInterruptException:
        raise e

2 - Make sure that there are subscribers (using get_num_connections) when you starts publishing.

publishing. In the "main" part of your code it would be like:

if __name__ == "__main__":
    try:
        m = PoseChange()
        rate = rospy.Rate(10)  # 10hz
        while not rospy.is_shutdown():
            connections = m.pub.get_num_connections()
            rospy.loginfo('Connections: %d', connections)
            if connections > 0:
                m.run()
                rospy.loginfo('Published')
                break
            rate.sleep()

    except rospy.ROSInterruptException, e:
        raise e

If you still have doubts, I made a video (https://youtu.be/BGV6DI_PItA) that shows how to solve exactly this question by checking the subscribers.