Skip to content

Commit f474496

Browse files
committed
Added 350 interface
1 parent 0d919a5 commit f474496

File tree

4 files changed

+287
-20
lines changed

4 files changed

+287
-20
lines changed

demo/drag_trial_teaching350.py

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import time
2+
import os
3+
import sys
4+
import termios
5+
import tty
6+
import threading
7+
import json
8+
import serial
9+
import serial.tools.list_ports
10+
11+
from pymycobot.mycobot import MyCobot
12+
13+
14+
port: str
15+
mc: MyCobot
16+
sp: int = 80
17+
18+
19+
def setup():
20+
print("")
21+
global port, mc
22+
plist = list(serial.tools.list_ports.comports())
23+
idx = 1
24+
for port in plist:
25+
print("{} : {}".format(idx, port))
26+
idx += 1
27+
28+
_in = input("\nPlease input 1 - {} to choice:".format(idx - 1))
29+
port = str(plist[int(_in) - 1]).split(" - ")[0].strip()
30+
print(port)
31+
print("")
32+
33+
baud = 115200
34+
_baud = input("Please input baud(default:115200):")
35+
try:
36+
baud = int(_baud)
37+
except Exception:
38+
pass
39+
print(baud)
40+
print("")
41+
42+
DEBUG = False
43+
f = input("Wether DEBUG mode[Y/n]:")
44+
if f in ["y", "Y", "yes", "Yes"]:
45+
DEBUG = True
46+
# mc = MyCobot(port, debug=True)
47+
mc = MyCobot(port, baud, debug=DEBUG)
48+
49+
50+
class Raw(object):
51+
"""Set raw input mode for device"""
52+
53+
def __init__(self, stream):
54+
self.stream = stream
55+
self.fd = self.stream.fileno()
56+
57+
def __enter__(self):
58+
self.original_stty = termios.tcgetattr(self.stream)
59+
tty.setcbreak(self.stream)
60+
61+
def __exit__(self, type, value, traceback):
62+
termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty)
63+
64+
65+
class Helper(object):
66+
def __init__(self) -> None:
67+
self.w, self.h = os.get_terminal_size()
68+
69+
def echo(self, msg):
70+
print("\r{}".format(" " * self.w), end="")
71+
print("\r{}".format(msg), end="")
72+
73+
74+
class TeachingTest(Helper):
75+
def __init__(self, mycobot) -> None:
76+
super().__init__()
77+
self.mc = mycobot
78+
self.recording = False
79+
self.playing = False
80+
self.record_list = []
81+
self.record_t = None
82+
self.play_t = None
83+
84+
def record(self):
85+
self.record_list = []
86+
self.recording = True
87+
88+
def _record():
89+
start_t = time.time()
90+
91+
while self.recording:
92+
angles = self.mc.get_encoders()
93+
speeds = self.mc.get_servo_speeds()
94+
if angles:
95+
self.record_list.append([angles, speeds])
96+
time.sleep(0.1)
97+
print("\r {}".format(time.time() - start_t), end="")
98+
99+
self.echo("Start recording.")
100+
self.record_t = threading.Thread(target=_record, daemon=True)
101+
self.record_t.start()
102+
103+
def stop_record(self):
104+
if self.recording:
105+
self.recording = False
106+
self.record_t.join()
107+
self.echo("Stop record")
108+
109+
def play(self):
110+
self.echo("Start play")
111+
for angles in self.record_list:
112+
# print(angles)
113+
self.mc.send_encoders_drag(angles[0], angles[1])
114+
time.sleep(0.1)
115+
self.echo("Finish play")
116+
117+
def loop_play(self):
118+
self.playing = True
119+
120+
def _loop():
121+
len_ = len(self.record_list)
122+
i = 0
123+
while self.playing:
124+
idx_ = i % len_
125+
i += 1
126+
self.mc.send_encoders_drag(self.record_list[idx_][0], self.record_list[idx_][1])
127+
time.sleep(0.1)
128+
129+
self.echo("Start loop play.")
130+
self.play_t = threading.Thread(target=_loop, daemon=True)
131+
self.play_t.start()
132+
133+
def stop_loop_play(self):
134+
if self.playing:
135+
self.playing = False
136+
self.play_t.join()
137+
self.echo("Stop loop play.")
138+
139+
def save_to_local(self):
140+
if not self.record_list:
141+
self.echo("No data should save.")
142+
return
143+
144+
with open(os.path.dirname(__file__) + "/record.txt", "w") as f:
145+
json.dump(self.record_list, f, indent=2)
146+
self.echo("save dir: {}".format(os.path.dirname(__file__)))
147+
148+
def load_from_local(self):
149+
150+
with open(os.path.dirname(__file__) + "/record.txt", "r") as f:
151+
try:
152+
data = json.load(f)
153+
self.record_list = data
154+
self.echo("Load data success.")
155+
except Exception:
156+
self.echo("Error: invalid data.")
157+
158+
def print_menu(self):
159+
print(
160+
"""\
161+
\r q: quit
162+
\r r: start record
163+
\r c: stop record
164+
\r p: play once
165+
\r P: loop play / stop loop play
166+
\r s: save to local
167+
\r l: load from local
168+
\r f: release mycobot
169+
\r----------------------------------
170+
"""
171+
)
172+
173+
def start(self):
174+
self.print_menu()
175+
176+
while not False:
177+
with Raw(sys.stdin):
178+
key = sys.stdin.read(1)
179+
if key == "q":
180+
break
181+
elif key == "r": # recorder
182+
self.record()
183+
elif key == "c": # stop recorder
184+
self.stop_record()
185+
elif key == "p": # play
186+
self.play()
187+
elif key == "P": # loop play
188+
if not self.playing:
189+
self.loop_play()
190+
else:
191+
self.stop_loop_play()
192+
elif key == "s": # save to local
193+
self.save_to_local()
194+
elif key == "l": # load from local
195+
self.load_from_local()
196+
elif key == "f": # free move
197+
self.mc.release_all_servos()
198+
self.echo("Released")
199+
else:
200+
print(key)
201+
continue
202+
203+
204+
if __name__ == "__main__":
205+
setup()
206+
recorder = TeachingTest(mc)
207+
recorder.start()

pymycobot/common.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class ProtocolCode(object):
1111
FOOTER = 0xFA
1212

1313
# System status
14-
VERSION = 0x01
14+
ROBOT_VERSION = 0x01
15+
SOFTWARE_VERSION = 0x02
16+
GET_ROBOT_ID = 0x03
17+
SET_ROBOT_ID = 0x04
1518

1619
# Overall status
1720
POWER_ON = 0x10
@@ -20,6 +23,7 @@ class ProtocolCode(object):
2023
RELEASE_ALL_SERVOS = 0x13
2124
IS_CONTROLLER_CONNECTED = 0x14
2225
READ_NEXT_ERROR = 0x15
26+
SET_FRESH_MODE = 0x16
2327
SET_FREE_MODE = 0x1A
2428
IS_FREE_MODE = 0x1B
2529

@@ -39,12 +43,15 @@ class ProtocolCode(object):
3943

4044
# JOG MODE AND OPERATION
4145
JOG_ANGLE = 0x30
46+
JOG_ABSOLUTE = 0x31
4247
JOG_COORD = 0x32
48+
JOG_INCREMENT = 0x33
4349
JOG_STOP = 0x34
4450
SET_ENCODER = 0x3A
4551
GET_ENCODER = 0x3B
4652
SET_ENCODERS = 0x3C
4753
GET_ENCODERS = 0x3D
54+
SET_ENCODERS_DRAG = 0x3E
4855

4956
# RUNNING STATUS AND SETTINGS
5057
GET_SPEED = 0x40
@@ -62,6 +69,7 @@ class ProtocolCode(object):
6269
SET_SERVO_DATA = 0x52
6370
GET_SERVO_DATA = 0x53
6471
SET_SERVO_CALIBRATION = 0x54
72+
JOINT_BRAKE = 0x55
6573
RELEASE_SERVO = 0x56
6674
FOCUS_SERVO = 0x57
6775

@@ -74,9 +82,11 @@ class ProtocolCode(object):
7482
GET_GRIPPER_VALUE = 0x65
7583
SET_GRIPPER_STATE = 0x66
7684
SET_GRIPPER_VALUE = 0x67
77-
SET_GRIPPER_INI = 0x68
85+
SET_GRIPPER_CALIBRATION = 0x68
7886
IS_GRIPPER_MOVING = 0x69
7987
SET_COLOR = 0x6A
88+
SET_ELETRIC_GRIPPER = 0x6B
89+
INIT_ELETRIC_GRIPPER = 0x6C
8090

8191
# Basic
8292
SET_BASIC_OUTPUT = 0xA0
@@ -108,6 +118,11 @@ class ProtocolCode(object):
108118
SET_END_TYPE = 0x89
109119
GET_END_TYPE = 0x8A
110120

121+
# Impact checking
122+
SET_JOINT_CURRENT = 0x90
123+
GET_JOINT_CURRENT = 0x91
124+
SET_CURRENT_STATE = 0x92
125+
111126
# planning speed
112127
GET_PLAN_SPEED = 0xD0
113128
GET_PLAN_ACCELERATION = 0xD1
@@ -120,6 +135,11 @@ class ProtocolCode(object):
120135
GET_SERVO_VOLTAGES = 0xE3
121136
GET_SERVO_STATUS = 0xE4
122137
GET_SERVO_TEMPS = 0xE5
138+
139+
# IIC
140+
# SET_IIC_STATE = 0xA4
141+
# GET_IIS_BYTE = 0xA5
142+
# SET_IIC_BYTE = 0xA6
123143

124144
class DataProcessor(object):
125145
# Functional approach
@@ -204,7 +224,7 @@ def _process_received(self, data, genre):
204224

205225
# process valid data
206226
res = []
207-
if data_len == 12 or data_len == 8:
227+
if data_len == 12 or data_len == 8 or data_len == 24:
208228
for header_i in range(0, len(valid_data), 2):
209229
one = valid_data[header_i: header_i + 2]
210230
res.append(self._decode_int16(one))

0 commit comments

Comments
 (0)