Skip to content

Commit 115238c

Browse files
committed
feat: add new Exception class.
1 parent e3d20ae commit 115238c

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

pymycobot/mycobot.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
sys.path.append('.')
3-
import time, serial, struct, math
3+
import time, serial, struct, math, functools
44

55

66
class Command():
@@ -48,6 +48,10 @@ class Command():
4848
SET_CLAW = 0x66
4949

5050

51+
class MyCobotDataException(Exception):
52+
pass
53+
54+
5155
class DataProcesser():
5256
# Functional approach
5357
def _encode_int8(self, data):
@@ -94,17 +98,14 @@ def _process_data(self, data, genre):
9498
return _data_list
9599

96100
elif genre in [Command.SEND_ANGLE, Command.SEND_COORD]:
97-
return self._flatten([
98-
data[0],
99-
self._flatten(self._encode_int16(data[1])),
100-
data[2]
101-
])
102-
103-
elif genre in [Command.JOG_ANGLE, Command.JOG_COORD]:
104-
return self._flatten([
105-
data[0], data[1],
106-
data[2]
107-
])
101+
return self._flatten(
102+
[data[0],
103+
self._flatten(self._encode_int16(data[1])), data[2]])
104+
105+
elif genre in [
106+
Command.JOG_ANGLE, Command.JOG_COORD, Command.SET_COLOR
107+
]:
108+
return self._flatten([data[0], data[1], data[2]])
108109

109110
else:
110111
return [data[0]]
@@ -145,6 +146,18 @@ def _process_recived(self, data, genre):
145146
def _process_bool(self, data):
146147
return data[0] if data else -1
147148

149+
@staticmethod
150+
def check_id(func):
151+
152+
@functools.wraps(func)
153+
def _wapper(*args, **kwargs):
154+
if not 0 <= args[0] <= 6:
155+
raise MyCobotDataException('id not right, should be 1 ~ 6')
156+
157+
func(args, kwargs)
158+
159+
return _wapper
160+
148161

149162
class MyCobot(DataProcesser):
150163
'''MyCobot Python API
@@ -250,8 +263,8 @@ def is_power_on(self):
250263
-1: error data
251264
252265
'''
253-
received = self.__mesg(Command.IS_POWER_ON, has_reply=True)
254-
return self._process_bool(received)
266+
return self._process_bool(
267+
self.__mesg(Command.IS_POWER_ON, has_reply=True))
255268

256269
def set_free_mode(self):
257270
# self._write('fefe0213fa')
@@ -360,7 +373,7 @@ def send_coords(self, coords, speed, mode):
360373
361374
'''
362375
if len(coords) != 6:
363-
raise Exception('The leght of coords should be 6.')
376+
raise MyCobotDataException('The leght of coords should be 6.')
364377

365378
coord_list = []
366379
for idx in range(3):
@@ -395,7 +408,7 @@ def is_in_position(self, data, id):
395408
-1: error data
396409
'''
397410
if len(data) != 6:
398-
raise Exception('The lenght of coords is not right')
411+
raise MyCobotDataException('The lenght of coords is not right')
399412

400413
if id == 1:
401414
data_list = []
@@ -406,7 +419,7 @@ def is_in_position(self, data, id):
406419
elif id == 0:
407420
data_list = [self._coord_to_int(i) for i in data]
408421
else:
409-
raise Exception("id is not right, please input 0 or 1")
422+
raise MyCobotDataException("id is not right, please input 0 or 1")
410423

411424
received = self.__mesg(Command.IS_IN_POSITION,
412425
data=data_list,
@@ -428,6 +441,7 @@ def is_moving(self):
428441
def jog_angle(self, joint_id, direction, speed):
429442
'''Joint control
430443
444+
Args:
431445
joint_id: string
432446
direction: int [0, 1]
433447
speed: int (0 - 100)
@@ -437,6 +451,7 @@ def jog_angle(self, joint_id, direction, speed):
437451
def jog_coord(self, coord_id, direction, speed):
438452
'''Coord control
439453
454+
Args:
440455
coord: string
441456
direction: int [0, 1]
442457
speed: int (0 - 100)
@@ -458,7 +473,8 @@ def set_speed(self, speed):
458473
speed (int): 0 - 100
459474
'''
460475
if not 0 <= speed <= 100:
461-
raise Exception('speed value not right, should be 0 ~ 100 ')
476+
raise MyCobotDataException(
477+
'speed value not right, should be 0 ~ 100 ')
462478

463479
self.__mesg(Command.SET_SPEED, data=[speed])
464480

@@ -491,15 +507,11 @@ def set_led_color(self, rgb):
491507
rgs (str): example 'ff0000'
492508
493509
'''
494-
# rgb = struct.pack('bbb', rgb)
495-
command = 'fefe056a{}fa'.format(rgb)
496-
if self._version == 2:
497-
command = command.decode('hex')
498-
elif self._version == 3:
499-
command = bytes.fromhex(command)
500-
self._serial_port.write(command)
501-
self._serial_port.flush()
502-
time.sleep(0.05)
510+
if len(rgb) != 6:
511+
raise MyCobotDataException('rgb format should be like: "FF0000"')
512+
513+
data = list(bytearray(rgb))
514+
self.__mesg(Command.SET_COLOR, data=data)
503515

504516
def set_claw(self, flag):
505517
'''Set claw switch
@@ -509,6 +521,6 @@ def set_claw(self, flag):
509521
510522
'''
511523
if not flag in [0, 1]:
512-
raise Exception('eror flag, please input 0 or 1')
524+
raise MyCobotDataException('eror flag, please input 0 or 1')
513525

514526
self.__mesg(Command.SET_CLAW, data=[flag])

0 commit comments

Comments
 (0)