ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

I think this tutorial about ROS2 Global Parameters can help you:

  • https://roboticsbackend.com/ros2-global-parameters/

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 the situation, and there is no other way than to create a global parameter server, then try to reduce the scope. If the parameters only apply to a sub-system, then don’t make them global to your application. Every application is different, so adapt this technique every time you need to use it. This is not a “one size fits all” solution.

All in all, the most important thing is that your robot works correctly, and that you keep your code modular enough, so you can scale your application later without too much hassle. If you manage to do that, with our without global parameters, well, that’s a job well done!"