-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
56 lines (52 loc) · 1.89 KB
/
Server.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
import socket, math, serial, time
HOST = ''
PORT = 100
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected to ', addr)
data = ''
azimuth = ''
pitch = ''
roll = ''
right = 127
left = 127
rPWM = 127
sensitivity = 10
ser = serial.Serial('/dev/ttyACM0', 9600)
try:
while 1:
if ser.in_waiting > 0:
print(str(ord(ser.read()))+ "," + str(ord(ser.read()))+","+ str(ord(ser.read())))
data = conn.recv(1024)
data = data.decode('utf-8')
dataArray = data.split(',')
#Check if messed up
if len(dataArray)<5:
roll = float(dataArray[len(dataArray) - 1])
pitch = (float( dataArray[len(dataArray) - 2]) - 34)
rPWM = ((abs(pitch)/180*255-127)*sensitivity)+127
azimuth = dataArray[len(dataArray) - 3]
print('rPWM ' + str(rPWM) + '\n')
#print('pitch ' + pitch)
count = rPWM
#print(chr (count))
#right,left
if roll>0:
right = int(max(0, min(count, 255)))
left = int(max(0, min(count - roll, 255)))
else:
right = int(max(0, min(count + roll, 255)))
left = int(max(0, min(count, 255)))
ser.write(chr(right) + chr(44)+ chr(left))
#print('right ' + str(right) + 'left ' + str(left))
else:
print('Error occurred with data: ' + str(dataArray) + '\n')
except(KeyboardInterrupt, SystemExit, Exception):
ser.write(chr(127) + chr(44)+ chr(127))
s.shutdown(socket.SHUT_RDWR)
conn.close()
s.close()