0
$\begingroup$

Rosanswers logo

If I have a huge rosbag of which I only wish to examine the time at which a particular message was published to a particular topic, how would I do that?

Currently, I have to play through the entire rosbag and wait for that message to appear and note down the timestamp.

Is there a smarter way to get the timestamp directly from the bag?


Originally posted by Rufus on ROS Answers with karma: 1083 on 2019-09-17

Post score: 0


Original comments

Comment by gvdhoorn on 2019-09-17:
Whenever you're not actually interested in replaying .bag files to feed "old" information to a running ROS application, use the rosbag Python/C++ API.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Thx to gvdhoorn's comment, I managed to write the following simple script to do what I want

#!/usr/bin/python2

import yaml
import rosbag
import argparse
import re

parser = argparse.ArgumentParser(description="Greps /rosout for a specific message and reports its timestamps")
parser.add_argument("bagfile", nargs=1, help="input bag file")
parser.add_argument("regex", nargs=1, help="regex pattern to search for")
args = parser.parse_args()

bagfile = args.bagfile[0]
regex = args.regex[0]

info_dict = yaml.load(rosbag.Bag(bagfile, 'r')._get_yaml_info())

for topic, msg, t in rosbag.Bag(bagfile).read_messages():
    if topic == "rosout":
        if re.search(regex, msg.msg) or re.search(regex, msg.name):
            seconds_from_start = t.to_sec() - info_dict["start"]
            print("[" + str(seconds_from_start) + "] " + msg.msg)

Originally posted by Rufus with karma: 1083 on 2019-09-20

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.