Skip to content

Commit bb546c0

Browse files
authored
Merge pull request #91 from Mrkun5018/main
Add set_led_mode api & Fix stop and plan_left interface bug
2 parents 981c6ad + 978d36a commit bb546c0

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

pymycobot/myagv.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ProtocolCode(enum.Enum):
1212
HEADER = 0xFE
1313
RESTORE = [0x01, 0x00]
1414
SET_LED = [0x01, 0x02]
15+
SET_LED_MODE = [0x01, 0x0A]
1516
GET_FIRMWARE_VERSION = [0x01, 0x03]
1617
GET_MOTORS_CURRENT = [0x01, 0x04]
1718
GET_BATTERY_INFO = [0x01, 0x05]
@@ -123,7 +124,6 @@ def _mesg(self, genre, *args, **kwargs):
123124
elif genre == ProtocolCode.GET_BATTERY_INFO.value:
124125
command.append(6)
125126
else:
126-
# del command[2]
127127
command.append(sum(command[2:]) & 0xff)
128128
self._write(command)
129129
if has_reply:
@@ -162,6 +162,16 @@ def set_led(self, mode, R, G, B):
162162
calibration_parameters(class_name=self.__class__.__name__, rgb=[R, G, B], led_mode=mode)
163163
return self._mesg(ProtocolCode.SET_LED.value, mode, R, G, B)
164164

165+
def set_led_mode(self, mode: int):
166+
"""Set the LED light mode
167+
168+
Args:
169+
mode (int): 0 - charging mode, 1 - DIY mode
170+
"""
171+
if mode not in [0, 1]:
172+
raise ValueError("mode must be 0 or 1")
173+
return self._mesg(ProtocolCode.SET_LED_MODE.value, mode)
174+
165175
def get_firmware_version(self):
166176
"""Get firmware version number
167177
"""
@@ -195,9 +205,11 @@ def get_battery_info(self):
195205
def __basic_move_control(self, *genre, timeout: int = 5):
196206
t = time.time()
197207
self.__movement = True
198-
while time.time() - t < timeout and self.__movement is True:
208+
while time.time() - t < timeout:
209+
if self.__movement is False:
210+
break
199211
self._mesg(*genre)
200-
time.sleep(0.05)
212+
time.sleep(0.1)
201213
self.stop()
202214

203215
def go_ahead(self, speed: int, timeout: int = 5):
@@ -238,7 +250,7 @@ def pan_left(self, speed, timeout=5):
238250
"""
239251
if not (0 < speed < 128):
240252
raise ValueError("pan_left_speed must be between 1 and 127")
241-
self.__basic_move_control(128, 128, 128 + speed, timeout=timeout)
253+
self.__basic_move_control(128, 128 + speed, 128, timeout=timeout)
242254

243255
def pan_right(self, speed: int, timeout=5):
244256
"""
@@ -280,9 +292,8 @@ def counterclockwise_rotation(self, speed: int, timeout=5):
280292

281293
def stop(self):
282294
"""stop-motion"""
283-
if self.__movement is True:
284-
self._mesg(128, 128, 128)
285-
self.__movement = False
295+
self.__movement = False
296+
self._mesg(128, 128, 128)
286297

287298
def get_mcu_info(self, version: float = None) -> list:
288299
"""
@@ -386,12 +397,3 @@ def get_gyro_state(self):
386397

387398
def get_modified_version(self):
388399
return self._mesg(ProtocolCode.GET_MODIFIED_VERSION.value, has_reply=True)
389-
390-
# def get_battery_voltage(self, num=1):
391-
# """Get battery voltage
392-
393-
# Args:
394-
# num (int, optional): Battery ID number. Defaults to 1.
395-
# """
396-
# mcu_data = self.get_mcu_info()
397-
# return self._mesg(ProtocolCode.GET_BATTERY_INFO.value, has_reply = True)

0 commit comments

Comments
 (0)