-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathkraz3.py
More file actions
214 lines (166 loc) · 6.92 KB
/
kraz3.py
File metadata and controls
214 lines (166 loc) · 6.92 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
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
#!/usr/bin/env python3
from ev3dev2.motor import MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C
from ev3dev2.sensor import INPUT_1, INPUT_3
from ev3dev2.sensor.lego import TouchSensor, ColorSensor
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.control.rc_tank import RemoteControlledTank
from multiprocessing import Process
from random import randint
from time import sleep
class Kraz3(RemoteControlledTank):
def __init__(
self,
left_motor_port: str = OUTPUT_C, right_motor_port: str = OUTPUT_B,
wiggle_motor_port: str = OUTPUT_A,
touch_sensor_port: str = INPUT_1, color_sensor_port: str = INPUT_3,
ir_beacon_channel: int = 1):
super().__init__(
left_motor_port=left_motor_port, right_motor_port=right_motor_port,
polarity='inversed',
speed=1000,
channel=ir_beacon_channel)
self.wiggle_motor = MediumMotor(address=wiggle_motor_port)
self.touch_sensor = TouchSensor(address=touch_sensor_port)
self.color_sensor = ColorSensor(address=color_sensor_port)
self.leds = Leds()
self.speaker = Sound()
def kungfu_manoeuvre_if_touched_or_remote_controlled(self):
"""
Kung-Fu manoeuvre via Touch Sensor and Remote Control of head and arms
"""
while True:
if self.touch_sensor.is_pressed:
self.speaker.play_file(
wav_file='/home/robot/sound/Kung fu.wav',
volume=100,
play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
self.wiggle_motor.on_for_rotations(
speed=50,
rotations=1,
brake=True,
block=True)
elif self.remote.beacon(channel=self.channel):
self.wiggle_motor.on(
speed=11,
brake=False,
block=False)
else:
self.wiggle_motor.off(brake=True)
sleep(0.01)
def keep_reacting_to_colors(self):
while True:
detected_color = self.color_sensor.color
if detected_color == ColorSensor.COLOR_YELLOW:
self.speaker.play_file(
wav_file='/home/robot/sound/Yellow.wav',
volume=100,
play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
self.wiggle_motor.on_for_rotations(
speed=-86,
rotations=1,
brake=True,
block=True)
self.speaker.play_file(
wav_file='/home/robot/sound/Uh-oh.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
sleep(0.5)
self.speaker.play_file(
wav_file='/home/robot/sound/Sneezing.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
sleep(0.5)
elif detected_color == ColorSensor.COLOR_RED:
self.speaker.play_file(
wav_file='/home/robot/sound/Shouting.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
for _ in range(randint(1, 6)):
self.speaker.play_file(
wav_file='/home/robot/sound/Smack.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.leds.set_color(
group='LEFT',
color='RED',
pct=1)
self.leds.set_color(
group='RIGHT',
color='RED',
pct=1)
self.wiggle_motor.on_for_rotations(
speed=17,
rotations=1,
brake=True,
block=True)
self.speaker.play_file(
wav_file='/home/robot/sound/LEGO.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.speaker.play_file(
wav_file='/home/robot/sound/MINDSTORMS.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.leds.all_off()
elif detected_color == ColorSensor.COLOR_BROWN:
self.speaker.play_file(
wav_file='/home/robot/sound/Brown.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
sleep(1)
self.wiggle_motor.on_for_rotations(
speed=-20,
rotations=1,
brake=True,
block=True)
self.speaker.play_file(
wav_file='/home/robot/sound/Crying.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
elif detected_color == ColorSensor.COLOR_GREEN:
self.speaker.play_file(
wav_file='/home/robot/sound/Green.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.wiggle_motor.on_for_rotations(
speed=-40,
rotations=1,
brake=True,
block=True)
self.speaker.play_file(
wav_file='/home/robot/sound/Yes.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
sleep(1)
elif detected_color == ColorSensor.COLOR_BLUE:
self.speaker.play_file(
wav_file='/home/robot/sound/Blue.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.speaker.play_file(
wav_file='/home/robot/sound/Fantastic.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
self.speaker.play_file(
wav_file='/home/robot/sound/Good job.wav',
volume=100,
play_type=Sound.PLAY_NO_WAIT_FOR_COMPLETE)
self.wiggle_motor.on_for_rotations(
speed=75,
rotations=1,
brake=True,
block=True)
self.speaker.play_file(
wav_file='/home/robot/sound/Magic wand.wav',
volume=100,
play_type=Sound.PLAY_WAIT_FOR_COMPLETE)
sleep(0.01)
def main(self):
Process(target=self.kungfu_manoeuvre_if_touched_or_remote_controlled) \
.start()
Process(target=self.keep_reacting_to_colors).start()
super().main()
if __name__ == '__main__':
KRAZ3 = Kraz3()
KRAZ3.main()