-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
170 lines (147 loc) · 5.5 KB
/
main.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
import art
import logging
import time
import tkinter as tk
import config
from modules.conducter import Conducter
from modules.data_writer import DataWriter
from nebula.hivemind import DataBorg
from nebula.nebula import Nebula
class Visualiser:
def __init__(self, hivemind):
self.hivemind = hivemind
# Build UI
self.root = tk.Tk()
self.root.title("Jess +")
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
self.canvas = tk.Canvas(self.root, height=600, width=500)
self.canvas.pack()
# Assign location points and colors for visuals
self.centers = {
'T3': [100, 250],
'T4': [400, 250],
'O1': [190, 350],
'O2': [310, 350],
'EDA': [250, 150]
}
self.label_centre = [250, 500]
self.colors = {
'T3': '#ff6600',
'T4': '#ff6600',
'O1': '#ff6600',
'O2': '#ff6600',
'EDA': '#0099ff'
}
# Assign initial sizes of the circles (between 0 and 1)
self.sizes = {
'T3': 0.5,
'T4': 0.5,
'O1': 0.5,
'O2': 0.5,
'EDA': 0.5,
}
# Build graphics
self.items = {}
for ch in self.centers:
items_xys = [self.centers[ch][0]-80*self.sizes[ch],
self.centers[ch][1]-80*self.sizes[ch],
self.centers[ch][0]+80*self.sizes[ch],
self.centers[ch][1]+80*self.sizes[ch]]
self.items[ch] = self.canvas.create_oval(
*items_xys, fill=self.colors[ch], outline=self.colors[ch])
# Add labels
for ch in self.centers:
self.canvas.create_text(
self.centers[ch], text=ch, font=('Helvetica', '15', 'bold'))
# Stream label
label_xys = [self.label_centre[0]-150,
self.label_centre[1]-50,
self.label_centre[0]+150,
self.label_centre[1]+50]
self.frame = self.canvas.create_rectangle(*label_xys, width=3)
self.label = self.canvas.create_text(
self.label_centre, text='STREAM', font=('Helvetica', '15', 'bold'))
self.stream_mapping = {
'mic_in': 'Microphone',
'rnd_poetry': 'Poetry',
'eeg2flow': 'Brain',
'flow2core': 'Brain groove',
'core2flow': 'Self awareness',
'audio2core': 'Audio groove',
'audio2flow': 'Audio flow',
'flow2audio': 'Brain audio',
'eda2flow': 'EDA'
}
self.window_closed = False
def on_close(self):
"""
Callback function for when the window is closed.
"""
self.window_closed = True
def callback(self):
"""
Callback function for updating the circle sizes based on the hivemind
data.
"""
self.sizes['T3'] = self.hivemind.eeg_buffer[0][-5:].mean()
self.sizes['T4'] = self.hivemind.eeg_buffer[1][-5:].mean()
self.sizes['O1'] = self.hivemind.eeg_buffer[2][-5:].mean()
self.sizes['O2'] = self.hivemind.eeg_buffer[3][-5:].mean()
self.sizes['EDA'] = self.hivemind.eda_buffer[0][-5:].mean()
for ch in self.centers:
items_xys = [self.centers[ch][0]-80*self.sizes[ch],
self.centers[ch][1]-80*self.sizes[ch],
self.centers[ch][0]+80*self.sizes[ch],
self.centers[ch][1]+80*self.sizes[ch]]
self.canvas.coords(self.items[ch], *items_xys)
new_label = 'NO STREAM'
if self.hivemind.thought_train_stream in self.stream_mapping:
new_label = self.stream_mapping[self.hivemind.thought_train_stream]
self.canvas.itemconfigure(
self.label, text=new_label)
def make_viz(self):
"""
Loop to update the window content at 10 Hz.
"""
while self.hivemind.running:
self.root.update_idletasks()
self.root.update()
if self.window_closed is True:
self.root.destroy()
break
self.callback()
time.sleep(0.1)
class Main:
"""
Main script to start the robot arm drawing digital score work.
Conducter calls the local interpreter for project specific functions. This
communicates directly to the robot libraries.
Nebula kick-starts the AI Factory for generating NNet data and affect
flows.
This script also controls the live mic audio analyser.
Paramaters are to be modified in config.py.
"""
def __init__(self):
art.tprint("Jess +")
# Build initial dataclass filled with random numbers
self.hivemind = DataBorg()
# Logging for all modules
logging.basicConfig(level=logging.WARNING)
# Init the AI factory (inherits AIFactory, Listener)
nebula = Nebula(speed=config.speed)
# Init Conducter & Gesture management (controls Drawbot)
robot = Conducter(speed=config.speed)
# Init data writer
dw = DataWriter()
# Start Nebula AI Factory after conducter starts data moving
nebula.endtime = time.time() + config.duration_of_piece
self.hivemind.running = True
robot.main_loop()
nebula.main_loop()
dw.main_loop()
# Visualiser
if config.viz:
viz = Visualiser(self.hivemind)
viz.make_viz()
if __name__ == "__main__":
Main()