http://wiki.ros.org/rosbag/Code%20API
http://docs.ros.org/melodic/api/rosbag_storage/html/c++/
https://github.com/sofiathefirst/imagesCpp/tree/master/bagdemo_ros_img
writebag.cpp
#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
ros::init(argc, argv, "eyeInScreen");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
// ros::Publisher chatter_pub = n.advertise<geometry_msgs::Point>("/point", 10);
ros::Rate loop_rate(10);
rosbag::Bag bag;
bag.open("test.bag", rosbag::bagmode::Write);
int count = 0;
while (ros::ok() && count<1000)
{
count++;
std_msgs::String str;
str.data = std::string("foo");
std_msgs::Int32 i;
i.data = count;
bag.write("chatter", ros::Time::now(), str);
bag.write("numbers", ros::Time::now(), i);
ros::spinOnce();
loop_rate.sleep();
++count;
}
bag.close();
return 0;
}
readbag.cpp
#include "ros/ros.h"
#include "geometry_msgs/Point.h"
#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
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
ros::init(argc, argv, "eyeInScreen");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<geometry_msgs::Point>("/point", 10);
ros::Rate loop_rate(10);
int count = 0;
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();
return 0;
}
编译时报
... undefined reference to `rosbag::Bag::close()'
...undefined reference to `rosbag::View::~View()'
...undefined reference to `rosbag::Bag::~Bag()'
.....
是因为CmakeLists.txt文件没有添加rosbag的依赖,在CMakeLists.txt 和 package.xml 中添加rosbag 项即可。
find_package(catkin REQUIRED COMPONENTS
roscpp
rosbag
std_msgs
....
)
<build_depend>roscpp</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>rosbag</build_depend>
....
或者用下列命令重建项目:
~/cat_ws/src$catkin_create_pkg bagdemo roscpp rospy rosbag std_msgs geometry_msgs
另外电脑时区设置一定要对,否则运行时可能会报错:Tried to insert a message with time less than ros::TIME_MIN Aborted (core dumped)