-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.py
executable file
·74 lines (61 loc) · 1.86 KB
/
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
import socket
import json
import time
"""
MotionControl - MissionControl Socket Client
By Daniel Swoboda (@snoato, swobo.space)
"""
class Client:
connreq = ' {"ConnREQ" : {"HardwareType" : "Smartphone"} }'
connstt = ' {"ConnSTT" : {}} '
def __init__(self, address, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.sock.connect((address, port))
self.connect()
def connect(self):
self.sock.send(self.connreq)
resp = self.receive()
if(resp.keys()[0] == "ConnACK"):
self.receive()
self.sock.send(self.connstt)
print("Connected to Robot")
else:
raise Exception()
def receive(self):
finished = False
jsonMsg = None
counter = 0
msg = self.sock.recv(2048).decode("utf-8")
msg = msg.strip(' ')
if not msg:
return False
while not finished:
try:
counter += 1
jsonMsg = json.loads(msg)
finished = True
except ValueError:
if counter > 10:
return False
msg1 = self.sock.recv(2048).decode("utf-8")
msg = msg+msg1
if not msg1:
return False
return json.loads(msg)
def control_motor(self, lvalue, rvalue):
if (lvalue == 0 and rvalue == 0):
self.send('{ "Control" : { "Stop" : "Click" } }')
else:
if(lvalue == 0):
lvalue = 1
else:
lvalue = lvalue*15
if(rvalue == 0):
rvalue = 1
else:
rvalue = rvalue*15
self.send('{ "Control" : { "Motor Left" : '+str(lvalue)+', "Motor Right" : '+str(rvalue)+' }}')
def send(self, message):
#print(message)
self.sock.sendall(message)