I think this tutorial about ROS2 Global Parameters can help you:
In this tutorial I will show you how
to create a sort of “global parameter
server” node to keep ROS2 global
parameters for all your other nodes.
The thing is, in ROS2, params are
specific to a specific node. You set
params for a node, you start the node,
and if you kill the node, the params
are gone ( if you don’t know how ROS2
params work, first read: how to
get/set params from a node with Python
and Cpp).
So, what can you do if you want to:
Keep some parameters alive for the entire duration of the application,
And have parameters that are used by multiple nodes?
Well, there is a way to solve that
problem. You create a node that keeps
some global parameters. When you start
your application, the first thing you
do is start this node, which stays
alive as long as your application is
alive. Then, any node you create at
launch time, or at any time after
that, can retrieve parameters from
this “global parameter server”.
Note that doing this is not
necessarily the best solution for your
application. It’s actually kind of a
hack, and goes in the opposite way of
how ROS2 was designed. So, before you
do that, be sure there is no other
option.
Notes at the end are especially important:
ROS2 global parameters: good idea or not?
"If you’re starting your ROS2 application and directly implement a global parameter server, well, stop right now and think twice.
There are many debates on what’s the best way to handle “global” parameters, and one of the question you should ask first is not “how to do” but “do I need to do that for my application?”.
ROS2, by design, and for good reasons, makes you create parameters that are specific to a node. Unless you’re a true ROS expert, it might be good to try to follow the framework rules and conventions.
Here are some alternatives:
- If that’s possible for you, you could create a variable on top of your launch file, and pass it to all of your nodes as a parameter. Thus, all nodes (at launch time) will get this parameter. However, if you manually add another node after that, it won’t be able to retrieve the parameter from a “global” source.
- Some robots, like the turtlebot3, uses an environment variable for the robot’s name. Not necessarily what you should do, but that’s an option.
- Instead of a global parameter node, you could have a database (for example with sqlite) containing global values.
- …
Also, it could be interesting to look at your design choices. Maybe you have to create global parameters because of a poorly designed application. In this case it’s maybe better to work on your design instead of finding little hacks to make things work.
If you have carefully examined ... (more)