In this tutorial, we first present how to create a simple subscribers in C++ and then we'll explore message filters, an useful tool to combine multiple streams of messages.
Related Readings
Create a simple subscriber
Creating subscribers in ROS2 is a relatively simple process that requires specifying a topic and a callback function. Typically, this callback function is a member function of a node. To adapt a member function into a function reference suitable for the subscriber, we can employ techniques such as std::bind or a lambda function. For instance:
While CLion suggests using the lambda function format, the choice ultimately boils down to personal preference. It's important to note that the callback function can accept the message reference as its argument.
It's important to note that the callback accepts the pointer of the message as its argument.
Demo setup
In the demo code, we will set up
Two publisher nodes. Each of these publishers emits a string message at a fixed rate.The first publisher does so every second, while the second publisher sends out its message every three seconds.
A subscriber node. The subscriber node subscribes to the topics using two different method. The first method involves the standard subscription created from the node. Custom callback functions are detailed in the demo code, showcasing synchronization techniques. The second method uses the message_filters::Subscriber class and pass the two message streams to an approximate time synchronizer. A callback function that outputs the synced message pairs is attached to the filter.
A message filter can be viewed as a function applied to one or more incoming message streams and generates an output message stream. The message_filters package in ROS2 provides tools such as the Subscriber class and policy-based Synchronizer class to streamline the process of combining message streams. The following code snippet illustrates the creation and application of a message filter using the approximate time synchronizer. An illustration of the synchronization algorithm can be found at this .