Robotics StackExchange | Archived questions

roslaunch: add further condition in launch-prefix

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;"/>

Error:

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.

Asked by holunder on 2020-12-04 04:18:38 UTC

Comments

Answers

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)

Asked by gvdhoorn on 2020-12-04 05:42:43 UTC

Comments

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

#!/bin/bash
if (( `nproc` > 4 ));then
  taskset -c $1 ${@:2}
fi

Asked by holunder on 2020-12-04 13:02:43 UTC