-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv_subproc_UDP.py
82 lines (75 loc) · 2.38 KB
/
recv_subproc_UDP.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
import numpy as np
import socket
from datetime import datetime
from json import loads
from multiprocessing import Process, Queue
from sys import exit
from time import sleep, time
def get_data(udp, queue):
while True:
data, addr = udp.recvfrom(1024)
data = loads(data[:-2].decode('utf-8'))
queue.put(data['data'])
def flush_queue(queue):
while not queue.empty():
queue.get()
def run(t_move, t_rest, protocol, name, sr):
# configurações de conexão com o UDP
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = ('127.0.0.1', 5002)
udp.bind(host)
# configurações de inicialização de processo
queue = Queue()
proc = Process(target=get_data, args=(udp, queue))
proc.start()
# inicia o protocolo do experimento
n_data = sr * t_move
dataset = list()
for p in protocol:
# aplicando um tempo de descanço
print('\nDescanço...\n')
sleep(time_rest)
# obtenção e tratamento dos dados
trial = list()
print(p)
flush_queue(queue)
now = time()
while (time() - now) < t_move:
try:
data = queue.get()
trial.append(data)
except KeyboardInterrupt:
udp.close()
proc.terminate()
exit()
print('N Trial:', len(trial))
if len(trial) > n_data:
print('1ªs {} amostras descartadas'.format(len(trial) - n_data))
dataset.append(trial[(len(trial) - n_data):])
elif len(trial) == n_data:
dataset.append(trial)
else:
print('Atraso no envio dos dados! Programa finalizado...')
udp.close()
proc.terminate()
exit()
# finaliza a conexão e o subprocesso
udp.close()
proc.terminate()
# salva os dados
dataset = np.array(dataset)
oclock = datetime.now().strftime('%d-%m-%Y_%H:%M:%S')
file_name = '{}_{}'.format(name, oclock)
np.save(file_name, dataset)
print()
print('-' * 60)
print('Arquivo "{}.npy" salvo! Dimensionalidade: {}'.format(
file_name, dataset.shape))
print('-' * 60)
if __name__ == "__main__":
time_move = 5
time_rest = 3
protocol = ['hand_open', 'hand_close', 'okay', 'do_gun']
name = '1_EMG_Rodrigo'
sample_rate = 200
run(time_move, time_rest, protocol, name, sample_rate)