-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcalib.py
More file actions
124 lines (99 loc) · 4.05 KB
/
Copy pathcalib.py
File metadata and controls
124 lines (99 loc) · 4.05 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
#!/usr/bin/env python
import time
import serial
import cPickle
import sys
import os
class Calibrator:
''' Rudimentary Calibrator class to read digital values sent by Arduino
over a serial connection and store them for classification with live data.
Prompts the user to move their eyes to different positions in sequential order.
Uses values acquired at different positions to populate a dictionary with
keys as eye positions and values as the corresponding set of values acquired.
The larger the dataset (user preference), the more accurate the classification.'''
# The dictionary of positions:values for serialization.
positions = {'BLINK' : [],
'UP' : [],
'UP-RIGHT' : [],
'RIGHT' : [],
'DOWN-RIGHT' : [],
'DOWN' : [],
'DOWN-LEFT' : [],
'LEFT' : [],
'UP-LEFT' : [],
'STRAIGHT' : []}
def __init__(self):
# Prompt the user to enter preferred size of dataset until valid integer is input.
while True:
try:
self.maxVal = int(raw_input("How many values per dataset? "))
break
except:
print 'Enter valid integer value.'
continue
# If dataset exists already, ask for confirmation to overwrite.
if os.path.exists('datasets.p'):
calAgain = str(raw_input('Calibrated dataset exists already. Recalibrate? (y(default)/n): '))
if calAgain == 'n':
return
else:
pass
self.calibrate(self.maxVal)
self.store(self.positions)
def countdown(self, bufferTime = 3, printCount = True):
''' Countdown to help in stabilizing values
received over the USB port before calibrating.
Default countdown is 3 seconds. '''
while bufferTime > 0:
print bufferTime
time.sleep(1)
bufferTime -= 1
return True
def openSerialPort(self, port, baudrate):
''' Open a serial connection at port "port"
and baud rate "baudrate". Return the
connection object. '''
s = serial.Serial(port, baudrate)
return s
def calibrate(self, upper):
''' Main calibration function to build the
dataset and serialize it to a Pickle.
upper - The size of the dataset entered by the user. '''
serialObj = self.openSerialPort('/dev/ttyACM0', 9600)
print 'Stabilizing serial data'
self.countdown()
# Iterate through eye positions and prompt the user
# to move their eyes correspondingly for calibrating.
for eachPos in range(len(self.positions)):
pos = self.positions.keys()[eachPos]
print 'Make the following eye gesture: %s' % pos
self.countdown()
i = 0
while (i < upper):
try:
# Parse out (split around comma) the values into left electrode signal value l
# and right electrode signal value r, gotten from the Arduino,
# getting rid of the cruft.
(l, r, _) = serialObj.readline().strip('\x00\r\n').strip().split(',')
# Append values to corresponding position in the dataset.
self.positions[pos].append((int(l), int(r)))
except:
continue
i += 1
goAhead = str(raw_input('Continue? (y(default)/n) '))
if goAhead == 'n':
sys.exit(0)
else:
continue
# Clear input buffer.
serialObj.flushInput()
# Close serial port.
serialObj.close()
def store(self, dataset):
''' Serialize the dataset to a pickle. '''
cPickle.dump(dataset, open('datasets.p', 'wb'))
def read(self):
''' Read the dataset from the pickle. '''
return cPickle.load(open('datasets.p', 'rb'))
if __name__ == "__main__":
a = Calibrator()