rosbag_example.cpp
Go to the documentation of this file.
2 #include <ros/ros.h>
3 #include <rosbag/bag.h>
4 #include <rosbag/view.h>
5 
7 
8 
9 // usage: pass the name of the file as command line argument
10 int main(int argc, char** argv)
11 {
12  using namespace RosIntrospection;
13 
14  if( argc != 2 ){
15  printf("Usage: rosbag_example rosbag_file.bag\n");
16  return 1;
17  }
18  rosbag::Bag bag;
19 
20  try{
21  bag.open( argv[1] );
22  }
23  catch( rosbag::BagException& ex)
24  {
25  printf("rosbag::open thrown an exception:\n");
26  return 1;
27  }
28 
29  // this rosbag::View will accept ALL the messages
30  rosbag::View bag_view ( bag );
31 
32  // register (only once at the beginning) the type of messages
33  for(const rosbag::ConnectionInfo* connection: bag_view.getConnections() )
34  {
35  const std::string& topic_name = connection->topic;
36  const std::string& datatype = connection->datatype;
37  const std::string& definition = connection->msg_def;
38  // register the type using the topic_name as identifier.
39  parser.registerMessageDefinition(topic_name, ROSType(datatype), definition);
40  }
41 
42  // it is efficient to reuse the same instance of FlatMessage and RenamedValues
43  // over and over again, to reduce the amount of memory allocations
44  std::map<std::string, FlatMessage> flat_containers;
45  std::map<std::string, RenamedValues> renamed_vectors;
46 
47  // This buffer will store the raw bytes where the message is encoded
48  std::vector<uint8_t> buffer;
49 
50  for(rosbag::MessageInstance msg_instance: bag_view)
51  {
52  const std::string& topic_name = msg_instance.getTopic();
53 
54  // write the message into the buffer
55  const size_t msg_size = msg_instance.size();
56  buffer.resize(msg_size);
57  ros::serialization::OStream stream(buffer.data(), buffer.size());
58  msg_instance.write(stream);
59 
60  FlatMessage& flat_container = flat_containers[topic_name];
61  RenamedValues& renamed_values = renamed_vectors[topic_name];
62 
63  // deserialize and rename the vectors
64  parser.deserializeIntoFlatContainer( topic_name,
65  absl::Span<uint8_t>(buffer),
66  &flat_container, 100 );
67  parser.applyNameTransform( topic_name,
68  flat_container,
69  &renamed_values );
70 
71  // Print the content of the message
72  printf("--------- %s ----------\n", topic_name.c_str() );
73  for (auto it: renamed_values)
74  {
75  const std::string& key = it.first;
76  const Variant& value = it.second;
77  printf(" %s = %f\n", key.c_str(), value.convert<double>() );
78  }
79  for (auto it: flat_container.name)
80  {
81  const std::string& key = it.first.toStdString();
82  const std::string& value = it.second;
83  printf(" %s = %s\n", key.c_str(), value.c_str() );
84  }
85  }
86 
87  return 0;
88 }
std::vector< std::pair< StringTreeLeaf, std::string > > name
std::vector< const ConnectionInfo * > getConnections()
void open(std::string const &filename, uint32_t mode=bagmode::Read)
RosIntrospection::Parser parser
void applyNameTransform(const std::string &msg_identifier, const FlatMessage &container, RenamedValues *renamed_value)
int main(int argc, char **argv)
bool deserializeIntoFlatContainer(const std::string &msg_identifier, absl::Span< uint8_t > buffer, FlatMessage *flat_container_output, const uint32_t max_array_size) const
FlatMessage flat_container
std::vector< std::pair< std::string, Variant > > RenamedValues
ros::serialization::OStream stream(buffer.data(), buffer.size())
std::vector< uint8_t > buffer(ros::serialization::serializationLength(imu))
void registerMessageDefinition(const std::string &message_identifier, const ROSType &main_type, const std::string &definition)


type_introspection_tests
Author(s): Davide Faconti
autogenerated on Fri Apr 13 2018 02:21:58