-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathparser_mmw_demo.py
319 lines (255 loc) · 16.1 KB
/
parser_mmw_demo.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# ****************************************************************************
# * (C) Copyright 2020, Texas Instruments Incorporated. - www.ti.com
# ****************************************************************************
# *
# * Redistribution and use in source and binary forms, with or without
# * modification, are permitted provided that the following conditions are
# * met:
# *
# * Redistributions of source code must retain the above copyright notice,
# * this list of conditions and the following disclaimer.
# *
# * Redistributions in binary form must reproduce the above copyright
# * notice, this list of conditions and the following disclaimer in the
# * documentation and/or other materials provided with the distribution.
# *
# * Neither the name of Texas Instruments Incorporated nor the names of its
# * contributors may be used to endorse or promote products derived from
# * this software without specific prior written permission.
# *
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# * PARTICULAR TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# * A PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# * EXEMPLARY, ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# *
# import the required Python packages
import struct
import math
import binascii
import codecs
import numpy as np
# definations for parser pass/fail
TC_PASS = 0
TC_FAIL = 1
def getUint32(data):
"""!
This function coverts 4 bytes to a 32-bit unsigned integer.
@param data : 1-demension byte array
@return : 32-bit unsigned integer
"""
return (data[0] +
data[1]*256 +
data[2]*65536 +
data[3]*16777216)
def getUint16(data):
"""!
This function coverts 2 bytes to a 16-bit unsigned integer.
@param data : 1-demension byte array
@return : 16-bit unsigned integer
"""
return (data[0] +
data[1]*256)
def getHex(data):
"""!
This function coverts 4 bytes to a 32-bit unsigned integer in hex.
@param data : 1-demension byte array
@return : 32-bit unsigned integer in hex
"""
#return (binascii.hexlify(data[::-1]))
word = [1, 2**8, 2**16, 2**24]
return np.matmul(data,word)
def checkMagicPattern(data):
"""!
This function check if data arrary contains the magic pattern which is the start of one mmw demo output packet.
@param data : 1-demension byte array
@return : 1 if magic pattern is found
0 if magic pattern is not found
"""
found = 0
if (data[0] == 2 and data[1] == 1 and data[2] == 4 and data[3] == 3 and data[4] == 6 and data[5] == 5 and data[6] == 8 and data[7] == 7):
found = 1
return (found)
def parser_helper(data, readNumBytes,debug=False):
"""!
This function is called by parser_one_mmw_demo_output_packet() function or application to read the input buffer, find the magic number, header location, the length of frame, the number of detected object and the number of TLV contained in this mmw demo output packet.
@param data : 1-demension byte array holds the the data read from mmw demo output. It ignorant of the fact that data is coming from UART directly or file read.
@param readNumBytes : the number of bytes contained in this input byte array
@return headerStartIndex : the mmw demo output packet header start location
@return totalPacketNumBytes : the mmw demo output packet lenght
@return numDetObj : the number of detected objects contained in this mmw demo output packet
@return numTlv : the number of TLV contained in this mmw demo output packet
@return subFrameNumber : the sbuframe index (0,1,2 or 3) of the frame contained in this mmw demo output packet
"""
headerStartIndex = -1
for index in range (readNumBytes):
if checkMagicPattern(data[index:index+8:1]) == 1:
headerStartIndex = index
break
if headerStartIndex == -1: # does not find the magic number i.e output packet header
totalPacketNumBytes = -1
numDetObj = -1
numTlv = -1
subFrameNumber = -1
platform = -1
frameNumber = -1
timeCpuCycles = -1
else: # find the magic number i.e output packet header
totalPacketNumBytes = getUint32(data[headerStartIndex+12:headerStartIndex+16:1])
platform = getHex(data[headerStartIndex+16:headerStartIndex+20:1])
frameNumber = getUint32(data[headerStartIndex+20:headerStartIndex+24:1])
timeCpuCycles = getUint32(data[headerStartIndex+24:headerStartIndex+28:1])
numDetObj = getUint32(data[headerStartIndex+28:headerStartIndex+32:1])
numTlv = getUint32(data[headerStartIndex+32:headerStartIndex+36:1])
subFrameNumber = getUint32(data[headerStartIndex+36:headerStartIndex+40:1])
if(debug):
print("headerStartIndex = %d" % (headerStartIndex))
print("totalPacketNumBytes = %d" % (totalPacketNumBytes))
print("platform = %s" % (platform))
print("frameNumber = %d" % (frameNumber))
print("timeCpuCycles = %d" % (timeCpuCycles))
print("numDetObj = %d" % (numDetObj))
print("numTlv = %d" % (numTlv))
print("subFrameNumber = %d" % (subFrameNumber))
return (headerStartIndex, totalPacketNumBytes, numDetObj, numTlv, subFrameNumber)
def parser_one_mmw_demo_output_packet(data, readNumBytes,debug=False):
"""!
This function is called by application. Firstly it calls parser_helper() function to find the start location of the mmw demo output packet, then extract the contents from the output packet.
Each invocation of this function handles only one frame at a time and user needs to manage looping around to parse data for multiple frames.
@param data : 1-demension byte array holds the the data read from mmw demo output. It ignorant of the fact that data is coming from UART directly or file read.
@param readNumBytes : the number of bytes contained in this input byte array
@return result : parser result. 0 pass otherwise fail
@return headerStartIndex : the mmw demo output packet header start location
@return totalPacketNumBytes : the mmw demo output packet lenght
@return numDetObj : the number of detected objects contained in this mmw demo output packet
@return numTlv : the number of TLV contained in this mmw demo output packet
@return subFrameNumber : the sbuframe index (0,1,2 or 3) of the frame contained in this mmw demo output packet
@return detectedX_array : 1-demension array holds each detected target's x of the mmw demo output packet
@return detectedY_array : 1-demension array holds each detected target's y of the mmw demo output packet
@return detectedZ_array : 1-demension array holds each detected target's z of the mmw demo output packet
@return detectedV_array : 1-demension array holds each detected target's v of the mmw demo output packet
@return detectedRange_array : 1-demension array holds each detected target's range profile of the mmw demo output packet
@return detectedAzimuth_array : 1-demension array holds each detected target's azimuth of the mmw demo output packet
@return detectedElevAngle_array : 1-demension array holds each detected target's elevAngle of the mmw demo output packet
@return detectedSNR_array : 1-demension array holds each detected target's snr of the mmw demo output packet
@return detectedNoise_array : 1-demension array holds each detected target's noise of the mmw demo output packet
"""
headerNumBytes = 40
PI = 3.14159265
detectedX_array = []
detectedY_array = []
detectedZ_array = []
detectedV_array = []
detectedRange_array = []
detectedAzimuth_array = []
detectedElevAngle_array = []
detectedSNR_array = []
detectedNoise_array = []
result = TC_PASS
# call parser_helper() function to find the output packet header start location and packet size
(headerStartIndex, totalPacketNumBytes, numDetObj, numTlv, subFrameNumber) = parser_helper(data, readNumBytes, debug)
if headerStartIndex == -1:
result = TC_FAIL
print("************ Frame Fail, cannot find the magic words *****************")
else:
nextHeaderStartIndex = headerStartIndex + totalPacketNumBytes
if headerStartIndex + totalPacketNumBytes > readNumBytes:
result = TC_FAIL
print("********** Frame Fail, readNumBytes may not long enough ***********")
elif nextHeaderStartIndex + 8 < readNumBytes and checkMagicPattern(data[nextHeaderStartIndex:nextHeaderStartIndex+8:1]) == 0:
result = TC_FAIL
print("********** Frame Fail, incomplete packet **********")
elif numDetObj <= 0:
result = TC_FAIL
print("************ Frame Fail, numDetObj = %d *****************" % (numDetObj))
elif subFrameNumber > 3:
result = TC_FAIL
print("************ Frame Fail, subFrameNumber = %d *****************" % (subFrameNumber))
else:
# process the 1st TLV
tlvStart = headerStartIndex + headerNumBytes
tlvType = getUint32(data[tlvStart+0:tlvStart+4:1])
tlvLen = getUint32(data[tlvStart+4:tlvStart+8:1])
offset = 8
if(debug):
print("The 1st TLV")
print(" type %d" % (tlvType))
print(" len %d bytes" % (tlvLen))
# the 1st TLV must be type 1
if tlvType == 1 and tlvLen < totalPacketNumBytes:#MMWDEMO_UART_MSG_DETECTED_POINTS
# TLV type 1 contains x, y, z, v values of all detect objects.
# each x, y, z, v are 32-bit float in IEEE 754 single-precision binary floating-point format, so every 16 bytes represent x, y, z, v values of one detect objects.
# for each detect objects, extract/convert float x, y, z, v values and calculate range profile and azimuth
for obj in range(numDetObj):
# convert byte0 to byte3 to float x value
x = struct.unpack('<f', codecs.decode(binascii.hexlify(data[tlvStart + offset:tlvStart + offset+4:1]),'hex'))[0]
# convert byte4 to byte7 to float y value
y = struct.unpack('<f', codecs.decode(binascii.hexlify(data[tlvStart + offset+4:tlvStart + offset+8:1]),'hex'))[0]
# convert byte8 to byte11 to float z value
z = struct.unpack('<f', codecs.decode(binascii.hexlify(data[tlvStart + offset+8:tlvStart + offset+12:1]),'hex'))[0]
# convert byte12 to byte15 to float v value
v = struct.unpack('<f', codecs.decode(binascii.hexlify(data[tlvStart + offset+12:tlvStart + offset+16:1]),'hex'))[0]
# calculate range profile from x, y, z
compDetectedRange = math.sqrt((x * x)+(y * y)+(z * z))
# calculate azimuth from x, y
if y == 0:
if x >= 0:
detectedAzimuth = 90
else:
detectedAzimuth = -90
else:
detectedAzimuth = math.atan(x/y) * 180 / PI
# calculate elevation angle from x, y, z
if x == 0 and y == 0:
if z >= 0:
detectedElevAngle = 90
else:
detectedElevAngle = -90
else:
detectedElevAngle = math.atan(z/math.sqrt((x * x)+(y * y))) * 180 / PI
detectedX_array.append(x)
detectedY_array.append(y)
detectedZ_array.append(z)
detectedV_array.append(v)
detectedRange_array.append(compDetectedRange)
detectedAzimuth_array.append(detectedAzimuth)
detectedElevAngle_array.append(detectedElevAngle)
offset = offset + 16
# end of for obj in range(numDetObj) for 1st TLV
# Process the 2nd TLV
tlvStart = tlvStart + 8 + tlvLen
tlvType = getUint32(data[tlvStart+0:tlvStart+4:1])
tlvLen = getUint32(data[tlvStart+4:tlvStart+8:1])
offset = 8
if(debug):
print("The 2nd TLV")
print(" type %d" % (tlvType))
print(" len %d bytes" % (tlvLen))
if tlvType == 7:
# TLV type 7 contains snr and noise of all detect objects.
# each snr and noise are 16-bit integer represented by 2 bytes, so every 4 bytes represent snr and noise of one detect objects.
# for each detect objects, extract snr and noise
for obj in range(numDetObj):
# byte0 and byte1 represent snr. convert 2 bytes to 16-bit integer
snr = getUint16(data[tlvStart + offset + 0:tlvStart + offset + 2:1])
# byte2 and byte3 represent noise. convert 2 bytes to 16-bit integer
noise = getUint16(data[tlvStart + offset + 2:tlvStart + offset + 4:1])
detectedSNR_array.append(snr)
detectedNoise_array.append(noise)
offset = offset + 4
else:
for obj in range(numDetObj):
detectedSNR_array.append(0)
detectedNoise_array.append(0)
# end of if tlvType == 7
if(debug):
print(" x(m) y(m) z(m) v(m/s) Com0range(m) azimuth(deg) elevAngle(deg) snr(0.1dB) noise(0.1dB)")
for obj in range(numDetObj):
print(" obj%3d: %12f %12f %12f %12f %12f %12f %12d %12d %12d" % (obj, detectedX_array[obj], detectedY_array[obj], detectedZ_array[obj], detectedV_array[obj], detectedRange_array[obj], detectedAzimuth_array[obj], detectedElevAngle_array[obj], detectedSNR_array[obj], detectedNoise_array[obj]))
return (result, headerStartIndex, totalPacketNumBytes, numDetObj, numTlv, subFrameNumber, detectedX_array, detectedY_array, detectedZ_array, detectedV_array, detectedRange_array, detectedAzimuth_array, detectedElevAngle_array, detectedSNR_array, detectedNoise_array)