-
Notifications
You must be signed in to change notification settings - Fork 1
/
EMonitorPyglet.py
408 lines (298 loc) · 11.6 KB
/
EMonitorPyglet.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""EMonitorPyglet
Replaces .NET EMonitor for experiment at Northwestern RSC Lab
Press escape to exit!!!
Worklog:
J. Bremen, July 2021
- Created program
- NOTE!! This file does not change based on state; always displays the circle
"""
import socket, struct, ifaddr, pyglet
class EMonitor:
def __init__(self, ip="localhost", port=5005, screen_index=0):
self.n_sounds = 13
# Initialize the target forces
self.target_tor = 1
self.up_lim_tor = 1
self.target_tor = 1
self.match_tor = 1
self.low_lim_tor = 1
# Initialize Force Parameters
self.targetF = 1
self.up_limF = 1
self.low_limF = 1
self.matchF = 1
self.sound_trigger = [False for i in range(self.n_sounds)]
self.sounds_playing = list([False] * self.n_sounds)
self.players = []
self.stop_trigger = 0
# UDP Stuff
self.UDP_IP = ip
self.UDP_PORT = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((self.UDP_IP, self.UDP_PORT))
# Makes the socket nonblocking
self.sock.setblocking(0)
# Graphics stuff below here
self.thread_running = True
def unpack_bytes_to_double(self, bytes):
return struct.unpack("d", bytes)[0]
def unpack_udp_package(self, data):
"""
Validate package size in bytes first
Bytes structured as:
0-7: Target Torque (double)
8-15: Target Low Limit Torque (double)
16-23: Target Up Limit Torque (double)
24-31: Currently produced torque (double)
32-39: Target Force NM (double)
40-47: Target Low Limit Force (double)
48-55: Target Upper Limit Force (double)
56-63: Currently produced force (double)
64:64+n_sounds-1- one boolean byte for each sound cue (bool)
64+n_sounds- one byte representing if sounds should be stopped (bool)
"""
if len(data) != 65 + self.n_sounds:
print("ERROR!")
return
self.target_tor = self.unpack_bytes_to_double(data[0:8])
self.low_lim_tor = self.unpack_bytes_to_double(data[8:16])
self.up_lim_tor = self.unpack_bytes_to_double(data[16:24])
self.match_tor = self.unpack_bytes_to_double(data[24:32])
self.targetF = self.unpack_bytes_to_double(data[32:40])
self.low_limF = self.unpack_bytes_to_double(data[40:48])
self.up_limF = self.unpack_bytes_to_double(data[48:56])
self.matchF = self.unpack_bytes_to_double(data[56:64])
for i in range(self.n_sounds):
self.sound_trigger[i] = bool(data[64 + i])
self.stop_trigger = data[64 + self.n_sounds]
def recieve_single_udp(self, dt):
# Function clears udp buffer and uses the most recent udp message
# dt will not be used, but we need to give an extra input for pyglet
data = None
while True:
try:
# This 1460 buffer size is connected to the buffer size in the
# MATLAB sending code; can likely be reduced for slightly
# faster IO
data, addr = self.sock.recvfrom(1460)
except BlockingIOError:
break
if data:
self.unpack_udp_package(data)
# Ethernet setup
# First, get the IP address
ip = None
for adapter in ifaddr.get_adapters():
if adapter.ips[0].nice_name == "Ethernet":
ip = [x.ip for x in adapter.ips]
ETHERNET_IP = None
PORT = 5005
if ip:
for i in ip:
if type(i) == str and i.count(".") == 3:
ETHERNET_IP = i
if not ETHERNET_IP:
print("Issue detecting ethernet interface!")
print("Reverting to localhost ip")
ETHERNET_IP = "localhost"
print("Using Ethernet IP:", ETHERNET_IP)
print("Using Ethernet Remote Port", PORT)
"""
NOTE
ETHERNET NOTES!
If issues with ethernet connection arise, I recommend first unplugging
and replugging in the ethernet wires in the switch (all of them)
If this fails, restart the PC with the Ethernet cable unplugged
If the NU Ethernet system is down, do not plug the Ethernet wire into the
switch; this will prevent the computer from creating its own local ip
If the connection starts being slow, check the ping speeds with cmd prompt
Odds are it's due to a loose wire
"""
# Define RGB colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 175)
RED = (255, 0, 0)
SCREEN_INDEX = 1
display = pyglet.canvas.get_display()
screens = display.get_screens()
event_loop = pyglet.app.EventLoop()
if len(screens) <= SCREEN_INDEX:
print("Using default display")
SCREEN_INDEX = 0
print(f"{SCREEN_INDEX = }")
# Create objects for the pyglet window and fps display
window = pyglet.window.Window(fullscreen=True, screen=screens[SCREEN_INDEX])
fps_display = pyglet.window.FPSDisplay(window=window)
# set background color as white
pyglet.gl.glClearColor(*WHITE, 255)
# Load the sounds
SOUND_DIRECTORY = "soundCues\\"
FILE_NAMES = [
"hold.wav",
"in.wav",
"out.wav",
"match.wav",
"relax.wav",
"startingtrial.wav",
"endingtrial.wav",
"Out of Range.wav",
"Wrong Direction.wav",
"in.wav",
"out.wav",
"up.wav",
"down.wav",
]
SOUND_CUES = []
for file in FILE_NAMES:
n = SOUND_DIRECTORY + file
# print(f"Loading {n}")
SOUND_CUES.append(pyglet.media.load(n, streaming=False))
print(f"Loaded {len(SOUND_CUES)} / {len(FILE_NAMES)} sounds successfully")
# Initialize the EMonitor
emonitor = EMonitor(ETHERNET_IP, PORT)
@event_loop.event
def on_window_close(window):
print("Hey")
event_loop.exit()
return pyglet.event.EVENT_HANDLED
def custom_draw_circle_one_thick(x_center, y_center, radius, color, batch):
# Implements mid-point circle drawing algorithm
X = radius
Y = 0
points = []
points.append([X + x_center, y_center])
points.append([x_center + X, y_center])
points.append([x_center - X, y_center])
points.append([x_center, y_center + X])
points.append([x_center, y_center - X])
if radius > 0:
points.append([X + x_center, -Y + y_center])
points.append([Y + x_center, X + y_center])
points.append([-Y + x_center, X + y_center])
P = 1 - radius
while X > Y:
Y += 1
# Midpoint is inside or on the perimeter
if P <= 0:
P = P + 2 * Y + 1
else:
X -= 1
P = P + 2 * Y - 2 * X + 1
if X < Y:
break
points.append([X + x_center, Y + y_center])
points.append([-X + x_center, Y + y_center])
points.append([X + x_center, -Y + y_center])
points.append([-X + x_center, -Y + y_center])
if X != Y:
points.append([Y + x_center, X + y_center])
points.append([-Y + x_center, X + y_center])
points.append([Y + x_center, -X + y_center])
points.append([-Y + x_center, -X + y_center])
num_points = len(points)
# Concatanate points list; gl expects list in format [x0, y0, x1, y1...]
collapsed_points = [j for i in points for j in i]
color_list = color * num_points
batch.add(
num_points,
pyglet.gl.GL_POINTS,
None,
("v2i", collapsed_points),
("c3B", color_list),
)
def custom_draw_circle(x_center, y_center, radius, color, thickness, batch):
edges = min(thickness, radius)
for t in range(edges):
rad = radius - t
custom_draw_circle_one_thick(x_center, y_center, rad, color, batch)
def draw_circle(x, y, radius, color, bg_color, thickness, batch):
a = pyglet.shapes.Circle(x, y, radius, color=color, batch=batch)
b = None
if radius - (2 * thickness) > 0:
b = pyglet.shapes.Circle(
x, y, radius - (2 * thickness), color=bg_color, batch=batch
)
return [a, b]
def draw_full_line(y, length, color, width, batch):
return pyglet.shapes.Line(0, y, length, y, width=width, color=color, batch=batch)
@window.event
def on_draw():
pyglet.clock.tick()
try:
WIDTH = window.width
HEIGHT = window.height
center_x = WIDTH // 2
center_y = HEIGHT // 2
# Define the radii of the circles
m = emonitor
match_target_radius = int(HEIGHT / 1.5)
targetF_line = center_y
if m.target_tor == 0:
m.target_tor = 1
if m.targetF == 0:
m.targetF = 1
lower_range_radius = int(match_target_radius * (m.low_lim_tor / m.target_tor))
upper_range_radius = int(match_target_radius * (m.up_lim_tor / m.target_tor))
representation_radius = int(match_target_radius * (m.match_tor / m.target_tor))
# Code to set the moving Y coordinates
lowF_line = targetF_line * (m.low_limF / m.targetF)
upF_line = targetF_line * (m.up_limF / m.targetF)
# The C# Code has matchY = center_y * ((2 - m.matchF) / m.targetF)
# i'm not sure why the 2.0 - is present though, so I deleted it
# This might have to be reintroduced sometime, or could be a side
# effect of the way that # does graphics
matchY = center_y * ((m.matchF) / m.targetF)
# Need to sort the radii, as the circles will draw on top of each other
radii = sorted(
[
(match_target_radius, BLACK),
(lower_range_radius, BLUE),
(upper_range_radius, BLUE),
],
key=lambda t: t[0],
reverse=True,
)
lines = [
(lowF_line, BLUE),
(upF_line, BLUE),
(matchY, RED),
(targetF_line, BLACK),
]
batch = pyglet.graphics.Batch()
# Using a array to hold the graphics objects, to ensure that they
# aren't destroyed before being drawn
a = []
for radius in radii:
a.append(draw_circle(center_x, center_y, *radius, WHITE, 3, batch))
for line in lines:
a.append(draw_full_line(line[0], WIDTH, line[1], 3, batch))
# The custom drawing function is very slow, so only use it for the
# moving circle. Perhaps make this better someday?
window.clear()
fps_display.draw()
batch.draw()
# Make a new batch to ensure that openGL optimization does not
# erase the moving circle
batch = pyglet.graphics.Batch()
custom_draw_circle(center_x, center_y, representation_radius, RED, 3, batch)
batch.draw()
# Sound stuff here
for i in range(emonitor.n_sounds):
if emonitor.sound_trigger[i] and not emonitor.sounds_playing[i]:
# print(f"Sound {i} is playing")
emonitor.players.append(SOUND_CUES[i].play())
emonitor.sounds_playing[i] = True
if emonitor.stop_trigger:
# print("Stop sounds")
while emonitor.players:
emonitor.players.pop().pause()
emonitor.sounds_playing = [False] * emonitor.n_sounds
except ZeroDivisionError:
print("Zero division detected")
fps_display.draw()
pass
if __name__ == "__main__":
# Call the update function to be run on every frame
pyglet.clock.schedule(emonitor.recieve_single_udp)
pyglet.app.run()