Cannot use empty lists as default value for parameters using rclpy

asked 2022-12-14 11:03:58 -0500

jdayboll gravatar image

I've been trying to declare a parameter using the following call:

self.declare_parameter(
    devices_param,
    [],
    ParameterDescriptor(
        description='Devices on %s output' % output.name,
        type=ParameterType.PARAMETER_STRING_ARRAY))

The follow error occurs when I try to set the parameters through a config file:

rclpy.exceptions.InvalidParameterTypeException: Trying to set parameter 'outputs.5v.devices' to '['radio']' of type 'STRING_ARRAY', expecting type 'BYTE_ARRAY'

This error occurs because the ParameterDescriptor type is ignored, and instead the default value's type is inferred. Since BYTE_ARRAY is the first array type in the enum, it is chosen for an empty list.

I found an issue regarding this behavior with this proposed workaround:

param = Parameter('my_param', type_=Parameter.Type.STRING_ARRAY, value=[])
node.declare_parameter(param.name, param.get_parameter_value())

However, this doesn't fix the problem. Looking into the source code, there doesn't seem to be a situation where this approach ever works since it tries to instantiate a new Parameter class using the ParameterValue from get_parameter_value, which isn't what Parameter's constructor is expecting.

I know I can use dynamic typing to get around this, but I am trying to maintain a degree of backwards compatibility to Foxy, which doesn't have the dynamic_typing flag (Foxy's default is to not use static types in parameters).

Does anyone have a solution to using empty lists with parameters in Galactic?

edit retag flag offensive close merge delete