publish.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 publisherTopic;
27 std::string publisherType;
28 double publisherRate = 0.0;
29 size_t publisherQueueSize = 100;
31 
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 publishOnce() {
56  if (!messageDefinition.isValid())
57  messageDefinition.load(publisherType);
58 
59  if (!messageType.isValid())
60  messageType = messageDefinition.getMessageDataType();
61 
62  if (!publisher)
63  publisher = messageType.advertise(*nodeHandle, publisherTopic,
64  publisherQueueSize, true);
65 
66  variant_topic_tools::MessageVariant variant = messageDefinition.
67  getMessageDataType().createVariant();
68 
69  publisher.publish(variant);
70 }
71 
72 void publish(const ros::TimerEvent& event) {
73  publishOnce();
74 }
75 
76 int main(int argc, char **argv) {
77  if (argc < 3) {
78  printf("\nusage: echo TOPIC TYPE [RATE]\n\n");
79  return 1;
80  }
81 
82  if (!getTopicBase((argv[1]), publisherTopic)) {
83  ROS_ERROR("Failed to extract topic base from [%s]", argv[1]);
84  return 1;
85  }
86 
87  ros::init(argc, argv, publisherTopic+"_publish",
89 
90  nodeHandle.reset(new ros::NodeHandle("~"));
91 
92  publisherTopic = argv[1];
93  publisherType = argv[2];
94  if (argc > 3)
95  publisherRate = boost::lexical_cast<double>(argv[3]);
96 
97  if (publisherRate > 0.0) {
98  publisherTimer = nodeHandle->createTimer(ros::Rate(publisherRate).
99  expectedCycleTime(), &publish);
100  }
101  else
102  publishOnce();
103 
104  ros::AsyncSpinner spinner(1);
105  spinner.start();
107 
108  publisherTimer.stop();
109 
110  return 0;
111 }
Publisher advertise(ros::NodeHandle &nodeHandle, const std::string &topic, size_t queueSize, bool latch=false, const ros::SubscriberStatusCallback &connectCallback=ros::SubscriberStatusCallback())
Advertise this message type.
void publish(const MessageVariant &variant)
Publish a message variant on the topic associated with this publisher.
Definition: Publisher.cpp:117
Variant message publisher.
Definition: Publisher.h:36
Variant message type information.
Definition: MessageType.h:33
void stop()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Header file providing the Publisher class interface.
ros::NodeHandlePtr nodeHandle
Definition: publish.cpp:23
void load(const std::string &messageDataType)
Attempt to load the message definition for the specified message data type.
variant_topic_tools::Publisher publisher
Definition: publish.cpp:25
variant_topic_tools::MessageDefinition messageDefinition
Definition: publish.cpp:32
MessageDataType getMessageDataType() const
Retrieve the message data type represented by this message definition.
ros::Timer publisherTimer
Definition: publish.cpp:30
void publish(const ros::TimerEvent &event)
Definition: publish.cpp:72
bool getTopicBase(const std::string &topic, std::string &topicBase)
Definition: publish.cpp:35
int main(int argc, char **argv)
Definition: publish.cpp:76
std::string publisherTopic
Definition: publish.cpp:26
void publishOnce()
Definition: publish.cpp:55
bool isValid() const
True, if this message type is valid.
Definition: MessageType.cpp:94
variant_topic_tools::MessageType messageType
Definition: publish.cpp:33
double publisherRate
Definition: publish.cpp:28
bool isValid() const
True, if this message definition is valid.
#define ROS_ERROR(...)
size_t publisherQueueSize
Definition: publish.cpp:29
ROSCPP_DECL void waitForShutdown()
std::string publisherType
Definition: publish.cpp:27


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