Template Class RealtimePublisher

Class Documentation

template<class MessageT>
class RealtimePublisher

Public Types

using PublisherType = rclcpp::Publisher<MessageT>

Provide various typedefs to resemble the rclcpp::Publisher type.

using PublisherSharedPtr = typename rclcpp::Publisher<MessageT>::SharedPtr
using PublishedType = typename rclcpp::TypeAdapter<MessageT>::custom_type
using ROSMessageType = typename rclcpp::TypeAdapter<MessageT>::ros_message_type

Public Functions

inline explicit RealtimePublisher(PublisherSharedPtr publisher)

Constructor for the realtime publisher.

Starts a dedicated thread for message publishing. The publishing thread runs the publishingLoop() function to handle message delivery in a non-realtime context.

Parameters:

publisher – the ROS publisher to wrap

inline ~RealtimePublisher()

Destructor.

inline void stop()

Stop the realtime publisher.

Signals the publishing thread to exit by setting keep_running_ to false and notifying the condition variable. This allows the publishing loop to break out of its wait state and exit cleanly.

inline bool can_publish() const

Check if the realtime publisher is in a state to publish messages.

Returns:

true if the publisher is in a state to publish messages

inline bool try_publish(const MessageT &msg)

Try to publish the given message.

This method attempts to publish the given message if the publisher is in a state to do so. It uses a try_lock to avoid blocking if the mutex is already held by another thread.

Parameters:

msg[in] The message to publish

Returns:

true if the message was successfully published, false otherwise

inline std::thread &get_thread()

Get the thread object for the publishing thread.

This can be used to set thread properties.

inline const std::thread &get_thread() const

Get the thread object for the publishing thread.

This can be used to set thread properties.

inline const MessageT &get_msg() const
inline std::mutex &get_mutex()

Get the mutex protecting the stored message.

inline const std::mutex &get_mutex() const

Get the mutex protecting the stored message.

Public Members

MessageT msg_