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