-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_trying_to_be_clever.py
executable file
·144 lines (104 loc) · 3.03 KB
/
pid_trying_to_be_clever.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
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3
from ev3dev2.sensor.lego import TouchSensor, ColorSensor
from ev3dev2.sound import Sound
from typing import Tuple
##################
# #
# SETTINGS #
# #
##################
FORWARD_LINE_SPEED = 40
FORWARD_SPEED = 25
CONSTANT_P = 5.15
CONSTANT_I = 1.51
CONSTANT_D = 4.2
HISTORY_LOSS = 0.5
AMPLIFIER = 0.05
###################
# #
# CONSTANTS #
# #
###################
SOUND_VOLUME = 100
LEFT = 0
RIGHT = 1
#####################
# #
# PERIPHERALS #
# #
#####################
sound = Sound()
button = TouchSensor(INPUT_3)
left_motor = LargeMotor(OUTPUT_A)
right_motor = LargeMotor(OUTPUT_B)
motors = [left_motor, right_motor]
left_sensor = ColorSensor(INPUT_1)
right_sensor = ColorSensor(INPUT_2)
sensors = [left_sensor, right_sensor]
######################
# #
# MAIN PROGRAM #
# #
######################
def speak(message: str) -> None:
sound.speak(message)
print(message)
def work() -> None:
integral = (0.0, 0.0)
last_error = 0
expected = configure()
while True:
if button.is_pressed:
handle_button_pressed()
expected = configure()
else:
integral, last_error = iterate(integral, last_error, expected)
def configure() -> Tuple[int, int]:
return left_sensor.reflected_light_intensity, right_sensor.reflected_light_intensity
def handle_button_pressed() -> None:
stop()
speak('STOP')
button.wait_for_released()
button.wait_for_bump()
speak('START')
def iterate(integral: Tuple[float, float], last_error: int, expected: Tuple[int, int]) -> Tuple[Tuple[float, float], int]:
if left_sensor.color == ColorSensor.COLOR_WHITE and right_sensor.color == ColorSensor.COLOR_WHITE:
forward_speed = FORWARD_LINE_SPEED
else:
forward_speed = FORWARD_SPEED
error = (
left_sensor.reflected_light_intensity - expected[LEFT],
right_sensor.reflected_light_intensity - expected[RIGHT]
)
integral = (
HISTORY_LOSS * integral[LEFT] + error[LEFT],
HISTORY_LOSS * integral[RIGHT] + error[RIGHT]
)
temp_error = error[LEFT] - error[RIGHT]
derivative = temp_error - last_error
last_error = temp_error
turn_speed = (
CONSTANT_P * temp_error
+ CONSTANT_I * (integral[LEFT] - integral[RIGHT])
+ CONSTANT_D * derivative
)
left_motor.on(forward_speed + AMPLIFIER * turn_speed)
right_motor.on(forward_speed - AMPLIFIER * turn_speed)
return integral, last_error
def stop() -> None:
for motor in motors:
motor.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()