-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
180 lines (158 loc) · 5.23 KB
/
Copy pathplotting.py
File metadata and controls
180 lines (158 loc) · 5.23 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
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
import sys
import serial
import numpy as np
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTimer, Qt
import time
import csv
# ==========================================
# 1. AYARLAR
# ==========================================
SERIAL_PORT = '/dev/cu.usbmodem1101'
BAUD_RATE = 921600
MAX_POINTS = 10000
OUTPUT_FILE = 'emg_kayit_ch1.csv'
# Etiket eslemesi
# 1 = REST ('1' basili tut)
# 2 = BICEPS ('2' basili tut)
# Tus yok = kayit yok (gecis anlarini etiketleme)
LABELS = {'1': 1, '2': 2}
LABEL_NAMES = {0: 'IDLE', 1: 'REST', 2: 'BICEPS'}
LABEL_COLORS = {0: '#94a3b8', 1: '#22c55e', 2: '#ef4444'}
current_label = 0 # tus yok = idle, kayit yok
active_keys = set()
# ==========================================
# 2. SERIAL + CSV
# ==========================================
print(f"🔄 {SERIAL_PORT} portuna bağlanılıyor...")
try:
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=0.01)
print("✅ Bağlantı OK!")
except Exception as e:
print(f"❌ Hata: {e}")
sys.exit()
try:
csv_file = open(OUTPUT_FILE, mode='w', newline='')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Zaman (sn)', 'Voltaj (V)', 'Label'])
print(f"📁 Kayit: '{OUTPUT_FILE}'")
except Exception as e:
print(f"❌ Dosya hatasi: {e}")
ser.close()
sys.exit()
print("-" * 50)
print("ETIKETLEME (PLOT PENCERESI ODAKTA OLMALI):")
print(" Tus yok -> kayit yok (idle)")
print(" '1' basili tut -> REST")
print(" '2' basili tut -> BICEPS")
print("-" * 50)
start_time = time.time()
# ==========================================
# 3. GUI - Qt klavye event'leri
# ==========================================
class PlotWindow(pg.GraphicsLayoutWidget):
def keyPressEvent(self, event):
global current_label
if event.isAutoRepeat():
return
ch = event.text()
if ch in LABELS:
active_keys.add(ch)
current_label = LABELS[ch]
else:
super().keyPressEvent(event)
def keyReleaseEvent(self, event):
global current_label
if event.isAutoRepeat():
return
ch = event.text()
if ch in active_keys:
active_keys.discard(ch)
current_label = LABELS[next(iter(active_keys))] if active_keys else 0
else:
super().keyReleaseEvent(event)
app = QApplication(sys.argv)
pg.setConfigOptions(antialias=True)
win = PlotWindow(show=True, title="EMG + Etiketleme")
win.resize(1100, 500)
win.setWindowTitle('EMG Bionic Hand - Etiketli Kayit')
win.setBackground('#0f172a')
win.setFocusPolicy(Qt.StrongFocus)
win.setFocus()
p = win.addPlot()
p.setLabel('bottom', 'Ornekler (Samples)')
p.setLabel('left', 'CH1: Sensor (Raw ADC)')
p.setYRange(0, 4095, padding=0)
p.setXRange(0, MAX_POINTS, padding=0)
p.showGrid(x=True, y=True, alpha=0.3)
curve = p.plot(pen=pg.mkPen(LABEL_COLORS[0], width=2.5))
label_text = pg.TextItem(text='IDLE', color=LABEL_COLORS[0], anchor=(0, 0))
label_text.setPos(50, 3.15)
font = pg.QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
label_text.setFont(font)
p.addItem(label_text)
count_text = pg.TextItem(text='', color='#cbd5e1', anchor=(1, 0))
count_text.setPos(MAX_POINTS - 50, 3.15)
font2 = pg.QtGui.QFont()
font2.setPointSize(11)
count_text.setFont(font2)
p.addItem(count_text)
data_buffer = np.zeros(MAX_POINTS)
counts = {1: 0, 2: 0}
last_drawn_label = -1
# ==========================================
# 4. UPDATE
# ==========================================
def update():
global data_buffer, last_drawn_label
has_new_data = False
lines_read = 0
while ser.in_waiting > 0 and lines_read < 1000:
try:
line_str = ser.readline().decode('utf-8', errors='ignore').strip()
if line_str:
try:
raw_val = float(line_str)
voltage_val = (raw_val / 4095.0) * 3.3
t = time.time() - start_time
if current_label > 0:
csv_writer.writerow([f"{t:.4f}", f"{voltage_val:.4f}", current_label])
counts[current_label] += 1
data_buffer = np.roll(data_buffer, -1)
data_buffer[-1] = raw_val
has_new_data = True
except ValueError:
pass
except Exception:
pass
lines_read += 1
if has_new_data:
curve.setData(data_buffer)
if current_label != last_drawn_label:
label_text.setText(LABEL_NAMES[current_label])
label_text.setColor(LABEL_COLORS[current_label])
curve.setPen(pg.mkPen(LABEL_COLORS[current_label], width=2.5))
last_drawn_label = current_label
count_text.setText(
f"REST={counts[1]} BICEPS={counts[2]}"
)
timer = QTimer()
timer.timeout.connect(update)
timer.start(0)
# ==========================================
# 5. CALISTIR
# ==========================================
if __name__ == '__main__':
try:
sys.exit(app.exec_())
except KeyboardInterrupt:
print("\nDurduruldu.")
finally:
ser.close()
csv_file.close()
print("-" * 50)
print(f"REST={counts[1]} BICEPS={counts[2]}")
print(f"Kaydedildi: {OUTPUT_FILE}")