rosbag_example.cpp
Go to the documentation of this file.
00001 #include "ros_type_introspection/ros_introspection.hpp"
00002 #include <ros/ros.h>
00003 #include <rosbag/bag.h>
00004 #include <rosbag/view.h>
00005 
00006 RosIntrospection::Parser parser;
00007 
00008 
00009 // usage: pass the name of the file as command line argument
00010 int main(int argc, char** argv)
00011 {
00012     using namespace RosIntrospection;
00013 
00014     if( argc != 2 ){
00015         printf("Usage: rosbag_example rosbag_file.bag\n");
00016         return 1;
00017     }
00018     rosbag::Bag bag;
00019 
00020     try{
00021         bag.open( argv[1] );
00022     }
00023     catch( rosbag::BagException&  ex)
00024     {
00025         printf("rosbag::open thrown an exception:\n");
00026         return 1;
00027     }
00028 
00029     // this  rosbag::View will accept ALL the messages
00030     rosbag::View bag_view ( bag );
00031 
00032     // register (only once at the beginning) the type of messages
00033     for(const rosbag::ConnectionInfo* connection: bag_view.getConnections() )
00034     {
00035         const std::string&  topic_name =  connection->topic;
00036         const std::string&  datatype   =  connection->datatype;
00037         const std::string&  definition =  connection->msg_def;
00038         // register the type using the topic_name as identifier.
00039         parser.registerMessageDefinition(topic_name, ROSType(datatype), definition);
00040     }
00041 
00042     // it is efficient to reuse the same instance of FlatMessage and RenamedValues
00043     // over and over again, to reduce the amount of memory allocations
00044     std::map<std::string, FlatMessage>   flat_containers;
00045     std::map<std::string, RenamedValues> renamed_vectors;
00046 
00047     // This buffer will store the raw bytes where the message is encoded
00048     std::vector<uint8_t> buffer;
00049 
00050     for(rosbag::MessageInstance msg_instance: bag_view)
00051     {
00052         const std::string& topic_name  = msg_instance.getTopic();
00053 
00054         // write the message into the buffer
00055         const size_t msg_size  = msg_instance.size();
00056         buffer.resize(msg_size);
00057         ros::serialization::OStream stream(buffer.data(), buffer.size());
00058         msg_instance.write(stream);
00059 
00060         FlatMessage&   flat_container = flat_containers[topic_name];
00061         RenamedValues& renamed_values = renamed_vectors[topic_name];
00062 
00063         // deserialize and rename the vectors
00064         parser.deserializeIntoFlatContainer( topic_name,
00065                                              absl::Span<uint8_t>(buffer),
00066                                              &flat_container, 100 );
00067         parser.applyNameTransform( topic_name,
00068                                    flat_container,
00069                                    &renamed_values );
00070 
00071         // Print the content of the message
00072         printf("--------- %s ----------\n", topic_name.c_str() );
00073         for (auto it: renamed_values)
00074         {
00075             const std::string& key = it.first;
00076             const Variant& value   = it.second;
00077             printf(" %s = %f\n", key.c_str(), value.convert<double>() );
00078         }
00079         for (auto it: flat_container.name)
00080         {
00081             const std::string& key    = it.first.toStdString();
00082             const std::string& value  = it.second;
00083             printf(" %s = %s\n", key.c_str(), value.c_str() );
00084         }
00085     }
00086 
00087     return 0;
00088 }


type_introspection_tests
Author(s): Davide Faconti
autogenerated on Sat Apr 14 2018 03:30:59