-
Notifications
You must be signed in to change notification settings - Fork 17
/
main_jackal.py
executable file
·444 lines (349 loc) · 17.1 KB
/
main_jackal.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
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Qt GUI for launching the 3dnav 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
from pathlib import Path
# --------------------------------------------
# small lib for getting value in a config file
def lines_that_equal(line_to_match, fp):
return [line for line in fp if line == line_to_match]
def lines_that_contain(string, fp):
return [line for line in fp if string in line]
def lines_that_start_with(string, fp):
return [line for line in fp if line.startswith(string)]
def lines_that_end_with(string, fp):
return [line for line in fp if line.endswith(string)]
def get_config_var_value_from_config_file(filename, varname):
start_code="=\"" # ="
end_code="\"" # "
res = ''
with open(filename, "r") as fp:
for line in lines_that_contain(varname, fp):
start_index = line.find(start_code) + len(start_code)
end_index = start_index + line[start_index:].find(end_code)
res = line[start_index:end_index]
#print(f'line: {line}')
#print(f'res: {res}')
return res
# --------------------------------------------
# get the location of this file!
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__)))
SCRIPT_DIRECTORY = Path(__file__).resolve().parent
kDefaultGazeboWorldFolder = __location__ + '/jackal_ws/src/gazebo/jackal/jackal_3dmain/worlds'
print(f'kDefaultGazeboWorldFolder: {kDefaultGazeboWorldFolder}')
kDefaultGazeboWorldName = 'jackal_baylands.world'
kDefaultGazeboExplWorldName = 'jackal_race.world'
# --------------------------------------------
# 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 = 400
self.gazebo_worlds_folder = kDefaultGazeboWorldFolder
self.gazebo_world_name = kDefaultGazeboWorldName
self.is_gazebo_launched = 0
self.gazebo_mode = 0 # normal:1 launch Gazebo in normal mode, nheadless:0 launch VREP in headless mode (hidden)
self.exploration_world_name = kDefaultGazeboExplWorldName
self.exploration_worlds_folder = kDefaultGazeboWorldFolder
self.exploration_enable = True
self.exploration_auto_launch = True
self.teb_enable = 0
self.voxblox_enable = 0
self.elevation_mapping_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 - JACKAL')
self.mainLayout = QVBoxLayout()
self.setLayout(self.mainLayout)
#
self.btn_gazebo = QPushButton('Launch gazebo', self)
self.btn_gazebo.setToolTip('Test separately gazebo simulator.')
self.btn_gazebo_world = QPushButton('Select gazebo world', self)
self.btn_gazebo_world.setToolTip('Select a gazebo world. A default world is already set.')
#
self.btn_nav = QPushButton('Launch navigation', self)
self.btn_nav.setToolTip('Launch navigation system: segmentation, trav analysis, path planner, trajectory control')
#
# self.btn_teb_nav = QPushButton('Launch TEB navigation', self)
# self.btn_teb_nav.setToolTip('Launch reduced navigation system for TEB + TEB local planner')
#
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)
#
self.checkbtn_elevation_mapping_enable = QCheckBox(
'Use elevation mapping', self)
self.checkbtn_elevation_mapping_enable.setToolTip(
'Enable/disable elevation mapping.')
self.checkbtn_elevation_mapping_enable.toggled.connect(
self.slot_elevation_mapping_enable)
self.checkbtn_elevation_mapping_enable.setChecked(self.elevation_mapping_enable)
#
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.hbox_gazebo = QGroupBox()
#hbox_layout_gazebo = QHBoxLayout()
#self.hbox_gazebo.setLayout(hbox_layout_gazebo)
gazebo_mode_help_string = 'normal:1 launch Gazebo in normal mode \nheadless:0 launch VREP in headless mode (hidden) '
self.label_gazebo_mode = QLabel()
self.label_gazebo_mode.setText("Mode")
self.label_gazebo_mode.setToolTip(gazebo_mode_help_string)
self.cb_gazebo_mode = QComboBox()
self.cb_gazebo_mode.setToolTip(gazebo_mode_help_string)
self.cb_gazebo_mode.addItems(["headless", "normal"])
self.cb_gazebo_mode.setCurrentIndex(self.gazebo_mode)
self.cb_gazebo_mode.currentIndexChanged.connect(
self.slot_gazebo_mode_change)
#hbox_layout_gazebo.addWidget(self.label_gazebo_mode)
#hbox_layout_gazebo.addWidget(self.cb_gazebo_mode)
#
self.btn_save_map = QPushButton('Save nav map', self)
self.btn_save_map.setToolTip('Save the built navigation map')
#
self.btn_load_map = QPushButton('Load nav map', self)
self.btn_load_map.setToolTip('Load the built navigation map')
#
self.btn_kill = QPushButton('Kill all', self)
self.btn_kill.setToolTip('Kill all nodes of the system')
# set layouts
self.create_exploration_layout()
self.create_nav_layout()
self.create_gazebo_layout()
#self.mainLayout.addWidget(self.hbox_gazebo)
self.mainLayout.addWidget(self.btn_save_map)
self.mainLayout.addWidget(self.btn_load_map)
self.mainLayout.addWidget(self.btn_kill)
# connections
self.btn_gazebo.clicked.connect(self.slot_gazebo)
self.btn_gazebo_world.clicked.connect(self.slot_gazebo_world)
self.btn_nav.clicked.connect(self.slot_nav)
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_gazebo_layout(self):
self.horizontalGroupBox_gazebo = QGroupBox("Gazebo")
self.gridLayout_gazebo = QGridLayout()
#self.gridLayout_gazebo.setColumnStretch(1, 3)
#self.gridLayout_gazebo.setColumnStretch(2, 3)
self.gridLayout_gazebo.addWidget(self.btn_gazebo, 0, 0)
self.gridLayout_gazebo.addWidget(self.btn_gazebo_world, 0, 1)
self.gridLayout_gazebo.addWidget(self.label_gazebo_mode, 1, 0)
self.gridLayout_gazebo.addWidget(self.cb_gazebo_mode, 1, 1)
self.horizontalGroupBox_gazebo.setLayout(
self.gridLayout_gazebo)
self.mainLayout.addWidget(self.horizontalGroupBox_gazebo)
def create_nav_layout(self):
self.horizontalGroupBox_nav = QGroupBox("Navigation")
self.gridLayout_nav = QGridLayout()
#self.gridLayout_nav.setColumnStretch(1, 3)
#self.gridLayout_nav.setColumnStretch(2, 3)
self.gridLayout_nav.addWidget(self.btn_nav, 1, 0)
self.gridLayout_nav.addWidget(self.checkbtn_voxblox_enable, 2, 0)
self.gridLayout_nav.addWidget(self.checkbtn_teb_enable, 3, 0)
self.gridLayout_nav.addWidget(self.checkbtn_elevation_mapping_enable, 4, 0)
self.horizontalGroupBox_nav.setLayout(self.gridLayout_nav)
self.mainLayout.addWidget(self.horizontalGroupBox_nav)
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_gazebo(self):
print('launch gazebo')
cmd_envs = ''
cmd_envs += 'export LAUNCH_GAZEBO_MODE_ENV=' + str(self.gazebo_mode) + '; '
cmd = self.sourceCmd + cmd_envs + \
'rosrun jackal_3dmain jackal_ugvs.sh ' + self.gazebo_world_name
print(cmd)
run_command(cmd)
self.is_gazebo_launched = 1
def slot_gazebo_world(self):
print(f'select gazebo world in {self.gazebo_worlds_folder}')
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getOpenFileName(
self, "Select gazebo world", self.gazebo_worlds_folder, "Gazebo worlds (*.world)", options=options)
if fileName:
self.gazebo_world_name = os.path.basename(fileName) # getBaseFileNameNoExt(fileName)
print('world: ', self.gazebo_world_name)
self.slot_gazebo()
def slot_nav(self):
print('launch path planner system')
cmd_envs = ''
if self.is_gazebo_launched:
cmd_envs += 'export LAUNCH_GAZEBO_ENV=0; '
cmd_envs += 'export LAUNCH_GAZEBO_MODE_ENV=' + str(self.gazebo_mode) + '; '
if self.teb_enable:
cmd_envs += 'export ENABLE_TEB_LOCAL_PLANNER=1; '
if self.voxblox_enable:
cmd_envs += 'export ENABLE_VOXBLOX=1; '
if self.elevation_mapping_enable:
cmd_envs += 'export ENABLE_ELEVATION_MAPPING=1; '
cmd = self.sourceCmd + cmd_envs + 'rosrun jackal_3dnav 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_GAZEBO_MODE_ENV=' + str(self.gazebo_mode) + '; '
cmd = self.sourceCmd + cmd_envs + \
'rosrun jackal_3dexplorer sim_launcher_exploration ' + self.exploration_world_name
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.exploration_worlds_folder, "Gazebo worlds (*.world)", options=options)
if fileName:
self.exploration_world_name = getBaseFileNameNoExt(fileName)
print('world: ', self.exploration_world_name)
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_gazebo_mode_change(self, i):
print('change gazebo mode')
self.gazebo_mode = i
print('Items in the list are :')
for count in range(self.cb_gazebo_mode.count()):
print(self.cb_gazebo_mode.itemText(count))
print('Current index', self.gazebo_mode,
'selection changed ', self.cb_gazebo_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_elevation_mapping_enable(self):
self.elevation_mapping_enable = self.checkbtn_elevation_mapping_enable.isChecked()
print('elevation_mapping_enable: ', self.elevation_mapping_enable)
#
def slot_save_map(self):
print('save_map')
cmd = self.sourceCmd + 'rosrun path_planner save_map'
print(cmd)
run_command(cmd)
def slot_load_map(self):
print('load_map')
cmd = self.sourceCmd + 'rosrun path_planner load_map'
print(cmd)
run_command(cmd)
def slot_kill(self):
print('kill all nodes')
cmd = self.sourceCmd + 'rosrun jackal_3dnav kill_gazebo_sim'
print(cmd)
run_command(cmd)
self.is_gazebo_launched = 0
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())