-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (130 loc) · 3.78 KB
/
main.py
File metadata and controls
148 lines (130 loc) · 3.78 KB
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
from ArduinoReader import ArduinoReader
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.animation as animation
import threading
from pandas import DataFrame
import pandas as pd
from Ampl import Ampl
from MedianArray import MedianArray
from PrevValues import PrevValues
from MinMaxValues import MinMaxValues
from Data import Data
from RegCatboost import RegCatboost
rc = RegCatboost('model_8_mark')
ar = ArduinoReader("/dev/ttyACM0")
ar.start()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []
reg = False
save = False
# cc = ['unixtime', 'emg0', 'emg1', 'emg2', 'emg0_ampl', 'emg1_ampl', 'emg2_ampl', 'emg0_med', 'emg1_med',]
d = Data({'emg':[3], 'emg_ampl':[3], 'emg_med':[3], 'emg_min':[3], 'emg_max':[3], 'emg_prev':[3, 20]})
cc = d.columns
df = DataFrame([[0 for i in cc]], columns = cc)
def animate(i, xs, ys):
xs.append(time.time())
ys.append(ar.values[0])
# Limit x and y lists to 20 items
xs = xs[-100:]
ys = ys[-100:]
# Draw x and y lists
ax.clear()
plt.ylim((0,1025))
ax.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0)
plt.title('Unixtime')
plt.ylabel('Markovka')
def press(event):
global reg
global save
print('press', event.key)
# sys.stdout.flush()
if event.key == 'x':
reg = not reg
if event.key == 'y':
save = True
def setEmg(dq, vals):
for i in range(len(vals)):
dq.updateData("emg_"+str(i), vals[i])
return dq
def setEmgObj(dq, vals, su):
for i in range(len(vals)):
dq.updateData("emg_" + su + "_" + str(i), vals[i].getVal())
return dq
def setEmgObj2(dq, vals, su, c):
for i in range(len(vals)):
dq.updateData("emg_" + su + "_" + str(i), vals[i].getVal(c))
return dq
def setEmgObj3(dq, vals, su, c, y):
for i in range(len(vals)):
dq.updateData("emg_" + su + "_" + str(i) + "_" + str(y), vals[i].getVal(c))
return dq
def updateEmgObj(objs, vals):
for i in range(len(vals)):
objs[i].update(vals[i])
ampls = [Ampl(45) for i in range(3)]
meds = [MedianArray(45) for i in range(3)]
prev_vals = [PrevValues(20, 500) for i in range(3)]
min_max_vals = [MinMaxValues(20) for i in range(3)]
vals = [500, 500, 500]
# Set up plot to call animate() function periodically
def up():
global df
global save
global reg
global d
global vals
while True:
# vals = ar.values
# updateEmgObj(ampls, vals)
# updateEmgObj(meds, vals)
# updateEmgObj(prev_vals, vals)
# updateEmgObj(min_max_vals, vals)
# time.sleep(0.001)
d = setEmg(d, vals)
d = setEmgObj(d, ampls, "ampl")
d = setEmgObj(d, meds, "med")
d = setEmgObj2(d, min_max_vals, "min", 0)
d = setEmgObj2(d, min_max_vals, "max", 1)
for i in range(20):
d = setEmgObj3(d, prev_vals, "prev", i, i)
# print(rc.predict(d.df))
if reg:
df = pd.concat([df, d.df], axis=0)
print(df)
if save:
df.to_csv('./3_sen_mark' + str(int(time.time())) +'.csv')
df = d.df
save = False
time.sleep(0.001)
def calc():
global df
global save
global reg
global d
global vals
while True:
vals = ar.values
updateEmgObj(ampls, vals)
updateEmgObj(meds, vals)
updateEmgObj(prev_vals, vals)
updateEmgObj(min_max_vals, vals)
time.sleep(0.01)
t2 = threading.Thread(target=calc, args=())
t2.daemon = True
t2.start()
t = threading.Thread(target=up, args=())
t.daemon = True
t.start()
fig.canvas.mpl_connect('key_press_event', press)
# ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=25)
plt.show()
while True:
pass