Hi, I wrote the following class that iterates through the messages of a bagfile:
BagReader.h:
class BagReader {
public:
BagReader(std::string const &filename);
~BagReader();
void addTopic(std::string const &name);
void readNextMessage();
template
boost::shared_ptr getMessage() {
if (!view) {
view.reset(new rosbag::View(*bag, rosbag::TopicQuery(topics)));
messageIterator = view->begin();
}
// The instantiation of the message will be a smart pointer.
boost::shared_ptr ptr = messageIterator->instantiate();
return ptr;
}
private:
boost::scoped_ptr bag;
boost::scoped_ptr view;
rosbag::View::iterator messageIterator;
std::vector topics;
};
BagReader.cpp:
#include "BagReader.h"
BagReader::BagReader(std::string const &filename) : bag(new rosbag::Bag(filename)) {
}
BagReader::~BagReader() {
bag->close();
}
void BagReader::addTopic(std::string const &name) {
topics.push_back(name);
}
void BagReader::readNextMessage() {
if (messageIterator != view->end())
++messageIterator;
else
messageIterator = view->begin();
}
Now I initialize my reader giving it the path to a bagfile and adding the two topics i want to read from:
std::string const &path = bagfilesDirectory + "/dice_detection.bag";
bagReader.reset(new BagReader(path));
bagReader->addTopic("/camera/depth_registered/points");
bagReader->addTopic("/tf");
Then I do the following to iterate through the messages of the bagfile:
sensor_msgs::PointCloud2ConstPtr cloud = bagReader->getMessage();
// Do something with the cloud...
bagReader->readNextMessage();
Now my problem is, that ptr is set to NULL and I don't know why. I roughly followed [this](http://wiki.ros.org/rosbag/Code%20API#cpp_api) tutorial.
↧