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

roslaunch: add further condition in launch-prefix

asked 2020-12-04 03:19:29 -0500

holunder gravatar image

updated 2020-12-04 04:32:37 -0500

To bind a node to a specific I'm using the approach mentioned here.

As I want to use the launch file on different hardware, I need to check whether the number of cpu cores allows this setting. In other words, assign a fixed CPU only if this cpu is available on actual hardware. In bash, I tried something like this, which seems to work

if (("$(nproc)" > 4));then taskset -c 5;fi;

When I try to add this to launch-prefix I get an error:

<node pkg = "my_pkg" name = "myname" type = "mytype" launch-prefix ="if ((""$(nproc)"" > 4));then taskset -c 5;fi;"/>


Invalid roslaunch XML syntax: not well-formed (invalid token): line 14, column 119 The traceback for the exception was written to the log file

Other ideas I had were using the output of nproc as an argument, but I didn't manage to do so.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-04 04:42:43 -0500

gvdhoorn gravatar image

updated 2020-12-04 04:45:19 -0500

Values in launch-prefix are really used as prefixes: so instead of starting simply

binary_a [rest of args]

the command line changes to

$value_of_launch_prefix binary_a [rest of args]

So whatever you have as a launch-prefix must result in a valid command line when concatenated with the name of the binary and the rest of the arguments.

Separate from this, your .launch file will still need to be valid XML (as that's what .launch files are), which is what your immediate problem is: you cannot embed " in an XML attribute like you do now. It can be done, but you'd probably end up with all sorts of XML entity substitutions which may be hard to get right.

Easiest way out would probably be to write a separate Bash script which takes care of all of this. Then you'd pass the path to the script as your launch-prefix and you can make your conditional logic as complex as a Bash script would allow.

(you're obviously not limited to Bash scripts, anything which is executable could be used)

edit flag offensive delete link more


Thanks. I like the idea of adding a script with the conditional operator as launch-prefix. It took me a while to understand the the binary and args are simply further arguments of the bash. Now, it works well with this code

if (( `nproc` > 4 ));then
  taskset -c $1 ${@:2}
holunder gravatar image holunder  ( 2020-12-04 12:02:43 -0500 )edit

Question Tools

1 follower


Asked: 2020-12-04 03:18:38 -0500

Seen: 1,402 times

Last updated: Dec 04 '20