3 Created on Wed Oct 21 11:27:24 2020
10 from common
import connectionHandler
15 from matplotlib
import cm
16 from matplotlib.colors
import ListedColormap, LinearSegmentedColormap
17 import matplotlib.pyplot
as plt
18 from mpl_toolkits.mplot3d
import Axes3D
21 msgpack_tokenized_keys_str2int = {
30 "ChannelTheta" : 0x50,
34 "PropertiesValues" : 0x54,
36 "TimestampStart" : 0x71,
37 "TimestampStop" : 0x72,
45 "SegmentCounter" : 0x91,
47 "Availability" : 0x93,
51 "TelegramCounter" : 0xB0,
52 "TimestampTransmit" : 0xB1
54 msgpack_tokenized_keys_int2str = {
63 0x50 :
"ChannelTheta",
67 0x54 :
"PropertiesValues",
69 0x71 :
"TimestampStart",
70 0x72 :
"TimestampStop",
78 0x91 :
"SegmentCounter",
80 0x93 :
"Availability",
84 0xB0 :
"TelegramCounter",
85 0xB1 :
"TimestampTransmit"
94 return msgpack_tokenized_keys_str2int[s]
98 def __init__(self, scannerIP = "192.168.0.1", hostIP = "192.168.0.102", port=2115, nbScans = 100, writeMsgpackFile = ""):
125 self.
restAPI.postRequest(
"setPort", {
'args': {
'port': self.
port}})
126 self.
restAPI.postRequest(
"setIpAddress", {
'args': {
'ipAddress': self.
hostIP}})
128 print(
"REST API not active, nothing to setup")
136 UDPsocket.sendData(
"magicalActivate")
139 self.
restAPI.postRequest(
"start")
142 for i
in range(0, self.
nbScans):
143 data = UDPsocket.waitOnNewScan()
144 if(UDPsocket.hasNoError() ==
True):
145 print(
"received scan", i)
154 with open(msgpackfile +
".msgpack",
"wb")
as f:
156 payload_hex_str =
' '.join(format(b,
'02X')
for b
in payload)
157 with open(msgpackfile +
".msgpack.hex",
"w")
as f:
158 f.write(payload_hex_str)
161 print(
"Error extracting payload from data")
163 print(
"error receiving scan ",i)
166 self.
restAPI.postRequest(
"stop")
172 stxString = b
'\x02\x02\x02\x02'
176 stxReceived = msgAsBytes[0:4]
177 if stxString != stxReceived:
179 print(
"Error receiving stx")
185 stringLength = len(msgAsBytes) - 12
186 payloadlength = int.from_bytes(msgAsBytes[4:8],
'little')
187 if stringLength < payloadlength:
189 print(
"Error payloadlengths do not match: received payloadlength =", stringLength,
"decoded payloadlength =", payloadlength)
193 payLoad = msgAsBytes[8:8+payloadlength]
196 crcReceived = int.from_bytes(msgAsBytes[8+payloadlength:8+payloadlength+4],
'little')
197 crcComputed = zlib.crc32(payLoad)
198 if crcReceived != crcComputed:
200 print(
"Error Checksum: crcReceived =", crcReceived,
"crcComputed =", crcComputed)
209 dataDict = msgpack.unpackb(payload, strict_map_key=
False)
234 print(
"multiscan_receiver: parseData failed")
240 nbLayers = len(scanVector)
241 segmentXYZI = [
None for _
in range(nbLayers)]
242 for layerIdx
in range(nbLayers):
244 segmentXYZI[layerIdx] = [
None for _
in range(nbEchoes)]
252 elevation = -1 * elevationPseudoArr[0]
253 formatFloatArray = str(nbBeams ) +
'f'
255 azimuth = np.asarray(azimuthPseudoArr)
259 print(
"phi =", elevationPseudoArr[0])
260 print(
"theta =", azimuth)
263 for echoIdx
in range(nbEchoes):
264 segmentXYZI[layerIdx][echoIdx] = np.zeros([nbBeams , 4])
265 destArr = segmentXYZI[layerIdx][echoIdx]
268 dist = np.asarray(distPseudoArr) * 0.001
270 rssi = np.asarray(rssiPseudoArr)
273 destArr[:,0] = dist * np.cos(azimuth) * np.cos(elevation)
274 destArr[:,1] = dist * np.sin(azimuth) * np.cos(elevation)
275 destArr[:,2] = dist * np.sin(elevation)
280 print(
"dist =", distPseudoArr)
281 print(
"rssi =", rssiPseudoArr)
292 self.
ax = self.
fig.add_subplot(111, projection=
'3d')
298 self.
ax.set_title(
'multiScan Emulation Segment {}'.format(self.
showCount))
299 self.
ax.set_xlim(-1, 1)
300 self.
ax.set_ylim(-1, 1)
301 self.
ax.set_zlim(-1, 1)
302 self.
ax.set_xlabel(
'X Label')
303 self.
ax.set_ylabel(
'Y Label')
304 self.
ax.set_zlabel(
'Z Label')
306 for layerIdx
in range(len(pointCloud)):
307 for echoIdx
in range(len(pointCloud[layerIdx])):
308 curCloud = pointCloud[layerIdx][echoIdx]
309 self.
ax.scatter(curCloud[:, 0], curCloud[:, 1], curCloud[:, 2], s=1)
312 plt.show(block=
False)
322 with open(fileName,
"rb")
as f:
324 msgpack_dict = msgpack.unpackb(byte_data)
326 for d
in msgpack_dict:
327 if 'BeginMagic' in d:
328 dataDict = d[
'Tracks'][0][
'Data'][0]
330 frameNumber = dataDict[
'data'][
'FrameNumber']
331 segmentCounter = dataDict[
'data'][
'SegmentCounter']
332 telegramCounter = dataDict[
'data'][
'TelegramCounter']
333 timeStampTransmit = dataDict[
'data'][
'TimestampTransmit']
335 print(
"tgmCounter = {}, timeStampTransmit = {}, frameNumber = {}, segmentCounter = {}"
336 .format(telegramCounter, timeStampTransmit, frameNumber, segmentCounter))
339 segmentData = dataDict[
'data'][
'SegmentData']
345 if __name__ ==
"__main__":
346 receiver =
MultiScanReceiver(scannerIP =
"127.0.0.1", hostIP =
"127.0.0.1", port = 2115, nbScans = 1000, writeMsgpackFile =
"./multiscan_dump")
351 receiver.setEnableVisualization(
False)
352 receiver.setEnableMagicalActivate(
False)
353 receiver.setEnableRestAPI(
False)
354 receiver.setupRecording()
355 receiver.doRecording()
357 receiver.readFromMSGPACKFile(
"data\\recordFile_16.msgpack")
359 print(
"Error: Unkown mode")