Optional list of strings as launch argument in python

asked 2022-10-20 11:08:28 -0500

Dben gravatar image

updated 2022-10-20 11:19:04 -0500

How can I pass optional list of strings as parameter on python?

Here is my current test

In my launch:

my_param = LaunchConfiguration("my_param")
...
DeclareLaunchArgument(
    "my_param",
    default_value=[],
),
...
launch_ros.actions.Node(
    package="X", executable="Y",
    parameters=[
        { 
            "my_param": my_param,
        }
    ],

And then, in my node class:

declare_parameter(
    name="my_param",
    descriptor=ParameterDescriptor(
        type=ParameterType.PARAMETER_STRING_ARRAY,
    ),
)

my_param = self.get_parameter("my_param").get_parameter_value().string_array_value

It triggers this:

rclpy.exceptions.InvalidParameterTypeException: Trying to set parameter 'my_param' to '' of type 'STRING', expecting type 'STRING_ARRAY'

It seems the variable is converted to a string somehow in the meantime.

Edit: it seems I was right https://answers.ros.org/question/3415.... By passing default_value="['']", instead, and then ignoring it in my code, it works. But this is not clean...Any alternative?

edit retag flag offensive close merge delete