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

Revision history [back]

click to hide/show revision 1
initial version

Hi

you can dump the message into a binary buffer. Attached you can see how I did it with a sensor_msgs::LaserScan.

void LaserLineFilterNode::callback (const sensor_msgs::LaserScan::ConstPtr& _msg) {

    {
        // Write to File
        std::ofstream ofs("/tmp/filename.txt", std::ios::out|std::ios::binary);

        uint32_t serial_size = ros::serialization::serializationLength(*_msg);
        boost::shared_array<uint8_t> obuffer(new uint8_t[serial_size]);

        ros::serialization::OStream ostream(obuffer.get(), serial_size);
        ros::serialization::serialize(ostream, *_msg);
        ofs.write((char*) obuffer.get(), serial_size);
        ofs.close();
    }
    {
        // Read from File to msg_scan_
        //sensor_msgs::LaserScan msg_scan_ --> is a class variable
        std::ifstream ifs("/tmp/filename.txt", std::ios::in|std::ios::binary);
        ifs.seekg (0, std::ios::end);
        std::streampos end = ifs.tellg();
        ifs.seekg (0, std::ios::beg);
        std::streampos begin = ifs.tellg();

        uint32_t file_size = end-begin;
        boost::shared_array<uint8_t> ibuffer(new uint8_t[file_size]);
        ifs.read((char*) ibuffer.get(), file_size);
        ros::serialization::IStream istream(ibuffer.get(), file_size);
        ros::serialization::deserialize(istream, msg_scan_);
        ifs.close();
    }
    ....

But I there is still an issue, I had to create a memory buffer fist, it would be nice to see how one can create a ros::serialization::OStream to stream directly into a file. More details on http://wiki.ros.org/roscpp/Overview/MessagesSerializationAndAdaptingTypes

Hmm... I will post a separate question for that :-)

Greetings Markus

Hi

you can dump the message into a binary buffer. Attached you can see how I did it with a sensor_msgs::LaserScan.

void LaserLineFilterNode::callback (const sensor_msgs::LaserScan::ConstPtr& _msg) {

    {
        // Write to File
        std::ofstream ofs("/tmp/filename.txt", std::ios::out|std::ios::binary);

        uint32_t serial_size = ros::serialization::serializationLength(*_msg);
        boost::shared_array<uint8_t> obuffer(new uint8_t[serial_size]);

        ros::serialization::OStream ostream(obuffer.get(), serial_size);
        ros::serialization::serialize(ostream, *_msg);
        ofs.write((char*) obuffer.get(), serial_size);
        ofs.close();
    }
    {
        // Read from File to msg_scan_
        //sensor_msgs::LaserScan msg_scan_ --> is a class variable
        std::ifstream ifs("/tmp/filename.txt", std::ios::in|std::ios::binary);
        ifs.seekg (0, std::ios::end);
        std::streampos end = ifs.tellg();
        ifs.seekg (0, std::ios::beg);
        std::streampos begin = ifs.tellg();

        uint32_t file_size = end-begin;
        boost::shared_array<uint8_t> ibuffer(new uint8_t[file_size]);
        ifs.read((char*) ibuffer.get(), file_size);
        ros::serialization::IStream istream(ibuffer.get(), file_size);
        ros::serialization::deserialize(istream, msg_scan_);
        ifs.close();
    }
    ....

More details on http://wiki.ros.org/roscpp/Overview/MessagesSerializationAndAdaptingTypes

But I there is still an issue, I had to create a memory buffer fist, it would be nice to see how one can create a ros::serialization::OStream to stream directly into a file. More details on http://wiki.ros.org/roscpp/Overview/MessagesSerializationAndAdaptingTypesfile.

Hmm... I will post a separate question for that :-)

Greetings Markus