-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.py
executable file
·433 lines (345 loc) · 16.9 KB
/
main.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# This file is part of the framework 3dpatrolling.
#
# Copyright (C) 2016-present Luigi Freda <luigifreda at gmail dot com>
# For more information see <https://github.com/luigifreda/3dmr>
#
# 3dpatrolling is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 3dpatrolling is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with 3DMR. If not, see <http://www.gnu.org/licenses/>.
"""
Qt GUI for launching the patrolling system or the path planning system.
Author: Luigi Freda
"""
import os
import sys
from PyQt5.QtWidgets import (
QWidget, QToolTip, QDesktopWidget, QPushButton, QApplication)
from PyQt5.QtWidgets import (
QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, QGridLayout, QFileDialog)
from PyQt5.QtWidgets import (QCheckBox, QComboBox, QLabel)
from PyQt5 import QtCore
from PyQt5.QtGui import QFont
import subprocess
# get the location of this file!
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__)))
# --------------------------------------------
# run-command utils
def run_command(command, debug=False):
""" runs command and returns the output."""
if debug:
print("$ {}".format(command))
#p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, executable='/bin/bash')
p = subprocess.Popen(command, shell=True, executable='/bin/bash')
# return iter(p.stdout.readline, b'')
def executeCommand(command, stderr=None, stdout=None, debug=False):
""" Raises an error when a command fails. """
if debug:
print("$ {}".format(command))
subprocess.check_call(command, stderr=stderr,
stdout=stdout, env=dict(os.environ), shell=True)
def executeCommands(commands, stderr=None, stdout=None, debug=False):
for command in commands:
executeCommand(command, stderr=stderr, stdout=stdout, debug=debug)
def executeCommandNoCheck(command, stderr=None, stdout=None, debug=False):
""" Does not raise an error when a command fails. """
if debug:
print("$ {}".format(command))
subprocess.call(command, stderr=stderr, stdout=stdout,
env=dict(os.environ), shell=True)
def executeCommandsNoCheck(commands, stderr=None, stdout=None, debug=False):
for command in commands:
executeCommandNoCheck(command, stderr=stderr,
stdout=stdout, debug=debug)
# --------------------------------------------
# Utils
def getBaseFileNameNoExt(filename):
base = os.path.basename(filename)
os.path.splitext(base)
return os.path.splitext(base)[0]
# --------------------------------------------
# Main Widget
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.cmdDebug = False
self.mainWidgetWidth = 400
self.mainWidgetHeight = 600
self.patrollingWorldName = ''
self.patrollingWorldsFolder = __location__ + \
'/patrolling_ws/src/multirobot/patrolling3d_sim/maps'
self.patrolling_enable = True
self.patrolling_interactive = False
self.explorationWorldName = ''
self.explorationWorldsFolder = __location__ + \
'/exploration_ws/src/ugv_3dexplorer/maps'
self.exploration_enable = True
self.exploration_auto_launch = True
self.vrep_mode = 1
self.teb_enable = 0
self.voxblox_enable = 0
self.sourceCmd = 'source source_all.bash; '
self.initUI()
self.center()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setGeometry(0, 0, self.mainWidgetWidth, self.mainWidgetHeight)
self.setWindowTitle('3DMR - TRADR')
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
#
self.btn_patrolling = QPushButton('Launch patrolling', self)
self.btn_patrolling.setToolTip('Launch the patrolling system.')
self.btn_patrolling_world = QPushButton('Select patrol world', self)
self.btn_patrolling_world.setToolTip(
'Select a patrolling world in one of the subfolder of maps. A default world is already set in the script sim_launcher_patrolling.')
self.checkbtn_patrolling_enable = QCheckBox('Enable patrolling', self)
self.checkbtn_patrolling_enable.setToolTip(
'Enable/disable the patrolling agent. Disabling can be useful for building and saving a map in a selected world (please, read the documentation).')
self.checkbtn_patrolling_enable.toggled.connect(
self.slot_patrolling_enable)
self.checkbtn_patrolling_enable.setChecked(self.patrolling_enable)
self.checkbtn_patrolling_interactive = QCheckBox(
'Build graph on start', self)
self.checkbtn_patrolling_interactive.setToolTip(
'Interactively build the patrolling graph on start and save it, instead of loading a pre-built graph.')
self.checkbtn_patrolling_interactive.toggled.connect(
self.slot_patrolling_interactive)
self.checkbtn_patrolling_interactive.setChecked(
self.patrolling_interactive)
#
self.btn_path_planner = QPushButton('Launch navigation', self)
self.btn_path_planner.setToolTip('Launch path planner system')
#
self.btn_exploration = QPushButton('Launch exploration', self)
self.btn_exploration.setToolTip('Launch the exploration system')
self.btn_exploration_world = QPushButton('Select exploration world', self)
self.btn_exploration_world.setToolTip('Select an exploration world in one of the subfolder of maps. A default world is already set in the script sim_launcher_exploration.')
self.checkbtn_exploration_enable = QCheckBox(
'Enable exploration', self)
self.checkbtn_exploration_enable.setToolTip(
'Enable/disable the exploration agent. Disabling can be useful for building and saving a map in a selected world (please, read the documentation).')
self.checkbtn_exploration_enable.toggled.connect(
self.slot_exploration_enable)
self.checkbtn_exploration_enable.setChecked(self.exploration_enable)
self.checkbtn_exploration_auto_launch = QCheckBox(
'Auto launch exploration', self)
self.checkbtn_exploration_auto_launch.setToolTip(
'Auto launch exploration.')
self.checkbtn_exploration_auto_launch.toggled.connect(
self.slot_exploration_auto_launch)
self.checkbtn_exploration_auto_launch.setChecked(
self.exploration_auto_launch)
#
self.checkbtn_voxblox_enable = QCheckBox(
'Use voxblox mapping', self)
self.checkbtn_voxblox_enable.setToolTip(
'Enable/disable voxblox for mapping instead of using octomap.')
self.checkbtn_voxblox_enable.toggled.connect(
self.slot_voxblox_enable)
self.checkbtn_voxblox_enable.setChecked(self.voxblox_enable)
#
self.checkbtn_teb_enable = QCheckBox(
'Use TEB local planner', self)
self.checkbtn_teb_enable.setToolTip(
'Enable/disable TEB as local planner and trajectory control.')
self.checkbtn_teb_enable.toggled.connect(
self.slot_teb_enable)
self.checkbtn_teb_enable.setChecked(self.teb_enable)
#
hbox_vrep = QGroupBox()
hboxlayout_vrep = QHBoxLayout()
hbox_vrep.setLayout(hboxlayout_vrep)
vrep_mode_help_string = 'normal:0 launch VREP in normal mode (you have to press the button play to start \nheadless:1 launch VREP in headless mode (hidden) and automatically start it \nnormal-autostart:2 launch VREP in normal mode and automatically start it (you will not be able to pause!)'
self.label_vrep_mode = QLabel()
self.label_vrep_mode.setText("V-REP mode")
self.label_vrep_mode.setToolTip(vrep_mode_help_string)
self.cb_vrep_mode = QComboBox()
self.cb_vrep_mode.setToolTip(vrep_mode_help_string)
self.cb_vrep_mode.addItems(["normal", "headless", "normal autostart"])
self.cb_vrep_mode.setCurrentIndex(self.vrep_mode)
self.cb_vrep_mode.currentIndexChanged.connect(
self.slot_vrep_mode_change)
hboxlayout_vrep.addWidget(self.label_vrep_mode)
hboxlayout_vrep.addWidget(self.cb_vrep_mode)
#
self.btn_save_map = QPushButton('Save map', self)
self.btn_save_map.setToolTip('Save the built map')
#
self.btn_load_map = QPushButton('Load map', self)
self.btn_load_map.setToolTip('Load the built map')
#
self.btn_kill = QPushButton('Kill all', self)
self.btn_kill.setToolTip('Kill all nodes of the system')
# set layouts
self.create_patrolling_layout()
self.create_exploration_layout()
self.create_nav_layout()
self.mainLayout.addWidget(hbox_vrep)
self.mainLayout.addWidget(self.btn_save_map)
self.mainLayout.addWidget(self.btn_load_map)
self.mainLayout.addWidget(self.btn_kill)
# connections
self.btn_patrolling.clicked.connect(self.slot_patrolling)
self.btn_patrolling_world.clicked.connect(self.slot_patrolling_world)
self.btn_path_planner.clicked.connect(self.slot_path_planner)
self.btn_exploration.clicked.connect(self.slot_exploration)
self.btn_exploration_world.clicked.connect(self.slot_exploration_world)
self.btn_save_map.clicked.connect(self.slot_save_map)
self.btn_load_map.clicked.connect(self.slot_load_map)
self.btn_kill.clicked.connect(self.slot_kill)
# show the widget
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def create_patrolling_layout(self):
self.horizontalGroupBox_patrolling = QGroupBox("Patrolling")
self.gridLayout_patrolling = QGridLayout()
#self.gridLayout_patrolling.setColumnStretch(1, 3)
#self.gridLayout_patrolling.setColumnStretch(2, 3)
self.gridLayout_patrolling.addWidget(self.btn_patrolling, 0, 0)
self.gridLayout_patrolling.addWidget(self.btn_patrolling_world, 0, 1)
self.gridLayout_patrolling.addWidget(
self.checkbtn_patrolling_enable, 1, 1)
self.gridLayout_patrolling.addWidget(
self.checkbtn_patrolling_interactive, 2, 1)
self.horizontalGroupBox_patrolling.setLayout(
self.gridLayout_patrolling)
self.mainLayout.addWidget(self.horizontalGroupBox_patrolling)
def create_nav_layout(self):
self.horizontalGroupBox_pp = QGroupBox("Navigation")
self.gridLayout_pp = QGridLayout()
#self.gridLayout_pp.setColumnStretch(1, 3)
#self.gridLayout_pp.setColumnStretch(2, 3)
self.gridLayout_pp.addWidget(self.btn_path_planner, 1, 0)
self.gridLayout_pp.addWidget(self.checkbtn_voxblox_enable, 2, 0)
self.gridLayout_pp.addWidget(self.checkbtn_teb_enable, 3, 0)
self.horizontalGroupBox_pp.setLayout(self.gridLayout_pp)
self.mainLayout.addWidget(self.horizontalGroupBox_pp)
def create_exploration_layout(self):
self.horizontalGroupBox_expl = QGroupBox("Exploration")
self.gridLayout_expl = QGridLayout()
#self.gridLayout_expl.setColumnStretch(1, 3)
#self.gridLayout_expl.setColumnStretch(2, 3)
self.gridLayout_expl.addWidget(self.btn_exploration, 0, 0)
self.gridLayout_expl.addWidget(self.btn_exploration_world, 0, 1)
self.gridLayout_expl.addWidget(self.checkbtn_exploration_enable, 1, 1)
self.gridLayout_expl.addWidget(self.checkbtn_exploration_auto_launch, 2, 1)
self.horizontalGroupBox_expl.setLayout(self.gridLayout_expl)
self.mainLayout.addWidget(self.horizontalGroupBox_expl)
# slots
def slot_patrolling(self):
print('launch patrolling')
cmd_envs = ''
if not self.patrolling_enable:
cmd_envs += 'export ENABLE_PATROLLING_ENV=0; ' # must be integer
if self.patrolling_interactive:
cmd_envs += 'export BUILD_PATROLLING_GRAPH_ON_START_ENV=false; ' # must be boolean
cmd_envs += 'export LAUNCH_VREP_MODE_ENV=' + str(self.vrep_mode) + '; '
cmd = self.sourceCmd + cmd_envs + \
'rosrun patrolling3d_sim sim_launcher_patrolling ' + self.patrollingWorldName
print(cmd)
run_command(cmd)
def slot_patrolling_world(self):
print('select patrolling world')
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self, "Select patrolling world", self.patrollingWorldsFolder, "V-REP scenes (*.ttt)", options=options)
if fileName:
self.patrollingWorldName = getBaseFileNameNoExt(fileName)
print('world: ', self.patrollingWorldName)
self.slot_patrolling()
def slot_patrolling_enable(self):
self.patrolling_enable = self.checkbtn_patrolling_enable.isChecked()
print('patrolling_enable: ', self.patrolling_enable)
def slot_patrolling_interactive(self):
self.patrolling_interactive = self.checkbtn_patrolling_interactive.isChecked()
print('patrolling_interactive: ', self.patrolling_interactive)
def slot_path_planner(self):
print('launch path planner system')
cmd_envs = ''
cmd_envs += 'export LAUNCH_VREP_MODE_ENV=' + str(self.vrep_mode) + '; '
if self.teb_enable:
cmd_envs += 'export ENABLE_TEB_LOCAL_PLANNER=1; '
if self.voxblox_enable:
cmd_envs += 'export ENABLE_VOXBLOX=1; '
cmd = self.sourceCmd + cmd_envs + 'rosrun path_planner sim_launcher_navigation'
print(cmd)
run_command(cmd)
def slot_exploration(self):
print('launch exploration')
cmd_envs = ''
if not self.exploration_enable:
cmd_envs += 'export ENABLE_EXPLORATION_ENV=0; ' # must be integer
if not self.exploration_auto_launch:
cmd_envs += 'export EXPL_AUTO_LAUNCH_ENV=0; ' # must be integer
cmd_envs += 'export LAUNCH_VREP_MODE_ENV=' + str(self.vrep_mode) + '; '
cmd = self.sourceCmd + cmd_envs + \
'rosrun ugv_3dexplorer sim_launcher_exploration ' + self.explorationWorldName
print(cmd)
run_command(cmd)
def slot_exploration_world(self):
print('select expl. world')
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self, "Select exploration world", self.explorationWorldsFolder, "V-REP scenes (*.ttt)", options=options)
if fileName:
self.explorationWorldName = getBaseFileNameNoExt(fileName)
print('world: ', self.explorationWorldName)
self.slot_exploration()
def slot_exploration_enable(self):
self.exploration_enable = self.checkbtn_exploration_enable.isChecked()
print('exploration_enable: ', self.exploration_enable)
def slot_exploration_auto_launch(self):
self.exploration_auto_launch = self.checkbtn_exploration_auto_launch.isChecked()
print('exploration_auto_launch: ', self.exploration_auto_launch)
def slot_vrep_mode_change(self, i):
print('change vrep mode')
self.vrep_mode = i
print('Items in the list are :')
for count in range(self.cb_vrep_mode.count()):
print(self.cb_vrep_mode.itemText(count))
print('Current index', self.vrep_mode,
'selection changed ', self.cb_vrep_mode.currentText())
def slot_voxblox_enable(self):
self.voxblox_enable = self.checkbtn_voxblox_enable.isChecked()
print('voxblox_enable: ', self.voxblox_enable)
def slot_teb_enable(self):
self.teb_enable = self.checkbtn_teb_enable.isChecked()
print('teb_enable: ', self.teb_enable)
def slot_save_map(self):
print('save_map')
cmd = self.sourceCmd + 'rosrun patrolling3d_sim save_map'
print(cmd)
run_command(cmd)
def slot_load_map(self):
print('load_map')
cmd = self.sourceCmd + 'rosrun patrolling3d_sim load_map'
print(cmd)
run_command(cmd)
def slot_kill(self):
print('kill all nodes')
cmd = self.sourceCmd + 'rosrun patrolling3d_sim kill_vrep_sim'
print(cmd)
run_command(cmd)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())