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

Revision history [back]

If you want a particular callback for a topic only to be run for the first message received, you can unsubscribe from within the callback after you've finished your processing. For example:

def cb_once(msg, subscriber):
    #do processing here
    subscriber.unregister()

sub_once = None
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once, sub_once)

In your case, however, you could have a single callback, and just keep a flag around like wrote_header, which you can set to true after the first message was received so you only write the header once.

If you want a particular callback for a topic only to be run for the first message received, you can unsubscribe from within the callback after you've finished your processing. For example:

def cb_once(msg, subscriber):
    #do processing here
    subscriber.unregister()

sub_once = None
sub_once = rospy.Subscriber('my_topic,', MyType, cb_once, sub_once)

In your case, however, you could have a single callback, and just keep a flag around like wrote_header, wrote_header, which you can set to true after the first message was received so you only write the header once.