Ask Your Question
0

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

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

gmongar gravatar image

Hello,

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
2

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;
bag.open("test.bag", rosbag::bagmode::Read);

std::vector<std::string> topics;
topics.push_back(std::string("chatter"));
topics.push_back(std::string("numbers"));

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.
    }
}

bag.close();

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

Comments

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 imagegmongar ( 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 imagePeteBlackerThe3rd ( 2018-11-26 06:48:53 -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

Stats

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

Seen: 341 times

Last updated: Nov 26 '18