forked from heechul/DeepPicar-v3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
input_stream.py
351 lines (305 loc) · 12.2 KB
/
input_stream.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
#!/usr/bin/python3
import os
import time
import atexit
import termios, fcntl, sys, os
import select
from multiprocessing import Process, Lock, Array, Value
# WEB INPUT
from http.server import BaseHTTPRequestHandler, HTTPServer, ThreadingHTTPServer
from functools import partial
import json
#import cv2
#import logging
#import params
#camera = __import__(params.camera)
class input_stream:
def __init__(self, speed=50):
self.buffer = ' '
self.direction = 0.
self.speed = speed
def read_inp():
return self.buffer, self.direction
def stop(self):
return
def __del__(self):
self.stop()
class input_kbd(input_stream):
def __init__(self, speed=50):
super().__init__(speed)
def init(self):
fd = sys.stdin.fileno()
# save old state
flags_save = fcntl.fcntl(fd, fcntl.F_GETFL)
attrs_save = termios.tcgetattr(fd)
# make raw - the way to do this comes from the termios(3) man page.
attrs = list(attrs_save) # copy the stored version to update
# iflag
attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK
| termios.ISTRIP | termios.INLCR | termios. IGNCR
| termios.ICRNL | termios.IXON )
# oflag
attrs[1] &= ~termios.OPOST
# cflag
attrs[2] &= ~(termios.CSIZE | termios. PARENB)
attrs[2] |= termios.CS8
# lflag
attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON
| termios.ISIG | termios.IEXTEN)
termios.tcsetattr(fd, termios.TCSANOW, attrs)
# turn off non-blocking
fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK)
# read a single keystroke
return (flags_save, attrs_save)
def deinit(self,state):
fd = sys.stdin.fileno()
# restore old state
termios.tcsetattr(fd, termios.TCSAFLUSH, state[1])
fcntl.fcntl(fd, fcntl.F_SETFL, state[0])
def read_inp(self):
state = self.init()
r, w, e = select.select([sys.stdin], [], [], 0.000)
self.buffer = ' '
for s in r:
if s == sys.stdin:
self.buffer = sys.stdin.read(1)
break
self.deinit(state)
if self.buffer == 'j': # left
self.direction = -1.0
elif self.buffer == 'l': # right
self.direction = 1.0
elif self.buffer == 'k': # center
self.direction = .0
return self.buffer, self.direction, self.speed
class input_gamepad(input_stream):
def __init__(self, speed=50):
self.shared_arr = Array('d', [0.]*8) # joystick pos and other buttons and finish state
#self.finish = Value('i', 1)
self.lock=Lock()
self.gamepad_process = Process(target=self.inputs_process, \
args=(), daemon=True )#args=(self.shared_arr, self.finish, lock,))
self.gamepad_process.start()
super().__init__(speed)
def inputs_process(self): #shr_gamepad_state, finish, lock):
import inputs
pads = inputs.devices.gamepads
if len(pads) == 0:
raise Exception("Couldn't find any Gamepads!")
#shr_gamepad_state, finish, lock = self.shared_arr, self.finish, self.lock
shr_gamepad_state, lock = self.shared_arr, self.lock
# Empty buffer
gamepad_events = inputs.get_gamepad()
print('Joystick is ready')
disable_joystick=False
while True: #finish.value != 0:
gamepad_events = inputs.get_gamepad()
if disable_joystick and time.time() - gamepad_disable_time > 0.3: # 300 ms
disable_joystick = False
lock.acquire()
for event in gamepad_events:
if not disable_joystick and event.ev_type == 'Absolute' and event.code == 'ABS_X':
val = int(event.state)
if val <= -256 or val >= 256: # calib, dead area
shr_gamepad_state[0] = val / 32768 #/ -32768 to 32767
elif event.ev_type == 'Absolute' and event.code == 'ABS_HAT0Y':
if int(event.state) == -1:
shr_gamepad_state[1]=1.
elif int(event.state) == 1:
shr_gamepad_state[2]=1.
elif event.ev_type == 'Absolute' and event.code == 'ABS_HAT0X':
if int(event.state) == -1:
shr_gamepad_state[0]= -1.
elif int(event.state) == 1:
shr_gamepad_state[0]= 1.
elif int(event.state) == 0:
shr_gamepad_state[0]= 0.
elif event.ev_type == 'Key' and event.code == 'BTN_NORTH' and int(event.state) == 1:
shr_gamepad_state[3]=1. # stop
elif event.ev_type == 'Key' and event.code == 'BTN_EAST' and int(event.state) == 1:
shr_gamepad_state[4]=1. # record
elif event.ev_type == 'Key' and event.code == 'BTN_START' and int(event.state) == 1:
shr_gamepad_state[5]=1.
elif event.ev_type == 'Key' and event.code == 'BTN_SELECT':
shr_gamepad_state[6]=1.
#finish.value=1
elif event.ev_type == 'Key' and event.code == 'BTN_WEST' and int(event.state) == 1:
shr_gamepad_state[7]=1.
elif event.ev_type == 'Key' and event.code == 'BTN_SOUTH' and int(event.state) == 1:
shr_gamepad_state[0]=0.
disable_joystick=True
gamepad_disable_time = time.time()
#if shr_gamepad_state[0] < 32768//2 and shr_gamepad_state[0] > -32768//2:
# shr_gamepad_state[0] = 0. # dead area
lock.release()
def read_inp(self):
self.buffer = ' '
self.lock.acquire()
if self.shared_arr[1] == 1.:
self.shared_arr[1] = 0.
self.buffer='a'
#print ("accel")
elif self.shared_arr[2] == 1.:
self.shared_arr[2] = 0.
self.buffer='z'
#print ("reverse")
elif self.shared_arr[3] == 1.:
self.shared_arr[3] = 0.
self.buffer='s'
#print ("stop")
elif self.shared_arr[4] == 1.:
self.shared_arr[4] = 0.
self.buffer='r'
#print ("toggle record mode")
elif self.shared_arr[5] == 1.:
self.shared_arr[5] = 0.
self.buffer='d'
#print ("toggle DNN mode")
elif self.shared_arr[6] == 1.:
self.shared_arr[6] = 0.
self.buffer='q'
#self.finish.value = 0
elif self.shared_arr[7] == 1.:
self.shared_arr[7] = 0.
self.buffer='t'
#print ("toggle video mode")
self.direction = self.shared_arr[0]
self.lock.release()
return self.buffer, self.direction, self.speed
def stop(self):
#self.finish.value = 0
self.gamepad_process.terminate()
class input_web_handler(BaseHTTPRequestHandler):
def __init__(self, shared_arr, lock, *args, **kwargs):
self.shared_arr = shared_arr
self.lock = lock
#cfg_cam_res = (320, 240)
#self.cfg_cam_fps = 20
#camera.init(res=cfg_cam_res, fps=self.cfg_cam_fps, threading=True)
super().__init__(*args, **kwargs)
def do_OPTIONS(self):
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST')
self.send_header("Access-Control-Allow-Headers", "X-Requested-With")
self.send_header("Access-Control-Allow-Headers", "Content-Type")
self.end_headers()
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
else:
self.send_error(404)
self.end_headers()
def do_POST(self):
if self.path == '/actuate':
self.send_response(301)
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
self.lock.acquire()
if data['params']['direction'] == 'left':
self.shared_arr[0] = -1.
elif data['params']['direction'] == 'center':
self.shared_arr[0] = 0.
elif data['params']['direction'] == 'right':
self.shared_arr[0] = 1.
elif data['params']['direction'] == 'forward':
self.shared_arr[1] = 1.
elif data['params']['direction'] == 'stop':
self.shared_arr[3] = 1.
elif data['params']['direction'] == 'reverse':
self.shared_arr[2] = 1.
self.shared_arr[8] = float(data['params']['speed'])
self.lock.release()
#actuator.set_speed(data['params']['speed'])
elif self.path == '/record':
self.send_response(301)
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
self.lock.acquire()
self.shared_arr[4] = 1.
self.lock.release()
elif self.path == '/dnn':
self.send_response(301)
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = json.loads(self.data_string)
self.lock.acquire()
self.shared_arr[5] = 1.
self.lock.release()
#if data['params']['action'] == 'start':
# use_dnn = True
#if data['params']['action'] == 'stop':
# use_dnn = False
# this takes the p
class input_web(input_stream):
def __init__(self, speed=50):
self.shared_arr = Array('d', [0.]*9) # joystick pos and other buttons and finish state
self.lock = Lock()
self.ws_process = Process(target=self.web_server_process, \
args=(), daemon=True )
self.ws_process.start()
super().__init__(speed)
def web_server_process(self):
address = ('', 8000) # backend - frontend connection
handler = partial(input_web_handler, self.shared_arr, self.lock)
#server = HTTPServer(address, handler)
server = ThreadingHTTPServer(address, handler)
try:
server.serve_forever(poll_interval=0.05) # until terminated
except KeyboardInterrupt:
pass
server.server_close()
def read_inp(self):
self.buffer = ' '
self.lock.acquire()
if self.shared_arr[1] == 1.:
self.shared_arr[1] = 0.
self.buffer='a'
#print ("accel")
elif self.shared_arr[2] == 1.:
self.shared_arr[2] = 0.
self.buffer='z'
#print ("reverse")
elif self.shared_arr[3] == 1.:
self.shared_arr[3] = 0.
self.buffer='s'
#print ("stop")
elif self.shared_arr[4] == 1.:
self.shared_arr[4] = 0.
self.buffer='r'
#print ("toggle record mode")
elif self.shared_arr[5] == 1.:
self.shared_arr[5] = 0.
self.buffer='d'
#print ("toggle DNN mode")
elif self.shared_arr[6] == 1.:
self.shared_arr[6] = 0.
self.buffer='q'
#self.finish.value = 0
elif self.shared_arr[7] == 1.:
self.shared_arr[7] = 0.
self.buffer='t'
#print ("toggle video mode")
self.direction = self.shared_arr[0]
self.speed = self.shared_arr[8]
self.lock.release()
return self.buffer, self.direction, self.speed
def stop(self):
self.ws_process.terminate()
class input_type:
KEYBOARD=0
GAMEPAD=1
WEB=2
def instantiate_inp_stream(inp_type, def_throttle):
inp_stream = None
if inp_type == input_type.KEYBOARD:
inp_stream= input_kbd(def_throttle)
elif inp_type == input_type.GAMEPAD:
inp_stream= input_gamepad(def_throttle)
elif inp_type == input_type.WEB:
inp_stream= input_web(def_throttle)
return inp_stream