-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathig2u_auto.py
309 lines (273 loc) · 9.86 KB
/
ig2u_auto.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
import curses
import os
import socketio
import datetime
import time
import threading
import time
import sys
from threading import Thread
import re
import traceback
import subprocess
from subprocess import Popen, PIPE, STDOUT
import select
import signal
import psutil
import board
from CollisionAvoidanceManager import CollisionAvoidanceManager
dit=None
ads = None
PAUSE_START_VIDEO = 0.0
pro=None
inRasbery = True
sio = socketio.Client()
start_time = time.time()
connected = False
last_response = None
ms = 0
video = None
g_speed_command = 0
g_mot_command = "stop"
g_coav = None
#убиваем процесс по имени (вызов из stop_video)
def kill_name():
for proc in psutil.process_iter():
if proc.name() == 'gst-launch-1.0':
proc.kill()
#старт стереовидео (вызов из test_broadcast_message)
def start_videoS():
print("before start videotranslation")
global pro
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('стерео: ', pro)
pro = subprocess.Popen(['/home/pi/to_janusIg2u.sh'], shell=True, preexec_fn=os.setsid)
print("after start videotranslation")
#старт моновидео (вызов из test_broadcast_message)
def start_video():
global pro
print("before start videotranslation")
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('моно: ', pro)
pro = subprocess.Popen(['/home/pi/to_janusIg2u_crop.sh'], shell=True, preexec_fn=os.setsid)
# return str(pro.communicate()[0])
print("after start videotranslation")
#старт стереовидео со звуком (вызов из test_broadcast_message)
def start_videoSZ():
print("before start videotranslation")
global pro
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('стерео: ', pro)
pro = subprocess.Popen(['/home/pi/to_janusIg2u_A.sh'], shell=True, preexec_fn=os.setsid)
print("after start videotranslation")
#старт моновидео со звуком(вызов из test_broadcast_message)
def start_videoZ():
global pro
print("before start videotranslation")
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('моно: ', pro)
pro = subprocess.Popen(['/home/pi/to_janusIg2u_crop_A.sh'], shell=True, preexec_fn=os.setsid)
# return str(pro.communicate()[0])
print("after start videotranslation")
#старт камеры заднего вида (вызов из test_broadcast_message)
def start_videoZad():
global pro
print("before start videotranslation")
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('моно: ', pro)
pro = subprocess.Popen(['/home/pi/to_janusIg2u_zad.sh'], shell=True, preexec_fn=os.setsid)
# return str(pro.communicate()[0])
print("after start videotranslation")
#старт трансляции звука(вызов из test_broadcast_message)
def start_noise():
global pro
print("before start videotranslation")
if pro != None:
stop_video()
# os.killpg(os.getpgid(pro.pid), signal.SIGTERM) # Send the signal to all the process groups
time.sleep(1)
print('моно: ', pro)
pro = subprocess.Popen(['/home/pi/audIg2u.sh'], shell=True, preexec_fn=os.setsid)
# return str(pro.communicate()[0])
print("after start videotranslation")
#останавливаем стрим видео с робота на сервер (вызов из test_broadcast_message)
def stop_video():
global pro
print("before stop videotranslation")
try:
print(pro.pid)
kill_name()
time.sleep(3)
pro=None
print('kill', pro)
except Exception as e:
pass
#перевод углов в команды, а - углы, b - сигналы, s - угол (вызов из test_broadcast_message)
def map_range(a,b,s):
(a1,a2),(b1,b2)=a,b
return b1 + ((s-a1)*(b2-b1))/(a2-a1)
@sio.event(namespace='/test') #действия при запуске
def connect():
global dit
global ads
global connected
# global start_time
global ms
last_response = datetime.datetime.utcnow()
connected = True
print('connection established')
def handle_motion_command_i2c(motComand, speedCommand):
global g_coav
if g_coav == None:
g_coav = CollisionAvoidanceManager()
print("handle_motion_command_i2c {0} {1}\r\n".format(motComand, speedCommand))
g_coav.updateCmd(motComand, speedCommand)
#ОБРАБОТКА управляющих сообщений от сервера (запускается после получения сообщения от сервера)
@sio.on('my_responseIO', namespace='/test')
def test_broadcast_message(data):
print('message received with ', data)
global last_response
global dit
global video
last_response = datetime.datetime.utcnow()
params = data.split("-") # разбиваем строку сообщения от сервера
if 1==1:
mode = params[0]
if mode == "ser":
#===== запускаем и глушим видео в засисимости от цифры в сообщении от сервера
if len(params)>3:
video=int(params[3])
if video!=None:
print('video = ', video)
if(video==2):
threading.Timer(PAUSE_START_VIDEO, start_videoS).start()
start_videoS()
pass
elif(video==1):
threading.Timer(PAUSE_START_VIDEO, start_video).start()
start_video()
pass
elif(video==0):
threading.Timer(PAUSE_START_VIDEO, stop_video).start()
stop_video()
pass
elif(video==3):
threading.Timer(PAUSE_START_VIDEO, start_videoZ).start()
start_videoZ()
pass
elif(video==4):
threading.Timer(PAUSE_START_VIDEO, start_videoSZ).start()
start_videoSZ()
pass
elif(video==5):
threading.Timer(PAUSE_START_VIDEO, start_noise).start()
start_noise()
elif(video==6):
threading.Timer(PAUSE_START_VIDEO, start_videoZad).start()
start_videoZad()
pass
if mode == "mot":
try:
if inRasbery:
g_mot_command = params[1]
g_speed_command = int(params[2])
#handle_motion_command_gpio_pwm(g_mot_command, g_speed_command)
handle_motion_command_i2c(g_mot_command, g_speed_command)
except Exception as e:
pass
@sio.event(namespace='/test')
def disconnect():
print('disconnected from server')
global dit
global connected
global video
connected = False
if dit!=None:
dit.set_servo_pulsewidth(18,0) #отключаем питание моторов
#dit.set_servo_pulsewidth(27,0)
dit.stop() #создаем объект для работы с моторами
dit=None
print('dit stop 98', dit)
if video!=0:
stop_video()
def ping_pongStart():
global start_time
start_time = datetime.datetime.utcnow()
sio.emit('my_pingR', {u'data': u''}, namespace='/test')
#ответ на событие my_pongR с сервера - расчет задержки
@sio.on('my_pongR', namespace='/test')
def latencyR(data):
global start_time
# global ping_pong_time
global ms
ping_pong_time = (str)((datetime.datetime.utcnow() - start_time)/2)
# ping_pong_time = (datetime.datetime.utcnow() - start_time)/2
secondS = ping_pong_time.split(':')
ms = round(float(secondS[2])*1000, 2)
# print('задержка = ', ms)
return ms
#передача данных на сервер
def ping_pongR(data):
global start_time
start_time = datetime.datetime.utcnow()
sio.emit('my_pingR', {u'data': data}, namespace='/test')
#передача данных на сервер
def ping_pongV(data):
sio.emit('my_pingV', {u'data': data}, namespace='/test')
def main_remote():
while True:
try:
sio.connect('https://evgenium.fvds.ru:5000') #адрес для коннекта
sio.wait()
except Exception as e:
print(e)
def handle_keypress(key):
global g_speed_command
global g_mot_command
CmdDic = {
curses.KEY_UP : "top",
curses.KEY_DOWN : "down",
curses.KEY_LEFT : "left",
curses.KEY_RIGHT : "right",
ord(' ') : "stop"
}
SpeedDic = {
ord('+') : lambda cmdVal : min(cmdVal + 10, 100),
ord('-') : lambda cmdVal : max(cmdVal - 10, 0)
}
speedFunc = SpeedDic.get(key)
if speedFunc != None:
g_speed_command = speedFunc(g_speed_command)
motCmd = CmdDic.get(key)
if motCmd != None:
g_mot_command = motCmd
#handle_motion_command_gpio_pwm(g_mot_command, g_speed_command)
handle_motion_command_i2c(g_mot_command, g_speed_command)
def main_console(win):
win.timeout(10) #msec
while True:
try:
key = win.getch()
if key == ord('q'):
quit()
if key != -1:
handle_keypress(key)
except Exception as e:
print ("Exception '" + str(e) + "'\r\n")
#curses.wrapper(main_console)
main_remote()