-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (151 loc) · 5.02 KB
/
main.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
import re
import os
import errno
import threading
import brick_control.soundControl as soundControl
import brick_control.bluetoothConnection as bluetoothConnection
import brick_control.motorControl as motorControl
import brick_control.usbConnection as usbConnection
import brick_control.colorControl as colorControl
import ledsControl
#global variables
ID = '00:16:53:0C:93:59'
FIFO = '/tmp/pixy'
running_events = False
#connect to brick via usb
usbConn = usbConnection.USBConnection()
brick = usbConn.connect()
#create sound controller object
soundCtrl = soundControl.SoundControl(brick)
#create motor controller object
motorCtrl = motorControl.MotorControl(brick, 80)
#create leds controller object
ledsCtrl = ledsControl.LEDsControl(brick)
#create color controller object
colorCtrl = colorControl.ColorControl(brick)
class Object:
width = 0
height = 0
x = 0
y = 0
signature = 0
def __init__(self, signature, width, height, x, y):
self.width = width
self.height = height
self.x = x
self.y = y
self.signature = signature
def darudeRun(distance):
global motorCtrl
motorCtrl.move(distance, 1)
motorCtrl.spinAround(180, 1)
motorCtrl.move(distance, 1)
motorCtrl.spinAround(180, 1)
def dance():
global motorCtrl
motorCtrl.spinAround(180, 1)
motorCtrl.spinAround(100, -1)
motorCtrl.spinAround(80, 1)
motorCtrl.spinAround(60, -1)
motorCtrl.spinAround(60, 1)
motorCtrl.spinAround(160, -1)
def waveArm(full):
global motorCtrl
motorCtrl.moveArm(650, 1)
motorCtrl.moveArm(650, -1)
motorCtrl.moveArm(170, 1)
if full:
motorCtrl.moveArm(130, -1)
def minorShake():
global motorCtrl
motorCtrl.spinAround(60, 1)
motorCtrl.spinAround(60, -1)
motorCtrl.spinAround(40, 1)
motorCtrl.spinAround(50, -1)
motorCtrl.spinAround(50, 1)
motorCtrl.spinAround(40, -1)
def filter(frame):
global running_events
global soundCtrl
global motorCtrl
global ledsCtrl
for obj in frame:
if obj.signature == '1':
#half spin
motorCtrl.spinAround(180, 1)
t1 = threading.Thread(target=soundCtrl.play, args=("Super Mario", ))
t2 = threading.Thread(target=waveArm, args=(True,))
t3 = threading.Thread(target=ledsCtrl.handle_music, args=("Super Mario", ))
t4 = threading.Thread(target=minorShake)
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
break
elif obj.signature == '4':
t1 = threading.Thread(target=soundCtrl.play, args=("Darude Sandstorm", ))
t2 = threading.Thread(target=waveArm, args=(False,))
t3 = threading.Thread(target=ledsCtrl.handle_music, args=("Darude Sandstorm", ))
t4 = threading.Thread(target=darudeRun, args=(360,))
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
break
elif obj.signature == '6':
#blue_action()
t1 = threading.Thread(target=soundCtrl.play, args=("Super Mario Underworld", ))
t2 = threading.Thread(target=waveArm, args=(False,))
t3 = threading.Thread(target=ledsCtrl.handle_music, args=("Super Mario Underworld", ))
t4 = threading.Thread(target=dance)
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
break
running_events = False
def processFrame (objectsFromFrame = []):
frame = []
for line in objectsFromFrame:
#print(line)
objectAttributeList = re.findall(r'[0-9]+', line)
if len(objectAttributeList) > 4:
obj = Object(objectAttributeList[0],objectAttributeList[1],
objectAttributeList[2],objectAttributeList[3],
objectAttributeList[4])
frame.append(obj)
return frame
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
#print("Waiting for camera connection")
with open(FIFO) as fifo:
#print("Camera has been connected")
soundCtrl.playNote(500, 1000)
while True:
data = fifo.readline()
#color = colorCtrl.get_color()
#print color
if len(data) == 0:
#print("Writer closed")
break
#.decode("utf-8")
if running_events == False:
running_events = True
frame = processFrame(data.split('\n'))
x = threading.Thread(target=filter, args=(frame,))
x.start()