/
hack2.py
79 lines (70 loc) · 2.29 KB
/
hack2.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
import emokit
from emokit import emotiv
import platform
if platform.system() == "Windows":
import socket
import gevent
import numpy as np
import serial
if __name__ == "__main__":
headset = emotiv.Emotiv()
gevent.spawn(headset.setup)
gevent.sleep(0)
try:
SCALE_CONSTANT = 10
arrP8 = [0]*SCALE_CONSTANT # Might have to tweak this amount.
#arrAF4 = [0]*SCALE_CONSTANT
ser = serial.Serial('COM3', 9600)
while True:
packet = headset.dequeue()
#packet.gyro_x, packet.gyro_y
arrP8.append(packet.P8[0])
del arrP8[0]
#arrAF4.append(packet.AF4[0])
#del arrAF4[0]
varianceP8 = np.var(arrP8)
#varianceAF4 = np.var(arrAF4)
buffer = ''
# Straight-line movement
#if (varianceAF4 > 800):
# buffer += 'f'
# print "BACKWARD \n"
#varianceP8 > 950
if (varianceP8 > 1200): # Might have to tweak this threshold.
buffer += 'b'
print "FORWARD \n"
else:
buffer += 's'
print "STOP \n"
# Directional movement (left/right)
# correct calibrate -2 from x
# 4 to -4 sx
# 4 to 10 rl
# 10+ rh
# -4 to -10 ll
# -10- lh
if (packet.gyro_x-2 < 10 and packet.gyro_x-2 > -10):
buffer += 'sx'
print "STILL \n"
elif (packet.gyro_x-2 >= 10):
buffer += 'll'
print "RIGHT LOW \n"
elif (packet.gyro_x-2 <= -10):
buffer += 'rl'
print "LEFT LOW \n"
#elif (packet.gyro_x-2 >= 10):
# buffer += 'lh'
# print "RIGHT HIGH \n"
#elif (packet.gyro_x-2 <= -10):
# buffer += 'rh'
# print "LEFT HIGH \n"
else:
print "????????!!!!!!"
ser.write(buffer)
gevent.sleep(0)
except KeyboardInterrupt:
headset.close()
ser.close()
finally:
headset.close()
ser.close()