1、rosbag 写


    #include <rosbag/bag.h>    #include <std_msgs/Int32.h>    #include <std_msgs/String.h>    rosbag::Bag bag;    bag.open("test.bag", rosbag::bagmode::Write);    std_msgs::String str;    str.data = std::string("foo");    std_msgs::Int32 i;    i.data = 42;    bag.write("chatter", ros::Time::now(), str);    bag.write("numbers", ros::Time::now(), i);    bag.close();

2、rosbag读


#include <rosbag/bag.h> #include <rosbag/view.h> #include <std_msgs/Int32.h> #include <std_msgs/String.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)); foreach(rosbag::MessageInstance const m, view) { std_msgs::String::ConstPtr s = m.instantiate<std_msgs::String>(); if (s != NULL) std::cout << s->data << std::endl; std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>(); if (i != NULL) std::cout << i->data << std::endl; } bag.close();

C++11版本

#include <rosbag/bag.h> #include <rosbag/view.h> #include <std_msgs/Int32.h> rosbag::Bag bag; bag.open("test.bag"); // BagMode is Read by default for(rosbag::MessageInstance const m: rosbag::View(bag)) { std_msgs::Int32::ConstPtr i = m.instantiate<std_msgs::Int32>(); if (i != nullptr) std::cout << i->data << std::endl; } bag.close();






标签: none

评论已关闭