-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_ver1.py
340 lines (292 loc) · 12 KB
/
main_ver1.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
from piotimer import Piotimer as Timer
from ssd1306 import SSD1306_I2C
from machine import Pin, ADC, I2C, PWM
from fifo import Fifo
import utime
import array
import time
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# GPIO #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
# ADC-converter
adc = ADC(26)
# OLED
i2c = I2C(1, scl = Pin(15), sda = Pin(14))
oled = SSD1306_I2C(128, 64, i2c)
# LEDs
led_onboard = Pin("LED", Pin.OUT)
led21 = PWM(Pin(21))
led21.freq(1000)
#led = [20, 21, 22]
#list_of_led = []
#for x in range(0,3):
# list_of_led.append(PWM(Pin(led[x])))
# list_of_led[x].freq(1000)
# Rotary Encoder
rot_push = Pin(12, mode = Pin.IN, pull = Pin.PULL_UP)
rota = Pin(10, mode = Pin.IN, pull = Pin.PULL_UP)
rotb = Pin(11, mode = Pin.IN, pull = Pin.PULL_UP)
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# FREQUENCY #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
# Sample Rate, Buffer
samplerate = 250
samples = Fifo(samplerate / 5)
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# VARIABLES #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
# Menu selection variables and switch filtering
mode = 0
count = 0
switch_state = 0
# Variables related to peak detection and peak-to-peak intervals
x1 = -1
y1 = 32
m0 = 65535 / 2
a = 1/10
disp_div = samplerate / 25
disp_count = 0
buffer = []
capture_length = samplerate * 10
index = 0
avg_size = int(samplerate * 0.5)
sample_sum = 0
min_bpm = 30
max_bpm = 200
sample_peak = 0
sample_index = 0
previous_peak = 0
previous_index = 0
PPI_array = []
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# FUNCTIONS #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
########################################
# Functions for reading the signal #
########################################
def read_adc(tid):
x = adc.read_u16()
samples.put(x)
tmr = Timer(freq = samplerate, callback = read_adc)
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# Functions for HRV calculations #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
###########################
# Mean PPI Calculator #
###########################
def meanPPI_calculator(data):
sumPPI = 0
for i in data:
sumPPI += i
PPI = sumPPI/len(data)
rounded_PPI = round(PPI, 0)
#print('Mean PPI value:', int(rounded_PPI), 'ms')
return int(rounded_PPI)
##########################
# Mean HR Calculator #
##########################
def meanHR_calculator(meanPPI):
HR = 60*1000/meanPPI
rounded_HR = round(HR, 0)
#print('Mean HR value:', int(rounded_HR), 'bpm')
return int(rounded_HR)
#######################
# SDNN Calculator #
#######################
def SDNN_calculator(data, PPI):
summary = 0
for i in data:
summary += (i-PPI)**2
SDNN = (summary/(len(data)-1))**(1/2)
rounded_SDNN = round(SDNN, 0)
#print('SDNN value:', int(rounded_SDNN), 'ms')
return int(rounded_SDNN)
########################
# RMSSD Calculator #
########################
def RMSSD_calculator(data):
i = 0
summary = 0
while i < len(data)-1:
summary += (data[i+1]-data[i])**2
i +=1
RMSSD = (summary/(len(data)-1))**(1/2)
rounded_RMSSD = round(RMSSD, 0)
#print('RMSSD value:', int(rounded_RMSSD), 'ms')
return int(rounded_RMSSD)
#######################
# SDSD Calculator #
#######################
def SDSD_calculator(data):
i = 0
PP_array = array.array('l')
first_value = 0
second_value = 0
while i < len(data)-1:
PP_array.append(int(data[i+1])-int(data[i]))
i += 1
i = 0
while i < len(PP_array)-1:
first_value += float(PP_array[i]**2)
second_value += float(PP_array[i])
i += 1
first = first_value/(len(PP_array)-1)
second = (second_value/(len(PP_array)))**2
SDSD = (first - second)**(1/2)
rounded_SDSD = round(SDSD, 0)
#print('SDSD value:', int(rounded_SDSD))
return int(rounded_SDSD)
######################
# SD1 Calculator #
######################
def SD1_calculator(SDSD):
SD1 = ((SDSD**2)/2)**(1/2)
rounded_SD1 = round(SD1, 0)
#print('SD1 value:', int(rounded_SD1))
return int(rounded_SD1)
######################
# SD2 Calculator #
######################
def SD2_calculator(SDNN, SDSD):
SD2 = ((2*(SDNN**2))-((SDSD**2)/2))**(1/2)
rounded_SD2 = round(SD2, 0)
#print('SD2 value:', int(rounded_SD2))
return int(rounded_SD2)
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# WELCOME TEXT #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
oled.fill(1)
oled.text("Welcome to", 23, 17, 0)
oled.text("Group 5's", 26, 27, 0)
oled.text("project!", 30, 37, 0)
oled.show()
utime.sleep_ms(2500)
oled.fill(0)
oled.show()
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.#
# #
# MAIN PROGRAMME #
# #
#*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*:*.*.*.*.*.*.*.*.*.#
while True:
new_state = rot_push.value()
if new_state != switch_state:
count += 1
if count > 3:
if new_state == 0:
if mode == 0:
mode = 1
else:
mode = 0
switch_state = new_state
count = 0
else:
count = 0
utime.sleep(0.01)
if mode == 0:
oled.fill(0)
oled.text("Press to start", 0, 22, 1)
oled.text("the measurement", 0, 32, 1)
oled.text(" ------->", 0, 55, 1)
oled.show()
else:
oled.fill(0)
oled.show()
#####################################
# Plotting the signal, Sampling #
#####################################
while len(buffer) < capture_length:
if not samples.empty():
x = samples.get()
buffer.append(x)
disp_count += 1
if disp_count >= disp_div:
disp_count = 0
m0 = (1-a)*m0 + a*x
y2 = int(32*(m0-x)/10000 + 32)
y2 = max(0, min(64, y2))
x2 = x1 + 1
oled.line(x2, 0, x2, 64, 0)
oled.line(x1, y1, x2, y2, 1)
oled.show()
x1 = x2
if x1 > 127:
x1 = -1
y1 = y2
tmr.deinit()
######################
# Peak Detection #
######################
while(index < avg_size):
sample_sum = sample_sum + buffer[index]
index += 1
while(index < len(buffer)):
sample_avg = sample_sum / avg_size
sample_val = buffer[index]
if sample_val > sample_avg * 1.05:
if sample_val > sample_peak:
sample_peak = sample_val
sample_index = index
else:
if sample_peak > 0:
if (sample_index - previous_index) > (60 * samplerate / min_bpm):
previous_peak = 0
previous_index = 0
else:
if sample_peak >= (0.8 * previous_peak):
if (sample_index - previous_index) > (60 * samplerate / max_bpm):
if previous_peak > 0:
interval = sample_index - previous_index
interval_ms = int(interval * 1000 / samplerate)
PPI_array.append(interval_ms)
#print("BPM: " + str((samplerate / interval) * 60))
previous_peak = sample_peak
previous_index = sample_index
print("Sample " + str(sample_index) + " peak: " + str(sample_peak))
sample_peak = 0
sample_sum = sample_sum + buffer[index] - buffer[index-avg_size]
index += 1
#######################
# HRV calculation #
#######################
#PPI_array2 = array.array('H', [754, 854, 968, 796, 785, 983, 1012, 879, 846, 794, 689, 987, 834, 821, 768, 895])
oled.fill(0)
if len(PPI_array) > 0:
mean_PPI = meanPPI_calculator(PPI_array)
mean_HR = meanHR_calculator(mean_PPI)
SDNN = SDNN_calculator(PPI_array, mean_PPI)
RMSSD = RMSSD_calculator(PPI_array)
SDSD = SDSD_calculator(PPI_array)
SD1 = SD1_calculator(SDSD)
SD2 = SD2_calculator(SDNN, SDSD)
oled.text('MeanPPI:'+ str(int(mean_PPI)) +'ms', 0, 0, 1)
oled.text('MeanHR:'+ str(int(mean_HR)) +'bpm', 0, 10, 1)
oled.text('SDNN:'+str(int(SDNN)) +'ms', 0, 20, 1)
oled.text('RMSSD:'+str(int(RMSSD)) +'ms', 0, 30, 1)
oled.text('SD1:'+str(int(SD1)), 0, 40, 1)
oled.text('SD2:'+str(int(SD2)), 0, 50, 1)
oled.show()
utime.sleep_ms(4500)
oled.fill(0)
oled.show()
else:
oled.text('Error', 45, 10, 1)
oled.text('Please restart', 8, 30, 1)
oled.text('measurement', 20, 40, 1)
oled.show()
utime.sleep_ms(4500)
oled.fill(0)
oled.show()
mode = 0
PPI_array.clear()
buffer.clear()