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

get rosbag duration in C++

asked 2020-09-03 03:03:57 -0500

ballubuoy gravatar image

updated 2021-04-24 02:34:20 -0500

miura gravatar image

ros cookbook shows how to get bag summary in Python in C++. I just need to figure out the duration of a rosbag in secs (the way we get it in rosbag info) and length of each of the recorded topics.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-09-08 05:40:08 -0500

Weasfas gravatar image

Hi @ballubuoy,

In the first example of the ros cookbook, the time is not extracted using a bag object. The time is obtained by calling a subprocess executing the command robag info my_bag.bag and redirecting the output into a yaml object. In the second example, after searching on the C++ API, it seems there is no way to directly get the info from a function like in Python with _get_yaml_info(), but what you can do, instead of using a subprocess is using the View class like this:

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>

int main(int argc, char **argv)
{
    rosbag::Bag bag;
    bag.open("/path/to/my_bag.bag", rosbag::bagmode::Read);

    rosbag::View view(bag);

    ros::Time bag_begin_time = view.getBeginTime();
    ros::Time bag_end_time = view.getEndTime();

    std::cout << "ROS bag time: " << (bag_end_time-bag_begin_time).toSec() << "(s)" << std::endl;

    bag.close();

    return 0;
}

Since the bag may be not beginning at time 0 we just extract the first time and the end time to compute the time elapsed in the bag, the convert that ros::Time into secs.

Hope that can help you.

Regards.

edit flag offensive delete link more

Comments

1

@Weasfas: might be nice to add this to the C++ section of the Cookbook page?

gvdhoorn gravatar image gvdhoorn  ( 2020-09-08 06:32:17 -0500 )edit
1

@gvdhoorn, yes, It would be good. I have already added a new section to the ros cookbook. Hope this can help more people =).

Regards.

Weasfas gravatar image Weasfas  ( 2020-09-09 04:11:30 -0500 )edit

NIce. Thanks.

I've made some small edits.

gvdhoorn gravatar image gvdhoorn  ( 2020-09-09 04:14:50 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-03 03:03:57 -0500

Seen: 860 times

Last updated: Sep 08 '20