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

Save specific data from a rosbag into an C++ array variable.

asked 2018-11-26 04:52:43 -0600

gmongar gravatar image


Currently I am working on a project and we need to extract msgs from a topic inside a rosbag file.

Our first idea was to save all the msgs of the topic into a .txt and after that process that data with C++ but there is too much data and is not the optimal solution. (rostopic echo /topic > data.txt)

My question is the next: How can I extract some specific msg for an specific timestamp in C++ and save those msg into an array? After that i would like to use that array for my C++ code.

PD: I have checked rosbag c++ API but I didn´t find how to do that.

Thanks for your help

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-11-26 05:01:56 -0600

updated 2018-11-26 06:47:50 -0600

You're on the right track. rosbag api is definitely the way to do this, here's how to go about it. I've modified one of the code examples from the rosbag page so that it uses a stamped message so you can test test time of the message.

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <geometry_msgs/PoseStamped.h>

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

rosbag::Bag bag;"test.bag", rosbag::bagmode::Read);

std::vector<std::string> topics;

rosbag::View view(bag, rosbag::TopicQuery(topics));

std::vector<double> myArray;

foreach(rosbag::MessageInstance const m, view)
    geometry_msgs::PoseStamped::ConstPtr pose = m.instantiate<geometry_msgs::PoseStamped>();
    if (pose != NULL)
        if (pose.header.stamp > startTime && pose.header.stamp < endTime)
            myArray.push_back(pose.pose.position.x); // Add this message to your C++ array here.


This code will loop through every message in the bag testing if it's a PoseStamped message, then if it is checking if it's time stamp is within a given range.

Hope this gets you started.

edit flag offensive delete link more


First of all thanks for your help.

I saw this example and I didn´t understand it well.

With your answer I can see a way to start with it.

Also I see that in your code you save the pose. Could be possible to save an specific variable of the pose? for example pose.x?

gmongar gravatar image gmongar  ( 2018-11-26 06:15:50 -0600 )edit

Yes you can access any property of the message as use usually would. I've updated my code example so it stores the linear x position from the pose into an array as an example for you.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2018-11-26 06:48:53 -0600 )edit

Question Tools


Asked: 2018-11-26 04:52:43 -0600

Seen: 1,178 times

Last updated: Nov 26 '18