def encode_config(config, flat=True): msg = ConfigMsg() for k, v in config.items(): ## @todo add more checks here? if type(v) == int: msg.ints.append(IntParameter(k, v)) elif type(v) == bool: msg.bools.append(BoolParameter(k, v)) elif type(v) == str: msg.strs.append(StrParameter(k, v)) elif sys.version_info.major < 3 and isinstance(v, unicode): msg.strs.append(StrParameter(k, v)) elif type(v) == float: msg.doubles.append(DoubleParameter(k, v)) elif type(v) == dict or isinstance(v, Config): if flat is True: def flatten(g): groups = [] for _name, group in g['groups'].items(): groups.extend(flatten(group)) groups.append(GroupState(group['name'], group['state'], group['id'], group['parent'])) return groups msg.groups.append(GroupState(v['name'], v['state'], v['id'], v['parent'])) msg.groups.extend(flatten(v)) else: msg.groups = [GroupState(x['name'], x['state'], x['id'], x['parent']) for x in v] return msg
def generate_ros_msg(self): msg = Config() b = BoolParameter() b.name = "antiwindup" b.value = False msg.bools = [b] p = DoubleParameter() p.name = "p" p.value = self.p i = DoubleParameter() i.name = "i" i.value = self.i d = DoubleParameter() d.name = "d" d.value = self.d msg.doubles = [p, i, d] g = GroupState() g.name = "Default" g.state = True g.id = 0 g.parent = 0 msg.groups = [g] return msg