Skip to content

Commit 1e3b303

Browse files
committed
增加320 状态查询接口
1 parent 7865fa5 commit 1e3b303

File tree

3 files changed

+106
-18
lines changed

3 files changed

+106
-18
lines changed

pymycobot/common.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import time
55
import struct
66
import sys
7+
import locale
8+
9+
from pymycobot.robot_info import Robot320Info
710

811

912
class ProtocolCode(object):
@@ -514,17 +517,40 @@ def _process_received(self, data, genre, arm=6):
514517
) or data_len == 6 and cmd_id in ignor_t:
515518
for v in valid_data:
516519
res.append(v)
517-
return res
518520
elif data_len == 8 and arm == 14 and cmd_id == ProtocolCode.GET_DOWN_ENCODERS:
519521
i = 0
520522
while i < data_len:
521523
byte_value = int.from_bytes(valid_data[i:i+4], byteorder='big', signed=True)
522524
i+=4
523525
res.append(byte_value)
524526
return res
525-
for header_i in range(0, len(valid_data), 2):
526-
one = valid_data[header_i : header_i + 2]
527-
res.append(self._decode_int16(one))
527+
else:
528+
for header_i in range(0, len(valid_data), 2):
529+
one = valid_data[header_i : header_i + 2]
530+
res.append(self._decode_int16(one))
531+
error_key = None
532+
if genre == ProtocolCode.GET_ROBOT_STATUS:
533+
error_key = "robot_error"
534+
if genre == ProtocolCode.GET_SERVO_STATUS:
535+
error_key = "servo_error"
536+
if error_key is not None:
537+
robot_320_info = Robot320Info.error_info
538+
locale_lang = locale.getdefaultlocale()[0]
539+
if locale_lang not in ["zh_CN", "en_US"]:
540+
locale_lang = "en_US"
541+
for i in range(len(res)):
542+
if res[i] != 0:
543+
data_bin = bin(res[i])[2:]
544+
error_list = []
545+
data_bin_len = len(data_bin)
546+
for j in range(data_bin_len):
547+
if data_bin[data_bin_len - 1 -j] != 0:
548+
error_list.append(j)
549+
if locale_lang == "zh_CN":
550+
print("错误: 关节{} - {}".format(i+1, robot_320_info[locale_lang][error_key].get(j, 255)))
551+
else:
552+
print("Error: Joint{} - {}".format(i+1, robot_320_info[locale_lang][error_key].get(j, 255)))
553+
res[i] = error_list
528554
elif data_len == 2:
529555
if genre in [
530556
ProtocolCode.GET_PLAN_SPEED,
@@ -728,9 +754,11 @@ def read(self, genre, method=None, command=None, _class=None, timeout=None):
728754
return datas
729755
elif genre == ProtocolCode.GET_ACCEI_DATA:
730756
wait_time = 1
731-
while True and time.time() - t < wait_time:
757+
while True and time.time() - t < wait_time:
758+
# return b'\xfe\xfe\x0e\x19\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\xfa'
759+
return b'\xfe\xfe\x08\xe4\x00\x00\x00\x00\x00\x06\xfa'
732760
data = self._serial_port.read()
733-
self.log.debug("data: {}".format(data))
761+
# self.log.debug("data: {}".format(data))
734762
k += 1
735763
if _class in ["Mercury", "MercurySocket"]:
736764
if data_len == 3:
@@ -783,5 +811,5 @@ def read(self, genre, method=None, command=None, _class=None, timeout=None):
783811
for d in datas:
784812
command_log += hex(d)[2:] + " "
785813
self.log.debug("_read : {}".format(command_log))
786-
814+
print(datas)
787815
return datas

pymycobot/mycobot.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,19 @@ def _res(self, real_command, has_reply, genre):
166166
else:
167167
r.append(self._int2angle(res[index]))
168168
return r
169-
elif genre == ProtocolCode.GET_ROBOT_STATUS:
170-
for i in range(len(res)):
171-
if res[i] != 0:
172-
data = bin(res[i])[2:]
173-
res[i] = []
174-
while len(data) != 16:
175-
data = "0"+data
176-
for j in range(16):
177-
if data[j] != "0":
178-
res[i].append(15-j)
179-
return res
169+
# elif genre == ProtocolCode.GET_ROBOT_STATUS:
170+
# print(res)
171+
# for i in range(len(res)):
172+
# if res[i] != 0:
173+
# print(res[i])
174+
# data = bin(res[i])[2:]
175+
# res[i] = []
176+
# while len(data) != 16:
177+
# data = "0"+data
178+
# for j in range(16):
179+
# if data[j] != "0":
180+
# res[i].append(15-j)
181+
# return res
180182
else:
181183
return res
182184
return None

pymycobot/robot_info.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# coding=utf-8
2+
3+
class Robot320Info(object):
4+
error_info = {
5+
"zh_CN": {
6+
"robot_error":
7+
{
8+
0: "通信异常,请检查线路、舵机固件版本是否正常、电源是否插上、固件是否烧录正确,波特率是否正确等",
9+
1: "伺服电机型号错误,需要更换电机",
10+
2: "伺服电机固件版本较低,需要使用FD升级",
11+
3: "伺服电机p值异常,默认32,此异常会自动恢复",
12+
4: "伺服电机D值异常,默认8,此异常会自动恢复",
13+
5: "伺服电机I值异常,默认0,此异常会自动恢复",
14+
6: "伺服电机顺时针不灵敏区参数异常,默认3,此异常会自动恢复",
15+
7: "伺服电机逆时针不灵敏区参数异常,默认3,此异常会自动恢复",
16+
8: "伺服电机相位异常,此异常会自动恢复",
17+
9: "伺服电机返回延时异常,默认0,此异常会自动恢复",
18+
10: "伺服电机最小启动力异常,默认0,此异常会自动恢复",
19+
11: "伺服电机异常,当舵机异常时,无法控制机器运动,请查询舵机反馈接口get_servo_status,查看具体报错",
20+
255: "未知错误"
21+
},
22+
"servo_error":
23+
{
24+
0: "伺服电机欠压/过压,查看电压,如果为0,需要修改舵机参数;如果大于实际,可能散热片损坏",
25+
1: "伺服电机磁编码异常",
26+
2: "伺服电机过温",
27+
3: "伺服电机过流",
28+
5: "伺服电机过载",
29+
}
30+
},
31+
"en_US": {
32+
"robot_error":
33+
{
34+
0: "Communication exception, please check the line, servo motor firmware version, power supply, firmware burning, baud rate, etc.",
35+
1: "Servo motor model error, need to replace the motor",
36+
2: "Servo motor firmware version is too low, need to use FD upgrade",
37+
3: "Servo motor p value exception, default 32, this exception will be automatically restored",
38+
4: "Servo motor D value exception, default 8, this exception will be automatically restored",
39+
5: "Servo motor I value exception, default 0, this exception will be automatically restored",
40+
6: "Servo motor clockwise insensitive zone parameter exception, default 3, this exception will be automatically restored",
41+
7: "Servo motor counterclockwise insensitive zone parameter exception, default 3, this exception will be automatically restored",
42+
8: "Servo motor phase exception, this exception will be automatically restored",
43+
9: "Servo motor return delay exception, default 0, this exception will be automatically restored",
44+
10: "Servo motor minimum starting force exception, default 0, this exception will be automatically restored",
45+
11: "Servo motor exception, when the servo is abnormal, the robot cannot move, please query the servo feedback interface get_servo_status, view the specific error",
46+
255: "Unknown error"
47+
},
48+
"servo_error":
49+
{
50+
0: "Servo motor under-voltage/over-voltage, check the voltage, if it is 0, you need to modify the servo parameters; if it is greater than the actual, it may be damaged",
51+
1: "Servo motor magnetic encoder exception",
52+
2: "Servo motor over-temperature",
53+
3: "Servo motor over-current",
54+
5: "Servo motor over-load",
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)