Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <ros/ros.h>
00020
00021 #include <variant_topic_tools/Subscriber.h>
00022
00023 ros::NodeHandlePtr nodeHandle;
00024
00025 variant_topic_tools::Subscriber subscriber;
00026 std::string subscriberTopic;
00027 size_t subscriberQueueSize = 100;
00028
00029 void callback(const variant_topic_tools::MessageVariant& variant, const
00030 ros::Time& receiptTime);
00031
00032 bool getTopicBase(const std::string& topic, std::string& topicBase) {
00033 std::string tmp = topic;
00034 int i = tmp.rfind('/');
00035
00036 while( (tmp.size() > 0) && (i >= (int)(tmp.size()-1))) {
00037 tmp = tmp.substr(0,tmp.size()-1);
00038 i = tmp.rfind('/');
00039 }
00040
00041 if (tmp.size() == 0)
00042 return false;
00043
00044 if(i < 0)
00045 topicBase = tmp;
00046 else
00047 topicBase = tmp.substr(i+1, tmp.size()-i-1);
00048
00049 return true;
00050 }
00051
00052 void subscribe() {
00053 variant_topic_tools::MessageType type;
00054 subscriber = type.subscribe(*nodeHandle, subscriberTopic,
00055 subscriberQueueSize, &callback);
00056 }
00057
00058 void callback(const variant_topic_tools::MessageVariant& variant, const
00059 ros::Time& receiptTime) {
00060 std::cout << variant << "\n---\n";
00061 }
00062
00063 int main(int argc, char **argv) {
00064 if (argc < 2) {
00065 printf("\nusage: echo TOPIC\n\n");
00066 return 1;
00067 }
00068
00069 if (!getTopicBase((argv[1]), subscriberTopic)) {
00070 ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
00071 return 1;
00072 }
00073
00074 ros::init(argc, argv, subscriberTopic+"_echo",
00075 ros::init_options::AnonymousName);
00076
00077 subscriberTopic = argv[1];
00078
00079 nodeHandle.reset(new ros::NodeHandle("~"));
00080
00081 subscribe();
00082 ros::spin();
00083
00084 return 0;
00085 }