mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
publisher_handler.h
Go to the documentation of this file.
1// clang: TomasFormat
6#ifndef PUBLISHER_HANDLER_H
7#define PUBLISHER_HANDLER_H
8
9#include <memory>
10
11#include <rclcpp/rclcpp.hpp>
12#include <rclcpp/node.hpp>
13#include <rclcpp/time_source.hpp>
14
15#include <atomic>
16#include <string>
17#include <mutex>
18#include <optional>
19
20#include <mrs_lib/internal/rate_throttle.hpp>
21
22namespace mrs_lib
23{
24
25 struct PublisherHandlerOptions
26 {
27 PublisherHandlerOptions(const rclcpp::Node::SharedPtr& node) : node(node)
28 {
29 }
30
31 PublisherHandlerOptions() = default;
32
33 rclcpp::Node::SharedPtr node;
34
38 double throttle_rate = 0;
39
40 rclcpp::QoS qos = rclcpp::SystemDefaultsQoS();
41 };
42
43 /* class PublisherHandler_impl //{ */
44
48 template <class TopicType>
50 {
51
52 public:
57
62
69 PublisherHandler_impl(const PublisherHandlerOptions& options, const std::string& address);
70
77 void publish(const TopicType& msg);
78
84 void publish(const std::shared_ptr<TopicType>& msg);
85
91 /* void publish(const std::shared_ptr<TopicType const>& msg); */
92
98 void publish(typename TopicType::ConstSharedPtr msg);
99
105 void publish(std::unique_ptr<TopicType> msg);
106
112 unsigned int getNumSubscribers(void);
113
114 private:
115 rclcpp::Node::SharedPtr node_;
116
117 std::shared_ptr<rclcpp::Publisher<TopicType>> publisher_;
118
119 std::mutex mutex_publisher_;
120
121 std::atomic<bool> publisher_initialized_;
122
123 std::string address_;
124
125 std::optional<internal::RateThrottle> throttle_;
126
130 bool throttleAccept(void);
131 };
132
133 //}
134
135 /* class PublisherHandler //{ */
136
140 template <class TopicType>
142 {
143
144 public:
149
154
163
170
177 PublisherHandler(const rclcpp::Node::SharedPtr& node, const std::string& address);
178
185 PublisherHandler(const PublisherHandlerOptions& options, const std::string& address);
186
192 void publish(const TopicType& msg);
193
199 void publish(const std::shared_ptr<TopicType>& msg);
200
206 /* void publish(const std::shared_ptr<TopicType const>& msg); */
207
213 void publish(typename TopicType::ConstSharedPtr msg);
214
220 void publish(std::unique_ptr<TopicType> msg);
221
227 unsigned int getNumSubscribers(void);
228
229 private:
230 std::shared_ptr<PublisherHandler_impl<TopicType>> impl_;
231 };
232
233 //}
234
235} // namespace mrs_lib
236
237#ifndef PUBLISHER_HANDLER_HPP
238#include <mrs_lib/impl/publisher_handler.hpp>
239#endif // PUBLISHER_HANDLER_H
240
241#endif // PUBLISHER_HANDLER_H
unsigned int getNumSubscribers(void)
get number of subscribers
Definition publisher_handler.hpp:167
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:52
~PublisherHandler_impl(void)
default destructor
Definition publisher_handler.h:61
PublisherHandler_impl(void)
default constructor
Definition publisher_handler.hpp:17
user wrapper of the publisher handler implementation
Definition publisher_handler.h:142
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:247
PublisherHandler & operator=(const PublisherHandler &other)
operator=
Definition publisher_handler.hpp:186
~PublisherHandler(void)
generic destructor
Definition publisher_handler.h:153
unsigned int getNumSubscribers(void)
get number of subscribers
Definition publisher_handler.hpp:303
PublisherHandler(void)
generic constructor
Definition publisher_handler.h:148
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Definition publisher_handler.h:26
double throttle_rate
when > 0, the publisher output is limited to this rate in [Hz]
Definition publisher_handler.h:38