Skip to content

Alvik lifted/dropped events #41

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

Merged
merged 8 commits into from
Mar 19, 2025
48 changes: 46 additions & 2 deletions arduino_alvik/arduino_alvik.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def set_wheels_position(self, left_angle: float, right_angle: float, unit: str =
Sets left/right motor angle
:param left_angle:
:param right_angle:
:param unit: the speed unit of measurement (default: 'rpm')
:param unit: the speed unit of measurement (default: 'deg')
:param blocking:
:return:
"""
Expand Down Expand Up @@ -1259,6 +1259,24 @@ def on_shake(self, callback: callable, args: tuple = ()) -> None:
"""
self._move_events.register_callback('on_shake', callback, args)

def on_lift(self, callback: callable, args: tuple = ()) -> None:
"""
Register callback when Alvik is lifted
:param callback:
:param args:
:return:
"""
self._move_events.register_callback('on_lift', callback, args)

def on_drop(self, callback: callable, args: tuple = ()) -> None:
"""
Register callback when Alvik is dropped
:param callback:
:param args:
:return:
"""
self._move_events.register_callback('on_drop', callback, args)

def on_x_tilt(self, callback: callable, args: tuple = ()) -> None:
"""
Register callback when Alvik is tilted on X-axis
Expand Down Expand Up @@ -1965,7 +1983,7 @@ class _ArduinoAlvikMoveEvents(_ArduinoAlvikEvents):
Event class to handle move events
"""

available_events = ['on_shake', 'on_x_tilt', 'on_y_tilt', 'on_z_tilt',
available_events = ['on_shake', 'on_lift', 'on_drop', 'on_x_tilt', 'on_y_tilt', 'on_z_tilt',
'on_nx_tilt', 'on_ny_tilt', 'on_nz_tilt']

NZ_TILT = 0x80
Expand All @@ -1992,6 +2010,26 @@ def _is_shaken(current_state, new_state) -> bool:
"""
return not bool(current_state & 0b00000001) and bool(new_state & 0b00000001)

@staticmethod
def _is_lifted(current_state, new_state) -> bool:
"""
True if Alvik was lifted
:param current_state:
:param new_state:
:return:
"""
return not bool(current_state & 0b00000010) and bool(new_state & 0b00000010)

@staticmethod
def _is_dropped(current_state, new_state) -> bool:
"""
True if Alvik was dropped
:param current_state:
:param new_state:
:return:
"""
return bool(current_state & 0b00000010) and not bool(new_state & 0b00000010)

@staticmethod
def _is_x_tilted(current_state, new_state) -> bool:
"""
Expand Down Expand Up @@ -2065,6 +2103,12 @@ def update_state(self, state: int | None):
if self._is_shaken(self._current_state, state):
self.execute_callback('on_shake')

if self._is_lifted(self._current_state, state):
self.execute_callback('on_lift')

if self._is_dropped(self._current_state, state):
self.execute_callback('on_drop')

if self._is_x_tilted(self._current_state, state):
self.execute_callback('on_x_tilt')

Expand Down
44 changes: 44 additions & 0 deletions examples/actuators/hot_wheels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from arduino_alvik import ArduinoAlvik
from time import sleep_ms


def stop_when_up(alvik):
print("lift")
alvik.set_wheels_speed(0, 0)


def run_when_down(alvik):
print("drop")
alvik.set_wheels_speed(20, 20)


alvik = ArduinoAlvik()
alvik.on_lift(stop_when_up, (alvik,))
alvik.on_drop(run_when_down, (alvik,))
alvik.begin()
color_val = 0


def blinking_leds(val):
alvik.left_led.set_color(val & 0x01, val & 0x02, val & 0x04)
alvik.right_led.set_color(val & 0x02, val & 0x04, val & 0x01)


while not alvik.get_touch_ok():
sleep_ms(100)

alvik.set_wheels_speed(20, 20)

while not alvik.get_touch_cancel():

try:
blinking_leds(color_val)
color_val = (color_val + 1) % 7
sleep_ms(500)

except KeyboardInterrupt as e:
print('over')
alvik.stop()
break

alvik.stop()
2 changes: 2 additions & 0 deletions examples/events/motion_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def simple_print(custom_text: str = '') -> None:

alvik = ArduinoAlvik()
alvik.on_shake(toggle_left_led, ("ALVIK WAS SHAKEN... YOU MAKE ME SHIVER :)", toggle_value(), ))
alvik.on_lift(simple_print, ("ALVIK WAS LIFTED",))
alvik.on_drop(simple_print, ("ALVIK WAS DROPPED",))
alvik.on_x_tilt(simple_print, ("TILTED ON X",))
alvik.on_nx_tilt(simple_print, ("TILTED ON -X",))
alvik.on_y_tilt(simple_print, ("TILTED ON Y",))
Expand Down