Writing a topic subscriber
To continue improving our application, let’s create a new node that will subscribe to the /number
topic. Each number that’s received will be added to a counter. We want to print the counter every time it’s updated.
As we did previously, let’s start the full explanations with Python, and then see the syntax specificities with C++.
Writing a Python subscriber
You can find the complete code for this Python node on GitHub. Many things we need to do here are identical to what we did previously, so I won’t fully detail every step. Instead, we will focus on the most important things so that we can write the subscriber.
Creating a Python node with a subscriber
Create a new node named number_counter
inside the my_py_pkg
package:
$ cd ~/ros2_ws/src/my_py_pkg/my_py_pkg/ $ touch number_counter.py $ chmod +x number_counter.py
In this file, you can write the code for the node and add a subscriber. Here’s the...