Created
November 5, 2015 01:55
-
-
Save peci1/912549b79fd6e8801023 to your computer and use it in GitHub Desktop.
ROS dynamic_reconfigure enum with variable value set.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def change_enum_items(type, parameter_name, new_items, default=None): | |
""" | |
Take an enum-typed parameter in the given autogenerated cfg type and reset its possible values to new_items. | |
You can then start a dynamic_reconfigure server which advertises the changed enum domain. | |
To achieve this, you need to call this function before creating the server. | |
:param type type: One of the autogenerated config types (package.cfg.*Config). | |
:param basestring parameter_name: Name of the enum parameter to change. | |
:param new_items: The items that will form the new domain of the enum. | |
:type new_items: array of {'name': ..., 'value': ..., 'description': ...} | |
:param any default: If provided, this value is used as the default. If not, the first value in new_items is used. | |
:raises RuntimeError: If there is no valid enum parameter with the given name in the given type. | |
""" | |
if not hasattr(type, 'config_description') or not hasattr(type, 'defaults'): | |
raise RuntimeError('Type %s is not a valid dynamic reconfigure type.' % str(type)) | |
for param_desc in type.config_description['parameters']: | |
if param_desc['name'] == parameter_name: | |
if param_desc['edit_method'] == '': | |
raise RuntimeError('Type %s has empty edit_method, which means it is not a proper enum.' % str(type)) | |
edit_method = eval(param_desc['edit_method']) | |
enum = edit_method['enum'] | |
if len(enum) == 0: | |
raise RuntimeError('Type %s edit_method has empty enum, which means it is not a proper enum.' % | |
str(type)) | |
# we will copy all "unnecesarry" info (line number, filename etc.) from the first item | |
sample_item = enum[0] | |
new_enum = [] | |
for item in new_items: | |
new_item = sample_item.copy() | |
new_item['name'] = item['name'] | |
new_item['value'] = item['value'] | |
new_item['description'] = item['description'] | |
new_enum.append(new_item) | |
edit_method['enum'] = new_enum | |
param_desc['edit_method'] = repr(edit_method) | |
# if no default value was explicitly specified, use the first value | |
if default is None: | |
default = new_enum[0]['value'] | |
param_desc['default'] = default | |
type.defaults[param_desc['name']] = default | |
return | |
raise RuntimeError('Parameter "%s" not found.' % parameter_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment