info.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <ros/ros.h>
20 
23 
25 
27 std::string subscriberTopic;
28 size_t subscriberQueueSize = 100;
29 
31 
33  messageEvent);
34 
35 bool getTopicBase(const std::string& topic, std::string& topicBase) {
36  std::string tmp = topic;
37  int i = tmp.rfind('/');
38 
39  while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
40  tmp = tmp.substr(0,tmp.size()-1);
41  i = tmp.rfind('/');
42  }
43 
44  if (tmp.size() == 0)
45  return false;
46 
47  if(i < 0)
48  topicBase = tmp;
49  else
50  topicBase = tmp.substr(i+1, tmp.size()-i-1);
51 
52  return true;
53 }
54 
55 void subscribe() {
56  subscriber = nodeHandle->subscribe(subscriberTopic, subscriberQueueSize,
57  &callback);
58 }
59 
61  messageEvent) {
63  messageEvent.getConstMessage();
64 
65  if (!messageDefinition.isValid()) {
66  messageDefinition.setMessageType(message->getType());
67 
68  std::cout << "Topic: " << message->getHeader().getTopic() << "\n";
69  std::cout << "Publisher: " << message->getHeader().getPublisher() << "\n";
70  std::cout << "Latched: " << (message->getHeader().isLatched() ?
71  "yes" : "no") << "\n";
72  std::cout << "---\n";
73  std::cout << "Type: " << message->getType().getDataType() << "\n";
74  std::cout << "MD5 Sum: " << message->getType().getMD5Sum() << "\n";
75  std::cout << "Definition:\n";
76  std::cout << "---\n";
77  std::cout << messageDefinition;
78  std::cout << "---\n";
79 
80  ros::shutdown();
81  }
82 }
83 
84 int main(int argc, char **argv) {
85  if (argc < 2) {
86  printf("\nusage: info TOPIC\n\n");
87  return 1;
88  }
89 
90  if (!getTopicBase((argv[1]), subscriberTopic)) {
91  ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
92  return 1;
93  }
94 
95  ros::init(argc, argv, subscriberTopic+"_info",
97 
98  subscriberTopic = argv[1];
99 
100  nodeHandle.reset(new ros::NodeHandle("~"));
101 
102  subscribe();
103  ros::spin();
104 
105  return 0;
106 }
Header file providing the MessageDefinition class interface.
Header file providing the Message class interface.
variant_topic_tools::MessageDefinition messageDefinition
Definition: info.cpp:30
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
const boost::shared_ptr< ConstMessage > & getConstMessage() const
void callback(const ros::MessageEvent< variant_topic_tools::Message > &messageEvent)
Definition: info.cpp:60
size_t subscriberQueueSize
Definition: info.cpp:28
int main(int argc, char **argv)
Definition: info.cpp:84
void setMessageType(const MessageType &messageType)
Set the message type represented by this message definition.
ROSCPP_DECL void spin()
ros::Subscriber subscriber
Definition: info.cpp:26
ROSCPP_DECL void shutdown()
ros::NodeHandlePtr nodeHandle
Definition: info.cpp:24
bool isValid() const
True, if this message definition is valid.
#define ROS_ERROR(...)
void subscribe()
Definition: info.cpp:55
std::string subscriberTopic
Definition: info.cpp:27
bool getTopicBase(const std::string &topic, std::string &topicBase)
Definition: info.cpp:35


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:49