-
Notifications
You must be signed in to change notification settings - Fork 0
/
3. Reacting to Lines.py
66 lines (53 loc) · 2.4 KB
/
3. Reacting to Lines.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
from hub import button, light_matrix, port, sound, motion_sensor
import runloop, motor_pair, motor, distance_sensor, color_sensor, color
DEFAULT_VELOCITY = 360
DEGREES_PER_CM = 360/17.5
async def main_left():
await runloop.until(lambda: button.pressed(button.LEFT))
motor_pair.unpair(motor_pair.PAIR_1)
motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)
DEFAULT_VELOCITY= 10 * 30
motor_pair.move(motor_pair.PAIR_1, 0, velocity=DEFAULT_VELOCITY)
await runloop.until(lambda: color_sensor.color(port.B) == color.BLACK)
motor_pair.stop(motor_pair.PAIR_1)
async def main_right():
await runloop.until(lambda: button.pressed(button.RIGHT))
motor_pair.unpair(motor_pair.PAIR_1)
motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)
DEFAULT_VELOCITY= 10 * 30
while True:
if (color_sensor.color(port.B) == color.BLACK):
motor_pair.move(motor_pair.PAIR_1, 50, velocity=DEFAULT_VELOCITY)
else:
motor_pair.move(motor_pair.PAIR_1, -50, velocity=DEFAULT_VELOCITY)
async def solution_right():
await runloop.until(lambda: button.pressed(button.RIGHT))
motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)
power = 30
while True:
if (color_sensor.reflection(port.B) < 50):
motor_pair.move_tank(motor_pair.PAIR_1, 50, power * 10)
else:
motor_pair.move_tank(motor_pair.PAIR_1, power * 10, 50)
desired_power = 25
# use propertional line following
async def propertional_line_following():
motor_pair.pair(motor_pair.PAIR_1, port.C, port.D)
gain = 0.3 # 0.20
average_white_black = 55
while True:
#set left power to desired_power+(gain * average_white_black-reflection)
#set right power to desired_power-(gain * average_white_black-reflection)
delta = gain * (average_white_black - color_sensor.reflection(port.A))
motor_pair.move_tank(motor_pair.PAIR_1, int(10*(desired_power + delta)), int(10*(desired_power - delta)))
async def other_left():
await runloop.until(lambda: button.pressed(button.RIGHT))
global desired_power
desired_power =- 10
async def other_right():
await runloop.until(lambda: button.pressed(button.RIGHT))
global desired_power
desired_power =+ 10
#runloop.run(main_left(), main_right())
#runloop.run(solution_right())
runloop.run(propertional_line_following(), other_left(), other_right())