Skip to content

Commit c10d963

Browse files
committed
imporve code compatibility.
1 parent 22545b5 commit c10d963

File tree

9 files changed

+239
-166
lines changed

9 files changed

+239
-166
lines changed

pymycobot/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# coding=utf-8
2+
13
from __future__ import absolute_import
4+
import datetime
25

36
from pymycobot.mycobot import MyCobot
47
from pymycobot.generate import MycobotCommandGenerater
@@ -11,6 +14,10 @@
1114
__author__ = "Zachary zhang"
1215
__email__ = "[email protected]"
1316
__git_url__ = "https://github.com/elephantrobotics/pymycobot"
17+
__copyright__ = "CopyRight (c) 2020-{0} Shenzhen Elephantrobotics technology".format(
18+
datetime.datetime.now().year
19+
)
1420

21+
# For raspberry mycobot 280.
1522
PI_PORT = "/dev/ttyAMA0"
1623
PI_BAUD = 1000000

pymycobot/common.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# coding=utf-8
2+
13
from __future__ import division
24
import struct
35

46

5-
class Command(object):
7+
class ProtocolCode(object):
68
# BASIC
79
HEADER = 0xFE
810
FOOTER = 0xFA
@@ -92,16 +94,16 @@ def _decode_int8(self, data):
9294
def _decode_int16(self, data):
9395
return struct.unpack(">h", data)[0]
9496

95-
def _angle_to_int(self, angle):
97+
def _angle2int(self, angle):
9698
return int(angle * 100)
9799

98-
def _coord_to_int(self, coord):
100+
def _coord2int(self, coord):
99101
return int(coord * 10)
100102

101-
def _int_to_angle(self, _int):
103+
def _int2angle(self, _int):
102104
return round(_int / 100.0, 3)
103105

104-
def _int_to_coord(self, _int):
106+
def _int2coord(self, _int):
105107
return round(_int / 10.0, 2)
106108

107109
def _flatten(self, _list):
@@ -121,7 +123,7 @@ def _process_data_command(self, args):
121123
)
122124

123125
def _is_frame_header(self, data, pos):
124-
return data[pos] == Command.HEADER and data[pos + 1] == Command.HEADER
126+
return data[pos] == ProtocolCode.HEADER and data[pos + 1] == ProtocolCode.HEADER
125127

126128
def _process_received(self, data, genre):
127129
if not data:
@@ -152,7 +154,7 @@ def _process_received(self, data, genre):
152154
one = valid_data[idx : idx + 2]
153155
res.append(self._decode_int16(one))
154156
elif data_len == 2:
155-
if genre in [Command.IS_SERVO_ENABLE]:
157+
if genre in [ProtocolCode.IS_SERVO_ENABLE]:
156158
return [self._decode_int8(valid_data[1:2])]
157159
res.append(self._decode_int16(valid_data))
158160
else:

pymycobot/error.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
_MINANGLE = -190.0
2-
_MAXANGLE = 190.0
1+
# coding=utf-8
2+
3+
# In order to adapt to the atom side, ID of 0-5 or 1-6 are allowed.
4+
# In order to support end control, ID 7 is allowed.
5+
MIN_ID = 0
6+
MAX_ID = 7
7+
8+
# In fact, most joints cannot reach plus or minus 180 degrees.
9+
# There may be a value greater than 180 when reading the angle,
10+
# and the maximum and minimum values are expanded for compatibility.
11+
MIN_ANGLE = -190.0
12+
MAX_ANGLE = 190.0
313

414

515
class MyCobotDataException(Exception):
@@ -11,65 +21,55 @@ def check_boolean(b):
1121
raise MyCobotDataException("This parameter needs to be 0 or 1")
1222

1323

14-
def check_range(v, ra):
15-
min, max = ra
16-
if min <= v <= max:
17-
return True
18-
else:
19-
return False
20-
21-
22-
def check_coord(id, v):
23-
coords = ["x", "y", "z", "rx", "ry", "rz"]
24-
if id < 3:
25-
pass
26-
else:
27-
if not check_range(v, [-180, 180]):
28-
raise MyCobotDataException(
29-
"{} value not right, should be -180 ~ 180".format(coords[id])
30-
)
31-
32-
33-
def check_rgb(args):
34-
rgb_str = ["r", "g", "b"]
35-
for i, v in enumerate(args):
36-
if not check_range(v, [0, 255]):
37-
raise MyCobotDataException(
38-
"The RGB value needs be 0 ~ 255, but the %s is %s" % (rgb_str[i], v)
39-
)
40-
41-
42-
def check_datas(**kwargs):
43-
if kwargs.get("joint_id", None) is not None and not 0 < kwargs["joint_id"] < 7:
24+
def calibration_parameters(**kwargs):
25+
if kwargs.get("id", None) is not None and not MIN_ID <= kwargs["id"] <= MAX_ID:
4426
raise MyCobotDataException(
45-
"The joint id not right, should be 1 ~ 6, but received %s."
46-
% kwargs["joint_id"]
27+
"The id not right, should be {0} ~ {1}, but received {2}.".format(
28+
MIN_ID, MAX_ID, kwargs["id"]
29+
)
4730
)
31+
4832
if (
4933
kwargs.get("degree", None) is not None
50-
and not _MINANGLE <= kwargs["degree"] <= _MAXANGLE
34+
and not MIN_ANGLE <= kwargs["degree"] <= MAX_ANGLE
5135
):
5236
raise MyCobotDataException(
53-
"degree value not right, should be -180 ~ 180, but received %s"
54-
% kwargs["degree"]
55-
)
56-
if kwargs.get("len6", None) is not None:
57-
len_ = len(kwargs["len6"])
58-
if len_ != 6:
59-
raise MyCobotDataException(
60-
"The length of data should be 6, the truth is %s" % len_
37+
"degree value not right, should be {0} ~ {1}, but received {2}".format(
38+
MIN_ANGLE, MAX_ANGLE, kwargs["degree"]
6139
)
40+
)
41+
6242
if kwargs.get("degrees", None) is not None:
63-
for idx, angle in enumerate(kwargs["degrees"]):
64-
if not _MINANGLE <= angle <= _MAXANGLE:
43+
degrees = kwargs["degrees"]
44+
if not isinstance(degrees, list):
45+
raise MyCobotDataException("`degrees` must be a list.")
46+
if len(degrees) != 6:
47+
raise MyCobotDataException("The length of `degrees` must be 6.")
48+
for idx, angle in enumerate(degrees):
49+
if not MIN_ANGLE <= angle <= MAX_ANGLE:
6550
raise MyCobotDataException(
66-
"degree value not right, should be -180 ~ 180, the error index is %s"
67-
% idx
51+
"Has invalid degree value, error on index {0}. Degree should be {1} ~ {2}.".format(
52+
idx, MIN_ANGLE, MAX_ANGLE
53+
)
6854
)
55+
56+
if kwargs.get("coords", None) is not None:
57+
coords = kwargs["coords"]
58+
if not isinstance(coords, list):
59+
raise MyCobotDataException("`coords` must be a list.")
60+
if len(coords) != 6:
61+
raise MyCobotDataException("The length of `coords` must be 6.")
62+
6963
if kwargs.get("speed", None) is not None and not 0 <= kwargs["speed"] <= 100:
7064
raise MyCobotDataException(
7165
"speed value not right, should be 0 ~ 100, the error speed is %s"
7266
% kwargs["speed"]
7367
)
68+
7469
if kwargs.get("rgb", None) is not None:
75-
check_rgb(kwargs["rgb"])
70+
rgb_str = ["r", "g", "b"]
71+
for i, v in enumerate(kwargs["rgb"]):
72+
if not (0 <= v <= 255):
73+
raise MyCobotDataException(
74+
"The RGB value needs be 0 ~ 255, but the %s is %s" % (rgb_str[i], v)
75+
)

0 commit comments

Comments
 (0)