|
1 | 1 | import functools |
| 2 | +from pymycobot.common import Command |
2 | 3 |
|
3 | 4 |
|
4 | 5 | class MyCobotDataException(Exception): |
5 | 6 | pass |
6 | 7 |
|
7 | | -def check_id(func): |
8 | 8 |
|
9 | | - @functools.wraps(func) |
10 | | - def _wapper(*args, **kwargs): |
11 | | - if not 0 <= args[1] <= 6: |
12 | | - raise MyCobotDataException('id not right, should be 1 ~ 6') |
| 9 | +def check_parameters(genre=0): |
13 | 10 |
|
14 | | - return func(*args, **kwargs) |
| 11 | + def check_decorator(func): |
15 | 12 |
|
16 | | - return _wapper |
| 13 | + @functools.wraps(func) |
| 14 | + def _wapper(*args, **kwargs): |
| 15 | + if genre == Command.SEND_ANGLE: |
| 16 | + check_id(args[1]) |
| 17 | + check_angle(args[2]) |
| 18 | + check_speed(args[3]) |
| 19 | + elif genre == Command.SEND_ANGLES: |
| 20 | + check_angles(args[1]) |
| 21 | + check_speed(args[2]) |
| 22 | + elif genre == 'radians': |
| 23 | + pass |
| 24 | + elif genre == Command.SEND_COORD: |
| 25 | + check_id(args[1]) |
| 26 | + check_coord(args[1] - 1, args[2]) |
| 27 | + check_speed(args[3]) |
| 28 | + elif genre == Command.SEND_COORDS: |
| 29 | + check_coords(args[1]) |
| 30 | + check_speed(args[2]) |
| 31 | + elif genre == Command.SET_SPEED: |
| 32 | + check_speed(args[1]) |
| 33 | + elif genre == Command.IS_IN_POSITION: |
| 34 | + check_boolean(args[2]) |
| 35 | + if args[2] == 0: |
| 36 | + check_angles(args[1]) |
| 37 | + elif args[2] == 1: |
| 38 | + check_coords(args[1]) |
| 39 | + elif genre == Command.SET_GRIPPER_STATE: |
| 40 | + check_boolean(args[1]) |
| 41 | + check_speed(args[2]) |
| 42 | + elif genre == Command.SET_COLOR: |
| 43 | + check_rgb(args[1:]) |
| 44 | + elif genre == Command.JOG_ANGLE or genre == Command.JOG_COORD: |
| 45 | + check_id(args[1]) |
| 46 | + check_boolean(args[2]) |
| 47 | + check_speed(args[3]) |
| 48 | + elif genre in [ |
| 49 | + Command.GET_JOINT_MIN_ANGLE, Command.GET_JOINT_MAX_ANGLE, |
| 50 | + Command.IS_SERVO_ENABLE, Command.RELEASE_SERVO, |
| 51 | + Command.FOCUS_SERVO |
| 52 | + ]: |
| 53 | + check_id(args[1]) |
17 | 54 |
|
| 55 | + return func(*args, **kwargs) |
18 | 56 |
|
| 57 | + return _wapper |
| 58 | + |
| 59 | + return check_decorator |
| 60 | + |
| 61 | + |
| 62 | +def check_boolean(b): |
| 63 | + if b != 0 and b != 1: |
| 64 | + raise MyCobotDataException('This parameter needs to be 0 or 1') |
| 65 | + |
| 66 | + |
| 67 | +def check_range(v, ra): |
| 68 | + if ra[0] <= v <= ra[1]: |
| 69 | + return True |
| 70 | + else: |
| 71 | + return False |
| 72 | + |
| 73 | + |
| 74 | +def check_len(d, l, n): |
| 75 | + if len(d) != l: |
| 76 | + raise MyCobotDataException('The length of {} needs be {}'.format(n, l)) |
| 77 | + |
| 78 | + |
| 79 | +def check_speed(sp): |
| 80 | + if not check_range(sp, [0, 100]): |
| 81 | + raise MyCobotDataException('speed not right, should be 0 ~ 100') |
| 82 | + |
| 83 | + |
| 84 | +def check_id(id): |
| 85 | + if not check_range(id, [1, 6]): |
| 86 | + raise MyCobotDataException('id not right, should be 1 ~ 6') |
| 87 | + |
| 88 | + |
| 89 | +def check_angle(v): |
| 90 | + if not check_range(id, [-180, 180]): |
| 91 | + raise MyCobotDataException( |
| 92 | + 'angle value not right, should be -180 ~ 180') |
| 93 | + |
| 94 | + |
| 95 | +def check_angles(vs): |
| 96 | + check_len(vs, 6, 'angles') |
| 97 | + for v in vs: |
| 98 | + check_angle(v) |
| 99 | + |
| 100 | + |
| 101 | +def check_coord(id, v): |
| 102 | + coords = ['x', 'y', 'z', 'rx', 'ry', 'rz'] |
| 103 | + if id < 3: |
| 104 | + pass |
| 105 | + else: |
| 106 | + if not check_range(id, [-180, 180]): |
| 107 | + raise MyCobotDataException( |
| 108 | + '{} value not right, should be -180 ~ 180'.format(coords[id])) |
| 109 | + |
| 110 | + |
| 111 | +def check_coords(vs): |
| 112 | + check_len(vs, 6, 'coords') |
| 113 | + for i, v in enumerate(vs): |
| 114 | + check_coord(i, v) |
| 115 | + |
| 116 | + |
| 117 | +def check_rgb(args): |
| 118 | + for i in args: |
| 119 | + if not check_range(id, [0, 255]): |
| 120 | + raise MyCobotDataException('The RGB value needs be 0 ~ 255') |
0 commit comments