Robotics StackExchange | Archived questions

Access next element in topics from rosbag C++ API

Hi,

I have bagfiles with a bunch of different topics (that are synchronized). I would like to access the next element from each of those topics to perform some operations on them. And this would go on in a loop until the end of the bag file.

The C++ API example just shows how to iterate over all the messages from all the topics and therefore needs another check to find out which topic the message came from. Is there a way to process a bunch of topics, but individually.

Also, I'm not sure of the purpose of the rosbag View class. Is this the only way to access the messages?

Thank you.

Asked by 2ROS0 on 2016-08-01 11:26:14 UTC

Comments

did you make it?

Asked by Augusto Luis Ballardini on 2017-03-16 15:16:30 UTC

Answers

I had to do something similar recently, I used two loops like this pseudo code, it does require checking the topic of every message but in the second loop the data is organized for processing (it could be split into n+1 loops: one loop on each specific topic plus a final processing, I don't know if that is more efficient or more self-documenting or not):

struct MySyncedTopics
{
  foo
  bar
}

std::map<ros::Time, MySyncedTopics> sync_map

foreach msg in bag
{
  if msg.topic_name == "foo"
        sync_map[msg.timestamp].foo = msg
  if msg.topic_name == "bar"
        sync_map[msg.timestamp].bar = msg
}

foreach timestamp in sorted sync_map keys
{
  if (not both messages exist at this timestamp)
  {
    print error message
    continue
  }
  // do something with synchronized messages
  ....
}

Asked by lucasw on 2017-03-17 08:42:24 UTC

Comments