-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_radarclient.py
1027 lines (909 loc) · 40.7 KB
/
live_radarclient.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import serial
import time
import numpy as np
import struct
import json
from processModule.serverConnect import connect_mqtt
import yaml
import traceback
from datetime import datetime
"""
Live radar client program for publishing live radar data to MQTT broker.
Note: This program runs as a subprocess of live_receiver.py.
"""
with open("config.yaml", "r") as f:
yml_config = yaml.safe_load(f)
# MQTT identifiers
TOPIC = yml_config["LiveData"]["radar"]["topic"]
CLIENT_ID = yml_config["LiveData"]["radar"]["client_id"]
# Change the configuration file name
# configFileName = 'xwr68xxconfig.cfg'
configFileName = "ODS_6m_default.cfg"
# configFileName = 'ISK_6m_default.cfg'
# configFileName = 'new_cfg.cfg'
# configFileName = 'demo.cfg'
write_radar = False
CLIport1 = {}
Dataport1 = {}
CLIport2 = {}
Dataport2 = {}
byteBuffer = np.zeros(2**15, dtype="uint8")
byteBuffer2 = np.zeros(2**15, dtype="uint8")
byteBufferLength = 0
byteBufferLength2 = 0
# -------------------------------------------------------------------
# Function to configure the serial ports and send the data from
# the configuration file to the radar
def serialConfig1(configFileName):
global CLIport1
global Dataport1
global CLIport2
global Dataport2
global yml_config
# Open the serial ports for the configuration and the data ports
# Raspberry pi
# CLIport = serial.Serial('/dev/ttyACM0', 115200)
# Dataport = serial.Serial('/dev/ttyACM1', 921600)
# Windows
str_cliport = yml_config["LiveData"]["radar"]["CLIport"]
str_dataport = yml_config["LiveData"]["radar"]["DataPort"]
str_cliport2 = yml_config["LiveData"]["radar"]["CLIport2"]
str_dataport2 = yml_config["LiveData"]["radar"]["DataPort2"]
CLIport1 = serial.Serial(str_cliport, 115200)
Dataport1 = serial.Serial(str_dataport, 921600)
# on reconnect, wait for the port to come back
time.sleep(1)
# flush buffers
CLIport1.flushInput()
CLIport1.flushOutput()
Dataport1.flushInput()
Dataport1.flushOutput()
# Read the configuration file and send it to the board
config = [line.rstrip("\r\n") for line in open(configFileName)]
for i in config:
CLIport1.write((i + "\n").encode())
# print(i)
time.sleep(0.01)
CLIport2 = serial.Serial(str_cliport2, 115200)
Dataport2 = serial.Serial(str_dataport2, 921600)
# on reconnect, wait for the port to come back
time.sleep(1)
# flush buffers
CLIport2.flushInput()
CLIport2.flushOutput()
Dataport2.flushInput()
Dataport2.flushOutput()
for i in config:
CLIport2.write((i + "\n").encode())
# print(i)
time.sleep(0.01)
return CLIport1, Dataport1, CLIport2, Dataport2
"""
def serialConfig2(configFileName):
global CLIport2
global Dataport2
global yml_config
# Open the serial ports for the configuration and the data ports
# Raspberry pi
#CLIport = serial.Serial('/dev/ttyACM0', 115200)
#Dataport = serial.Serial('/dev/ttyACM1', 921600)
# Windows
str_cliport2 = yml_config["LiveData"]["radar"]["CLIport2"]
str_dataport2 = yml_config["LiveData"]["radar"]["DataPort2"]
CLIport2 = serial.Serial(str_cliport2, 115200)
Dataport2 = serial.Serial(str_dataport2, 921600)
# on reconnect, wait for the port to come back
time.sleep(1)
# flush buffers
CLIport2.flushInput()
CLIport2.flushOutput()
Dataport2.flushInput()
Dataport2.flushOutput()
# Read the configuration file and send it to the board
config = [line.rstrip('\r\n') for line in open(configFileName)]
for i in config:
CLIport2.write((i+'\n').encode())
#print(i)
time.sleep(0.01)
return CLIport2, Dataport2
"""
# ------------------------------------------------------------------
# Function to parse the data inside the configuration file
def parseConfigFile(configFileName):
configParameters = (
{}
) # Initialize an empty dictionary to store the configuration parameters
# Read the configuration file and send it to the board
config = [line.rstrip("\r\n") for line in open(configFileName)]
for i in config:
# Split the line
splitWords = i.split(" ")
# Hard code the number of antennas, change if other configuration is used
numRxAnt = 4
numTxAnt = 3
# Get the information about the profile configuration
if "profileCfg" in splitWords[0]:
startFreq = int(float(splitWords[2]))
idleTime = int(splitWords[3])
rampEndTime = float(splitWords[5])
freqSlopeConst = float(splitWords[8])
numAdcSamples = int(splitWords[10])
numAdcSamplesRoundTo2 = 1
while numAdcSamples > numAdcSamplesRoundTo2:
numAdcSamplesRoundTo2 = numAdcSamplesRoundTo2 * 2
digOutSampleRate = int(splitWords[11])
# Get the information about the frame configuration
elif "frameCfg" in splitWords[0]:
chirpStartIdx = int(splitWords[1])
chirpEndIdx = int(splitWords[2])
numLoops = int(splitWords[3])
numFrames = int(splitWords[4])
framePeriodicity = int(splitWords[5])
# Combine the read data to obtain the configuration parameters
numChirpsPerFrame = (chirpEndIdx - chirpStartIdx + 1) * numLoops
configParameters["numDopplerBins"] = numChirpsPerFrame / numTxAnt
configParameters["numRangeBins"] = numAdcSamplesRoundTo2
configParameters["rangeResolutionMeters"] = (3e8 * digOutSampleRate * 1e3) / (
2 * freqSlopeConst * 1e12 * numAdcSamples
)
configParameters["rangeIdxToMeters"] = (3e8 * digOutSampleRate * 1e3) / (
2 * freqSlopeConst * 1e12 * configParameters["numRangeBins"]
)
configParameters["dopplerResolutionMps"] = 3e8 / (
2
* startFreq
* 1e9
* (idleTime + rampEndTime)
* 1e-6
* configParameters["numDopplerBins"]
* numTxAnt
)
configParameters["maxRange"] = (300 * 0.9 * digOutSampleRate) / (
2 * freqSlopeConst * 1e3
)
configParameters["maxVelocity"] = 3e8 / (
4 * startFreq * 1e9 * (idleTime + rampEndTime) * 1e-6 * numTxAnt
)
return configParameters
# ------------------------------------------------------------------
def parseCompressedSphericalPointCloudTLV(tlvData, tlvLength):
pUnitStruct = "5f" # Units for the 5 results to decompress them
pointStruct = "2bh2H" # Elevation, Azimuth, Doppler, Range, SNR
pUnitSize = struct.calcsize(pUnitStruct)
pointSize = struct.calcsize(pointStruct)
numPoints = int((tlvLength - pUnitSize) / pointSize)
pointCloud = np.empty((numPoints, 5))
# Parse the decompression factors
try:
pUnit = struct.unpack(pUnitStruct, tlvData[:pUnitSize])
except:
print("Error: Point Cloud TLV Parser Failed")
return 0, pointCloud
# Update data pointer
tlvData = tlvData[pUnitSize:]
# Parse each point
for i in range(numPoints):
try:
elevation, azimuth, doppler, rng, snr = struct.unpack(
pointStruct, tlvData[:pointSize]
)
except:
numPoints = i
print("Error: Point Cloud TLV Parser Failed")
break
tlvData = tlvData[pointSize:]
if azimuth >= 128:
print("Az greater than 127")
azimuth -= 256
if elevation >= 128:
print("Elev greater than 127")
elevation -= 256
if doppler >= 32768:
print("Doppler greater than 32768")
doppler -= 65536
# Decompress values
pointCloud[i, 0] = rng * pUnit[3] # Range
pointCloud[i, 1] = azimuth * pUnit[1] # Azimuth
pointCloud[i, 2] = elevation * pUnit[0] # Elevation
pointCloud[i, 3] = doppler * pUnit[2] # Doppler
pointCloud[i, 4] = snr * pUnit[4] # SNR
# Convert spherical to cartesian:
sphericalPointCloud = pointCloud.copy()
# Compute X
# Range * sin (azimuth) * cos (elevation)
pointCloud[:, 0] = (
sphericalPointCloud[:, 0]
* np.sin(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Y
# Range * cos (azimuth) * cos (elevation)
pointCloud[:, 1] = (
sphericalPointCloud[:, 0]
* np.cos(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Z
# Range * sin (elevation)
pointCloud[:, 2] = sphericalPointCloud[:, 0] * np.sin(sphericalPointCloud[:, 2])
return numPoints, pointCloud
# -------------------------------------------------------------------
# Funtion to read and parse the incoming data
# Decode TLV Header
def tlvHeaderDecode(data):
tlvType, tlvLength = struct.unpack("2I", data)
return tlvType, tlvLength
def readAndParseData(Dataport1, configParameters, client, sensor_id):
global byteBuffer, byteBufferLength
####################################TLV types:###############################
MMWDEMO_OUTPUT_MSG_TRACKERPROC_3D_TARGET_LIST = 1010
MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_INDEX = 1011
MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_HEIGHT = 1012
MMWDEMO_OUTPUT_MSG_COMPRESSED_POINTS = 1020
maxBufferSize = 2**15
magicWord = [2, 1, 4, 3, 6, 5, 8, 7]
# Initialize variables
magicOK = 0 # Checks if magic number has been read
dataOK = 0 # Checks if the data has been read correctly
frameNumber = 0
detObj = {}
detObj_log = None
readBuffer = Dataport1.read(Dataport1.in_waiting)
print(
"----------------------------------------------------------------------------------------------"
)
"""
with open('data_serial_log.txt', 'a') as file:
file.write(str(readBuffer)+'\n'+'\n'+'\n')
"""
byteVec = np.frombuffer(readBuffer, dtype="uint8")
byteCount = len(byteVec)
# print('byteCount is: '+str(byteCount))
# Check that the buffer is not full, and then add the data to the buffer
if (byteBufferLength + byteCount) < maxBufferSize:
byteBuffer[byteBufferLength : byteBufferLength + byteCount] = byteVec[
:byteCount
]
byteBufferLength = byteBufferLength + byteCount
# Check that the buffer has some data
if byteBufferLength > 16:
# Check for all possible locations of the magic word
possibleLocs = np.where(byteBuffer == magicWord[0])[0]
# Confirm that is the beginning of the magic word and store the index in startIdx
startIdx = []
for loc in possibleLocs:
check = byteBuffer[loc : loc + 8]
if np.all(check == magicWord):
startIdx.append(loc)
# Check that startIdx is not empty
if startIdx:
# Remove the data before the first start index
if startIdx[0] > 0 and startIdx[0] < byteBufferLength:
byteBuffer[: byteBufferLength - startIdx[0]] = byteBuffer[
startIdx[0] : byteBufferLength
]
byteBuffer[byteBufferLength - startIdx[0] :] = np.zeros(
len(byteBuffer[byteBufferLength - startIdx[0] :]), dtype="uint8"
)
byteBufferLength = byteBufferLength - startIdx[0]
# Check that there have no errors with the byte buffer length
if byteBufferLength < 0:
byteBufferLength = 0
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Read the total packet length
totalPacketLen = np.matmul(byteBuffer[12 : 12 + 4], word)
# Check that all the packet has been read
if (byteBufferLength >= totalPacketLen) and (byteBufferLength != 0):
magicOK = 1
# print(f"magicOK = {magicOK}")
# If magicOK is equal to 1 then process the message
if magicOK:
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Initialize the pointer index
idX = 0
# Read the header
magicNumber = byteBuffer[idX : idX + 8]
# print('magicNumber: '+str(magicNumber))
idX += 8
version = format(np.matmul(byteBuffer[idX : idX + 4], word), "x")
# print('version '+str(version))
idX += 4
totalPacketLen = np.matmul(byteBuffer[idX : idX + 4], word)
# print('totalPacketLen '+str(totalPacketLen))
idX += 4
platform = format(np.matmul(byteBuffer[idX : idX + 4], word), "x")
# print('platform '+str(platform))
idX += 4
frameNumber = np.matmul(byteBuffer[idX : idX + 4], word)
# print('frameNumber '+str(frameNumber))
idX += 4
timeCpuCycles = np.matmul(byteBuffer[idX : idX + 4], word)
# print('timeCpuCycles '+str(timeCpuCycles))
idX += 4
numDetectedObj = np.matmul(byteBuffer[idX : idX + 4], word)
# print('numDetectedObj '+str(numDetectedObj))
idX += 4
numTLVs = np.matmul(byteBuffer[idX : idX + 4], word)
# print('numTLVs '+str(numTLVs))
idX += 4
subFrameNumber = np.matmul(byteBuffer[idX : idX + 4], word)
# print('subFrameNumber '+str(subFrameNumber))
idX += 4
# print(f"magicNumber = {magicNumber} \t version = {version} \t totalPacketLen = {totalPacketLen} \t platform = {platform} \t frameNumber = {frameNumber} ")
# print(f"timeCpuCycles = {timeCpuCycles} \t\t numDetectedObj = {numDetectedObj} \t numTLVs = {numTLVs} \t\t idX = {idX}")
# UNCOMMENT IN CASE OF SDK 2
# subFrameNumber = np.matmul(byteBuffer[idX:idX+4],word)
# print(numTLVs)
# Read the TLV messages
for tlvIdx in range(numTLVs):
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Check the header of the TLV message
# print(f"byteBuffer[idX:idX+4] = {byteBuffer[idX:idX+4]}, word = {word}")
tlv_type, tlv_length = tlvHeaderDecode(byteBuffer[idX : idX + 8])
# tlv_type = np.matmul(byteBuffer[idX:idX+4],word)
idX += 4
# print('tlv_type is: '+str(tlv_type))
# tlv_length = np.matmul(byteBuffer[idX:idX+4],word)
idX += 4
# print('tlv_length is: '+str(tlv_length))
# print(f"tlv_type = {tlv_type} \t MMWDEMO_UART_MSG_DETECTED_POINTS = {MMWDEMO_UART_MSG_DETECTED_POINTS}")
# Read the data depending on the TLV message
##########################---Point Cloud TLV---############################
if tlv_type == MMWDEMO_OUTPUT_MSG_COMPRESSED_POINTS: # 1020
tlvData = byteBuffer[idX : idX + tlv_length]
dataOK = 1
idX += tlv_length
pUnitStruct = "5f" # Units for the 5 results to decompress them
pointStruct = "2bh2H" # Elevation, Azimuth, Doppler, Range, SNR
pUnitSize = struct.calcsize(pUnitStruct)
pointSize = struct.calcsize(pointStruct)
numPoints = int((tlv_length - pUnitSize) / pointSize)
pointCloud = np.empty((numPoints, 5))
# Parse the decompression factors
try:
pUnit = struct.unpack(pUnitStruct, tlvData[:pUnitSize])
except:
print("Error: Point Cloud TLV Parser Failed")
return 0, pointCloud
# Update data pointer
tlvData = tlvData[pUnitSize:]
# Parse each point
for i in range(numPoints):
try:
elevation, azimuth, doppler, rng, snr = struct.unpack(
pointStruct, tlvData[:pointSize]
)
except:
numPoints = i
print("Error: Point Cloud TLV Parser Failed")
break
tlvData = tlvData[pointSize:]
if azimuth >= 128:
print("Az greater than 127")
azimuth -= 256
if elevation >= 128:
print("Elev greater than 127")
elevation -= 256
if doppler >= 32768:
print("Doppler greater than 32768")
doppler -= 65536
# Decompress values
pointCloud[i, 0] = rng * pUnit[3] # Range
pointCloud[i, 1] = azimuth * pUnit[1] # Azimuth
pointCloud[i, 2] = elevation * pUnit[0] # Elevation
pointCloud[i, 3] = doppler * pUnit[2] # Doppler
pointCloud[i, 4] = snr * pUnit[4] # SNR
# Convert spherical to cartesian:
sphericalPointCloud = pointCloud.copy()
# Compute X
# Range * sin (azimuth) * cos (elevation)
pointCloud[:, 0] = (
sphericalPointCloud[:, 0]
* np.sin(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Y
# Range * cos (azimuth) * cos (elevation)
pointCloud[:, 1] = (
sphericalPointCloud[:, 0]
* np.cos(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Z
# Range * sin (elevation)
pointCloud[:, 2] = sphericalPointCloud[:, 0] * np.sin(
sphericalPointCloud[:, 2]
)
detObj = {
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": tlv_type,
"frame": frameNumber,
"x": pointCloud[:, 0],
"y": pointCloud[:, 1],
"z": pointCloud[:, 2],
}
detObj_log = json.dumps(
{
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": int(tlv_type),
"frame": int(frameNumber),
"x": pointCloud[:, 0].tolist(),
"y": pointCloud[:, 1].tolist(),
"z": pointCloud[:, 2].tolist(),
}
)
if write_radar:
with open("data/tlv_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_3D_TARGET_LIST: # 1010
targetStruct = "I27f"
targetSize = struct.calcsize(targetStruct)
numDetectedTargets = int(tlv_length / targetSize)
targets = np.empty((numDetectedTargets, 16))
tlvData = byteBuffer[idX : idX + tlv_length]
idX += tlv_length
for i in range(numDetectedTargets):
try:
targetData = struct.unpack(targetStruct, tlvData[:targetSize])
except:
print("ERROR: Target TLV parsing failed")
targetData = struct.unpack(targetStruct, tlvData[:targetSize])
targets[i, 0] = targetData[0] # Target ID
targets[i, 1] = targetData[1] # X Position
targets[i, 2] = targetData[2] # Y Position
targets[i, 3] = targetData[3] # Z Position
targets[i, 4] = targetData[4] # X Velocity
targets[i, 5] = targetData[5] # Y Velocity
targets[i, 6] = targetData[6] # Z Velocity
targets[i, 7] = targetData[7] # X Acceleration
targets[i, 8] = targetData[8] # Y Acceleration
targets[i, 9] = targetData[9] # Z Acceleration
targets[i, 10] = targetData[26] # G
targets[i, 11] = targetData[27] # Confidence Level
tlvData = tlvData[targetSize:]
# Store the data in the detObj dictionary
# detObj = {"Sensor_id": sensor_id, "TLV_type":tlv_type,"frame":frameNumber, "x": pointCloud[:,0], "y": pointCloud[:,1], "z": pointCloud[:,2]}
try:
detObj_log = json.dumps(
{
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": int(tlv_type),
"frame": int(frameNumber),
"x": pointCloud[:, 0].tolist(),
"y": pointCloud[:, 1].tolist(),
"z": pointCloud[:, 2].tolist(),
}
)
except UnboundLocalError:
detObj_log = None
if write_radar:
with open("data/tlv_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
with open("data/targets_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
"""
with open("sample_file.json", "a") as file:
json.dump(detObj_log, file)
#detObj = {"tid": tid, "x": posX, "y": posY, "z": posZ}
print(detObj)
"""
dataOK = 0
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_INDEX: # 1011
word = [1, 2**8, 2**16, 2**24]
tid = byteBuffer[idX]
idX += 1
idX += tlv_length - 1
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_HEIGHT:
word = [1, 2**8, 2**16, 2**24]
tid = byteBuffer[idX]
idX += 1
maxZ = np.matmul(byteBuffer[idX : idX + 4], word)
idX += 4
minZ = np.matmul(byteBuffer[idX : idX + 4], word)
idX += 4
idX += tlv_length - 9
# Remove already processed data
if idX > 0 and byteBufferLength > idX:
shiftSize = totalPacketLen
byteBuffer[: byteBufferLength - shiftSize] = byteBuffer[
shiftSize:byteBufferLength
]
byteBuffer[byteBufferLength - shiftSize :] = np.zeros(
len(byteBuffer[byteBufferLength - shiftSize :]), dtype="uint8"
)
byteBufferLength = byteBufferLength - shiftSize
# Check that there are no errors with the buffer length
if byteBufferLength < 0:
byteBufferLength = 0
print(detObj_log)
if detObj_log is not None:
res = client.publish(topic=TOPIC, payload=(detObj_log), qos=0)
# else:
# res = client.publish(topic=TOPIC, payload="Empty", qos=0)
status = res[0]
if status == 0:
# msg_str = str(detObj_log)[:10] + "..."
msg_str = "detObj_log"
print(f"{CLIENT_ID}: Send `{msg_str}` to topic `{TOPIC}`\n\n")
else:
print(f"{CLIENT_ID}: Failed to send radar message to topic `{TOPIC}`\n")
return dataOK, frameNumber, detObj
# ------------------------------------------------------------------
def readAndParseData2(Dataport2, configParameters, client, sensor_id):
global byteBuffer2, byteBufferLength2
####################################TLV types:###############################
MMWDEMO_OUTPUT_MSG_TRACKERPROC_3D_TARGET_LIST = 1010
MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_INDEX = 1011
MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_HEIGHT = 1012
MMWDEMO_OUTPUT_MSG_COMPRESSED_POINTS = 1020
maxBufferSize = 2**15
magicWord = [2, 1, 4, 3, 6, 5, 8, 7]
# Initialize variables
magicOK = 0 # Checks if magic number has been read
dataOK = 0 # Checks if the data has been read correctly
frameNumber = 0
detObj = {}
detObj_log = None
readBuffer = Dataport2.read(Dataport2.in_waiting)
print(
"----------------------------------------------------------------------------------------------"
)
"""
with open('data_serial_log.txt', 'a') as file:
file.write(str(readBuffer)+'\n'+'\n'+'\n')
"""
byteVec = np.frombuffer(readBuffer, dtype="uint8")
byteCount = len(byteVec)
# print('byteCount is: '+str(byteCount))
# Check that the buffer is not full, and then add the data to the buffer
if (byteBufferLength2 + byteCount) < maxBufferSize:
byteBuffer2[byteBufferLength2 : byteBufferLength2 + byteCount] = byteVec[
:byteCount
]
byteBufferLength2 = byteBufferLength2 + byteCount
# Check that the buffer has some data
if byteBufferLength2 > 16:
# Check for all possible locations of the magic word
possibleLocs = np.where(byteBuffer2 == magicWord[0])[0]
# Confirm that is the beginning of the magic word and store the index in startIdx
startIdx = []
for loc in possibleLocs:
check = byteBuffer2[loc : loc + 8]
if np.all(check == magicWord):
startIdx.append(loc)
# Check that startIdx is not empty
if startIdx:
# Remove the data before the first start index
if startIdx[0] > 0 and startIdx[0] < byteBufferLength2:
byteBuffer2[: byteBufferLength2 - startIdx[0]] = byteBuffer2[
startIdx[0] : byteBufferLength2
]
byteBuffer2[byteBufferLength2 - startIdx[0] :] = np.zeros(
len(byteBuffer2[byteBufferLength2 - startIdx[0] :]), dtype="uint8"
)
byteBufferLength2 = byteBufferLength2 - startIdx[0]
# Check that there have no errors with the byte buffer length
if byteBufferLength2 < 0:
byteBufferLength2 = 0
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Read the total packet length
totalPacketLen = np.matmul(byteBuffer2[12 : 12 + 4], word)
# Check that all the packet has been read
if (byteBufferLength2 >= totalPacketLen) and (byteBufferLength2 != 0):
magicOK = 1
# print(f"magicOK = {magicOK}")
# If magicOK is equal to 1 then process the message
if magicOK:
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Initialize the pointer index
idX = 0
# Read the header
magicNumber = byteBuffer2[idX : idX + 8]
# print('magicNumber: '+str(magicNumber))
idX += 8
version = format(np.matmul(byteBuffer2[idX : idX + 4], word), "x")
# print('version '+str(version))
idX += 4
totalPacketLen = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('totalPacketLen '+str(totalPacketLen))
idX += 4
platform = format(np.matmul(byteBuffer2[idX : idX + 4], word), "x")
# print('platform '+str(platform))
idX += 4
frameNumber = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('frameNumber '+str(frameNumber))
idX += 4
timeCpuCycles = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('timeCpuCycles '+str(timeCpuCycles))
idX += 4
numDetectedObj = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('numDetectedObj '+str(numDetectedObj))
idX += 4
numTLVs = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('numTLVs '+str(numTLVs))
idX += 4
subFrameNumber = np.matmul(byteBuffer2[idX : idX + 4], word)
# print('subFrameNumber '+str(subFrameNumber))
idX += 4
# print(f"magicNumber = {magicNumber} \t version = {version} \t totalPacketLen = {totalPacketLen} \t platform = {platform} \t frameNumber = {frameNumber} ")
# print(f"timeCpuCycles = {timeCpuCycles} \t\t numDetectedObj = {numDetectedObj} \t numTLVs = {numTLVs} \t\t idX = {idX}")
# UNCOMMENT IN CASE OF SDK 2
# subFrameNumber = np.matmul(byteBuffer[idX:idX+4],word)
# print(numTLVs)
# Read the TLV messages
for tlvIdx in range(numTLVs):
# word array to convert 4 bytes to a 32 bit number
word = [1, 2**8, 2**16, 2**24]
# Check the header of the TLV message
# print(f"byteBuffer[idX:idX+4] = {byteBuffer[idX:idX+4]}, word = {word}")
tlv_type, tlv_length = tlvHeaderDecode(byteBuffer2[idX : idX + 8])
# tlv_type = np.matmul(byteBuffer[idX:idX+4],word)
idX += 4
# print('tlv_type is: '+str(tlv_type))
# tlv_length = np.matmul(byteBuffer[idX:idX+4],word)
idX += 4
# print('tlv_length is: '+str(tlv_length))
# print(f"tlv_type = {tlv_type} \t MMWDEMO_UART_MSG_DETECTED_POINTS = {MMWDEMO_UART_MSG_DETECTED_POINTS}")
# Read the data depending on the TLV message
##########################---Point Cloud TLV---############################
if tlv_type == MMWDEMO_OUTPUT_MSG_COMPRESSED_POINTS: # 1020
tlvData = byteBuffer2[idX : idX + tlv_length]
dataOK = 1
idX += tlv_length
pUnitStruct = "5f" # Units for the 5 results to decompress them
pointStruct = "2bh2H" # Elevation, Azimuth, Doppler, Range, SNR
pUnitSize = struct.calcsize(pUnitStruct)
pointSize = struct.calcsize(pointStruct)
numPoints = int((tlv_length - pUnitSize) / pointSize)
pointCloud = np.empty((numPoints, 5))
# Parse the decompression factors
try:
pUnit = struct.unpack(pUnitStruct, tlvData[:pUnitSize])
except:
print("Error: Point Cloud TLV Parser Failed")
return 0, pointCloud
# Update data pointer
tlvData = tlvData[pUnitSize:]
# Parse each point
for i in range(numPoints):
try:
elevation, azimuth, doppler, rng, snr = struct.unpack(
pointStruct, tlvData[:pointSize]
)
except:
numPoints = i
print("Error: Point Cloud TLV Parser Failed")
break
tlvData = tlvData[pointSize:]
if azimuth >= 128:
print("Az greater than 127")
azimuth -= 256
if elevation >= 128:
print("Elev greater than 127")
elevation -= 256
if doppler >= 32768:
print("Doppler greater than 32768")
doppler -= 65536
# Decompress values
pointCloud[i, 0] = rng * pUnit[3] # Range
pointCloud[i, 1] = azimuth * pUnit[1] # Azimuth
pointCloud[i, 2] = elevation * pUnit[0] # Elevation
pointCloud[i, 3] = doppler * pUnit[2] # Doppler
pointCloud[i, 4] = snr * pUnit[4] # SNR
# Convert spherical to cartesian:
sphericalPointCloud = pointCloud.copy()
# Compute X
# Range * sin (azimuth) * cos (elevation)
pointCloud[:, 0] = (
sphericalPointCloud[:, 0]
* np.sin(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Y
# Range * cos (azimuth) * cos (elevation)
pointCloud[:, 1] = (
sphericalPointCloud[:, 0]
* np.cos(sphericalPointCloud[:, 1])
* np.cos(sphericalPointCloud[:, 2])
)
# Compute Z
# Range * sin (elevation)
pointCloud[:, 2] = sphericalPointCloud[:, 0] * np.sin(
sphericalPointCloud[:, 2]
)
detObj = {
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": tlv_type,
"frame": frameNumber,
"x": pointCloud[:, 0],
"y": pointCloud[:, 1],
"z": pointCloud[:, 2],
}
detObj_log = json.dumps(
{
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": int(tlv_type),
"frame": int(frameNumber),
"x": pointCloud[:, 0].tolist(),
"y": pointCloud[:, 1].tolist(),
"z": pointCloud[:, 2].tolist(),
}
)
if write_radar:
with open("data/tlv_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_3D_TARGET_LIST: # 1010
targetStruct = "I27f"
targetSize = struct.calcsize(targetStruct)
numDetectedTargets = int(tlv_length / targetSize)
targets = np.empty((numDetectedTargets, 16))
tlvData = byteBuffer2[idX : idX + tlv_length]
idX += tlv_length
for i in range(numDetectedTargets):
try:
targetData = struct.unpack(targetStruct, tlvData[:targetSize])
except:
print("ERROR: Target TLV parsing failed")
targetData = struct.unpack(targetStruct, tlvData[:targetSize])
targets[i, 0] = targetData[0] # Target ID
targets[i, 1] = targetData[1] # X Position
targets[i, 2] = targetData[2] # Y Position
targets[i, 3] = targetData[3] # Z Position
targets[i, 4] = targetData[4] # X Velocity
targets[i, 5] = targetData[5] # Y Velocity
targets[i, 6] = targetData[6] # Z Velocity
targets[i, 7] = targetData[7] # X Acceleration
targets[i, 8] = targetData[8] # Y Acceleration
targets[i, 9] = targetData[9] # Z Acceleration
targets[i, 10] = targetData[26] # G
targets[i, 11] = targetData[27] # Confidence Level
tlvData = tlvData[targetSize:]
# Store the data in the detObj dictionary
# detObj = {"Sensor_id": sensor_id, "TLV_type":tlv_type,"frame":frameNumber, "x": pointCloud[:,0], "y": pointCloud[:,1], "z": pointCloud[:,2]}
try:
detObj_log = json.dumps(
{
"time": datetime.now().strftime("%H:%M:%S.%f"),
"Sensor_id": int(sensor_id),
"TLV_type": int(tlv_type),
"frame": int(frameNumber),
"x": pointCloud[:, 0].tolist(),
"y": pointCloud[:, 1].tolist(),
"z": pointCloud[:, 2].tolist(),
}
)
except UnboundLocalError:
detObj_log = None
if write_radar:
with open("data/tlv_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
with open("data/targets_data_log.json", "a") as file:
file.write(str(detObj_log) + ",\n")
"""
with open("sample_file.json", "a") as file:
json.dump(detObj_log, file)
#detObj = {"tid": tid, "x": posX, "y": posY, "z": posZ}
print(detObj)
"""
dataOK = 0
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_INDEX: # 1011
word = [1, 2**8, 2**16, 2**24]
tid = byteBuffer2[idX]
idX += 1
idX += tlv_length - 1
##########################---Target List TLV---############################
elif tlv_type == MMWDEMO_OUTPUT_MSG_TRACKERPROC_TARGET_HEIGHT:
word = [1, 2**8, 2**16, 2**24]
tid = byteBuffer2[idX]
idX += 1
maxZ = np.matmul(byteBuffer2[idX : idX + 4], word)
idX += 4
minZ = np.matmul(byteBuffer2[idX : idX + 4], word)
idX += 4
idX += tlv_length - 9
# Remove already processed data
if idX > 0 and byteBufferLength2 > idX:
shiftSize = totalPacketLen
byteBuffer2[: byteBufferLength2 - shiftSize] = byteBuffer2[
shiftSize:byteBufferLength2
]
byteBuffer2[byteBufferLength2 - shiftSize :] = np.zeros(
len(byteBuffer2[byteBufferLength2 - shiftSize :]), dtype="uint8"
)
byteBufferLength2 = byteBufferLength2 - shiftSize
# Check that there are no errors with the buffer length
if byteBufferLength2 < 0:
byteBufferLength2 = 0
print(detObj_log)
if detObj_log is not None:
res = client.publish(topic=TOPIC, payload=(detObj_log), qos=0)
# else:
# res = client.publish(topic=TOPIC, payload="Empty", qos=0)
status = res[0]
if status == 0:
# msg_str = str(detObj_log)[:10] + "..."
msg_str = "detObj_log"
print(f"{CLIENT_ID}: Send `{msg_str}` to topic `{TOPIC}`\n\n")
else:
print(f"{CLIENT_ID}: Failed to send radar message to topic `{TOPIC}`\n")
return dataOK, frameNumber, detObj
# ------------------------------------------------------------------
# Funtion to update the data and display in the plot
def update(client):
dataOk = 0
global detObj
x = []
y = []
# Read and parse the received data
dataOk, frameNumber, detObj = readAndParseData(
Dataport1, configParameters, client, sensor_id=1
)
time.sleep(0.05)
dataOk2, frameNumber2, detObj2 = readAndParseData2(
Dataport2, configParameters, client, sensor_id=2
)
# print(f"dataOK = {dataOk}")
# if dataOk and len(detObj["x"]) > 0:
# print(detObj)
# update_demo(detObj)
return dataOk, dataOk2
# ------------------------- MAIN -----------------------------------------
# Configurate the serial port
if __name__ == "__main__":
CLIport1, Dataport1, CLIport2, Dataport2 = serialConfig1(configFileName)
status = (
CLIport1.isOpen(),
CLIport2.isOpen(),
Dataport1.isOpen(),
Dataport2.isOpen(),
)
print(status)
time.sleep(0.1)
# CLIport2, Dataport2 = serialConfig2(configFileName)
# Get the configuration parameters from the configuration file
configParameters = parseConfigFile(configFileName)
CLIport1.write(("sensorStart\n").encode())
CLIport2.write(("sensorStart\n").encode())
detObj = {}
client = connect_mqtt(CLIENT_ID)
while True: