Ask Your Question

Rosbag: play on demand service

asked 2013-02-27 16:23:52 -0600

TRS gravatar image

updated 2013-02-27 20:17:31 -0600

Is there a way to get rosbag to publish the next message by calling a service. A use-case is that a node request the rosbag player to publish the next message without a fixed hz, but when the node is ready to receive new info. This will allow for a lot of flexibility during the development of complex systems.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-02-28 07:17:22 -0600

pgrice gravatar image

updated 2013-02-28 07:19:12 -0600

I don't believe this exists out-of-the-box.

Rosbag command line tools do allow you to play back topics at a modified (possibly very slow) constant rate with --rate. If you want very strict control over the publishing, this code snippet should serve:

import roslib, rospy, rosbag
from std_srvs.srv import Empty

class BagByService(object):
    def __init__(self, bagfile):
        self.bag = rosbag.Bag(bagfile, 'r')
        self.message_generator = self.bag.read_messages()
        self.next_msgs_srv = rospy.Service('/next_msg', Empty, self.pub_next_msg)

        #Create a publisher for each topic
        self.publishers = {}
        for con in self.bag._get_connections():
            msg_class = roslib.message.get_message_class(con.datatype)
            self.publishers[con.topic] = rospy.Publisher(con.topic, msg_class)

    def pub_next_msg(self, req):
        topic, msg, time =

if __name__=='__main__':
    import sys
    bbs = BagByService(sys.argv[1])

Run this with the .bag file as the only commandline argument. Calling the Empty '/next_msg' service will publish the next message in the bag file. Clearly, more interesting things could be done from here (filtering by time, topic, etc.)

edit flag offensive delete link more


pub_next_msg() needs to return a response, otherwise a "service handler returned None" error occurs. I add: return EmptyResponse() at the end of pub_next_msg() which requires a from std_srvs.srv import EmptyResponse and that solves the problem

martinakos gravatar image martinakos  ( 2019-06-20 05:54:04 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-02-27 16:23:52 -0600

Seen: 575 times

Last updated: Feb 28 '13