This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robo.py
320 lines (297 loc) · 9.31 KB
/
robo.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
#Copyright (c) 2020 Derek Frombach
import time
import socket
import subprocess
connectmyrio=True
oip='0.0.0.0' #Don't Change This
remhost='192.168.0.2'
remport=8082
port=8081 #Hosting Port, Don't Change This
buff=1400 #Also Don't Change This
tout=0.5
giveup=2.0
fps=1/30
#Function Call Speedups
tc=time.perf_counter
tt=time.time
ts=time.sleep
ste=socket.timeout
rdwr=socket.SHUT_RDWR
se=socket.error
def secure_connect(ip,port):
#Initalisation of TCP Socket Server
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP/IP Socket
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #Unbind when Done
s.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) #Zero-Latency TCP
s.bind((ip,port)) #Starting Connect Back Server
s.listen(1) #Listen for Connections
ip=False
running=False
while True:
if not running:
print('Accepting Clients')
running=True
try:
s.settimeout(0.1)
conn,addr=s.accept()
except KeyboardInterrupt:
exit()
except ste:
try:
ts(0.1)
continue
except KeyboardInterrupt:
exit()
running=False
s.settimeout(None)
ct=conn.settimeout
ct(tout)
#Header Communication with Client
cs=conn.sendall #Connection Speedup
ts(tout)
#Client Timeout Handler
try:
data=conn.recv(buff)
except ste:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
continue
except se:
conn.shutdown(rdwr)
conn.close()
continue
if len(data)>1:
dstr=data.decode('utf-8')
data=dstr.find('Passwd: ')
if data>=0:
try:
q=int(dstr[data+8:data+12])
except:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
continue
try:
r=int(input('Enter the Key: '))
except:
print('Invalid Input')
conn.shutdown(rdwr)
conn.close()
continue
if q==r:
print('Keys Match!')
try:
cs(('Match: '+str(r)).encode('utf-8'))
except:
print('Early Disconnect!')
continue
ip=addr[0]
try:
conn.shutdown(rdwr)
conn.close()
except:
pass
break
else:
print("KEYS DO NOT MATCH!")
conn.shutdown(rdwr)
conn.close()
continue
else:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
continue
else:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
continue
#Recieving Pairing Request was successful, now off to pairing land
ts(2.0)
s.close()
return ip
def start_video():
proc=subprocess.Popen(['gst-launch-1.0 -v v4l2src ! \'video/x-raw,width=640,height=480,framerate=15/1,format=YUY2\' ! nvjpegenc idct-method=1 quality=50 ! queue max-size-bytes=5242880 max-size-buffers=3 min-threshold-buffers=1 leaky=2 ! multipartmux ! tcpserversink buffers-max=3 sync=false host=0.0.0.0 port=8082 recover-policy=1 timeout=5000000000'],shell=True)
return proc
def stop_video(proc):
proc.terminate()
proc.kill()
proc.communicate()
#NOW THE ACTUAL CODE
while True:
ip=secure_connect(oip,port)
#Initalisation of Camera
proc=start_video()
#Initalisation of Address
print("READY!")
addr=(ip,port)
first=True
redo=False
giveat=tc()
#Continuity Loop
while True:
conn=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP/IP Socket
conn.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) #Zero-Latency TCP
ct=conn.settimeout
if not redo:
#Do Not Change Anything Below, All of this is Security
print("Disconnected")
print(tt())
print(addr)
#Give Up Handler
if not first:
if tc()-giveat>=giveup:
print('Giving Up')
stop_video(proc)
break
ct(0.1)
#Ctrl-C Handler
try:
conn.connect(addr)
except KeyboardInterrupt:
if not redo:
stop_video(proc)
break
except ste:
conn.close()
ts(0.1)
redo=True
continue
except se:
conn.close()
ts(0.1)
continue
ct(tout)
first=False
redo=False
print("Connected")
print(tt())
print(addr)
ct(tout)
#Do Not Change Anything Above
#Header Communication with Client
cs=conn.sendall #Connection Speedup
cv=conn.recv #Connection Speed
#Client Timeout Handler
try:
data=cv(buff)
except ste:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
continue
except se:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
continue
#Server Connection Failure Handler
try:
cs(b'Hello!') #Sending Hello
except ste:
print("BOT!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
continue
except se:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
continue
if connectmyrio:
#Connecting to Serial Here
#Initalisation of TCP Socket Server
rs=socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP/IP Socket
rs.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) #Zero-Latency TCP
#connect with robot
rs.settimeout(tout)
rs.connect((remhost,remport))
rss=rs.sendall
#Capture Loop
while True:
a=tc()#Start Time for Frame Limiting
#Client Timeout Handler
try:
data=cv(buff)
except ste:
print("LATENCY!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
except se:
print("SOCKET ERROR!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
except KeyboardInterrupt:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
#Data Interpreter
if len(data)>=12:
if connectmyrio:
try:
rss(data)
except KeyboardInterrupt:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
except ste: #Handles Robot Comms Timeout
print("ROBOT LATENCY!")
conn.shutdown(rdwr)
conn.close()
stop_video(proc)
exit()
except se: #Handles Robot Comms Error
print("ROBOT SOCKET ERROR!")
conn.shutdown(rdwr)
conn.close()
stop_video(proc)
exit()
pass
#Sending Contents to Client
try:
cs(b'Ping!')
except ste:
print("LATENCY!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
except se:
print("SOCKET ERROR!")
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
except KeyboardInterrupt:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
#frame rate limiter
b=tc() #End Time for Frame Limiting
c=b-a
t=fps #seconds per frame
if t-c>0.0:
try:
ts(t-c) #delay remaining seconds
except KeyboardInterrupt:
conn.shutdown(rdwr)
conn.close()
giveat=tc()
break
elif c>t:
#pass
print(c)
#End of Program (when Ctrl-C)
conn.close()
stop_video(proc)