-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_visualizer.py
More file actions
executable file
·69 lines (49 loc) · 1.9 KB
/
encoder_visualizer.py
File metadata and controls
executable file
·69 lines (49 loc) · 1.9 KB
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
#!/usr/local/bin/python3
import socket
import time
import threading
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
HOST = "127.0.0.1"
SIZE = 45 # for 8 decimal points, set 41 for 7 decimal points
ENCODERS = [[None] * 4, [None] * 4, [None] * 4, [None] * 4]
ENCODERS_ZERO = [[None] * 4, [None] * 4, [None] * 4, [None] * 4]
HISTORY = [[[0] * 100] * 4, [[0] * 100] * 4, [[0] * 100] * 4, [[0] * 100] * 4]
def listen_rpi(index):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, 8080 + index))
s.listen()
print(f"Port {8080 + index} listening")
conn, addr = s.accept()
with conn:
print("Connected by", addr)
while True:
data = conn.recv(SIZE)
if not data:
break
data = data.decode("utf-8")
data = list(map(float, str(data[1:-1]).split(" ")))
if all([enc is None for enc in ENCODERS[index]]):
ENCODERS_ZERO[index] = data
for i, d in enumerate(data):
data[i] = d - ENCODERS_ZERO[index][i]
ENCODERS[index] = data
time.sleep(0.05)
def relay_encoders(i):
for i, enc_set in enumerate(ENCODERS):
for ii, enc in enumerate(enc_set):
HISTORY[i][ii].pop(0)
HISTORY[i][ii].append(enc)
for y, col in enumerate(axs):
for x, ax in enumerate(col):
ax.cla()
ax.plot(HISTORY[x][y])
ax.set_title(f"SET: {x} ENC: {y}", size=10)
threads = [threading.Thread(target=listen_rpi, args=(i,)) for i in range(4)]
for t in threads:
t.start()
fig, axs = plt.subplots(4, 4, figsize=(9, 9), sharex=True, sharey=True)
fig.tight_layout()
ani = FuncAnimation(fig, relay_encoders, interval=100)
plt.show()