ros2_control - jazzy
Loading...
Searching...
No Matches
realtime_tools::RealtimePublisher< MessageT > Class Template Reference

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 Member Functions

template<typename NodeT>
 RealtimePublisher (NodeT &&node, const std::string &topic_name, const rclcpp::QoS &qos, const rclcpp::PublisherOptions &options=rclcpp::PublisherOptions())
 Constructor for the realtime publisher that creates the publisher internally.
 RealtimePublisher (PublisherSharedPtr publisher)
 Constructor for the realtime publisher.
 ~RealtimePublisher ()
 Destructor.
void stop ()
 Stop the realtime publisher.
bool trylock ()
 Try to acquire the data lock for non-realtime message publishing.
bool can_publish () const
 Check if the realtime publisher is in a state to publish messages.
bool try_publish (const MessageT &msg)
 Try to publish the given message.
bool tryPublish (const MessageT &msg)
 Try to publish the given message (deprecated).
void unlockAndPublish ()
 Unlock the msg_ variable for the non-realtime thread to start publishing.
void lock ()
 Acquire the data lock.
void unlock ()
 Unlocks the data without publishing anything.
std::thread & get_thread ()
const std::thread & get_thread () const
const MessageT & get_msg () const
std::mutex & get_mutex ()
const std::mutex & get_mutex () const

Public Attributes

MessageT msg_

Constructor & Destructor Documentation

◆ RealtimePublisher() [1/2]

template<class MessageT>
template<typename NodeT>
realtime_tools::RealtimePublisher< MessageT >::RealtimePublisher ( NodeT && node,
const std::string & topic_name,
const rclcpp::QoS & qos,
const rclcpp::PublisherOptions & options = rclcpp::PublisherOptions() )
inlineexplicit

Constructor for the realtime publisher that creates the publisher internally.

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

Parameters
nodethe node (or node interface/pointer) to create the publisher for
topic_namethe topic name on which we want to publish
qosthe QoS settings for the publisher
optionsthe publisher options

◆ RealtimePublisher() [2/2]

template<class MessageT>
realtime_tools::RealtimePublisher< MessageT >::RealtimePublisher ( PublisherSharedPtr publisher)
inlineexplicit

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
publisherthe ROS publisher to wrap

Member Function Documentation

◆ can_publish()

template<class MessageT>
bool realtime_tools::RealtimePublisher< MessageT >::can_publish ( ) const
inline

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

Returns
true if the publisher is in a state to publish messages
Note
The msg_ variable can be safely accessed if this function returns true

◆ lock()

template<class MessageT>
void realtime_tools::RealtimePublisher< MessageT >::lock ( )
inline

Acquire the data lock.

This blocking call acquires exclusive access to the msg_ variable. Use trylock() for non-blocking attempts to acquire the lock.

◆ stop()

template<class MessageT>
void realtime_tools::RealtimePublisher< MessageT >::stop ( )
inline

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.

◆ try_publish()

template<class MessageT>
bool realtime_tools::RealtimePublisher< MessageT >::try_publish ( const MessageT & msg)
inline

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
[in]msgThe message to publish
Returns
true if the message was successfully published, false otherwise

◆ trylock()

template<class MessageT>
bool realtime_tools::RealtimePublisher< MessageT >::trylock ( )
inline

Try to acquire the data lock for non-realtime message publishing.

It first checks if the current state allows non-realtime message publishing (turn_ == REALTIME) and then attempts to lock

Returns
true if the lock was successfully acquired, false otherwise

◆ tryPublish()

template<class MessageT>
bool realtime_tools::RealtimePublisher< MessageT >::tryPublish ( const MessageT & msg)
inline

Try to publish the given message (deprecated).

Deprecated
This method is deprecated and should be replaced with try_publish()

This method is deprecated and should be replaced with try_publish(). It 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
[in]msgThe message to publish
Returns
true if the message was successfully published, false otherwise

◆ unlockAndPublish()

template<class MessageT>
void realtime_tools::RealtimePublisher< MessageT >::unlockAndPublish ( )
inline

Unlock the msg_ variable for the non-realtime thread to start publishing.

After a successful trylock and after the data is written to the mgs_ variable, the lock has to be released for the message to get published on the specified topic.


The documentation for this class was generated from the following file: