Validating non-consecutive enums in dynamic reconfigure

asked 2019-09-06 04:10:30 -0600

Magnus gravatar image

updated 2019-09-06 05:41:27 -0600

I'm using dynamic reconfigure to set a property to three possible values: 1, 3 or 5. I've used the edit_method as per the tutorials to limit the user's input to these 3 enum values.

#!/usr/bin/env python

PACKAGE='example_node'
import roslib; roslib.load_manifest(PACKAGE)

from dynamic_reconfigure.parameter_generator_catkin import *

gen = ParameterGenerator()

test_enum = gen.enum([ gen.const("1m", int_t, 1, "1 meter"),
                       gen.const("3m", int_t, 3, "3 meters"),
                       gen.const("5m", int_t, 5, "5 meters")],
                       "Use to set the test value")

gen.add("test_number", int_t, 0, "Must be 1, 3 or 5.", 3, 1, 5, edit_method=test_enum)

exit(gen.generate(PACKAGE, "example_node", "ExampleNode"))

This works correctly via the rqt_reconfigure GUI. However, using the python API's update_configuration or rosrun dynamic_reconfigure dynparam set example_node test_number 4, the value can set to anything between the min and max range, including 2 and 4 which should be invalid.

The dynamic_reconfig examples only show enums with consecutive values, which means values between the valid values are not possible.

Is there a way to validate non-consecutive enums, so only the specific values are allowed?

The only solution I can think of is to validate the enums in the dynamic reconfigure callback in my C++ node, then updateConfig to set the value to something valid. But this feels like a work-around, there must be a better way built into dynamic_reconfigure.

edit retag flag offensive close merge delete