-
Notifications
You must be signed in to change notification settings - Fork 4
/
lerp_from_serial.py
74 lines (64 loc) · 1.8 KB
/
lerp_from_serial.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
import multiprocessing
import re
import serial
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import bone
import serial_utils as s
# Use device manager to find the Arduino's serial port.
COM_PORT = "COM9"
MAX_SERIAL_VALUE = 4095 #maximum expected value coming from serial. set to 1023 if using an older arduino instead of a esp32
RESET_SCALE = True
LEGACY_DECODE = False # If false, will use alpha encodings
q = multiprocessing.Queue()
# Plot Setup
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.subplots_adjust(left=0.25, bottom=0.25)
ax.set_xlabel('X [m]')
ax.set_ylabel('Y [m]')
ax.set_zlabel('Z [m]')
# ------------ Serial Setup ---------------
def serial_worker(q):
# Open Serial Port
ser = serial.Serial(COM_PORT,'115200', timeout=1) # open serial port
print("Listening on "+COM_PORT)
while True:
try:
# Read from serial
read = ser.readline()
if LEGACY_DECODE:
fingers = s.decode_legacy_serial(read)
else:
fingers = s.decode_alpha_serial(read)
# Add the decoded values to the queue
q.put(fingers)
except KeyboardInterrupt:
print("Quitting thread...")
ser.close()
quit()
def animate(i):
fingers = [0,0,0,0,0]
while not(q.empty()):
fingers = list(q.get())
# Turn finger values into Lerp Vals
val = fingers[0] / MAX_SERIAL_VALUE
print("Finger val", val)
# Plot
ax.clear()
ax.set_xlabel('X [mm]')
ax.set_ylabel('Y [mm]')
ax.set_zlabel('Z [mm]')
pose = bone.lerp_pose(val)
points = bone.build_hand(pose, True)
# Plot the Points
bone.plot_steam_hand(points, "Lerped Pose", ax)
if __name__ == "__main__":
p = multiprocessing.Process(target=serial_worker, args=(q,), daemon=True)
p.start()
anim = animation.FuncAnimation(fig, animate, blit=False, interval=1)
try:
plt.show()
except KeyboardInterrupt:
quit()