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;
}
Tiempo de compilación
... referencia indefinida a `rosbag :: Bag :: close () '
... referencia indefinida a` rosbag :: View :: ~ View ()'
... referencia indefinida a `rosbag :: Bag :: ~ Bag () '
.....
Dado que el archivo CmakeLists.txt no agrega dependencia rosbag, simplemente agregue el elemento rosbag en CMakeLists.txt y package.xml.
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>
....
O use el siguiente comando para reconstruir el proyecto:
~/cat_ws/src$catkin_create_pkg bagdemo roscpp rospy rosbag std_msgs geometry_msgs
Además, la configuración de la zona horaria de la computadora debe ser correcta ; de lo contrario, se puede reportar un error durante la operación: Intentó insertar un mensaje con un tiempo menor a ros :: TIME_MIN Aborted (core volcado)