-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimestamps.py
158 lines (118 loc) · 3.84 KB
/
timestamps.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
import serial
import serial.tools.list_ports
import zmq
from time import time
import socket
import sys
import os
import datetime
ip = '127.0.0.1'
port = 50020
path = 'C:/Users/Neil Thomas/Desktop/recordings/'
def check_pupil_capture():
"""check pupil capture instance exists"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if not sock.connect_ex((ip, port)):
print('Found Pupil Capture')
else:
print ('Cannot find Pupil Capture')
sys.exit()
sock.close()
def connect_pupil_capture():
"""connect to pupil capture using zmq protocol"""
context = zmq.Context()
socket = zmq.Socket(context, zmq.REQ)
socket.connect('tcp://' + ip + ':' + str(port))
socket.send_string('t')
if not socket.recv():
print('Cannot connect to Pupil Capture')
sys.exit()
return socket
def connect_mocap():
"""
listen to motion capture server via arduino. Oneway trip time between
computer and arduino is ~0.5ms
"""
# find arduino
ports = [p.device for p in serial.tools.list_ports.comports()
if 'USB Serial Device' in p.description]
if not ports:
print('Cannot find arduino. Check connection to computer')
sys.exit()
if len(ports) > 1:
print('Multiple arduinos found. Remove redundant ones')
sys.exit()
ard = serial.Serial(ports[0], 115200)
print('Found arduino')
return ard
def get_pupil_time(socket):
t1 = time()
socket.send_string('t')
pct = socket.recv()
t2 = time()
oneway_dur = (t2-t1) / 2
return (float(pct.decode()) - oneway_dur) - 0.0005
def append_timestamp(start_stop, value):
now = datetime.datetime.now()
year = str(now.year)
month = now.month
day = now.day
if day >= 10:
day = str(day)
else:
day = '0' + str(day)
if month >= 10:
month = str(month)
else:
month = '0' + str(month)
date = year + '_' + month + '_' + day
target = [name for name in os.listdir(path) if name == date]
recordings = os.listdir(path + target[0])
current = max(recordings)
currentPath = path + target[0] + '/' + current + '/'
try:
file = open(currentPath + 'Mocap_timestamps.txt', 'a+')
file.write(start_stop + ': ' + value + '\n')
file.close()
except:
file = open(currentPath + 'Mocap_timestamps.txt', 'w+')
file.write(start_stop + ': ' + value + '\n')
file.close()
class Trigger:
def __init__(self, socket):
self.recording = False
self.socket = socket
print('Ready to start/stop mocap acquisition...')
def start_trigger(self):
if not self.recording:
pct = get_pupil_time(self.socket)
append_timestamp('start', str(pct))
print('Started mocap acquisition')
self.recording = True
def stop_trigger(self):
if self.recording:
pct = get_pupil_time(self.socket)
append_timestamp('stop', str(pct))
print('Stopped mocap acquisition')
self.recording = False
def main():
check_pupil_capture()
socket = connect_pupil_capture()
ard = connect_mocap()
trigger = Trigger(socket)
try:
while True:
data = ard.read(1)
if data.decode() == 'h':
trigger.start_trigger()
if data.decode() == 'l':
trigger.stop_trigger()
except (KeyboardInterrupt, SystemExit):
print('User exited program')
socket.close()
sys.exit()
except Exception as err:
print(err)
socket.close()
if __name__ == '__main__':
main()