Save specific data from a rosbag into an C++ array variable.
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
Asked by gmongar on 2018-11-26 05:52:43 UTC
Answers
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.
Asked by PeteBlackerThe3rd on 2018-11-26 06:01:56 UTC
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?
Asked by gmongar on 2018-11-26 07:15:50 UTC
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.
Asked by PeteBlackerThe3rd on 2018-11-26 07:48:53 UTC
Comments