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

roslaunch if/unless for non-boolean arguments

asked 2021-09-02 17:39:41 -0500

adityakamath gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-09-02 20:28:53 -0500

janindu gravatar image

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')" />
edit flag offensive delete link more

Comments

This is exactly what I was looking for, thank you!

adityakamath gravatar image adityakamath  ( 2021-09-03 05:47:06 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2021-09-02 17:39:41 -0500

Seen: 1,234 times

Last updated: Sep 02 '21