Using parameters in your nodes
We will continue with the code we have written in the previous chapters. Here, we will improve the number_publisher
node. As a quick recap, this node publishes a number on a topic, at a given rate. The number and publishing rate are directly written in the code.
Now, instead of hardcoding the number and publishing rate values, we will use parameters. This way, we will be able to specify what number to publish, and the publishing frequency or period, when we start the node.
You need to follow two steps to be able to use a parameter in your code:
- Declare the parameter in the node. This will make the parameter exist within the node so that you can set a value to it when starting the node with
ros2 run
. - Retrieve the parameter’s value so that you can use it in the code.
Let’s start with Python, and later on, we will also see the C++ code.
Declaring, getting, and using parameters with Python
Before using a parameter...