Skip to content

Commit b15c502

Browse files
committed
fix: line_follower.py
1 parent 49bcd32 commit b15c502

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

examples/line_follower.py

+30-29
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
def calculate_center(left: int, center: int, right: int):
77
centroid = 0
88
sum_weight = left + center + right
9-
sum_values = left + 2*center + 3*right
9+
sum_values = left + 2 * center + 3 * right
1010
if sum_weight != 0:
11-
centroid = sum_values/sum_weight
11+
centroid = sum_values / sum_weight
1212
centroid = 2 - centroid
1313
return centroid
1414

@@ -23,36 +23,37 @@ def calculate_center(left: int, center: int, right: int):
2323
alvik.left_led.set_color(0, 0, 1)
2424
alvik.right_led.set_color(0, 0, 1)
2525

26-
while not alvik.get_touch_up():
26+
while not alvik.get_touch_ok():
2727
sleep_ms(50)
2828

2929
try:
30-
while not alvik.get_touch_cancel():
31-
32-
line_sensors = alvik.get_line_sensors()
33-
print(f' {line_sensors}')
34-
35-
error = calculate_center(*line_sensors)
36-
control = error * kp
37-
38-
if control > 0.2:
39-
alvik.left_led.set_color(1, 0, 0)
40-
alvik.right_led.set_color(0, 0, 0)
41-
elif control < -0.2:
42-
alvik.left_led.set_color(1, 0, 0)
43-
alvik.right_led.set_color(0, 0, 0)
44-
else:
45-
alvik.left_led.set_color(0, 1, 0)
46-
alvik.right_led.set_color(0, 1, 0)
47-
48-
alvik.set_wheels_speed(30-control, 30+control)
49-
sleep_ms(100)
50-
51-
while not alvik.get_touch_ok():
52-
alvik.left_led.set_color(0, 0, 1)
53-
alvik.right_led.set_color(0, 0, 1)
54-
alvik.brake()
55-
sleep_ms(100)
30+
while True:
31+
while not alvik.get_touch_cancel():
32+
33+
line_sensors = alvik.get_line_sensors()
34+
print(f' {line_sensors}')
35+
36+
error = calculate_center(*line_sensors)
37+
control = error * kp
38+
39+
if control > 0.2:
40+
alvik.left_led.set_color(1, 0, 0)
41+
alvik.right_led.set_color(0, 0, 0)
42+
elif control < -0.2:
43+
alvik.left_led.set_color(1, 0, 0)
44+
alvik.right_led.set_color(0, 0, 0)
45+
else:
46+
alvik.left_led.set_color(0, 1, 0)
47+
alvik.right_led.set_color(0, 1, 0)
48+
49+
alvik.set_wheels_speed(30 - control, 30 + control)
50+
sleep_ms(100)
51+
52+
while not alvik.get_touch_ok():
53+
alvik.left_led.set_color(0, 0, 1)
54+
alvik.right_led.set_color(0, 0, 1)
55+
alvik.brake()
56+
sleep_ms(100)
5657

5758
except KeyboardInterrupt as e:
5859
print('over')

0 commit comments

Comments
 (0)