-
Notifications
You must be signed in to change notification settings - Fork 3
/
gestures.py
executable file
·243 lines (190 loc) · 6.97 KB
/
gestures.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
#!/usr/bin/env python
# encoding: utf-8
"""
gestures.py
"""
import os
import sys
import getopt
import serial
import pickle
import atexit
import time
from copy import deepcopy
help_message = '''
Use -l, --limits to set the min and max limits for the sensor
Use -c or --calibrate with name of pattern you want to calibrate
e.g. scan, build or attack
-l, --limits will set the range of the accelerometer
-g, --getSample will take sample data and compare it with save patterns
'''
# global stuff
try:
# ser = serial.Serial('/dev/tty.usbserial-A7004INu', 9600)
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.readline()
ser.readline()
ser.readline()
ser.flush()
except serial.serialutil.SerialException as detail:
print 'Serial error:', detail
# these are used to define the limits of the sensor
maxData = [0,0,0]
minData = [255,255,255]
areas = {}
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hc:lm", ["help", "calibrate=", "limits", "match"])
except getopt.error, msg:
raise Usage(msg)
for option, value in opts:
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-c", "--calibrate"):
calibratePattern(value)
if option in ("-l", "--limits"):
defineLimits()
if option in ("-m", "match"):
matchPattern()
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\t for help use --help"
return 2
def calibratePattern(pattern):
try:
tempPattern = loadPattern("pickles/"+pattern+"Pattern.pickle")
except IOError:
print "can't find saved data"
tempPattern = None
if tempPattern: tempPattern = getSampleData(100,tempPattern)
else: tempPattern = getSampleData(100)
print "pattern calibrated"
savePattern(tempPattern, "pickles/"+pattern+"Pattern.pickle")
def matchPattern():
attackRight = loadPattern("pickles/attackRightPattern.pickle")
attackLeft = loadPattern("pickles/attackLeftPattern.pickle")
scan = loadPattern("pickles/scanPattern.pickle")
build = loadPattern("pickles/buildPattern.pickle")
bestFit = {}
sample = None
while 1:
sample = getSampleData(2, sample)
bestFit['attackRight'] = patternDifference(sample, attackRight)
bestFit['attackLeft'] = patternDifference(sample, attackLeft)
bestFit['build'] = patternDifference(sample, build)
bestFit['scan'] = patternDifference(sample, scan)
print min(bestFit, key=bestFit.get)
def patternDifference(a,b):
totalDifference = float(sys.maxint)
for i in a.keys():
for j in a[i].keys():
totalDifference -= abs( a[i][j] - b[i][j] )
totalDifference = sys.maxint - totalDifference
return totalDifference
def defineLimits():
"""
Run this function to find the limits of the accelerometer. Constantly reads the values and keeps track of the min
and mac on each access. When keyboard interrupts, the min and max values are used to create a range and three regions
on each axis. the regions are used later on when creating patterns and sample data
"""
atexit.register(resetLimits)
while 1:
data = readSerial()
for i in range(len(maxData)):
if data[i] < 255 and data[i] > maxData[i]:
maxData[i] = data[i]
if data[i] < minData[i]:
minData[i] = data[i]
printResults(minData, maxData)
def getSampleData(sampleLength, averageSoFar=None):
"""
Get some sample data in the form of a nested dictionary.m length
Value is the percentage of times a transition happened, transition is defined in the dictionary
Keys of dictionaries are tuples that represent spacial coordinates.
"""
sampleData = initPattern(1)
lastPosition = (0,0,0)
areas = loadPattern("pickles/areas.pickle")
counter = 0
while counter < sampleLength:
data = readSerial()
keys = ['x', 'y', 'z']
data = {'x': data[0], 'y': data[1], 'z': data[2]}
results = {}
for k in keys:
if data[k] < areas[k][0]: results[k] = 0
elif data[k] < areas[k][1]: results[k] = 1
else: results[k] = 2
currentPosition = (results['x'], results['y'], results['z'])
if lastPosition != currentPosition:
#print "here is current position: " + repr(currentPosition)
sampleData[lastPosition][currentPosition]+=1
lastPosition = currentPosition
counter+=1
#print "done taking samples"
temp = deepcopy(sampleData)
for i in temp.keys():
for j in temp[i].keys():
sampleData[i][j] = temp[i][j] / float(sampleLength)
if averageSoFar:
temp = deepcopy(sampleData)
for i in temp.keys():
for j in temp[i]:
sampleData[i][j] = (temp[i][j] + averageSoFar[i][j]) / 2.0
return sampleData
#savePattern(sampleData, "pickles/sampleData.pickle")
def readSerial():
try:
data = ser.readline()
except (KeyboardInterrupt, SystemExit):
raise
except serial.serialutil.SerialException as detail:
print 'Serial error:', detail
else:
data = data.split()
for i in range(len(data)): data[i] = int(data[i])
return data
def printResults(*arg):
os.system('clear')
for value in arg:
print repr(value)
def savePattern(pattern, fileName):
with open(str(fileName), 'wb') as f:
pickle.dump(pattern, f)
def loadPattern(fileName):
with open(str(fileName), 'rb') as f:
return pickle.load(f)
def initPattern(level):
p = {}
for i in range(3):
for j in range(3):
for k in range(3):
if level > 0:
p[i,j,k] = initPattern(level-1)
else:
p[i,j,k] = 0
return p
def resetLimits():
"""
Quantize the range on each axis of the accelerometer, save range and areas to three seperate pickles
areas is a dictionary that defines the minimum, 1/3, 2/3 and maximum of the range on each axsis of the accelererometer
"""
xRange = maxData[0] - minData[0]
yRange = maxData[1] - minData[1]
zRange = maxData[2] - minData[2]
areas["x"] = (minData[0] + (xRange/3), minData[0] + (xRange/3)*2)
areas["y"] = (minData[1] + (yRange/3), minData[1] + (yRange/3)*2)
areas["z"] = (minData[2] + (zRange/3), minData[2] + (zRange/3)*2)
savePattern(areas, "pickles/areas.pickle")
print "everything saved"
def loadLimits():
areas = loadPattern("pickles/areas.pickle")
return areas
if __name__ == "__main__":
sys.exit(main())