-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_tank.py
More file actions
executable file
·110 lines (81 loc) · 2.04 KB
/
Copy pathmove_tank.py
File metadata and controls
executable file
·110 lines (81 loc) · 2.04 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
#!/usr/bin/env python3
from ev3dev2.sound import Sound
from ev3dev2.sensor.lego import TouchSensor, ColorSensor
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3
from ev3dev2.motor import OUTPUT_A, OUTPUT_B, MoveTank, SpeedPercent, LineFollowErrorTooFast, follow_for_ms # type: ignore
##################
# #
# SETTINGS #
# #
##################
CONSTANT_P = 11.3
CONSTANT_I = 0.05
CONSTANT_D = 3.2
SPEED_PERCENT = SpeedPercent(30)
CHECK_BUTTON_INTERVAL_MS = 1000
###################
# #
# CONSTANTS #
# #
###################
SOUND_VOLUME = 100
LEFT = 0
RIGHT = 1
#####################
# #
# PERIPHERALS #
# #
#####################
sound = Sound()
button = TouchSensor(INPUT_3)
left_sensor = ColorSensor(INPUT_1)
right_sensor = ColorSensor(INPUT_2)
sensors = [left_sensor, right_sensor]
move_tank = MoveTank(OUTPUT_A, OUTPUT_B)
######################
# #
# MAIN PROGRAM #
# #
######################
def speak(message: str) -> None:
sound.speak(message)
print(message)
def work() -> None:
while True:
if button.is_pressed:
handle_button_pressed()
else:
iterate()
def handle_button_pressed() -> None:
stop()
speak('STOP')
button.wait_for_released()
button.wait_for_bump()
speak('START')
def iterate() -> None:
try:
move_tank.follow_line(
kp=CONSTANT_P,
ki=CONSTANT_I,
kd=CONSTANT_D,
speed=SPEED_PERCENT,
follow_for=follow_for_ms, # type: ignore
ms=CHECK_BUTTON_INTERVAL_MS
)
except LineFollowErrorTooFast:
move_tank.stop()
raise
def stop() -> None:
move_tank.stop()
def main() -> None:
sound.set_volume(SOUND_VOLUME)
speak('READY')
button.wait_for_bump()
speak('START')
try:
work()
except KeyboardInterrupt as e:
stop()
raise e
if __name__ == '__main__':
main()