-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathteleop_client.py
executable file
·137 lines (96 loc) · 3.13 KB
/
teleop_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# Based on ROS teleop_twist_keyboard: http://wiki.ros.org/teleop_twist_keyboard
# https://github.com/ros-teleop/teleop_twist_keyboard/blob/master/teleop_twist_keyboard.py
import websocket
import threading
import sys
import json
if sys.platform == 'win32':
import msvcrt
else:
import termios
import tty
class PublishThread(threading.Thread):
def __init__(self, ws):
super(PublishThread, self).__init__()
self.ws = ws
self.key = ''
self.condition = threading.Condition()
self.done = False
self.start()
def update(self, key):
self.condition.acquire()
self.key = key
# Notify publish thread that we have a new message
self.condition.notify()
self.condition.release()
def stop(self):
self.done = True
self.update("teleop_stop") # Publish stop message when thread exits
self.join()
def run(self):
while not self.done:
self.condition.acquire()
# Wait for new message notification from update()
self.condition.wait()
key = self.key
self.condition.release()
# Publish
message = {"key": key}
self.ws.send(json.dumps(message))
def getKey(settings):
if sys.platform == 'win32':
# getwch() returns a string on Windows
key = msvcrt.getwch()
else:
tty.setraw(sys.stdin.fileno())
# sys.stdin.read() returns a string on Linux
key = sys.stdin.read(1)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
return key
def saveTerminalSettings():
if sys.platform == 'win32':
return None
return termios.tcgetattr(sys.stdin)
def restoreTerminalSettings(old_settings):
if sys.platform == 'win32':
return
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
def on_message(ws, message):
reply = json.loads(message)
if "prompt" in reply:
print(reply["prompt"], end='', flush=True)
def on_error(ws, error):
print()
print("Websocket error:", error)
def on_close(ws, close_status_code, close_msg):
print()
print("Closing websocket")
def on_open(ws):
def run(*args):
settings = saveTerminalSettings()
pub_thread = PublishThread(ws)
try:
pub_thread.update("teleop_start")
while True:
key = getKey(settings)
# Quit if Ctrl+C is pressed
if key == '\x03':
break
pub_thread.update(key)
except Exception as e:
print(e)
finally:
pub_thread.stop()
ws.close()
restoreTerminalSettings(settings)
threading.Thread(target=run).start()
if __name__ == "__main__":
host = "ws://localhost:7000/"
webs = websocket.WebSocketApp(host,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
webs.on_open = on_open
webs.run_forever()