1#ifndef MRS_LIB_PUBLISHER_IMPL_HPP_
2#define MRS_LIB_PUBLISHER_IMPL_HPP_
4#include "mrs_lib/publisher.hpp"
13#include <rclcpp/create_publisher.hpp>
14#include <rclcpp/publisher.hpp>
16#include "mrs_lib/internal/rate_throttle.hpp"
17#include "mrs_lib/logger.hpp"
27 template <
class MessageType>
31 using RosPublisherPimpl = Pimpl<rclcpp::Publisher<MessageType>, std::shared_ptr<rclcpp::Publisher<MessageType>>>;
34 Impl(
const PublisherOptions& options, std::string_view topic_name)
35 : node_interfaces_(options.node_interfaces),
36 logger_(node_interfaces_),
37 unresolved_topic_name_(topic_name),
38 publisher_(rclcpp::create_publisher<MessageType>(node_interfaces_, std::string(topic_name), options.qos))
40 if (options.throttle_duration.has_value())
42 throttle_.emplace(internal::RateThrottle(options.throttle_duration.value()));
45 logger_.info(
"Created publisher on topic '{}' -> '{}'", unresolved_topic_name_, publisher_->get_topic_name());
49 Impl(
const Impl&) =
delete;
50 Impl& operator=(
const Impl&) =
delete;
51 Impl(Impl&&) =
delete;
52 Impl& operator=(Impl&&) =
delete;
54 template <
typename Message>
57 bool should_publish = handle_publish_rate();
61 publisher_->publish(std::forward<Message>(msg));
67 return publisher_->get_topic_name();
72 return unresolved_topic_name_;
77 return publisher_->get_subscription_count();
93 bool handle_publish_rate()
95 if (!throttle_.has_value())
101 auto guard = throttle_->acquire();
102 return guard->accept(node_interfaces_.get_node_clock_interface()->get_clock()->now());
106 mrs_lib::Logger logger_;
108 std::string unresolved_topic_name_;
111 std::optional<OwningMutex<internal::RateThrottle>> throttle_;
113 RosPublisherPimpl publisher_;
117 template <
class MessageType>
122 template <
class MessageType>
128 template <
class MessageType>
131 impl_->publish(std::move(msg));
134 template <
class MessageType>
137 return impl_->get_topic_name();
140 template <
class MessageType>
143 return impl_->get_unresolved_topic_name();
146 template <
class MessageType>
149 return impl_->get_subscriber_count();
Wrapper for rclcpp::Publisher.
Definition publisher.hpp:48
void publish(const MessageType &msg)
Publish a message.
Definition publisher.impl.hpp:123
std::string get_unresolved_topic_name() const
Returns the unresolved name of the topic handled by this publisher.
Definition publisher.impl.hpp:141
std::string get_topic_name() const
Returns the resolved (full) name of the topic handled by this publisher.
Definition publisher.impl.hpp:135
size_t get_subscriber_count() const
Get number of subscribers on the published topic.
Definition publisher.impl.hpp:147
Publisher(const PublisherOptions &options, std::string_view topic_name)
construct a publisher using the specified options.
Definition publisher.impl.hpp:118
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
rclcpp::node_interfaces::NodeInterfaces< rclcpp::node_interfaces::NodeTopicsInterface, rclcpp::node_interfaces::NodeClockInterface, rclcpp::node_interfaces::NodeLoggingInterface > PublisherNodeInterfaces
ROS node interfaces required by the Publisher.
Definition publisher.hpp:25
Definition publisher.hpp:31