subscribe_example.cpp
Go to the documentation of this file.
2 #include <ros/ros.h>
4 
5 using namespace RosIntrospection;
6 
8  const std::string &topic_name,
10 {
11 
12  const std::string& datatype = msg->getDataType();
13  const std::string& definition = msg->getMessageDefinition();
14 
15  // don't worry if you do this more than once: already registered message are not overwritten.
16  parser.registerMessageDefinition( topic_name,
17  RosIntrospection::ROSType(datatype),
18  definition );
19 
20  // reuse these opbects to improve efficiency ("static" makes them persistent)
21  static std::vector<uint8_t> buffer;
22  static std::map<std::string,FlatMessage> flat_containers;
23  static std::map<std::string,RenamedValues> renamed_vectors;
24 
25  FlatMessage& flat_container = flat_containers[topic_name];
26  RenamedValues& renamed_values = renamed_vectors[topic_name];
27 
28  // copy raw memory into the buffer
29  buffer.resize( msg->size() );
30  ros::serialization::OStream stream(buffer.data(), buffer.size());
31  msg->write(stream);
32 
33 
34  // deserialize and rename the vectors
35  parser.deserializeIntoFlatContainer( topic_name, absl::Span<uint8_t>(buffer), &flat_container, 100);
36  parser.applyNameTransform( topic_name, flat_container, &renamed_values );
37 
38  // Print the content of the message
39  printf("--------- %s ----------\n", topic_name.c_str() );
40  for (auto it: renamed_values)
41  {
42  const std::string& key = it.first;
43  const Variant& value = it.second;
44  printf(" %s = %f\n", key.c_str(), value.convert<double>() );
45  }
46  for (auto it: flat_container.name)
47  {
48  const std::string& key = it.first.toStdString();
49  const std::string& value = it.second;
50  printf(" %s = %s\n", key.c_str(), value.c_str() );
51  }
52 }
53 
54 
55 // usage: pass the name of the file as command line argument
56 int main(int argc, char** argv)
57 {
58  Parser parser;
59 
60  if( argc == 1 ){
61  printf("Usage: rosbag_example list_of_topics_names\n");
62  return 1;
63  }
64 
65  ros::init(argc, argv, "universal_subscriber");
66 
67  std::set<std::string> topic_names;
68 
69  for (int i=1; i< argc; i++)
70  {
71  topic_names.insert( argv[i] );
72  }
73  ros::master::V_TopicInfo advertized_topics;
74  ros::master::getTopics(advertized_topics);
75 
76  for (const auto& topic_info: advertized_topics)
77  {
78  printf("advertized: %s\n", topic_info.name.c_str());
79  }
80 
81  // check that all the topics are being published
82  for (const std::string& topic_name: topic_names)
83  {
84  bool found = false;
85  for (const auto& topic_info: advertized_topics)
86  {
87  if( topic_info.name == topic_name)
88  {
89  found = true;
90  break;
91  }
92  }
93  if( !found )
94  {
95  printf("This topic has not been published yet: %s\n", topic_name.c_str() );
96  return 1;
97  }
98  }
99 
100  ros::NodeHandle nh;
101 
102  // subscribers will be created dynamically, one for each element in topic_names
103  std::vector<ros::Subscriber> subscribers;
104 
105  for (const std::string& topic_name: topic_names)
106  {
107  //who is afraid of lambdas and boost::functions ?
108  boost::function<void(const topic_tools::ShapeShifter::ConstPtr&) > callback;
109  callback = [&parser, topic_name](const topic_tools::ShapeShifter::ConstPtr& msg) -> void
110  {
111  topicCallback(msg, topic_name, parser) ;
112  };
113  subscribers.push_back( nh.subscribe(topic_name, 10, callback) );
114  }
115 
116  ros::spin();
117 
118  return 0;
119 }
std::vector< std::pair< StringTreeLeaf, std::string > > name
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
RosIntrospection::Parser parser
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL bool getTopics(V_TopicInfo &topics)
void applyNameTransform(const std::string &msg_identifier, const FlatMessage &container, RenamedValues *renamed_value)
int main(int argc, char **argv)
std::vector< TopicInfo > V_TopicInfo
ROSCPP_DECL void spin(Spinner &spinner)
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())
void topicCallback(const topic_tools::ShapeShifter::ConstPtr &msg, const std::string &topic_name, RosIntrospection::Parser &parser)
const char * msg
int i
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