This tutorial is a practical guide on how to use rosparam command line tool to debug ROS Params.
ROS params are really useful to launch your ROS application with different setup settings, without having to re-write and re-compile any part of your code. With the rosparam command line tool, you’ll be able to test your programs even faster when developing.
Launch file for this example
Let’s create a simple launch file which we’ll fill with 3 simple parameters. In a ROS package (in this example: my_robot_tutorials), create a launch/ folder. Inside this folder, create a launch file (in this example: example_rosparam.launch).
<launch> <param name="my_integer" type="int" value="7" /> <param name="my_float" type="double" value="3.14" /> <param name="my_string" type="str" value="hello" /> </launch>
Run this launch file:
- First make sure that you have run
catkin_makeso your environment knows that your package exists.
roslaunch my_robot_tutorials example_rosparam.launch.
A ROS master has been started and all your params are loaded inside the Parameter Server. Now let’s use rosparam to understand and do some debugging!
Find all the params (rosparam list)
rosparam list you can see the entire list of all params running in your Parameter Server.
$ rosparam list /my_float /my_integer /my_string /rosdistro /roslaunch/uris/host_user_pc__41077 /rosversion /run_id
We directly found the 3 params we previously set in the launch file!
rosparam list you only see the parameters’ name, not their value (we’ll get to that just below).
Also, as you can guess, as your application grows, so do the number of parameters. It’s not uncommon to have hundreds of parameters, and in this case it becomes quite hard to find what you want. Pro tip: use
grep to list only the params that match a specific string:
$ rosparam list | grep 'my_' /my_float /my_integer /my_string
Get a parameter’s value (rosparam get)
Now that you have the list of available parameters, simply run
rosparam get followed by the name of a parameter, to get its value.
$ rosparam get /my_float 3.14
If the parameter you are looking for doesn’t exist, you’ll get this error:
$ rosparam get /this_param_doesnt_exist ERROR: Parameter [/this_param_doesnt_exist] is not set
Set a parameter’s value (rosparam set)
With rosparam set, you can do 2 things: create a new parameter, or change the value of an existing one.
Let’s create a few new parameters. Note: each param’s name will start with a leading slash “/”, whether you manually add it or not, which means they are located in the “global” namespace.
$ rosparam set /new_param_int 7 $ rosparam set new_param_str "Another string" $ rosparam list | grep 'new_param' /new_param_int /new_param_str
Quite easy, right? The type (int, double, string, …) will be automatically set depending on the value you give.
Now, let’s modify an existing parameter.
$ rosparam get /my_float 3.14 $ rosparam set /my_float 5.06 $ rosparam get /my_float 5.06
The parameter “/my_float” has now a new value.
Important note: If you have fetched a parameter inside a node, and you change the parameter’s value after that, then it doesn’t change the value inside the node! You either have to kill and restart the node, or use dynamic_reconfigure.
Delete a parameter (rosparam delete)
Finally, if you can create a parameter and change its value, you can also choose to delete it from the Parameter Server, using
$ rosparam delete /new_param_str $ rosparam get /new_param_str ERROR: Parameter [/new_param_str] is not set
Again, if you delete a param that was previously used by a node in its setup phase, then the value inside the node stays the same. Only new nodes trying to fetch the parameter won’t be able to get it.
Using namespaces with rosparam
As you may know, it’s quite useful to organize your nodes, params, services, and topics with namespaces.
When you use no namespace, all parameters are set in the global namespace. If you want to use a specific namespace (or multiple ones), then you just need to add the namespaces in front of the parameter, each namespace separated by a slash “/”.
$ rosparam set /robot1/camera1/brightness 0.5 $ rosparam set /robot1/battery/milliamp 15000 $ rosparam set /robot2/camera1/brightness 0.8
Then it becomes easier for your nodes to get different params for different parts of your application.
And again, don’t forget the power of
$ rosparam list | grep '/robot1' /robot1/battery/milliamp /robot1/camera1/brightness
Going further with rosparam command line tool
rosparam dump and
rosparam load commands will help you debug your parameters when using YAML files for ROS params <– Check out this tutorial to know more about it.
All in all, the
rosparam command line tool is mostly used for debugging during development.
In production code all your params will be either loaded from YAML files or be started in different launch files. Then, you’ll get the parameters directly inside your nodes using rospy and roscpp.