Search code examples
c++ros

I don't know the meaning of the ROS code,and I can't understand the example of ros wiki


I expect the detailed description of this code. Thanks

rosbag::Bag  bag(rosbag, rosbag::bagmode::Read);//read bag?
rosbag::View view(bag, rosbag::TopicQuery(topics));//create a view on a bag.
BOOST_FOREACH(rosbag::MessageInstance const m, view){}//what?
const std::string& topic_name = m.getTopic();
if (topic_name == topics[0])//what?
{
  dvs_msgs::EventArray::ConstPtr msg = 
  m.instantiate<dvs_msgs::EventArray>();
  if (msg != NULL)
  {
    if(msg->events.empty())
    {
      continue;
    }
    const ros::Time& stamp = msg->events[0].ts;
  }

Solution

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

    Yeap, in this way you can read the bag file

    std::vector<std::string> topics;
    rosbag::View view(bag, rosbag::TopicQuery(topics));
    

    This will iterate through all the topic information in the provided bag file and push back into topics

    BOOST_FOREACH(rosbag::MessageInstance const m, view){}//what?
    

    you have a mistake here, BOOST_FOREACH is macro, and here how it can be used

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

    Include header file and then

    foreach(rosbag::MessageInstance const m, view)
    {
        const std::string& topic_name = m.getTopic();
        if (topic_name == topics[0])//what?
        {
          dvs_msgs::EventArray::ConstPtr msg = 
          m.instantiate<dvs_msgs::EventArray>();
          if (msg != NULL)
          {
             if(msg->events.empty())
             {
              continue;
             }
            const ros::Time& stamp = msg->events[0].ts;
          }
       }
    }
    

    This will iterate through each of the messages, m.getTopic() this will get the topic corresponding to the current message, msg is the current message, and stamp is the time which this message was recorded.