echo.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 
22 
24 
26 std::string subscriberTopic;
27 size_t subscriberQueueSize = 100;
28 
29 void callback(const variant_topic_tools::MessageVariant& variant, const
30  ros::Time& receiptTime);
31 
32 bool getTopicBase(const std::string& topic, std::string& topicBase) {
33  std::string tmp = topic;
34  int i = tmp.rfind('/');
35 
36  while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
37  tmp = tmp.substr(0,tmp.size()-1);
38  i = tmp.rfind('/');
39  }
40 
41  if (tmp.size() == 0)
42  return false;
43 
44  if(i < 0)
45  topicBase = tmp;
46  else
47  topicBase = tmp.substr(i+1, tmp.size()-i-1);
48 
49  return true;
50 }
51 
52 void subscribe() {
54  subscriber = type.subscribe(*nodeHandle, subscriberTopic,
56 }
57 
59  ros::Time& receiptTime) {
60  std::cout << variant << "\n---\n";
61 }
62 
63 int main(int argc, char **argv) {
64  if (argc < 2) {
65  printf("\nusage: echo TOPIC\n\n");
66  return 1;
67  }
68 
69  if (!getTopicBase((argv[1]), subscriberTopic)) {
70  ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
71  return 1;
72  }
73 
74  ros::init(argc, argv, subscriberTopic+"_echo",
76 
77  subscriberTopic = argv[1];
78 
79  nodeHandle.reset(new ros::NodeHandle("~"));
80 
81  subscribe();
82  ros::spin();
83 
84  return 0;
85 }
void subscribe()
Definition: echo.cpp:52
Variant message subscriber.
Definition: Subscriber.h:36
Variant message type information.
Definition: MessageType.h:33
Subscriber subscribe(ros::NodeHandle &nodeHandle, const std::string &topic, size_t queueSize, const SubscriberCallback &callback)
Subscribe to this message type.
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Header file providing the Subscriber class interface.
void callback(const variant_topic_tools::MessageVariant &variant, const ros::Time &receiptTime)
Definition: echo.cpp:58
variant_topic_tools::Subscriber subscriber
Definition: echo.cpp:25
size_t subscriberQueueSize
Definition: echo.cpp:27
ros::NodeHandlePtr nodeHandle
Definition: echo.cpp:23
bool getTopicBase(const std::string &topic, std::string &topicBase)
Definition: echo.cpp:32
ROSCPP_DECL void spin()
std::string subscriberTopic
Definition: echo.cpp:26
int main(int argc, char **argv)
Definition: echo.cpp:63
#define ROS_ERROR(...)


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