-
Notifications
You must be signed in to change notification settings - Fork 2
/
telemetry_client.py
59 lines (49 loc) · 1.6 KB
/
telemetry_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
import sys
import re
import traceback
import paramiko
from websocket import create_connection
from time import sleep
HOSTNAME = "192.168.0.117"
USERNAME = "deepracer"
PASSWORD = "deepracer"
SERVER_URL = "ws://localhost:8000/ws/0"
p = re.compile('msg: "Setting throttle to (\d+\.\d+)"')
def websocket_connect():
while True:
try:
websocket = create_connection(SERVER_URL, timeout=0.1)
print("Websocket connected")
return websocket
except Exception as e:
print("Failed to open websocket: %s" % e)
sleep(1)
ws = websocket_connect()
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOSTNAME, 22, USERNAME, PASSWORD)
print("connected")
stdin, stdout, stderr = client.exec_command("source /opt/ros/kinetic/setup.bash; rostopic echo /rosout_agg")
stdin.close()
for line in iter(lambda: stdout.readline(2048), ""):
# print(line, end="")
if "Setting throttle to" in line:
match = p.match(line)
throttle_raw = float(match.group(1))
throttle = round(throttle_raw * 100)
print(throttle)
try:
ws.send(str(throttle))
except Exception as e:
print("Failed to send to websocket: %s" % e)
ws = websocket_connect()
ws.send(str(throttle))
except Exception as e:
print("*** Caught exception: " + str(e.__class__) + ": " + str(e))
traceback.print_exc()
try:
client.close()
except:
pass
sys.exit(1)