-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialReader.py
77 lines (60 loc) · 2.64 KB
/
SerialReader.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
import csv
import struct
from Frame import ShortRangeRadarFrameHeader, Frame, FrameError
from datetime import datetime
from threading import Thread
from multiprocessing import Queue
from SerialInterface import SerialInterface
class SerialReader:
def __init__(self, file, control, data):
self.interface = SerialInterface(control, data, frame_type=ShortRangeRadarFrameHeader)
self.file = file
self.running = False
self.received_queue = Queue()
self.receiving_process = Thread(target=self.process_receiving)
self.storing_process = Thread(target=self.process_storing)
def process_receiving(self):
while self.running:
frame = self.interface.recv_item(self.interface.data_rx_queue)
if frame:
try:
frame = Frame(frame, frame_type=self.interface.frame_type)
for tlv in frame.tlvs:
objs = tlv.objects
if tlv.name == 'DETECTED_POINTS':
self.received_queue.put(objs)
except(KeyError, struct.error, IndexError, FrameError, OverflowError) as e:
print('Exception occurred: ', e)
print('Skipping frame')
def process_storing(self):
# if running or received queue has stuff in it
counter = 1
with open(self.file, 'x+', newline='\n') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(['time', 'frame_number', 'object_number', 'x (m)', 'y(m)', 'z(m)', 'doppler (m/s)'])
while self.running or not self.received_queue.empty():
received_objs = self.received_queue.get()
if received_objs.name == 'DETECTED_POINTS':
inner = 1
for obj in received_objs:
x = obj.x
y = obj.y
z = obj.z
speed = obj.doppler
time_instant = datetime.now().isoformat()
writer.writerow([time_instant, counter, inner, x, y, z, speed])
inner += 1
counter += 1
def send_profile(self, profile_file):
with open(profile_file) as f:
for line in f.readlines():
if line.startswith('%'):
continue
else:
self.interface.send_item(self.interface.control_tx_queue, line)
def start(self):
self.running = True
self.receiving_process.start()
def stop(self):
self.running = False
self.receiving_process.join()