0
$\begingroup$

Rosanswers logo

I am trying to make a launch file which checks an argument value (string) and loads parameters accordingly. I don't seem to find any documentation regarding this, the if/unless attributes section in the roslaunch wiki only works with booleans. Here's what I'm trying to achieve:

<arg name="mode"                 default="mode_a"/> <!-- mode_a, mode_b, mode_c... -->

<node pkg="node" type="node_type" name="node_name" output="screen">
    <rosparam command="load" file="$(find test)/config/mode_a_params.yaml" if="$(arg mode)"=="mode_a" />
    <rosparam command="load" file="$(find test)/config/mode_d_params.yaml" unless="$(arg mode)"=="mode_b" />
</node>

But since if/unless work only with booleans, this is not possible. The following code shows my work-around. I made two new flag arguments (boolean), but the issue here is that I need to set the flags every time I change the mode.

<arg name="mode"                 default="mode_a"/> <!-- mode_a, mode_b, mode_c... -->
<arg name="mode_a_flag"      default="true"/> <!-- set to true if mode = mode_a, else false -->
<arg name="mode_c_flag"      default="false"/> <!-- set to true if mode = mode_c, else false -->

<node pkg="node" type="node_type" name="node_name" output="screen">
    <rosparam command="load" file="$(find test)/config/mode_a_params.yaml" if="$(arg mode_a_flag)" />
    <rosparam command="load" file="$(find test)/config/mode_d_params.yaml" unless="$(arg mode_c_flag)" />
</node>

Is there a better way of doing this? I'm looking for a way where I just need to set the mode when launching the file and the flags are set automatically in the launch file.


Originally posted by adityakamath on ROS Answers with karma: 30 on 2021-09-02

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You can do this using eval which evaluates an expression and returns a boolean. Eg, in your case you can do

<rosparam command="load" file="$(find test)/config/mode_a_params.yaml" if="$(eval arg('mode') == 'mode_a')" />

Originally posted by janindu with karma: 849 on 2021-09-02

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by adityakamath on 2021-09-03:
This is exactly what I was looking for, thank you!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.