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

roslaunch XML "if" comparison

asked 2015-05-26 15:32:45 -0600

Jordan9 gravatar image

updated 2015-05-27 14:57:16 -0600

In the page on roslaunch XML, it discusses the use of "if" and "unless" attributes for tags. In the examples, however, it only shows how to do it if the argument you are checking is a boolean.

I need to do something like this:

<arg name="mode" default="simulate"/> 
<group unless="$(arg mode) == simulate">
     <!-- start a second node -->
</group>

where the "if" attribute is doing a comparison instead of checking a boolean. This syntax throws an error though. Is there an alternative way to do this?

EDIT: To give more info as to why this is complicated, I've edited the code sample a bit. Note that I need to start a new node unless the "mode" argument is of a specific type. That second node requires a whole bunch of arguments and parameters that are defined earlier in the master launch file. This makes the proposed solution of including sub-launch files more complex, because for each one of those files I would have to import a whole array of arguments to start that node, and there are a LOT of modes (and "simulate" is the only one where I don't want to start the second node). The reason I was asking about getting an argument back from a sub-launch file is because then I could just set a simple boolean in each sub-launch file, and in the master file (where all of the many arguments are available) I could start the second node if that boolean is true.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2020-08-12 23:10:31 -0600

lalala gravatar image

updated 2021-05-27 06:55:38 -0600

130s gravatar image

Since ROS Kinetic onward, you can use eval.

like this

if="$(eval model == '123')"

and your code should be written like this

<arg name="mode" default="simulate"/> 
<group unless="$(eval mode == 'simulate')">
     <!-- start a second node -->
</group>
edit flag offensive delete link more

Comments

I marked this as a selected answer now that this feature has been available for awhile (Even Kinetic is End-of-Life now).

130s gravatar image 130s  ( 2021-05-27 06:54:56 -0600 )edit
4

answered 2015-05-26 15:37:15 -0600

ahendrix gravatar image

I don't think the roslaunch XML syntax supports more complex comparisons.

Instead, you could conditionally include a launch file or configuration file with the same name as your mode:

<arg name="mode" default="simulate"/>
<include file="$(find my_package)/launch/modes/$(arg mode).launch"/>
<rosparam command="load" file="$(find my_package)/config/modes/$(arg mode).yaml"/>
edit flag offensive delete link more

Comments

Yep, this could work. Having some difficulty with it though. If I define an argument inside the .launch file that gets included, that argument doesn't seem to be available in the parent .launch file (it just says it "needs to be set"). Also, arguments from the parent aren't available in the child.

Jordan9 gravatar image Jordan9  ( 2015-05-27 11:43:46 -0600 )edit

roslaunch args are not global; you have to explicitly declare them and explicit pass them when you include launch files: http://wiki.ros.org/roslaunch/XML/arg

ahendrix gravatar image ahendrix  ( 2015-05-27 12:11:37 -0600 )edit

So I can declare them going into the include file, is there a way to use arguments that are declared in the include file in the parent file? (i.e. the reverse direction)

Jordan9 gravatar image Jordan9  ( 2015-05-27 12:27:51 -0600 )edit

no; you can't use arguments from an included launch file in the outer launch file. It sounds like what you're trying to do is a lot more complex than the simple question you asked...

ahendrix gravatar image ahendrix  ( 2015-05-27 14:35:01 -0600 )edit

I updated the OP to explain why it's way more complicated than I originally made it sound.

Jordan9 gravatar image Jordan9  ( 2015-05-27 14:57:34 -0600 )edit
1

I'm not sure of any easy way to implement what you're asking for. Perhaps you just need a separate launch file for simulation, or an explicit boolean parameter indicating if you're running in simulation or not?

ahendrix gravatar image ahendrix  ( 2015-06-03 02:47:55 -0600 )edit
-2

answered 2017-10-04 02:06:48 -0600

toblech gravatar image

This is supported as of ROS Kintec.

<group if="$(arg foo)">
  <!-- stuff that will only be evaluated if foo is true -->
</group>

See also http://wiki.ros.org/roslaunch/XML

edit flag offensive delete link more

Comments

This only works for booleans; it doesn't work for the more complex comparisons requested in the initial post.

ahendrix gravatar image ahendrix  ( 2017-10-04 15:21:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-05-26 15:32:45 -0600

Seen: 7,300 times

Last updated: May 27 '21