Skip to content

Commit

Permalink
Merge pull request #91 from Mrkun5018/main
Browse files Browse the repository at this point in the history
Add set_led_mode api & Fix stop and plan_left interface bug
  • Loading branch information
anla-xu authored Oct 17, 2024
2 parents 981c6ad + 978d36a commit bb546c0
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions pymycobot/myagv.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ProtocolCode(enum.Enum):
HEADER = 0xFE
RESTORE = [0x01, 0x00]
SET_LED = [0x01, 0x02]
SET_LED_MODE = [0x01, 0x0A]
GET_FIRMWARE_VERSION = [0x01, 0x03]
GET_MOTORS_CURRENT = [0x01, 0x04]
GET_BATTERY_INFO = [0x01, 0x05]
Expand Down Expand Up @@ -123,7 +124,6 @@ def _mesg(self, genre, *args, **kwargs):
elif genre == ProtocolCode.GET_BATTERY_INFO.value:
command.append(6)
else:
# del command[2]
command.append(sum(command[2:]) & 0xff)
self._write(command)
if has_reply:
Expand Down Expand Up @@ -162,6 +162,16 @@ def set_led(self, mode, R, G, B):
calibration_parameters(class_name=self.__class__.__name__, rgb=[R, G, B], led_mode=mode)
return self._mesg(ProtocolCode.SET_LED.value, mode, R, G, B)

def set_led_mode(self, mode: int):
"""Set the LED light mode
Args:
mode (int): 0 - charging mode, 1 - DIY mode
"""
if mode not in [0, 1]:
raise ValueError("mode must be 0 or 1")
return self._mesg(ProtocolCode.SET_LED_MODE.value, mode)

def get_firmware_version(self):
"""Get firmware version number
"""
Expand Down Expand Up @@ -195,9 +205,11 @@ def get_battery_info(self):
def __basic_move_control(self, *genre, timeout: int = 5):
t = time.time()
self.__movement = True
while time.time() - t < timeout and self.__movement is True:
while time.time() - t < timeout:
if self.__movement is False:
break
self._mesg(*genre)
time.sleep(0.05)
time.sleep(0.1)
self.stop()

def go_ahead(self, speed: int, timeout: int = 5):
Expand Down Expand Up @@ -238,7 +250,7 @@ def pan_left(self, speed, timeout=5):
"""
if not (0 < speed < 128):
raise ValueError("pan_left_speed must be between 1 and 127")
self.__basic_move_control(128, 128, 128 + speed, timeout=timeout)
self.__basic_move_control(128, 128 + speed, 128, timeout=timeout)

def pan_right(self, speed: int, timeout=5):
"""
Expand Down Expand Up @@ -280,9 +292,8 @@ def counterclockwise_rotation(self, speed: int, timeout=5):

def stop(self):
"""stop-motion"""
if self.__movement is True:
self._mesg(128, 128, 128)
self.__movement = False
self.__movement = False
self._mesg(128, 128, 128)

def get_mcu_info(self, version: float = None) -> list:
"""
Expand Down Expand Up @@ -386,12 +397,3 @@ def get_gyro_state(self):

def get_modified_version(self):
return self._mesg(ProtocolCode.GET_MODIFIED_VERSION.value, has_reply=True)

# def get_battery_voltage(self, num=1):
# """Get battery voltage

# Args:
# num (int, optional): Battery ID number. Defaults to 1.
# """
# mcu_data = self.get_mcu_info()
# return self._mesg(ProtocolCode.GET_BATTERY_INFO.value, has_reply = True)

0 comments on commit bb546c0

Please sign in to comment.