ros launch get param in launch file to append to param from config file
Using a common configuration file (not to be modified) - package_config.yaml:
param0: param0_value
param1: param1_value
...
Is it possible to append and override certain params only through the launch file? For example:
<launch>
<arg name="ns" value="namespace0"/>
<node name="node_name" pkg="package" type="package_node" clear_params="true">
<rosparam command="load" file="$(find package)/cfg/package_config.yaml"/>
<param name="param0" value="$(arg ns)$(param param0)"/>
</node>
</launch>
I know that param overriding from the launch file is allowed (calling <param name="param0" value="override_value"/>
after loading params from yaml file <rosparam command="load" file="$(find package)/cfg/package_config.yaml"/>
).
But (for some reason that is beyond me) there is no way to access params (e.g. $(param param0)
) in the param server from a launch file.
Is there any way to append to existing params from a launch file?
Asked by noob_ros on 2020-09-15 03:01:58 UTC
Answers
The .xml
launch files do not support reading from the ROS parameter server.
You can go around this limitation by creating a simple python script which reads from the parameter server, concatenates the value, and then stores the concatenated value back to the parameter server. Then you can call this script from your .launch
files passing the .xml
arguments to the python function.
my_launch.launch
file example:
<node
pkg="my_pkg"
name="add_to_param_server"
type="add_to_param_server.py"
args="$(value)"
output="screen"
/>
add_to_param_server.py
example:
import rospy, sys
if __name__ == '__main__':
try:
value = rospy.myargv(argv=sys.argv)[-1]
var = '/thing_to_append'
if not rospy.has_param(var):
rospy.set_param(var, [value])
else:
rospy.set_param(var, rospy.get_param(var) + [value])
except rospy.ROSInterruptException:
pass
Then you can just roslaunch my_launch.launch value=:<something>
. You can add another arg
to select the variable in the ROS parameter server you want to append to.
Asked by lsrosa on 2022-03-11 07:56:26 UTC
Comments
This has been discussed before: parameters are written before nodes are started. And this is for good reason: it keeps everything consistent (ie: all parameters defined in .launch
files are present on the parameters server, so all nodes see the same values).
The approach you suggest cannot guarantee that all nodes see the same parameters, as your script will likely have been started after a couple other node
s have already been running, and before a few others.
There is no way to do what the OP asks.
Edit: re-reading the OP's request, there might actually be a way -- although it won't be 'overriding' anything.
rosparam
supports the subst_value
attribute, which makes roslaunch
accept substitution args in the .yaml
. If the values he wants to override can be passed as roslaunch
arg
s, he could 'read' those using substitution args.
Asked by gvdhoorn on 2022-03-11 08:01:16 UTC
You are right, the approach I proposed does not append the value prior to the starting of the nodes. But it works if you are launching a second file. So it might be useful.
I did not find an way to use subst_value
that allow me to read from the parameter server. Do you have an example? That would be really helpful.
Best,
Asked by lsrosa on 2022-03-11 09:13:45 UTC
Comments