Haptic Controller
Loading...
Searching...
No Matches
finger_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 = "COM13"
10TRAILING_POINTS = 16
11MIN_MESSAGE_BYTES = 16
12FORCE_SCALING = 0.02
13
14ser = serial.Serial(
15 port=COM_PORT,
16 baudrate=115200,
17 parity=serial.PARITY_NONE,
18 stopbits=serial.STOPBITS_ONE,
19 bytesize=serial.EIGHTBITS,
20 timeout=0,
21)
22
23print("Connected to: " + ser.portstr)
24
25x_list = []
26z_list = []
27x_mirror_list = []
28z_mirror_list = []
29
31 "End Effector Position",
32 "Z [m]",
33 "X [m]",
34 xlim=[-0.15, 0.15],
35 ylim=[0, 0.15],
36 enable_grid=True,
37 enable_legend=True,
38)
39pp.register_plot("connected", alpha=0.4)
40pp.register_plot("mirror", alpha=0.4)
41# pp.register_plot("stiffness", m="-")
42# pp.register_plot("damping", m="-")
43# pp.register_plot("force", m="-")
44
45while True:
46 try:
47 # Read bytes goofery
48 bytes_to_read = ser.in_waiting
49 if bytes_to_read < MIN_MESSAGE_BYTES:
50 continue
51 line = ser.read(bytes_to_read).decode("utf-8")
52 segments = line.split()
53
54 # Parse the message by reading the value after each label
55 try:
56 # Do this in two steps so that values are not changed if not all values exist
57 def value_by_label(label):
58 return float(segments[segments.index(f"{label}:") + 1])
59
60 x = value_by_label("pos.at(0)")
61 z = value_by_label("pos.at(2)")
62 x_mirror = value_by_label("mirror_pos_0")
63 z_mirror = value_by_label("mirror_pos_2")
64 F_stiffness_x = 0
65 F_stiffness_z = 0
66 F_damping_x = 0
67 F_damping_z = 0
68 F_kx = 0
69 F_kz = 0
70
71 try:
72 F_stiffness_x = value_by_label("F_stiffness_x")
73 F_stiffness_z = value_by_label("F_stiffness_z")
74 F_damping_x = value_by_label("F_damping_x")
75 F_damping_z = value_by_label("F_damping_z")
76 F_kx = value_by_label("F_kx")
77 F_kz = value_by_label("F_kz")
78 except Exception:
79 pass
80
81 x_list.append(x)
82 z_list.append(z)
83 x_mirror_list.append(x_mirror)
84 z_mirror_list.append(z_mirror)
85 except Exception as e:
86 print(e)
87 continue
88
89 if len(x_list) > TRAILING_POINTS:
90 x_list.pop(0)
91 z_list.pop(0)
92 x_mirror_list.pop(0)
93 z_mirror_list.pop(0)
94
95 # Display results
96 pp.start_drawing()
97 pp.draw_points("connected", z_list, x_list)
98 pp.draw_points("mirror", z_mirror_list, x_mirror_list)
99 # pp.draw_points(
100 # "stiffness",
101 # [z, z + F_stiffness_z * FORCE_SCALING],
102 # [x, x + F_stiffness_x * FORCE_SCALING],
103 # )
104 # pp.draw_points(
105 # "damping",
106 # [z, z + F_damping_z * FORCE_SCALING],
107 # [x, x + F_damping_x * FORCE_SCALING],
108 # )
109 # pp.draw_points(
110 # "force", [z, z + F_kz * FORCE_SCALING], [x, x + F_kx * FORCE_SCALING]
111 # )
112 pp.end_drawing()
113 except Exception as e:
114 print(e)
115 ser.close()
116 print("Closed connection")
117 quit()
118
119ser.close()
120print("Closed connection")
value_by_label(label)
Definition finger_viz.py:57