ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Robert Grönsfeld's profile - activity

2021-06-30 06:09:57 -0500 received badge  Taxonomist
2018-03-26 15:32:14 -0500 received badge  Student (source)
2018-03-26 15:32:12 -0500 received badge  Famous Question (source)
2017-09-06 15:48:14 -0500 received badge  Famous Question (source)
2017-09-06 15:48:14 -0500 received badge  Notable Question (source)
2017-03-16 13:56:26 -0500 received badge  Popular Question (source)
2017-03-16 13:56:26 -0500 received badge  Notable Question (source)
2016-08-10 09:10:18 -0500 received badge  Organizer (source)
2016-08-10 09:07:57 -0500 received badge  Editor (source)
2016-08-08 07:51:36 -0500 asked a question Instantiating messages from bagfile yields nullpointer

Hi, I have problems changing the code from this example so that it fits my needs. This is the scenario: I want to benchmark my code with some real world data. So I'm trying to read a previously recorded bagfile and feed it into my benchmarking code. I use a library that executes a given function n times and then tells me how long it took. My goal is to read a new sensor_msgs::PointCloud2 and some transformation data in each of the n iterations and perform some calculations on it. The example I changed used a BOOST_FOREACH loop to iterate through the whole bagfile. As I only want to do 100 iterations, I only have to read the first 100 point clouds from the bagfile. This is why I want to access the rosbag::View::iterator directly.

Now my problem is, that I always end up with a nullpointer when I instantiate a message via the iterator. This is how I implemented it:

BagReader.h:

class BagReader {
public:
    BagReader(std::string const &filename);
    ~BagReader();
    void addTopic(std::string const &name);
    void readNextMessage();

    template <typename MessageType>
    boost::shared_ptr<MessageType> 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<MessageType> ptr = messageIterator->instantiate<MessageType>();
        return ptr; //  >>> THIS IS NULL. IT SHOULD BE AN INSTANCE OF MessageType. <<<
    }

private:
    boost::scoped_ptr<rosbag::Bag> bag;
    boost::scoped_ptr<rosbag::View> view;
    rosbag::View::iterator messageIterator;
    std::vector<std::string> 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<sensor_msgs::PointCloud2>();
// Do something with the cloud...
bagReader->readNextMessage();
2016-07-19 16:18:40 -0500 received badge  Popular Question (source)
2016-07-15 09:44:19 -0500 answered a question How can i copy non-binary files into my binaries directory with catkin and cmake?

The solution was to replace ${CMAKE_BINARY_DIR} with ${CATKIN_DEVEL_PREFIX}/${CATKIN_PACKAGE_BIN_DESTINATION}

See also this post.

2016-07-15 09:31:50 -0500 asked a question How can i copy non-binary files into my binaries directory with catkin and cmake?

Hi, I have a benchmarking executable in my project which pulls data from bagfiles. Now I want to copy the folder with my bagfiles into the directory where my binaries end up, because I have hardcoded paths to them in my code. I already tried to copy the folder with my bagfiles by adding this line to my CMakeLists.txt:

file(COPY src/benchmarking/bagfiles DESTINATION ${CMAKE_BINARY_DIR})

Unfortunately the variable CMAKE_BINARY_DIR only contains the absolute path to my source directory. Is there a variable that points to the binaries directory?