-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_discrete_forward_speed.py
executable file
·129 lines (90 loc) · 2.46 KB
/
pid_discrete_forward_speed.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
#!/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_SPEED = 30
MAX_FORWARD_SPEED = 50
CONSTANT_P = 8.0
CONSTANT_I = 0.45
CONSTANT_D = 0.003
HISTORY_LOSS = 0.5
AMPLIFIER = 0.10
###################
# #
# 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
last_error = 0
while True:
if button.is_pressed:
handle_button_pressed()
else:
integral, last_error = iterate(integral, last_error)
def handle_button_pressed() -> None:
stop()
speak('STOP')
button.wait_for_released()
button.wait_for_bump()
speak('START')
def iterate(integral: float, last_error: int) -> Tuple[float, int]:
error = left_sensor.reflected_light_intensity - \
right_sensor.reflected_light_intensity
integral = HISTORY_LOSS * integral + error
derivative = error - last_error
last_error = error
turn_speed = CONSTANT_P * error + CONSTANT_I * integral + CONSTANT_D * derivative
if abs(turn_speed) > 80:
forward_speed = FORWARD_SPEED
else:
forward_speed = MAX_FORWARD_SPEED
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()