Skip to content

Commit 9ba9745

Browse files
committed
feat: alvik events on_lift on_drop with examples
1 parent 524b360 commit 9ba9745

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

arduino_alvik/arduino_alvik.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ArduinoAlvik:
2222
_events_thread_running = False
2323
_events_thread_id = None
2424

25+
_ERROR_VAL = 999
26+
2527
def __new__(cls):
2628
if not hasattr(cls, '_instance'):
2729
cls._instance = super(ArduinoAlvik, cls).__new__(cls)
@@ -42,6 +44,7 @@ def __init__(self):
4244
rgb_mask=[0b00100000, 0b01000000, 0b10000000])
4345
self._battery_perc = None
4446
self._battery_is_charging = None
47+
self._battery_error = False
4548
self._touch_byte = None
4649
self._move_byte = None
4750
self._behaviour = None
@@ -432,7 +435,7 @@ def set_wheels_position(self, left_angle: float, right_angle: float, unit: str =
432435
Sets left/right motor angle
433436
:param left_angle:
434437
:param right_angle:
435-
:param unit: the speed unit of measurement (default: 'rpm')
438+
:param unit: the speed unit of measurement (default: 'deg')
436439
:param blocking:
437440
:return:
438441
"""
@@ -688,6 +691,8 @@ def _parse_message(self) -> int:
688691
_, battery_perc = self._packeter.unpacketC1F()
689692
self._battery_is_charging = battery_perc > 0
690693
self._battery_perc = abs(battery_perc)
694+
if self._battery_perc >= ArduinoAlvik._ERROR_VAL:
695+
self._battery_error = True
691696
elif code == ord('d'):
692697
# distance sensor
693698
_, self._left_tof, self._center_tof, self._right_tof = self._packeter.unpacketC3I()
@@ -736,6 +741,11 @@ def get_battery_charge(self) -> int | None:
736741
Returns the battery SOC
737742
:return:
738743
"""
744+
745+
if self._battery_error:
746+
print("BATTERY ERROR")
747+
return None
748+
739749
if self._battery_perc is None:
740750
return None
741751
if self._battery_perc > 100:
@@ -1259,6 +1269,24 @@ def on_shake(self, callback: callable, args: tuple = ()) -> None:
12591269
"""
12601270
self._move_events.register_callback('on_shake', callback, args)
12611271

1272+
def on_lift(self, callback: callable, args: tuple = ()) -> None:
1273+
"""
1274+
Register callback when Alvik is lifted
1275+
:param callback:
1276+
:param args:
1277+
:return:
1278+
"""
1279+
self._move_events.register_callback('on_lift', callback, args)
1280+
1281+
def on_drop(self, callback: callable, args: tuple = ()) -> None:
1282+
"""
1283+
Register callback when Alvik is dropped
1284+
:param callback:
1285+
:param args:
1286+
:return:
1287+
"""
1288+
self._move_events.register_callback('on_drop', callback, args)
1289+
12621290
def on_x_tilt(self, callback: callable, args: tuple = ()) -> None:
12631291
"""
12641292
Register callback when Alvik is tilted on X-axis
@@ -1965,7 +1993,7 @@ class _ArduinoAlvikMoveEvents(_ArduinoAlvikEvents):
19651993
Event class to handle move events
19661994
"""
19671995

1968-
available_events = ['on_shake', 'on_x_tilt', 'on_y_tilt', 'on_z_tilt',
1996+
available_events = ['on_shake', 'on_lift', 'on_drop', 'on_x_tilt', 'on_y_tilt', 'on_z_tilt',
19691997
'on_nx_tilt', 'on_ny_tilt', 'on_nz_tilt']
19701998

19711999
NZ_TILT = 0x80
@@ -1992,6 +2020,26 @@ def _is_shaken(current_state, new_state) -> bool:
19922020
"""
19932021
return not bool(current_state & 0b00000001) and bool(new_state & 0b00000001)
19942022

2023+
@staticmethod
2024+
def _is_lifted(current_state, new_state) -> bool:
2025+
"""
2026+
True if Alvik was lifted
2027+
:param current_state:
2028+
:param new_state:
2029+
:return:
2030+
"""
2031+
return not bool(current_state & 0b00000010) and bool(new_state & 0b00000010)
2032+
2033+
@staticmethod
2034+
def _is_dropped(current_state, new_state) -> bool:
2035+
"""
2036+
True if Alvik was dropped
2037+
:param current_state:
2038+
:param new_state:
2039+
:return:
2040+
"""
2041+
return bool(current_state & 0b00000010) and not bool(new_state & 0b00000010)
2042+
19952043
@staticmethod
19962044
def _is_x_tilted(current_state, new_state) -> bool:
19972045
"""
@@ -2065,6 +2113,12 @@ def update_state(self, state: int | None):
20652113
if self._is_shaken(self._current_state, state):
20662114
self.execute_callback('on_shake')
20672115

2116+
if self._is_lifted(self._current_state, state):
2117+
self.execute_callback('on_lift')
2118+
2119+
if self._is_dropped(self._current_state, state):
2120+
self.execute_callback('on_drop')
2121+
20682122
if self._is_x_tilted(self._current_state, state):
20692123
self.execute_callback('on_x_tilt')
20702124

examples/actuators/hot_wheels.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from arduino_alvik import ArduinoAlvik
2+
from time import sleep_ms
3+
4+
5+
def stop_when_up(alvik):
6+
print("lift")
7+
alvik.set_wheels_speed(0, 0)
8+
9+
10+
def run_when_down(alvik):
11+
print("drop")
12+
alvik.set_wheels_speed(20, 20)
13+
14+
15+
alvik = ArduinoAlvik()
16+
alvik.on_lift(stop_when_up, (alvik,))
17+
alvik.on_drop(run_when_down, (alvik,))
18+
alvik.begin()
19+
color_val = 0
20+
21+
22+
def blinking_leds(val):
23+
alvik.left_led.set_color(val & 0x01, val & 0x02, val & 0x04)
24+
alvik.right_led.set_color(val & 0x02, val & 0x04, val & 0x01)
25+
26+
27+
while not alvik.get_touch_ok():
28+
sleep_ms(100)
29+
30+
alvik.set_wheels_speed(20, 20)
31+
32+
while not alvik.get_touch_cancel():
33+
34+
try:
35+
blinking_leds(color_val)
36+
color_val = (color_val + 1) % 7
37+
sleep_ms(500)
38+
39+
except KeyboardInterrupt as e:
40+
print('over')
41+
alvik.stop()
42+
break
43+
44+
alvik.stop()

examples/events/motion_events.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def simple_print(custom_text: str = '') -> None:
3131

3232
alvik = ArduinoAlvik()
3333
alvik.on_shake(toggle_left_led, ("ALVIK WAS SHAKEN... YOU MAKE ME SHIVER :)", toggle_value(), ))
34+
alvik.on_lift(simple_print, ("ALVIK WAS LIFTED",))
35+
alvik.on_drop(simple_print, ("ALVIK WAS DROPPED",))
3436
alvik.on_x_tilt(simple_print, ("TILTED ON X",))
3537
alvik.on_nx_tilt(simple_print, ("TILTED ON -X",))
3638
alvik.on_y_tilt(simple_print, ("TILTED ON Y",))

0 commit comments

Comments
 (0)