-
Notifications
You must be signed in to change notification settings - Fork 11
/
rosmsg.py
283 lines (249 loc) · 10.5 KB
/
rosmsg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
ROS (Robot Operating System) Message Parser
"""
from threading import Thread
import struct
import math
from osgar.bus import BusShutdownException
ROS_MESSAGE_TYPES = {
'std_msgs/String': '992ce8a1687cec8c8bd883ec73ca41d1',
'geometry_msgs/Twist': '9f195f881246fdfa2798d1d3eebca84a',
'std_msgs/Imu': '6a62c6daae103f4ff57a132d6f95cec2',
}
def prefix4BytesLen(s):
"adding ROS length"
if type(s) == str:
s = bytes(s, encoding='ascii')
return struct.pack("I", len(s)) + s
def parse_imu( data ):
# http://docs.ros.org/api/sensor_msgs/html/msg/Imu.html
size = struct.unpack_from('<I', data)[0]
assert size in [324, 323], size # expected IMU packet size (beware of variable frameIdLen - should be removed!)
data = data[4:]
seq, stamp, stampNsec, frameIdLen = struct.unpack("IIII", data[:16])
# print(seq, stamp, stampNsec, frameIdLen)
# print(data[16:16+frameIdLen])
data = data[16+frameIdLen:]
# variable frameIdLen:
# b'X1/base_link' = 12 bytes
# b'X3/imu_link' = 11 bytes
assert len(data) == 296, len(data) # fixed data size (37 * 8)
orientation = struct.unpack("dddd", data[:4*8])
data = data[4*8+9*8:] # skip covariance matrix
angularVelocity = struct.unpack("ddd", data[:3*8])
data = data[3*8+9*8:] # skip velocity covariance
linearAcceleration = struct.unpack("ddd", data[:3*8])
# print('%d\t%f' % (stamp, sum([x*x for x in linearAcceleration])))
data = data[3*8+9*8:] # skip velocity covariance
assert len(data) == 0, len(data)
q0, q1, q2, q3 = orientation # quaternion
x = math.atan2(2*(q0*q1+q2*q3), 1-2*(q1*q1+q2*q2))
y = math.asin(2*(q0*q2-q3*q1))
z = math.atan2(2*(q0*q3+q1*q2), 1-2*(q2*q2+q3*q3))
# print('%d\t%f' % (stamp, math.degrees(y)))
return x, y, z # maybe later use orientation
def Xparse_image( data ):
seq, stamp, stampNsec, frameIdLen = struct.unpack("IIII", data[:16])
# print(seq, stamp, stampNsec, frameIdLen)
data = data[16+frameIdLen:]
# from rosbag.py
def parse_raw_image(data, dump_filename=None):
# http://docs.ros.org/melodic/api/sensor_msgs/html/msg/Image.html
size = struct.unpack_from('<I', data)[0]
assert size == 230467, size # expected size for raw image during experiment
# http://docs.ros.org/melodic/api/std_msgs/html/msg/Header.html
pos = 4
seq, timestamp_sec, timestamp_nsec, frame_id_size = struct.unpack_from('<IIII', data, pos)
pos += 4 + 4 + 4 + 4
frame_id = data[pos:pos+frame_id_size]
# print(frame_id, timestamp_sec, timestamp_nsec)
pos += frame_id_size
height, width, encoding_size = struct.unpack_from('<III', data, pos)
pos += 4 + 4 + 4
encoding = data[pos:pos+encoding_size]
pos += encoding_size
# print(height, width, encoding)
is_bigendian, step, image_arr_size = struct.unpack_from('<BII', data, pos)
pos += 1 + 4 + 4
if dump_filename is not None:
with open(dump_filename, 'wb') as f:
f.write(b'P6\n%d %d\n255\n' % (width, height))
f.write(data[pos:pos+image_arr_size])
def parse_jpeg_image(data, dump_filename=None):
# http://docs.ros.org/api/sensor_msgs/html/msg/CompressedImage.html
size = struct.unpack_from('<I', data)[0]
assert size < 230467, size # expected size for raw image during experiment
# http://docs.ros.org/melodic/api/std_msgs/html/msg/Header.html
pos = 4
seq, timestamp_sec, timestamp_nsec, frame_id_size = struct.unpack_from('<IIII', data, pos)
pos += 4 + 4 + 4 + 4
frame_id = data[pos:pos+frame_id_size]
# print(frame_id, timestamp_sec, timestamp_nsec)
pos += frame_id_size
encoding_size = struct.unpack_from('<I', data, pos)[0]
pos += 4
encoding = data[pos:pos+encoding_size]
pos += encoding_size
# print(encoding, size)
img_size = struct.unpack_from('<I', data, pos)[0]
pos += 4
if dump_filename is not None:
with open(dump_filename, 'wb') as f:
f.write(data[pos:])
return data[pos:]
def parse_laser(data):
# http://docs.ros.org/melodic/api/sensor_msgs/html/msg/LaserScan.html
size = struct.unpack_from('<I', data)[0]
assert size == 5826, size # expected size for short lidar
pos = 4
seq, timestamp_sec, timestamp_nsec, frame_id_size = struct.unpack_from('<IIII', data, pos)
pos += 4 + 4 + 4 + 4
frame_id = data[pos:pos+frame_id_size]
# print(frame_id, timestamp_sec, timestamp_nsec)
pos += frame_id_size
params = struct.unpack_from('<fffffff', data, pos)
# angle_min, angle_max, angle_increment, time_increment, scan_time
# range_min, range_max
# print(params)
pos += 7*4
ranges_size = struct.unpack_from('<I', data, pos)[0]
assert ranges_size == 720, ranges_size
pos += 4
scan = struct.unpack_from('<' + 'f' * ranges_size, data, pos)
pos += 4 * ranges_size
intensities_size = struct.unpack_from('<I', data, pos)[0]
assert intensities_size == 720, intensities_size
intensities = struct.unpack_from('<' + 'f' * intensities_size, data, pos)
# conversion to int and millimeters
scan = [int(x*1000) if x < 65.0 else 0 for x in scan]
return scan
def parse_odom(data):
# http://docs.ros.org/melodic/api/nav_msgs/html/msg/Odometry.html
size = struct.unpack_from('<I', data)[0]
assert size in [719, 724], size # expected size for odometry (beware of variable header! i.e. this assert is wrong in general)
pos = 4
seq, timestamp_sec, timestamp_nsec, frame_id_size = struct.unpack_from('<IIII', data, pos)
pos += 4 + 4 + 4 + 4
frame_id = data[pos:pos+frame_id_size]
# print(frame_id, timestamp_sec, timestamp_nsec)
pos += frame_id_size
child_frame_id = struct.unpack_from('<I', data, pos)[0]
pos += 4
#print(data[pos:pos + child_frame_id])
pos += child_frame_id
# http://docs.ros.org/melodic/api/geometry_msgs/html/msg/PoseWithCovariance.html
# http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Pose.html
# http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Point.html
# http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Quaternion.html
x, y, z = struct.unpack_from('<ddd', data, pos)
pos += 3 * 8
ori = struct.unpack_from('<dddd', data, pos)
pos += 4 * 8
# float64[36] covariance
pos += 36 * 8
#print(x, y, z)
# http://docs.ros.org/melodic/api/geometry_msgs/html/msg/Twist.html
linear = struct.unpack_from('<ddd', data, pos)
pos += 3 * 8
angular = struct.unpack_from('<ddd', data, pos)
pos += 3 * 8
# float64[36] covariance
pos += 36 * 8
assert pos == len(data), (pos, len(data))
q0, q1, q2, q3 = ori # quaternion
ax = math.atan2(2*(q0*q1+q2*q3), 1-2*(q1*q1+q2*q2))
ay = math.asin(2*(q0*q2-q3*q1))
az = math.atan2(2*(q0*q3+q1*q2), 1-2*(q2*q2+q3*q3))
return (x, y, ax)
def parse_points(data):
# http://docs.ros.org/melodic/api/sensor_msgs/html/msg/PointCloud2.html
size = struct.unpack_from('<I', data)[0]
assert size == 2457728, size # expected size for cloud points (beware of variable header! i.e. this assert is wrong in general)
pos = 4
seq, timestamp_sec, timestamp_nsec, frame_id_size = struct.unpack_from('<IIII', data, pos)
pos += 4 + 4 + 4 + 4
frame_id = data[pos:pos+frame_id_size]
# print(frame_id, timestamp_sec, timestamp_nsec)
pos += frame_id_size
height, width = struct.unpack_from('<II', data, pos)
pos += 8
# print(height, width)
point_field_size = struct.unpack_from('<I', data, pos)[0]
pos += 4
assert point_field_size == 4, point_field_size # RGBD?
# print(data[pos:pos + 20])
for i in range(point_field_size):
str_size = struct.unpack_from('<I', data, pos)[0]
pos += 4
assert str_size < 10, str_size
# print(data[pos:pos + str_size])
pos += str_size
offset, datatype, count = struct.unpack_from('<IBI', data, pos)
# print(offset, datatype, count)
assert datatype == 7, datatype # uint8 FLOAT32 = 7
assert count == 1, count
pos += 9
is_bigendian, point_step, row_step = struct.unpack_from('<?II', data, pos)
pos += 9
# print(is_bigendian, point_step, row_step)
arr_size = struct.unpack_from('<I', data, pos)[0]
pos += 4
assert arr_size == height * width * 32, arr_size
for i in range(height*width):
pt = struct.unpack_from('<ffff', data, pos + i * 32)
assert pt[3] == 0.0, pt
# if not math.isnan(pt[0]):
# print(pt[:3])
# assert str(pt) == str((nan, nan, nan, 0.0)), (i, pt)
pos += row_step * height
is_dense = struct.unpack_from('<?', data, pos)[0]
pos += 1
# print(is_dense)
assert pos == len(data), (pos, len(data))
class ROSMsgParser(Thread):
def __init__(self, config, bus):
Thread.__init__(self)
self.setDaemon(True)
self.bus = bus
self._buf = b''
self.topic_type = config.get('topic_type')
def get_packet(self):
data = self._buf
if len(data) < 4:
return None
size = struct.unpack_from('<I', data, 0)[0]
# print(size, len(self._buf))
assert size > 0, size
size += 4 # the length prefix
if len(data) < size:
return None
ret, self._buf = data[:size], data[size:]
return ret
def run(self):
try:
header = None
# initial message contains structure
while True:
timestamp, channel, data = self.bus.listen()
self._buf += data
packet = self.get_packet()
if packet is not None:
if header is None:
header = packet
elif self.topic_type == 'sensor_msgs/CompressedImage':
self.bus.publish('image', parse_jpeg_image(packet))
elif self.topic_type == 'sensor_msgs/LaserScan':
self.bus.publish('scan', parse_laser(packet))
elif self.topic_type == 'nav_msgs/Odometry':
x, y, heading = parse_odom(packet)
self.bus.publish('pose2d', [round(x*1000),
round(y*1000),
round(math.degrees(heading)*100)])
elif self.topic_type == 'std_msgs/Imu':
self.bus.publish('rot', [round(math.degrees(angle)*100)
for angle in parse_imu(packet)])
except BusShutdownException:
pass
def request_stop(self):
self.bus.shutdown()
# vim: expandtab sw=4 ts=4