@@ -22,6 +22,8 @@ class ArduinoAlvik:
22
22
_events_thread_running = False
23
23
_events_thread_id = None
24
24
25
+ _ERROR_VAL = 999
26
+
25
27
def __new__ (cls ):
26
28
if not hasattr (cls , '_instance' ):
27
29
cls ._instance = super (ArduinoAlvik , cls ).__new__ (cls )
@@ -42,6 +44,7 @@ def __init__(self):
42
44
rgb_mask = [0b00100000 , 0b01000000 , 0b10000000 ])
43
45
self ._battery_perc = None
44
46
self ._battery_is_charging = None
47
+ self ._battery_error = False
45
48
self ._touch_byte = None
46
49
self ._move_byte = None
47
50
self ._behaviour = None
@@ -432,7 +435,7 @@ def set_wheels_position(self, left_angle: float, right_angle: float, unit: str =
432
435
Sets left/right motor angle
433
436
:param left_angle:
434
437
:param right_angle:
435
- :param unit: the speed unit of measurement (default: 'rpm ')
438
+ :param unit: the speed unit of measurement (default: 'deg ')
436
439
:param blocking:
437
440
:return:
438
441
"""
@@ -688,6 +691,8 @@ def _parse_message(self) -> int:
688
691
_ , battery_perc = self ._packeter .unpacketC1F ()
689
692
self ._battery_is_charging = battery_perc > 0
690
693
self ._battery_perc = abs (battery_perc )
694
+ if self ._battery_perc >= ArduinoAlvik ._ERROR_VAL :
695
+ self ._battery_error = True
691
696
elif code == ord ('d' ):
692
697
# distance sensor
693
698
_ , self ._left_tof , self ._center_tof , self ._right_tof = self ._packeter .unpacketC3I ()
@@ -736,6 +741,11 @@ def get_battery_charge(self) -> int | None:
736
741
Returns the battery SOC
737
742
:return:
738
743
"""
744
+
745
+ if self ._battery_error :
746
+ print ("BATTERY ERROR" )
747
+ return None
748
+
739
749
if self ._battery_perc is None :
740
750
return None
741
751
if self ._battery_perc > 100 :
@@ -1259,6 +1269,24 @@ def on_shake(self, callback: callable, args: tuple = ()) -> None:
1259
1269
"""
1260
1270
self ._move_events .register_callback ('on_shake' , callback , args )
1261
1271
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
+
1262
1290
def on_x_tilt (self , callback : callable , args : tuple = ()) -> None :
1263
1291
"""
1264
1292
Register callback when Alvik is tilted on X-axis
@@ -1965,7 +1993,7 @@ class _ArduinoAlvikMoveEvents(_ArduinoAlvikEvents):
1965
1993
Event class to handle move events
1966
1994
"""
1967
1995
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' ,
1969
1997
'on_nx_tilt' , 'on_ny_tilt' , 'on_nz_tilt' ]
1970
1998
1971
1999
NZ_TILT = 0x80
@@ -1992,6 +2020,26 @@ def _is_shaken(current_state, new_state) -> bool:
1992
2020
"""
1993
2021
return not bool (current_state & 0b00000001 ) and bool (new_state & 0b00000001 )
1994
2022
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
+
1995
2043
@staticmethod
1996
2044
def _is_x_tilted (current_state , new_state ) -> bool :
1997
2045
"""
@@ -2065,6 +2113,12 @@ def update_state(self, state: int | None):
2065
2113
if self ._is_shaken (self ._current_state , state ):
2066
2114
self .execute_callback ('on_shake' )
2067
2115
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
+
2068
2122
if self ._is_x_tilted (self ._current_state , state ):
2069
2123
self .execute_callback ('on_x_tilt' )
2070
2124
0 commit comments