Implementing ROS 2 topics in C++
This section will show how to implement a publisher and subscriber using C++ with rclcpp APIs. We will see how to implement a publisher node using custom ROS 2 message interfaces and standard ROS 2 messages. The publisher’s objective is to publish string data using the standard string msg-type and a string with an integer using a custom message interface. The subscriber node will subscribe to both of these topics.
Figure 3.3 shows how communication happens between the publisher and subscriber nodes. The names of the topics the publisher publishes are /custom_topic and /std_string_topic. The /custom_topic publishes custom ROS 2 messages, whereas std_string_topic uses standard string ROS 2 messages.

Figure 3.3: ROS 2 publisher and subscriber
In Figure 3.3, you can see publisher_node on the left and subscriber_node on the right. In between, we can see the topics these nodes are publishing and subscribing to.
Now, let’...