Ejemplo n.º 1
0
    def _change_config(self, config, level):
        self.config = self.callback(config, level)
        if self.config is None:
            msg = 'Reconfigure callback should return a possibly updated configuration.'
            rospy.logerr(msg)
            raise DynamicReconfigureCallbackException(msg)

        self._copy_to_parameter_server()

        self.update_topic.publish(encode_config(self.config))

        return self.config
Ejemplo n.º 2
0
    def update_configuration(self, changes, timeout=None):
        """
        Change the server's configuration

        @param changes: dictionary of key value pairs for the parameters that are changing
        @type  changes: {str: value}
        """
        # Retrieve the parameter descriptions
        if self.param_description is None:
            self.get_parameter_descriptions(timeout)

        # Cast the parameters to the appropriate types
        if self.param_description is not None:
            for name, value in list(changes.items())[:]:
                if name != 'groups':
                    dest_type = self._param_types.get(name)
                    if dest_type is None:
                        raise DynamicReconfigureParameterException(
                            'don\'t know parameter: %s' % name)

                    try:
                        found = False
                        descr = [
                            x for x in self.param_description
                            if x['name'].lower() == name.lower()
                        ][0]

                        # Fix not converting bools properly
                        if dest_type is bool and type(value) is str:
                            changes[name] = value.lower() in ("yes", "true",
                                                              "t", "1")
                            found = True
                        # Handle enums
                        elif type(value
                                  ) is str and not descr['edit_method'] == '':
                            enum_descr = eval(descr['edit_method'])
                            found = False
                            for const in enum_descr['enum']:
                                if value.lower() == const['name'].lower():
                                    val_type = self._param_type_from_string(
                                        const['type'])
                                    changes[name] = val_type(const['value'])
                                    found = True
                        if not found:
                            if sys.version_info.major < 3:
                                if type(value) is unicode:
                                    changes[name] = unicode(value)
                                else:
                                    changes[name] = dest_type(value)
                            else:
                                changes[name] = dest_type(value)

                    except ValueError as e:
                        raise DynamicReconfigureParameterException(
                            'can\'t set parameter \'%s\' of %s: %s' %
                            (name, str(dest_type), e))

        if 'groups' in changes.keys():
            changes['groups'] = self.update_groups(changes['groups'])

        config = encode_config(changes)

        try:
            msg = self._set_service(config).config
        except ServiceException as e:
            raise DynamicReconfigureCallbackException('service call failed')

        if self.group_description is None:
            self.get_group_descriptions(timeout)
        resp = decode_config(msg, self.group_description)

        return resp