-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
251 lines (205 loc) · 7.36 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
"""@package docstring
This module is the server for MaRe.
Example:
To run the WiFi Remote Car server, use the following command:
$ python server.py
Author:
Gary Huang<gh.nctu+code@gmail.com>
License:
3-clause BSD License
"""
import RPi.GPIO as GPIO
import threading
import select
import socket
import time
import sys
class myCar:
pwm_frequency = 200
def __init__(self, right1, right2, left1, left2):
GPIO.setmode(GPIO.BOARD)
# pinMode
GPIO.setup(right1, GPIO.OUT)
GPIO.setup(right2, GPIO.OUT)
GPIO.setup(left1, GPIO.OUT)
GPIO.setup(left2, GPIO.OUT)
# pwm output settings
self.right1_pwm = GPIO.PWM(right1, self.pwm_frequency)
self.right1_pwm.start(0.0)
self.left1_pwm = GPIO.PWM(left1, self.pwm_frequency)
self.left1_pwm.start(0.0)
self.right2 = right2
self.left2 = left2
GPIO.output(self.right2, False)
GPIO.output(self.left2, False)
def rightWheel(self, value):
if value >= 0:
self.right1_pwm.ChangeDutyCycle(value)
GPIO.output(self.right2, False)
else:
self.right1_pwm.ChangeDutyCycle(100.0 + value)
GPIO.output(self.right2, True)
def leftWheel(self, value):
if value >= 0:
self.left1_pwm.ChangeDutyCycle(value)
GPIO.output(self.left2, False)
else:
self.left1_pwm.ChangeDutyCycle(100.0 + value)
GPIO.output(self.left2, True)
def __del__(self):
self.left1_pwm.stop()
self.right1_pwm.stop()
# Use BOARD numbering system
car = myCar(12, 11, 32, 31)
class Server:
host = ''
port_stream = 8888
port_control = 8889
def __init__(self):
self.socket_stream = None
self.socket_control = None
self.threads = []
def open_socket(self):
try:
# Image stream
self.socket_stream = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.socket_stream.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR, 1)
self.socket_stream.bind((self.host, self.port_stream))
self.socket_stream.listen(5)
# Control cmd stream
self.socket_control = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.socket_control.setsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR, 1)
self.socket_control.bind((self.host, self.port_control))
self.socket_control.listen(5)
print("[INFO] Server is listening on " + str(self.port_stream) +
" and " + str(self.port_control))
except socket.error:
if self.socket_control:
self.socket_control.close()
self.socket_stream.close()
print("[ERROR] Could not open socket")
sys.exit(1)
def run(self):
self.open_socket()
try:
input = [self.socket_stream, self.socket_control, sys.stdin]
running = 1
while running:
input_ready, output_ready, except_ready = select.select(
input, [], [])
for ss in input_ready:
if ss == self.socket_stream:
streamAcpt = self.socket_stream.accept()
cc = StreamClient(streamAcpt[0], streamAcpt[1])
cc.start()
self.threads.append(cc)
elif ss == self.socket_control:
ctrlAcpt = self.socket_control.accept()
cc = ControlClient(ctrlAcpt[0], ctrlAcpt[1])
cc.start()
self.threads.append(cc)
elif ss == sys.stdin:
cmd = sys.stdin.readline().strip()
if cmd == 'show':
# ss = ShowFrame()
ss.start()
self.threads.append(s)
elif cmd == 'quit':
running = 0
elif cmd == '':
pass
else:
print('Command not found: ' + cmd)
except KeyboardInterrupt:
pass
self.socket_stream.shutdown(socket.SHUT_WR)
self.socket_control.shutdown(socket.SHUT_WR)
self.socket_stream.close()
self.socket_control.close()
for tt in self.threads:
tt.running = 0
while len(self.threads) > 0:
self.threads = [
t.join(1) for t in self.threads
if t is not None and t.isAlive()
]
print('Thank you')
print('If it hangs here, please press Ctrl+\\ to quit')
"""
class ShowFrame(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = 1
def run(self):
while self.running:
camera_lock.acquire()
# grabbed, frame = camera.read()
camera_lock.release()
# cv2.imshow("Monitor", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.1)
cv2.destroyWindow("Monitor")
"""
class StreamClient(threading.Thread):
def __init__(self, client, address):
threading.Thread.__init__(self)
self.client = client
self.address = address
self.running = 1
print("connected to " + str(self.address))
def run(self):
print("tried to run module StreamClient")
class ControlClient(threading.Thread):
def __init__(self, client, address):
threading.Thread.__init__(self)
self.client = client
self.address = address
self.running = 1
self.lock = threading.Lock()
print("connected to " + str(self.address))
def run(self):
try:
while self.running:
self.lock.acquire()
car.rightWheel(0.0)
car.leftWheel(0.0)
self.lock.release()
data = self.client.recv(64)
if not data:
self.client.close()
self.running = 0
cmd = ''
for char in data:
if char == '\n' or char == '\r':
break
cmd += char
self.lock.acquire()
if 'NCTUEEclass20htlu' in cmd:
cmd = cmd.split(',')[1:]
try:
car.rightWheel(float(cmd[0]))
car.leftWheel( float(cmd[1]))
except ValueError:
car.rightWheel(0)
car.leftWheel(0)
print("got cmd:" + str(cmd[0]) + str(cmd[1]))
time.sleep(0.3)
else:
print("cannot interpret cmd")
print(cmd)
self.lock.release()
except socket.error as e:
print("error", e)
pass
self.client.close()
print("connection to " + self.address + "closed")
if __name__ == '__main__':
s = Server()
s.run()
GPIO.cleanup()