Skip to content

Commit a7b258a

Browse files
committed
feat: runtime examples line_follower
1 parent c9376b9 commit a7b258a

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

examples/arduino-runtime/blink.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setup():
1212

1313

1414
def loop():
15-
print('loopy loop')
15+
print('blinking LEDs')
1616
alvik.left_led.set_color(0, 0, 1)
1717
alvik.right_led.set_color(0, 0, 1)
1818
sleep_ms(500)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from time import sleep_ms
2+
3+
from arduino import start
4+
from arduino_alvik import ArduinoAlvik
5+
6+
7+
alvik = ArduinoAlvik()
8+
9+
10+
def calculate_center(left: int, center: int, right: int):
11+
centroid = 0
12+
sum_weight = left + center + right
13+
sum_values = left + 2 * center + 3 * right
14+
if sum_weight != 0:
15+
centroid = sum_values / sum_weight
16+
centroid = 2 - centroid
17+
return centroid
18+
19+
20+
def run_line_follower(alvik):
21+
22+
kp = 50.0
23+
line_sensors = alvik.get_line_sensors()
24+
print(f' {line_sensors}')
25+
26+
error = calculate_center(*line_sensors)
27+
control = error * kp
28+
29+
if control > 0.2:
30+
alvik.left_led.set_color(1, 0, 0)
31+
alvik.right_led.set_color(0, 0, 0)
32+
elif control < -0.2:
33+
alvik.left_led.set_color(1, 0, 0)
34+
alvik.right_led.set_color(0, 0, 0)
35+
else:
36+
alvik.left_led.set_color(0, 1, 0)
37+
alvik.right_led.set_color(0, 1, 0)
38+
39+
alvik.set_wheels_speed(30 - control, 30 + control)
40+
sleep_ms(100)
41+
42+
43+
def setup():
44+
alvik.begin()
45+
alvik.left_led.set_color(0, 0, 1)
46+
alvik.right_led.set_color(0, 0, 1)
47+
48+
49+
def loop():
50+
while not alvik.get_touch_cancel():
51+
run_line_follower(alvik)
52+
53+
while not alvik.get_touch_ok():
54+
alvik.left_led.set_color(0, 0, 1)
55+
alvik.right_led.set_color(0, 0, 1)
56+
alvik.brake()
57+
sleep_ms(100)
58+
59+
60+
start(setup, loop)

0 commit comments

Comments
 (0)