看不懂ROS代码的意思,看不懂ros wiki的例子

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

期待这段代码的详细说明。谢谢

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

是啊,这样你就可以读取包文件了

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

这将遍历提供的包文件中的所有主题信息并推回主题

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

你这里搞错了,BOOST_FOREACH是宏,这里是怎么用的

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

包括头文件然后

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

这将遍历每条消息,m.getTopic()这将获取与当前消息对应的主题,msg是当前消息,stamp是时间此消息已记录。