mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
publisher_handler.hpp
1 #ifndef PUBLISHER_HANDLER_HPP
2 #define PUBLISHER_HANDLER_HPP
3 
4 namespace mrs_lib
5 {
6 
7 // --------------------------------------------------------------
8 // | PublisherHandler_impl |
9 // --------------------------------------------------------------
10 
11 /* PublisherHandler_impl(void) //{ */
12 
13 template <class TopicType>
14 PublisherHandler_impl<TopicType>::PublisherHandler_impl(void) : publisher_initialized_(false) {
15 }
16 
17 //}
18 
19 /* PublisherHandler_impl(ros::NodeHandle& nh, const std::string& address, const unsigned int &buffer_size, const bool &latch) //{ */
20 
21 template <class TopicType>
22 PublisherHandler_impl<TopicType>::PublisherHandler_impl(ros::NodeHandle& nh, const std::string& address, const unsigned int& buffer_size, const bool& latch,
23  const double& rate) {
24 
25  {
26  std::scoped_lock lock(mutex_publisher_);
27 
28  publisher_ = nh.advertise<TopicType>(address, buffer_size, latch);
29 
30  if (rate > 0.0) {
31 
32  throttle_ = true;
33 
34  throttle_min_dt_ = 1.0 / rate;
35 
36  } else {
37 
38  throttle_ = false;
39 
40  throttle_min_dt_ = 0;
41  }
42 
43  last_time_published_ = ros::Time(0);
44  }
45 
46  publisher_initialized_ = true;
47 }
48 
49 //}
50 
51 /* publish(TopicType& msg) //{ */
52 
53 template <class TopicType>
54 void PublisherHandler_impl<TopicType>::publish(const TopicType& msg) {
55 
56  if (!publisher_initialized_) {
57  return;
58  }
59 
60  {
61  std::scoped_lock lock(mutex_publisher_);
62 
63  if (throttle_) {
64 
65  if ((ros::Time::now() - last_time_published_).toSec() < throttle_min_dt_) {
66  return;
67  }
68 
69  last_time_published_ = ros::Time::now();
70  }
71 
72  try {
73  publisher_.publish(msg);
74  }
75  catch (...) {
76  ROS_ERROR("exception caught during publishing topic '%s'", publisher_.getTopic().c_str());
77  }
78  }
79 }
80 
81 //}
82 
83 /* publish(const boost::shared_ptr<TopicType>& msg) //{ */
84 
85 template <class TopicType>
86 void PublisherHandler_impl<TopicType>::publish(const boost::shared_ptr<TopicType>& msg) {
87 
88  if (!publisher_initialized_) {
89  return;
90  }
91 
92  {
93  std::scoped_lock lock(mutex_publisher_);
94 
95  if (throttle_) {
96 
97  if ((ros::Time::now() - last_time_published_).toSec() < throttle_min_dt_) {
98  return;
99  }
100 
101  last_time_published_ = ros::Time::now();
102  }
103 
104  try {
105  publisher_.publish(msg);
106  }
107  catch (...) {
108  ROS_ERROR("exception caught during publishing topic '%s'", publisher_.getTopic().c_str());
109  }
110  }
111 }
112 
113 //}
114 
115 /* publish(const boost::shared_ptr<TopicType const>& msg) //{ */
116 
117 template <class TopicType>
118 void PublisherHandler_impl<TopicType>::publish(const boost::shared_ptr<TopicType const>& msg) {
119 
120  if (!publisher_initialized_) {
121  return;
122  }
123 
124  {
125  std::scoped_lock lock(mutex_publisher_);
126 
127  if (throttle_) {
128 
129  if ((ros::Time::now() - last_time_published_).toSec() < throttle_min_dt_) {
130  return;
131  }
132 
133  last_time_published_ = ros::Time::now();
134  }
135 
136  try {
137  publisher_.publish(msg);
138  }
139  catch (...) {
140  ROS_ERROR("exception caught during publishing topic '%s'", publisher_.getTopic().c_str());
141  }
142  }
143 }
144 
145 //}
146 
147 /* getNumSubscribers(void) //{ */
148 
149 template <class TopicType>
151 
152  {
153  std::scoped_lock lock(mutex_publisher_);
154 
155  return publisher_.getNumSubscribers();
156  }
157 }
158 
159 //}
160 
161 // --------------------------------------------------------------
162 // | PublisherHandler |
163 // --------------------------------------------------------------
164 
165 /* operator= //{ */
166 
167 template <class TopicType>
169 
170  if (this == &other) {
171  return *this;
172  }
173 
174  if (other.impl_) {
175  this->impl_ = other.impl_;
176  }
177 
178  return *this;
179 }
180 
181 //}
182 
183 /* copy constructor //{ */
184 
185 template <class TopicType>
187 
188  if (other.impl_) {
189  this->impl_ = other.impl_;
190  }
191 }
192 
193 //}
194 
195 /* PublisherHandler(ros::NodeHandle& nh, const std::string& address, const unsigned int &buffer_size, const bool &latch) //{ */
196 
197 template <class TopicType>
198 PublisherHandler<TopicType>::PublisherHandler(ros::NodeHandle& nh, const std::string& address, const unsigned int& buffer_size, const bool& latch,
199  const double& rate) {
200 
201  impl_ = std::make_shared<PublisherHandler_impl<TopicType>>(nh, address, buffer_size, latch, rate);
202 }
203 
204 //}
205 
206 /* publish(const TopicType& msg) //{ */
207 
208 template <class TopicType>
209 void PublisherHandler<TopicType>::publish(const TopicType& msg) {
210 
211  impl_->publish(msg);
212 }
213 
214 //}
215 
216 /* publish(const boost::shared_ptr<TopicType>& msg) //{ */
217 
218 template <class TopicType>
219 void PublisherHandler<TopicType>::publish(const boost::shared_ptr<TopicType>& msg) {
220 
221  impl_->publish(msg);
222 }
223 
224 //}
225 
226 /* publish(const boost::shared_ptr<TopicType const>& msg) //{ */
227 
228 template <class TopicType>
229 void PublisherHandler<TopicType>::publish(const boost::shared_ptr<TopicType const>& msg) {
230 
231  impl_->publish(msg);
232 }
233 
234 //}
235 
236 /* getNumSubscribers(void) //{ */
237 
238 template <class TopicType>
240 
241  return impl_->getNumSubscribers();
242 }
243 
244 //}
245 
246 } // namespace mrs_lib
247 
248 #endif // PUBLISHER_HANDLER_HPP
mrs_lib::PublisherHandler::PublisherHandler
PublisherHandler(void)
generic constructor
Definition: publisher_handler.h:101
mrs_lib::PublisherHandler::publish
void publish(const TopicType &msg)
publish message
Definition: publisher_handler.hpp:209
mrs_lib::PublisherHandler_impl::publish
void publish(const TopicType &msg)
publish message
Definition: publisher_handler.hpp:54
mrs_lib::PublisherHandler::operator=
PublisherHandler & operator=(const PublisherHandler &other)
operator=
Definition: publisher_handler.hpp:168
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::PublisherHandler_impl::getNumSubscribers
unsigned int getNumSubscribers(void)
get number of subscribers
Definition: publisher_handler.hpp:150
mrs_lib::PublisherHandler
user wrapper of the publisher handler implementation
Definition: publisher_handler.h:95
mrs_lib::PublisherHandler::getNumSubscribers
unsigned int getNumSubscribers(void)
get number of subscribers
Definition: publisher_handler.hpp:239
mrs_lib::PublisherHandler_impl::PublisherHandler_impl
PublisherHandler_impl(void)
default constructor
Definition: publisher_handler.hpp:14