Skip to content

Commit 49bcd32

Browse files
committed
examples: line_follower.py and minor mods
1 parent a60fc67 commit 49bcd32

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

examples/line_follower.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from arduino_alvik import ArduinoAlvik
2+
from time import sleep_ms
3+
import sys
4+
5+
6+
def calculate_center(left: int, center: int, right: int):
7+
centroid = 0
8+
sum_weight = left + center + right
9+
sum_values = left + 2*center + 3*right
10+
if sum_weight != 0:
11+
centroid = sum_values/sum_weight
12+
centroid = 2 - centroid
13+
return centroid
14+
15+
16+
alvik = ArduinoAlvik()
17+
alvik.begin()
18+
19+
error = 0
20+
control = 0
21+
kp = 50.0
22+
23+
alvik.left_led.set_color(0, 0, 1)
24+
alvik.right_led.set_color(0, 0, 1)
25+
26+
while not alvik.get_touch_up():
27+
sleep_ms(50)
28+
29+
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)
56+
57+
except KeyboardInterrupt as e:
58+
print('over')
59+
alvik.stop()
60+
sys.exit()

examples/message_reader.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import sys
44

55
alvik = ArduinoAlvik()
6-
if alvik.begin() < 0:
7-
sys.exit()
6+
alvik.begin()
87

98
speed = 0
109

File renamed without changes.

0 commit comments

Comments
 (0)