Skip to content

Commit 878090a

Browse files
committed
push code
1 parent 81517fd commit 878090a

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

demo/drag_trial_teaching_mubuddy.py

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
'''
2+
Drag and teach in windows version
3+
'''
14
import time
25
import os
36
import sys
4-
import termios
5-
import tty
7+
# import termios
8+
# import tty
69
import threading
710
import json
811
import serial
@@ -47,28 +50,28 @@ def setup():
4750
mc = MyBuddy(port, baud, debug=DEBUG)
4851

4952

50-
class Raw(object):
51-
"""Set raw input mode for device"""
53+
# class Raw(object):
54+
# """Set raw input mode for device"""
5255

53-
def __init__(self, stream):
54-
self.stream = stream
55-
self.fd = self.stream.fileno()
56+
# def __init__(self, stream):
57+
# self.stream = stream
58+
# self.fd = self.stream.fileno()
5659

57-
def __enter__(self):
58-
self.original_stty = termios.tcgetattr(self.stream)
59-
tty.setcbreak(self.stream)
60+
# def __enter__(self):
61+
# self.original_stty = termios.tcgetattr(self.stream)
62+
# tty.setcbreak(self.stream)
6063

61-
def __exit__(self, type, value, traceback):
62-
termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty)
64+
# def __exit__(self, type, value, traceback):
65+
# termios.tcsetattr(self.stream, termios.TCSANOW, self.original_stty)
6366

6467

6568
class Helper(object):
6669
def __init__(self) -> None:
6770
self.w, self.h = os.get_terminal_size()
6871

6972
def echo(self, msg):
70-
print("\r{}".format(" " * self.w), end="")
71-
print("\r{}".format(msg), end="")
73+
print("\r{}".format(" " * self.w))
74+
print("\r{}".format(msg))
7275

7376

7477
class TeachingTest(Helper):
@@ -89,12 +92,12 @@ def _record():
8992
start_t = time.time()
9093

9194
while self.recording:
92-
angles_1 = self.mc.get_encoders(1)
95+
angles_1 = self.mc.get_encoders(1)
9396
angles_2 = self.mc.get_encoders(2)
9497
if angles_1 and angles_2:
9598
self.record_list.append([angles_1,angles_2])
9699
time.sleep(0.1)
97-
print("\r {}".format(time.time() - start_t), end="")
100+
# print("\r {}".format(time.time() - start_t), end="")
98101

99102
self.echo("Start recording.")
100103
self.record_t = threading.Thread(target=_record, daemon=True)
@@ -109,9 +112,10 @@ def stop_record(self):
109112
def play(self):
110113
self.echo("Start play")
111114
for angles in self.record_list:
112-
# print(angles)
113-
self.mc.set_encoders(angles[0], 80)
114-
self.mc.set_encoders(angles[1], 80)
115+
print(angles[1])
116+
self.mc.set_encoders(1,angles[0], 80, 1)
117+
time.sleep(0.05)
118+
self.mc.set_encoders(2,angles[1], 80,1)
115119
time.sleep(0.1)
116120
self.echo("Finish play")
117121

@@ -159,14 +163,14 @@ def load_from_local(self):
159163
def print_menu(self):
160164
print(
161165
"""\
162-
\r q: quit
163-
\r r: start record
164-
\r c: stop record
165-
\r p: play once
166-
\r P: loop play / stop loop play
167-
\r s: save to local
168-
\r l: load from local
169-
\r f: release mycobot
166+
\r q + Enter: quit
167+
\r r + Enter: start record
168+
\r c + Enter: stop record
169+
\r p + Enter: play once
170+
\r P + Enter: loop play / stop loop play
171+
\r s + Enter: save to local
172+
\r l + Enter: load from local
173+
\r f + Enter: release mycobot
170174
\r----------------------------------
171175
"""
172176
)
@@ -175,31 +179,34 @@ def start(self):
175179
self.print_menu()
176180

177181
while not False:
178-
with Raw(sys.stdin):
179-
key = sys.stdin.read(1)
180-
if key == "q":
181-
break
182-
elif key == "r": # recorder
183-
self.record()
184-
elif key == "c": # stop recorder
185-
self.stop_record()
186-
elif key == "p": # play
187-
self.play()
188-
elif key == "P": # loop play
189-
if not self.playing:
190-
self.loop_play()
191-
else:
192-
self.stop_loop_play()
193-
elif key == "s": # save to local
194-
self.save_to_local()
195-
elif key == "l": # load from local
196-
self.load_from_local()
197-
elif key == "f": # free move
198-
self.mc.release_all_servos()
199-
self.echo("Released")
182+
key = input()
183+
# with Raw(sys.stdin):
184+
# key = sys.stdin.read(1)
185+
if key == "q":
186+
break
187+
elif key == "r": # recorder
188+
self.record()
189+
elif key == "c": # stop recorder
190+
self.stop_record()
191+
elif key == "p": # play
192+
self.play()
193+
elif key == "P": # loop play
194+
if not self.playing:
195+
self.loop_play()
200196
else:
201-
print(key)
202-
continue
197+
self.stop_loop_play()
198+
elif key == "s": # save to local
199+
self.save_to_local()
200+
elif key == "l": # load from local
201+
self.load_from_local()
202+
elif key == "f": # free move
203+
self.mc.release_all_servos(0)
204+
time.sleep(0.05)
205+
self.mc.release_all_servos(2)
206+
self.echo("Released")
207+
else:
208+
print(key)
209+
continue
203210

204211

205212
if __name__ == "__main__":

pymycobot/mybuddy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,16 @@ def _mesg(self, genre, *args, **kwargs):
173173
return res
174174
return None
175175

176-
def get_radians(self):
176+
def get_radians(self, id):
177177
"""Get the radians of all joints
178178
179+
Args:
180+
id: 1/2/3 (L/R/W)
181+
179182
Return:
180183
list: A list of float radians [radian1, ...]
181184
"""
182-
angles = self._mesg(ProtocolCode.GET_ANGLES, has_reply=True)
185+
angles = self._mesg(ProtocolCode.GET_ANGLES, id, has_reply=True)
183186
return [round(angle * (math.pi / 180), 3) for angle in angles]
184187

185188
def send_radians(self, id, radians, speed):

0 commit comments

Comments
 (0)