def
get_depth_array
(self):
'''
This function reads the data from the serial port and returns it
as
an array of 12 bit values with the shape 8x8
'''
got_frame =
False
while
not
got_frame:
with
self.serial_lock:
frame = self.port.readline()
if
len(frame) ==
269
:
if
ord(frame[
0
]) ==
0x11
and
self.crc_check(frame):
#
Check for range frame header and crc
dec_out = []
for
i
in
range(
1
,
65
):
rng = ord(frame[
2
* i -
1
]) <<
7
rng = rng | (ord(frame[
2
* i]) &
0x7F
)
dec_out.append(rng &
0x0FFF
)
depth_array = [dec_out[i:i +
8
]
for
i
in
range(
0
,
len(dec_out),
8
)]
depth_array = np.array(depth_array)
got_frame =
True
else
:
"Invalid frame length: {}"
.format(len(frame))
depth_array.astype(np.uint16)
return
depth_array
def
crc_check
(self, frame):
index = len(frame) -
9
# Start of CRC
crc_value = (ord(frame[index]) &
0x0F
) <<
28
crc_value |= (ord(frame[index +
1
]) &
0x0F
) <<
24
crc_value |= (ord(frame[index +
2
]) &
0x0F
) <<
20
crc_value |= (ord(frame[index +
3
]) &
0x0F
) <<
16
crc_value |= (ord(frame[index +
4
]) &
0x0F
) <<
12
crc_value |= (ord(frame[index +
5
]) &
0x0F
) <<
8
crc_value |= (ord(frame[index +
6
]) &
0x0F
) <<
4
crc_value |= (ord(frame[index +
7
]) &
0x0F
)
crc_value = crc_value &
0xFFFFFFFF
crc32 = self.crc32(frame[:index])
if
crc32 == crc_value:
return
True
else
:
Copyright
©
Terabee 2018
Terabee, 90 Rue Henri Fabre
01630, St Genis-Pouilly, France (next to CERN)
20/22