-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.py
181 lines (127 loc) · 4.03 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Gregory Ferguson
"""
import socket
import threading
import pyfirmata
import time
import struct
# raspberry pi's static ip
HOST = '192.168.4.1'
FORWARD = 'ABS_RZ'
BACKWARD = 'ABS_Z'
# setting up the board
board = pyfirmata.ArduinoMega('/dev/ttyACM0')
l_motor_pin = board.get_pin('d:11:p')
r_motor_pin = board.get_pin('d:12:p')
motor_toggle_pin = board.get_pin('d:24:o')
def Serial_Send(data, ser):
#Once you have the data in bytes, send to embedded system.
encoded_data = []
for i in range(0, len(data)):
length = len(data[i])
data[i] = data[i].encode('utf-8')
encoded_data.append(struct.pack(str(length) + 's', data[i]))
print(encoded_data)
def Serial_Receive(ser):
time.sleep(5)
#print("NA")
#t = threading.Thread(target = Client_Send, args = (clientsocket, data))
#t.start()
while True:
x = ser.read()
#x = x.decode("utf-8")
print(x)
ser.close()
#This should only be called inside Serial_Receive for now
#Later on, other functions may call it, but it should never always be running
def Client_Send(clientsocket, data):
print("NA")
#Easier to understand than binary data, send strings between server and client.
#Then convert to list to be used later on, data can be any type.
def Client_Receive(clientsocket):
global ser
counter = 0
while (true):
# no forward or reverse command, stop
motor_toggle_pin.write(0)
l_motor_pin.write(.49804)
r_motor_pin.write(.49804)
counter += 1
data = clientsocket.recv(2048)
strings = data.decode('utf8')
# Converting string to list
res = strings.strip('][').split(', ')
data = [res[0], res[1], res[2]]
# ensure our packets didn't get messed up
if(len(strings) > 30):
continue
l_value = float(res[0].strip("'"))
r_value = float(res[1].strip("'"))
if (res[2] == "'0'"):
motor_toggle_pin.write(1)
l_motor_pin.write(l_value)
r_motor_pin.write(r_value)
time.sleep(.05)
print(f"forwards, L = {l_value} R = {r_value}")
else:
motor_toggle_pin.write(1)
l_motor_pin.write(l_value)
r_motor_pin.write(r_value)
time.sleep(.05)
print(f"backwards, L = {l_value} R = {r_value}")
strings = ""
#At this point, need to check first index (res[0]) to determine what the packet data is for
#e.g. drive commands, arm commands, etc.
def main():
#################################
# Server and Client Setup
##################################
# Using IPv4 with a TCP socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
serversocket.bind((HOST, 5000))
# become a server socket
serversocket.listen(5)
clientsocket, address = serversocket.accept()
t1 = threading.Thread(target = Client_Receive, args = (clientsocket,))
t1.start()
#Once server and client are connected, will try to connect to a serial device.
#The rest of the system will still work if this fails
#Will attempt to connect until successful
###################################
# Embedded System Setup
###################################
#
# global ser
# ser = serial.Serial()
# ser.baudrate = com_port_baudrate
# ser.port = com_port
# ser.open()
# ser.flushInput()
#
# print(ser)
#
#
#
# if(ser.is_open != True):
# # print("NA")
# ser.open()
#
# if(ser.is_open == True):
#
# print("Port open")
# #If succesful in connecting and opening the com port, start threads
#
#
#
# t2 = threading.Thread(target = Serial_Receive, args = ( ser,))
# t2.start()
#Else, try to connect again.
# data = ["XR", "220"]
#
# #Send controller data to embedded system
# t = threading.Thread(target = Serial_Send, args = (data, ser))
# t.start()
if __name__=="__main__":
main()