1 from StringIO
import StringIO
2 from struct
import Struct, pack, unpack
10 match = re.match(
"(.*)\[([\d]*)\]", field_string)
12 num_string = match.group(2)
13 return (match.group(1), int(num_string))
15 return (field_string, 1)
17 field.replace(
"__",
"/")
20 slots = list(msg.__slots__)
21 slot_types = list(msg._slot_types)
22 fmt_stream = StringIO()
24 for slot, slot_type
in zip(slots, slot_types):
26 field_type = parsed_type[0]
27 field_length = parsed_type[1]
28 if field_type ==
"char":
29 fmt_stream.write(
"c" * field_length)
30 elif field_type ==
"bool":
31 fmt_stream.write(
"?" * field_length)
32 elif field_type ==
"int8":
33 fmt_stream.write(
"b" * field_length)
34 elif field_type ==
"uint8":
35 fmt_stream.write(
"B" * field_length)
36 elif field_type ==
"int16":
37 raise Exception(
"int16 is not supported")
38 elif field_type ==
"uint16":
39 fmt_stream.write(
"H" * field_length)
40 elif field_type ==
"int32":
41 fmt_stream.write(
"i" * field_length)
42 elif field_type ==
"uint32":
43 fmt_stream.write(
"I" * field_length)
44 elif field_type ==
"int64":
45 fmt_stream.write(
"q" * field_length)
46 elif field_type ==
"uint64":
47 fmt_stream.write(
"Q" * field_length)
48 elif field_type ==
"float32":
49 fmt_stream.write(
"f" * field_length)
50 elif field_type ==
"float64":
51 fmt_stream.write(
"d" * field_length)
52 elif field_type ==
"string":
53 raise Excception(
"string is not supported!, please use char[static_length]")
54 elif field_type ==
"duration" or field_type ==
"time":
55 raise Excception(
"duration and time are not supported")
56 return fmt_stream.getvalue()
59 if value_type ==
"uint8":
66 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
67 slot_value = getattr(msg, slot)
69 field_type = parsed_type[0]
70 if hasattr(slot_value,
"__len__"):
71 for i
in range(len(slot_value)):
75 data.append(slot_value)
76 packed = pack(fmt, *data)
80 if field_type ==
"bool":
81 return [v == 1
for v
in array]
86 if field_type ==
"bool":
92 unpacked_data = unpack(fmt, data)
95 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
96 slot_value = getattr(msg, slot)
98 field_type = parsed_type[0]
99 field_length = parsed_type[1]
100 target_data = unpacked_data[counter:counter + field_length]
101 if hasattr(slot_value,
"__len__"):
104 setattr(msg, slot,
unpackValue(target_data[0], field_type))
105 counter = counter + field_length
110 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
111 topic_name = prefix +
"/" + slot.replace(
"__",
"/")
113 msg_class = roslib.message.get_message_class(slot_type)
115 raise Exception(
"invalid topic type: %s"%slot_type)
116 ret.append(rospy.Publisher(topic_name, msg_class, latch=latch))
121 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
122 topic_name = prefix +
"/" + slot.replace(
"__",
"/")
123 ret[topic_name] = getattr(msg, slot)
129 for slot, slot_type
in zip(msg.__slots__, msg._slot_types):
130 topic_name =
"/" + slot.replace(
"__",
"/")
132 msg_class = roslib.message.get_message_class(slot_type)
134 raise Exception(
"invalid topic type: %s"%slot_type)
135 ret.append((topic_name, msg_class))
143 def __init__(self, seq_id, id, num, data, packet_size):
150 return pack(
"!III%ds" % (len(self.
data)),
158 unpacked = unpack(
"!III%ds" % (len(data) - cls.
headerSize()), data)
159 ret = cls(unpacked[0], unpacked[1], unpacked[2], unpacked[3],
165 buffer_packet_size = packet_size - LargeDataUDPPacket.headerSize()
166 num_packet = len(buffer) / buffer_packet_size
167 if len(buffer) % buffer_packet_size != 0:
168 num_packet = num_packet + 1
170 for i
in range(num_packet):
172 buffer[i*buffer_packet_size:(i+1)*buffer_packet_size],