-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
86 lines (73 loc) · 2.21 KB
/
robot.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
import socket
class Robot():
def __init__(self, number=33, debug=False):
self.number = number
self.left = 0
self.right = 0
self.led = 0
self.msg_out = '[000000]'
self.msg_in = ''
if not debug:
self.ip = "192.168.2." + str(self.number)
self.port = 8000
else:
self.ip = 'localhost'
self.port = 9999
@staticmethod
def convert_speed(x):
if x < 0:
x = 256 + x
return x
def connect(self):
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.ip, self.port))
except OSError:
return -1
def disconnect(self):
self.msg_out = '[000000]'
self.s.send(self.msg_out.encode())
self.s.close()
def update_left(self, left):
if -128 <= left <= 127:
self.left = left
return 0
else:
return -1
def update_right(self, right):
if -128 <= right <= 127:
self.right = right
return 0
else:
return -1
def update_led(self, led):
if led == 'off' or led == 0:
self.led = 0
elif led == 'green' or led == 'g' or led == 1:
self.led = 1
elif led == 'red' or led == 'r' or led == 2:
self.led = 2
elif led == 3:
self.led = 3
else:
pass
def update_message(self):
left = self.convert_speed(self.left)
right = self.convert_speed(self.right)
left = format(left, '02x')
right = format(right, '02x')
led = format(self.led, '02x')
self.msg_out = '[' + led + left + right + ']'
def send(self):
self.update_message()
self.s.send(self.msg_out.encode())
def receive(self):
msg = self.s.recv(28).decode('ascii')
self.msg_in = msg
self.status = int(msg[1:3], 16)
self.battery = int(msg[3:7], 16)
self.sensor1 = int(msg[7:11], 16)
self.sensor2 = int(msg[11:15], 16)
self.sensor3 = int(msg[15:19], 16)
self.sensor4 = int(msg[19:23], 16)
self.sensor5 = int(msg[23:27], 16)