-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_pid.py
executable file
·151 lines (116 loc) · 3.14 KB
/
naive_pid.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
145
146
147
148
149
150
151
#!/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 List, Tuple
##################
# #
# SETTINGS #
# #
##################
CONSTANT_P = 4.15
CONSTANT_I = 2.01
CONSTANT_D = 4.2
HISTORY_LOSS = 0.5
AMPLIFIER = 2
###################
# #
# 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:
last_error = [0, 0]
integral = [0.0, 0.0]
while True:
if button.is_pressed:
handle_button_pressed()
else:
last_error, integral = iterate(last_error, integral)
def handle_button_pressed() -> None:
stop()
speak('STOP')
button.wait_for_released()
button.wait_for_bump()
speak('START')
def iterate(last_error: List[int], integral: List[float]) -> Tuple[List[int], List[float]]:
color = [left_sensor.color, right_sensor.color]
error = [color[LEFT] - ColorSensor.COLOR_WHITE,
color[RIGHT] - ColorSensor.COLOR_WHITE]
if color[LEFT] == ColorSensor.COLOR_WHITE and color[RIGHT] == ColorSensor.COLOR_WHITE:
base_speed = 100
else:
base_speed = 25
integral = [
HISTORY_LOSS * integral[LEFT] + error[LEFT],
HISTORY_LOSS * integral[RIGHT] + error[RIGHT]
]
derivative = [
error[LEFT] - last_error[LEFT],
error[RIGHT] - last_error[RIGHT]
]
adjustment = [
CONSTANT_P * error[LEFT]
+ CONSTANT_I * integral[LEFT]
+ CONSTANT_D * derivative[LEFT],
CONSTANT_P * error[RIGHT]
+ CONSTANT_I * integral[RIGHT]
+ CONSTANT_D * derivative[RIGHT]
]
left_motor.run_forever(
speed_sp=round(
base_speed
- AMPLIFIER * adjustment[RIGHT]
+ AMPLIFIER * adjustment[LEFT]
),
stop_action='coast'
)
right_motor.run_forever(
speed_sp=round(
base_speed
- AMPLIFIER * adjustment[LEFT]
+ AMPLIFIER * adjustment[RIGHT]
),
stop_action='coast'
)
last_error = error
return last_error, integral
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()