Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples cleanup #37

Merged
merged 14 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/actuators/leds_setting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()
Expand Down Expand Up @@ -34,4 +34,4 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
break
4 changes: 2 additions & 2 deletions examples/actuators/move_wheels.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()
Expand Down Expand Up @@ -33,4 +33,4 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
break
126 changes: 62 additions & 64 deletions examples/actuators/pose_example.py
Original file line number Diff line number Diff line change
@@ -1,82 +1,80 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()

while True:
try:
try:

alvik.move(100.0, 'mm')
print("on target after move")

alvik.move(50.0, 'mm')
print("on target after move")

alvik.rotate(90.0, 'deg')
print("on target after rotation")

alvik.move(100.0, 'mm')
print("on target after move")
alvik.rotate(-45.00, 'deg')
print("on target after rotation")

alvik.move(50.0, 'mm')
print("on target after move")
x, y, theta = alvik.get_pose()
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')

alvik.rotate(90.0, 'deg')
print("on target after rotation")
alvik.reset_pose(0, 0, 0)

alvik.rotate(-45.00, 'deg')
print("on target after rotation")
x, y, theta = alvik.get_pose()
print(f'Updated pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
sleep_ms(500)

x, y, theta = alvik.get_pose()
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
print("___________NON-BLOCKING__________________")

alvik.reset_pose(0, 0, 0)
alvik.move(50.0, 'mm', blocking=False)

x, y, theta = alvik.get_pose()
print(f'Updated pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after move")

alvik.rotate(45.0, 'deg', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after rotation")

print("___________NON-BLOCKING__________________")

alvik.move(50.0, 'mm', blocking=False)

while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after move")

alvik.rotate(45.0, 'deg', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after rotation")

alvik.move(100.0, 'mm', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after move")

alvik.rotate(-90.00, 'deg', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after rotation")

x, y, theta = alvik.get_pose()
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')

alvik.reset_pose(0, 0, 0)

x, y, theta = alvik.get_pose()
print(f'Updated pose is x={x}, y={y}, theta(deg)={theta}')
alvik.move(100.0, 'mm', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after move")

alvik.rotate(-90.00, 'deg', blocking=False)
while not alvik.is_target_reached():
alvik.left_led.set_color(1, 0, 0)
sleep_ms(500)
alvik.left_led.set_color(0, 0, 0)
sleep_ms(500)
print("on target after rotation")

x, y, theta = alvik.get_pose()
print(f'Current pose is x(cm)={x}, y(cm)={y}, theta(deg)={theta}')

alvik.reset_pose(0, 0, 0)

x, y, theta = alvik.get_pose()
print(f'Updated pose is x={x}, y={y}, theta(deg)={theta}')
sleep_ms(500)

alvik.stop()
sys.exit()
except KeyboardInterrupt as e:
print('Test interrupted')

except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
finally:
alvik.stop()
print("END of pose example")
4 changes: 2 additions & 2 deletions examples/actuators/set_servo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()
Expand All @@ -19,4 +19,4 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
break
4 changes: 2 additions & 2 deletions examples/actuators/wheels_position.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from arduino_alvik import ArduinoAlvik
from time import sleep, sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()
Expand Down Expand Up @@ -62,4 +62,4 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
break
4 changes: 2 additions & 2 deletions examples/actuators/wheels_speed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


alvik = ArduinoAlvik()
alvik.begin()
Expand All @@ -21,4 +21,4 @@
except KeyboardInterrupt as e:
print('over')
alvik.stop()
sys.exit()
break
28 changes: 28 additions & 0 deletions examples/arduino-runtime/blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from time import sleep_ms

from arduino import start
from arduino_alvik import ArduinoAlvik


alvik = ArduinoAlvik()


def setup():
alvik.begin()


def loop():
print('blinking LEDs')
alvik.left_led.set_color(0, 0, 1)
alvik.right_led.set_color(0, 0, 1)
sleep_ms(500)
alvik.left_led.set_color(1, 0, 0)
alvik.right_led.set_color(1, 0, 0)
sleep_ms(500)


def cleanup():
alvik.stop()


start(setup=setup, loop=loop, cleanup=cleanup)
64 changes: 64 additions & 0 deletions examples/arduino-runtime/line_follower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from time import sleep_ms

from arduino import start
from arduino_alvik import ArduinoAlvik


alvik = ArduinoAlvik()


def calculate_center(left: int, center: int, right: int):
centroid = 0
sum_weight = left + center + right
sum_values = left + 2 * center + 3 * right
if sum_weight != 0:
centroid = sum_values / sum_weight
centroid = 2 - centroid
return centroid


def run_line_follower(alvik):

kp = 50.0
line_sensors = alvik.get_line_sensors()
print(f' {line_sensors}')

error = calculate_center(*line_sensors)
control = error * kp

if control > 0.2:
alvik.left_led.set_color(1, 0, 0)
alvik.right_led.set_color(0, 0, 0)
elif control < -0.2:
alvik.left_led.set_color(1, 0, 0)
alvik.right_led.set_color(0, 0, 0)
else:
alvik.left_led.set_color(0, 1, 0)
alvik.right_led.set_color(0, 1, 0)

alvik.set_wheels_speed(30 - control, 30 + control)
sleep_ms(100)


def setup():
alvik.begin()
alvik.left_led.set_color(0, 0, 1)
alvik.right_led.set_color(0, 0, 1)


def loop():
while not alvik.get_touch_ok():
alvik.left_led.set_color(0, 0, 1)
alvik.right_led.set_color(0, 0, 1)
alvik.brake()
sleep_ms(100)

while not alvik.get_touch_cancel():
run_line_follower(alvik)


def cleanup():
alvik.stop()


start(setup=setup, loop=loop, cleanup=cleanup)
26 changes: 26 additions & 0 deletions examples/arduino-runtime/read_tof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from time import sleep_ms

from arduino import start
from arduino_alvik import ArduinoAlvik


alvik = ArduinoAlvik()


def setup():
alvik.begin()


def loop():
L, CL, C, CR, R = alvik.get_distance()
T = alvik.get_distance_top()
B = alvik.get_distance_bottom()
print(f'T: {T} | B: {B} | L: {L} | CL: {CL} | C: {C} | CR: {CR} | R: {R}')
sleep_ms(100)


def cleanup():
alvik.stop()


start(setup=setup, loop=loop, cleanup=cleanup)
4 changes: 2 additions & 2 deletions examples/communication/i2c_scan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from time import sleep_ms
import sys


from arduino_alvik import ArduinoAlvik

Expand All @@ -22,4 +22,4 @@
sleep_ms(100)
except KeyboardInterrupt as e:
alvik.stop()
sys.exit()
break
9 changes: 5 additions & 4 deletions examples/communication/modulino.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms
import sys


try:
from modulino import ModulinoPixels
except ImportError as e:
print("ImportError: ModulinoPixels not installed")
sys.exit(-1)
print("\nImportError: ModulinoPixels not installed")
raise e

alvik = ArduinoAlvik()
alvik.begin()
Expand All @@ -32,4 +33,4 @@

except KeyboardInterrupt as e:
alvik.stop()
sys.exit()
break
Loading