Publisher

mrs_lib::Publisher

template<class MessageType>
class Publisher

Wrapper for rclcpp::Publisher.

Template Parameters:

MessageType – Type of the message to publish.

Public Functions

Publisher(const PublisherOptions &options, std::string_view topic_name)

construct a publisher using the specified options.

Parameters:
  • options – Configuration of the publisher.

  • topic_name – Name of the topic to publish to.

void publish(const MessageType &msg)

Publish a message.

Parameters:

msg – Message to publish.

void publish(std::unique_ptr<MessageType> msg)

Publish a message.

Parameters:

msg – Message to publish.

std::string get_topic_name() const

Returns the resolved (full) name of the topic handled by this publisher.

Returns:

The name of the handled topic.

std::string get_unresolved_topic_name() const

Returns the unresolved name of the topic handled by this publisher.

Returns:

The name of the handled topic.

size_t get_subscriber_count() const

Get number of subscribers on the published topic.

Returns:

The number of subscribers.

mrs_lib::PublisherOptions

struct PublisherOptions

Public Members

PublisherNodeInterfaces node_interfaces

ROS node interfaces used by the publisher.

rclcpp::QoS qos = rclcpp::SystemDefaultsQoS()

QOS settings for the publisher.

std::optional<std::chrono::nanoseconds> throttle_duration = std::nullopt

If set, the publisher will drop messages published within this time of a previous message.

Example

Example node using the mrs_lib::Publisher.
 1class ExampleNode : public rclcpp::Node
 2{
 3public:
 4  ExampleNode(const rclcpp::NodeOptions& options = rclcpp::NodeOptions{})
 5      : Node("example_node", options),
 6        logger_(*this),
 7        publisher_(
 8            mrs_lib::PublisherOptions{
 9                .node_interfaces = *this,
10            },
11            "example_topic")
12  {
13  }
14
15  void work()
16  {
17    auto val = next_val_.fetch_add(1);
18    logger_.info("Publishing value: {}", val);
19
20    std_msgs::msg::Int64 msg;
21    msg.data = val;
22
23    publisher_.publish(msg);
24  }
25
26private:
27  mrs_lib::Logger logger_;
28
29  std::atomic<int64_t> next_val_ = 1;
30
31  mrs_lib::Publisher<std_msgs::msg::Int64> publisher_;
32};