4 PYTHON2 = sys.version_info < (3, 0)
12 LIST_TYPES = [list, tuple]
13 INT_TYPES = [
'byte',
'char',
'int8',
'uint8',
'int16',
'uint16',
'int32',
'uint32',
'int64',
'uint64']
14 FLOAT_TYPES = [
'float32',
'float64']
15 STRING_TYPES = [
'string']
17 TIME_TYPES = [
'time',
'duration']
18 BOOL_ARRAY_TYPES = [
'bool[]']
19 BYTESTREAM_TYPES = [
'uint8[]',
'char[]']
23 TAGGED_ARRAY_FORMATS = {
24 'uint16[]': (69,
'<{}H'),
25 'uint32[]': (70,
'<{}I'),
26 'uint64[]': (71,
'<{}Q'),
27 'byte[]': (72,
'{}b'),
28 'int8[]': (72,
'{}b'),
29 'int16[]': (77,
'<{}h'),
30 'int32[]': (78,
'<{}i'),
31 'int64[]': (79,
'<{}q'),
32 'float32[]': (85,
'<{}f'),
33 'float64[]': (86,
'<{}d'),
38 """Extract a dictionary of CBOR-friendly values from a ROS message. 40 Primitive values will be casted to specific Python primitives. 42 Typed arrays will be tagged and packed into byte arrays. 45 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
46 val = getattr(msg, slot)
52 if slot_type
in STRING_TYPES:
53 out[slot] = unicode(val)
if PYTHON2
else str(val)
56 elif slot_type
in BOOL_TYPES:
60 elif slot_type
in INT_TYPES:
64 elif slot_type
in FLOAT_TYPES:
65 out[slot] = float(val)
68 elif slot_type
in TIME_TYPES:
70 'secs': int(val.secs),
71 'nsecs': int(val.nsecs),
75 elif slot_type
in BYTESTREAM_TYPES:
77 out[slot] = bytes(bytearray(val))
79 out[slot] = bytes(val)
82 elif slot_type
in BOOL_ARRAY_TYPES:
83 out[slot] = [bool(i)
for i
in val]
86 elif slot_type
in TAGGED_ARRAY_FORMATS:
87 tag, fmt = TAGGED_ARRAY_FORMATS[slot_type]
88 fmt_to_length = fmt.format(len(val))
89 packed = struct.pack(fmt_to_length, *val)
90 out[slot] =
Tag(tag=tag, value=packed)
93 elif type(val)
in LIST_TYPES:
def extract_cbor_values(msg)