-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastReader.py
139 lines (100 loc) · 3.33 KB
/
fastReader.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
# https://matplotlib.org/stable/gallery/animation/strip_chart.html
#
#
#
from matplotlib.figure import Figure
import numpy as np
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mpl
import matplotlib.colors as col
import serial
import serial.tools.list_ports as sertools
# pip install wxpython
# pip install pyside6 # (alternative if wxpython fails to build)
devices = sertools.comports()
device_reciever = 1
device_sender = 0
serialStream = None
receiveBuffer: str = ""
MAX_Y = 50000 # 4000
SERVO_MIN = 500
SERVO_MAX = 1833
class Scope:
def __init__(self, ax, scopeChannels = list(range(16)), maxt=2, dt=0.02, maxv=MAX_Y):
self.ax = ax
self.scopeChannels = scopeChannels
self.dt = dt
self.maxt = maxt
self.maxv = maxv
self.tdata = [0.0]
self.yData = [[0] for x in range(0, len(scopeChannels))]
self.lines: list[Line2D] = []
for l in range(0, len(scopeChannels)):
self.lines.append(Line2D(self.tdata, self.yData[l], color=col.hsv_to_rgb((1/len(scopeChannels)*l, 1, 1)), label="D"+str(scopeChannels[l])))
self.ax.add_line(self.lines[l])
self.ax.set_ylim(-.1, self.maxv)
self.ax.set_xlim(0, self.maxt)
self.ax.legend(handles=self.lines)
def update(self, y):
if y is None:
return []
lastt = self.tdata[-1]
if lastt >= self.tdata[0] + self.maxt: # reset the arrays
self.tdata = [self.tdata[-1]]
for l in range(0, len(self.scopeChannels)):
self.yData[l] = [self.yData[l][-1]]
self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
self.ax.figure.canvas.draw()
# This slightly more complex calculation avoids floating-point issues
# from just repeatedly adding `self.dt` to the previous value.
t = self.tdata[0] + len(self.tdata) * self.dt
self.tdata.append(t)
for l in range(0, len(self.scopeChannels)):
assert isinstance(self.lines[l], Line2D)
self.yData[l].append(y[self.scopeChannels[l]])
self.lines[l].set_data(self.tdata, self.yData[l])
return self.lines
def emitter(p=0.1):
"""Return an array from the serial console"""
global serialStream
global receiveBuffer
assert serialStream is not None
if serialStream.in_waiting > 0:
receiveBuffer += serialStream.read_all().decode('ASCII') # type: ignore
splitted = receiveBuffer.split('\n')
receiveBuffer = splitted[len(splitted) - 1]
if(len(splitted) > 1):
for l in splitted[ : -1]:
stripped = l.strip()
str_begin = stripped.find('[') + 1
str_end = stripped.find(']') - 1
if str_begin < 0 or str_end < 0 or str_begin >= str_end:
print("[Serial] " + l)
continue
else:
data = [float(i) for i in stripped[str_begin : str_end].split(',')]
#print(data)
yield data
return
yield None
def main():
global serialStream
print("Starting Serial Plotter")
fig, ax = plt.subplots()
#scope = Scope(ax, [1, 5, 9, 13])
scope = Scope(ax)
# Start Serial
print("Connecting to " + devices[device_reciever].name)
with serial.Serial(devices[device_reciever].name, 115200, timeout=5) as ser:
serialStream = ser
# pass a generator in "emitter" to produce data for the update func
ani = animation.FuncAnimation(fig, scope.update, emitter, interval=50, blit=False, save_count=100)
plt.show()
serialStream = None
# --- Main ---
if __name__ == "__main__":
#mpl.use('wxAgg')
#mpl.use('QtAgg')
main()