-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrust.py
372 lines (317 loc) · 11.7 KB
/
rust.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Starts the Python parts for the Rust control."""
import argparse
import os
import pwd
import signal
import socket
import subprocess
import sys
import threading
import time
from control.button import Button
from control.driver import Driver
from control.test.dummy_logger import DummyLogger
from control.test.dummy_telemetry import DummyTelemetry
from monitor.http_server import HttpServer
# pylint: disable=global-statement
# pylint: disable=broad-except
THREADS = []
POPEN = None
def terminate(signal_number, stack_frame): # pylint: disable=unused-argument
"""Terminates the program. Used when a signal is received."""
print(
'Received signal {signal_number}, quitting'.format(
signal_number=signal_number
)
)
if POPEN is not None and POPEN.poll() is None:
print('Killing image capture')
try:
POPEN.kill()
except OSError:
pass
for thread in THREADS:
thread.kill()
thread.join()
sys.exit(0)
class DriverListener(threading.Thread):
"""Receives commands on a socket from the controlling program to drive."""
def __init__(self, socket_file_name):
super(DriverListener, self).__init__()
self.name = self.__class__.__name__
self._socket_file_name = socket_file_name
self._run = True
self._connected = False
self._connection = None
dummy_logger = DummyLogger()
dummy_telemetry = DummyTelemetry(dummy_logger, (100, 100))
self._driver = Driver(dummy_telemetry, dummy_logger)
def run(self):
"""Runs in a thread. Waits for clients to connects then receives and
handles drive messages.
"""
try:
print('DriverListener waiting for commands')
while self._run:
try:
self.run_socket()
except Exception as exc:
print('Error in DriverListener: {}'.format(exc))
return
except Exception as exc:
print('DriverListener failed with exception {}'.format(exc))
def run_socket(self):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
os.unlink(self._socket_file_name)
except Exception:
pass
sock.bind(self._socket_file_name)
pi = pwd.getpwnam('pi')
os.chown(self._socket_file_name, pi.pw_uid, pi.pw_gid)
sock.listen(1)
sock.settimeout(1)
try:
self.wait_for_connections(sock)
except socket.timeout:
return
except socket.error as exc:
print('DriverListener error with socket: {}'.format(exc))
if exc.errno == 32: # Broken pipe
print('DriverListener closing socket')
sock.shutdown(socket.SHUT_RDWR)
sock.close()
return
elif exc.errno == 98: # Address already in use
print('DriverListener quitting waiting for connections')
return
else:
print('Unknown error')
return
def wait_for_connections(self, sock):
while self._run:
self._connection, _ = sock.accept()
self._connected = True
print('DriverListener client connected')
# Now we're connected, so just wait until someone
# calls handle_message
while self._run:
try:
command = self._connection.recv(4096)
throttle, steering = [float(i) for i in command.split(' ')]
print('DriverListener driving {} {}'.format(throttle, steering))
self._driver.drive(throttle, steering)
except socket.timeout:
continue
def kill(self):
"""Stops the thread."""
self._run = False
class CommandForwarder(threading.Thread):
"""Forwards commands to clients connected to a socket."""
VALID_COMMANDS = {'calibrate-compass', 'line-up', 'start', 'stop'}
def __init__(self, socket_file_name):
super(CommandForwarder, self).__init__()
self._socket_file_name = socket_file_name
self._run = True
self._connected = False
self._connection = None
def run(self):
"""Runs in a thread. Waits for clients to connects then forwards
command messages to them.
"""
try:
while self._run:
try:
self.run_socket()
except Exception as exc:
print('Error in CommandForwarder: {}'.format(exc))
return
except Exception as exc:
print('CommandForwarder failed with exception {}'.format(exc))
def run_socket(self):
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
os.unlink(self._socket_file_name)
except Exception:
pass
sock.bind(self._socket_file_name)
pi = pwd.getpwnam('pi')
os.chown(self._socket_file_name, pi.pw_uid, pi.pw_gid)
sock.listen(1)
sock.settimeout(1)
try:
self.wait_for_connections(sock)
except socket.error as exc:
print('CommandForwarder error with socket: {}'.format(exc))
self._connected = False
if exc.errno == 32: # Broken pipe
print('CommandForwarder closing socket')
sock.shutdown(socket.SHUT_RDWR)
sock.close()
return
elif exc.errno == 98: # Address already in use
print('CommandForwarder quitting waiting for connections')
return
else:
return
def wait_for_connections(self, sock):
while self._run:
try:
self._connection, _ = sock.accept()
self._connected = True
print('CommandForwarder command client connected')
# Now we're connected, so just wait until someone
# calls handle_message
while self._run:
time.sleep(1)
# Test to see if we're still connected
# TODO: If nobody's connected, this causes the program to
# quit. Goes directly to quit, does not throw an exception,
# does not collect $200.
self._connection.send(b'')
return
except socket.timeout:
continue
def kill(self):
"""Stops the thread."""
self._run = False
def handle_message(self, message):
"""Forwards command messages, e.g. 'start' or 'stop'."""
if not self._connected:
print('CommandForwarder received message "{}" but nobody is connected', message)
return
if 'command' not in message:
print('CommandForwarder no command in command message')
return
if message['command'] not in self.VALID_COMMANDS:
print(
'CommandForwarder unknown command: "{command}"'.format(
command=message['command']
)
)
return
try:
if message['command'] == 'line-up':
# TODO: Right now, line-up just means start recording the camera
pass
else:
self._connection.sendall(message['command'].encode('utf-8'))
if message['command'] == 'stop':
# TODO: We also want to stop the camera when someone clicks stop
pass
except Exception as exc:
print(
'Unable to forward command "{}": {}'.format(
message['command'],
exc
)
)
class StdinReader(threading.Thread):
"""Sends commands read from stdin."""
def __init__(self, command):
super(StdinReader, self).__init__()
self._command = command
self._run = True
def run(self):
try:
print('StdinReader waiting for commands')
while self._run:
command = sys.stdin.readline()
if command == '':
continue
else:
self._command.handle_message({'command': command})
except Exception as exc:
print('StdinReader failed with exception {}'.format(exc))
def kill(self):
"""Stops the thread."""
self._run = False
def start_threads(stdin, control_socket, driver_socket):
"""Runs everything."""
dummy_logger = DummyLogger()
forwarder = CommandForwarder(control_socket)
button = Button(forwarder, dummy_logger)
dummy_telemetry = DummyTelemetry(dummy_logger, (50.0, 50.0))
http_server = HttpServer(
forwarder,
dummy_telemetry,
dummy_logger,
port=8080,
address='0.0.0.0'
)
driver = DriverListener(driver_socket)
global THREADS
THREADS = [forwarder, button, driver, http_server]
if stdin:
reader = StdinReader(forwarder)
THREADS.append(reader)
for thread in THREADS:
thread.start()
print('Started all threads')
# Once forwarder quits, we can kill everything else
forwarder.join()
print('Forwarder thread exited, killing all threads')
for thread in THREADS:
thread.kill()
thread.join()
def make_parser():
"""Builds and returns an argument parser."""
parser = argparse.ArgumentParser(
description='Command and control software for the Sparkfun AVC.'
)
parser.add_argument(
'--control-socket',
dest='control_socket',
help='The Unix domain socket to send control commands on.',
default='/tmp/command-socket',
type=str,
)
parser.add_argument(
'--driver-socket',
dest='driver_socket',
help='The Unix domain socket to listen for drive commands on.',
default='/tmp/driver-socket',
type=str,
)
parser.add_argument(
'-i',
'--stdin',
dest='stdin',
help='Read control commands from stdin as well as from other sources.',
action='store_true'
)
parser.add_argument(
'-w',
'--watchdog',
dest='watchdog',
help='Run in a watchdog form with restart.',
action='store_true'
)
return parser
def main():
"""Sets up logging, signal handling, etc. and starts the threads."""
signal.signal(signal.SIGINT, terminate)
parser = make_parser()
args = parser.parse_args()
# I was getting an error where calling connection.send on a closed socket
# was immediately exiting the program. So, use a watchdog and fork a
# subprocess to restart instead.
if args.watchdog:
raw_args = ['python'] + [
arg for arg in sys.argv
if arg != '-w' and arg != '--watchdog'
]
for _ in range(10):
print('Forking subprocess for watchdog')
process = subprocess.Popen(raw_args)
exit_code = process.wait()
print('Child process exited with code {}'.format(exit_code))
return
# TODO: Use the Raspberry Pi camera module Python module to save video
print('Calling start_threads')
start_threads(args.stdin, args.control_socket, args.driver_socket)
print('Done calling start_threads')
if __name__ == '__main__':
main()