-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover_sbc.py
49 lines (36 loc) · 1.34 KB
/
rover_sbc.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
import socketserver
import time
import serial
hit = time.process_time()
hit_prior = hit
#arduino = serial.Serial('/dev/cu.usbmodem14101', 9600, timeout=.5)
#arduino.open()
arduino = serial.Serial('/dev/cu.usbmodem14101', 9600, timeout=.01)
def convert_basestation_values_to_arduino_values(number):
return int((float(number) / 0.2)) + 0x7
class Handler_TCPServer(socketserver.BaseRequestHandler):
def handle(self):
global hit
global hit_prior
hit_prior = hit
hit = time.process_time()
print("Time elapsed since last req: ", (hit-hit_prior))
self.data = self.request.recv(1024).strip()
print("{} sent:".format(self.client_address[0]))
#print(self.data)
self.request.sendall("ACK from TCP Server".encode())
arr = str(self.data)[2:-1].split(',')
narr = map(convert_basestation_values_to_arduino_values, arr)
#print(''.join('%01x'%i for i in narr))
resultantbyte = 0
for i in narr:
resultantbyte = (resultantbyte << 4) | i
#resultantbyte = (narr[0] << 4) & narr[1]
print('%02x'%resultantbyte)
arduino.write(bytes([resultantbyte]))
weh = arduino.read(5)
print(weh)
if __name__ == "__main__":
HOST, PORT = "0.0.0.0", 8082
tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)
tcp_server.serve_forever()