Haptic Controller
Loading...
Searching...
No Matches
thumb_viz.py
Go to the documentation of this file.
1"""
2Reads data from a serial port and displays it in real-time using matplotlib.
3Make sure to set COM_PORT to the correct value before running.
4"""
5
6import serial
7from rendering import PointsInSpace
8
9COM_PORT = "COM11"
10TRAILING_POINTS = 16
11MIN_MESSAGE_BYTES = 16
12
13ser = serial.Serial(
14 port=COM_PORT,
15 baudrate=115200,
16 parity=serial.PARITY_NONE,
17 stopbits=serial.STOPBITS_ONE,
18 bytesize=serial.EIGHTBITS,
19 timeout=0,
20)
21
22print("Connected to: " + ser.portstr)
23
24joint_0_list = []
25joint_1_list = []
26joint_0_mirror_list = []
27joint_1_mirror_list = []
28
30 "Tip",
31 "Joint 0 [Radians]",
32 "Joint 1 [Radians]",
33 xlim=[-2, 2],
34 enable_grid=True,
35 enable_legend=True,
36)
37pp.register_plot("connected", alpha=0.4)
38pp.register_plot("mirror", alpha=0.4)
39pp.register_plot("torque")
40
41while True:
42 try:
43 # Read bytes goofery
44 bytes_to_read = ser.in_waiting
45 if bytes_to_read < MIN_MESSAGE_BYTES:
46 continue
47 line = ser.read(bytes_to_read).decode("utf-8")
48 segments = line.split()
49
50 # Parse the message by reading the value after each label
51 try:
52 # Do this in two steps so that values are not changed if not all values exist
53 def value_by_label(label):
54 return float(segments[segments.index(f"{label}:") + 1])
55
56 joint_0 = value_by_label("joint_thetas.at(0)")
57 joint_1 = value_by_label("joint_thetas.at(1)")
58 joint_0_mirror = value_by_label("mirror_pos_0")
59 joint_1_mirror = value_by_label("mirror_pos_2")
60
61 joint_0_torque = 0
62 joint_1_torque = 0
63 try:
64 joint_0_torque = value_by_label("joint_0_torque")
65 joint_1_torque = value_by_label("joint_1_torque")
66 except Exception:
67 pass
68
69 # print(joint_1_mirror / joint_0_mirror)
70
71 joint_0_list.append(joint_0)
72 joint_1_list.append(joint_1)
73 joint_0_mirror_list.append(joint_0_mirror)
74 joint_1_mirror_list.append(joint_1_mirror)
75 except Exception as e:
76 print(e)
77 continue
78
79 if len(joint_0_list) > TRAILING_POINTS:
80 joint_0_list.pop(0)
81 joint_1_list.pop(0)
82 joint_0_mirror_list.pop(0)
83 joint_1_mirror_list.pop(0)
84
85 # Display results
86 pp.start_drawing()
87 pp.draw_points("connected", joint_0_list, joint_1_list)
88 pp.draw_points("mirror", joint_0_mirror_list, joint_1_mirror_list)
89 pp.draw_points("torque", joint_0_torque, joint_1_torque)
90 pp.end_drawing()
91 except Exception as e:
92 print(e)
93 ser.close()
94 print("Closed connection")
95 quit()
96
97ser.close()
98print("Closed connection")
value_by_label(label)
Definition thumb_viz.py:53