ros2_control - rolling
Public Types | Public Member Functions | Public Attributes | List of all members
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

 RealtimePublisher (PublisherSharedPtr publisher)
 Constructor for the realtime publisher. More...
 
 ~RealtimePublisher ()
 Destructor.
 
void stop ()
 Stop the realtime publisher from sending out more ROS messages.
 
bool trylock ()
 Try to get the data lock from realtime. More...
 
bool tryPublish (const MessageT &msg)
 Try to get the data lock from realtime and publish the given message. More...
 
void unlockAndPublish ()
 Unlock the msg_ variable. More...
 
void lock ()
 Get the data lock form non-realtime. More...
 
void unlock ()
 Unlocks the data without publishing anything.
 
std::thread & get_thread ()
 
const std::thread & get_thread () const
 

Public Attributes

MessageT msg_
 The msg_ variable contains the data that will get published on the ROS topic.
 

Constructor & Destructor Documentation

◆ RealtimePublisher()

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

Constructor for the realtime publisher.

Parameters
publisherthe publisher to wrap

Member Function Documentation

◆ lock()

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

Get the data lock form non-realtime.

To publish data from the realtime loop, you need to run trylock to attempt to get unique access to the msg_ variable. Trylock returns true if the lock was acquired, and false if it failed to get the lock.

◆ trylock()

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

Try to get the data lock from realtime.

To publish data from the realtime loop, you need to run trylock to attempt to get unique access to the msg_ variable. Trylock returns true if the lock was acquired, and false if it failed to get the lock.

◆ tryPublish()

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

Try to get the data lock from realtime and publish the given message.

Tries to gain unique access to msg_ variable. If this succeeds update the msg_ variable and call unlockAndPublish

Returns
false in case no lock for the realtime variable could be acquired

◆ unlockAndPublish()

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

Unlock the msg_ variable.

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: