ros2 / launch

Tools for launching multiple processes and for writing tests involving multiple processes.
Apache License 2.0
126 stars 144 forks source link

Substitutions for string manipulations and xacro #741

Open nlamprian opened 1 year ago

nlamprian commented 1 year ago

Feature request

Feature description

Coming back with another request for a substitution... I tried using a path join substitution with a filename that depends on a launch configuration. The path join substitution does not allow one to concatenate parts of a filename. It ended up using a python expression.

xacro_filename = PythonExpression(["'", LaunchConfiguration("model"), "' + '.xacro'"])
xacro_filepath = PathJoinSubstitution(["robot_description", "urdf", xacro_filename])

Implementation considerations

I would have hoped that text substitution could take multiple texts so that I could do

TextSubstitution(LaunchConfiguration("model"), ".xacro")

But this is not compatible, given how text substitution was put together.

So, how about having a substitution that concatenates strings and then also having the separator parameterized?

In the simple case, one would do

StringSubstitution([LaunchConfiguration("model"), ".xacro"])  # -> "model_value.xacro"

And then, this could be used in other practical ways, for example, to create a xacro substitution

class XacroSubstitution(Command):
    def __init__(
        self, 
        filepath: SomeSubstitutionsType, 
        arg_str: SomeSubstitutionsType = "",  # xacro arguments that are passed as a launch argument
        arg_dict: Dict = None,  # xacro arguments that are defined in the launch file
    ):
        command = [FindExecutable(name="xacro"), " ", filepath, " "]
        arg_str, arg_dict = normalize_to_list_of_substitutions(arg_str), arg_dict or {}
        arg_list = [StringSubstitution([key, value], sep=":=") for key, value in arg_dict.items()]
        command.extend(StringSubstitution(arg_list + arg_str, sep=" "))
        super().__init__(command)
matosinho commented 10 months ago

I did the same with Python expression for ".sdf". That was the exact thing I tried, to use a list with TextSubstitution, but then I discovered that it was not possible. Nice suggestion!