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

Access next element in topics from rosbag C++ API

asked 2016-08-01 11:26:14 -0600

2ROS0 gravatar image

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.

edit retag flag offensive close merge delete

Comments

did you make it?

Augusto Luis Ballardini gravatar image Augusto Luis Ballardini  ( 2017-03-16 15:16:30 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-03-17 08:42:24 -0600

lucasw gravatar image

updated 2017-03-17 08:44:46 -0600

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
  ....
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-08-01 11:26:14 -0600

Seen: 2,032 times

Last updated: Mar 17 '17