-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathkraz3.py
More file actions
163 lines (116 loc) · 5.14 KB
/
kraz3.py
File metadata and controls
163 lines (116 loc) · 5.14 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
#!/usr/bin/env python3
from ev3dev.ev3 import (
Motor, MediumMotor, OUTPUT_A, OUTPUT_B, OUTPUT_C,
TouchSensor, ColorSensor, INPUT_1, INPUT_3,
Leds, Sound
)
from ev3dev.helper 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):
super().__init__(
left_motor=left_motor_port, right_motor=right_motor_port,
polarity='inversed')
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(wav_file='media/Kung fu.wav')
self.wiggle_motor.run_to_rel_pos(
speed_sp=500,
position_sp=360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
elif self.remote.beacon:
self.wiggle_motor.run_forever(speed_sp=111)
else:
self.wiggle_motor.stop(stop_action=Motor.STOP_ACTION_HOLD)
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(wav_file='media/Yellow.wav')
self.wiggle_motor.run_to_rel_pos(
speed_sp=860,
position_sp=-360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
self.speaker.play(wav_file='media/Uh-oh.wav').wait()
sleep(0.5)
self.speaker.play(wav_file='media/Sneezing.wav').wait()
sleep(0.5)
elif detected_color == ColorSensor.COLOR_RED:
self.speaker.play(wav_file='media/Shouting.wav').wait()
for _ in range(randint(1, 6)):
self.speaker.play(wav_file='media/Smack.wav').wait()
self.leds.set_color(
group=Leds.LEFT,
color=Leds.RED,
pct=1)
self.leds.set_color(
group=Leds.RIGHT,
color=Leds.RED,
pct=1)
self.wiggle_motor.run_to_rel_pos(
speed_sp=170,
position_sp=360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
self.speaker.play(wav_file='media/LEGO.wav').wait()
self.speaker.play(wav_file='media/MINDSTORMS.wav').wait()
self.leds.all_off()
elif detected_color == ColorSensor.COLOR_BROWN:
self.speaker.play(wav_file='media/Brown.wav').wait()
sleep(1)
self.wiggle_motor.run_to_rel_pos(
speed_sp=200,
position_sp=-360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
self.speaker.play(wav_file='media/Crying.wav').wait()
elif detected_color == ColorSensor.COLOR_GREEN:
self.speaker.play(wav_file='media/Green.wav').wait()
self.wiggle_motor.run_to_rel_pos(
speed_sp=400,
position_sp=-360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
self.speaker.play(wav_file='media/Yes.wav').wait()
sleep(1)
elif detected_color == ColorSensor.COLOR_BLUE:
self.speaker.play(wav_file='media/Blue.wav').wait()
self.speaker.play(wav_file='media/Fantastic.wav').wait()
self.speaker.play(wav_file='media/Good job.wav')
self.wiggle_motor.run_to_rel_pos(
speed_sp=750,
position_sp=360,
stop_action=Motor.STOP_ACTION_HOLD)
self.wiggle_motor.wait_while(Motor.STATE_RUNNING)
self.speaker.play(wav_file='media/Magic wand.wav').wait()
sleep(0.01)
def main(self):
Process(
target=self.kungfu_manoeuvre_if_touched_or_remote_controlled,
daemon=True).start()
Process(
target=self.keep_reacting_to_colors,
daemon=True).start()
super().main()
if __name__ == '__main__':
KRAZ3 = Kraz3()
KRAZ3.main()