-
Notifications
You must be signed in to change notification settings - Fork 143
/
gz_sim.launch.py.in
165 lines (142 loc) · 6.28 KB
/
gz_sim.launch.py.in
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
# Copyright 2020 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Launch Gazebo Sim with command line arguments."""
import os
from os import environ
from ament_index_python.packages import get_package_share_directory
from catkin_pkg.package import InvalidPackage, PACKAGE_MANIFEST_FILENAME, parse_package
from ros2pkg.api import get_package_names
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, OpaqueFunction
from launch.actions import ExecuteProcess, Shutdown
from launch.substitutions import LaunchConfiguration
# Copied from https://github.com/ros-simulation/gazebo_ros_pkgs/blob/79fd94c6da76781a91499bc0f54b70560b90a9d2/gazebo_ros/scripts/gazebo_ros_paths.py
"""
Search for model, plugin and media paths exported by packages.
e.g. <export>
<gazebo_ros gazebo_model_path="${prefix}/../"/>
<gazebo_ros gazebo_media_path="${prefix}/../"/>
</export>
${prefix} is replaced by package's share directory in install.
Thus the required directory needs to be installed from CMakeLists.txt
e.g. install(DIRECTORY models
DESTINATION share/${PROJECT_NAME})
"""
class GazeboRosPaths:
@staticmethod
def get_paths():
gazebo_model_path = []
gazebo_plugin_path = []
gazebo_media_path = []
for package_name in get_package_names():
package_share_path = get_package_share_directory(package_name)
package_file_path = os.path.join(package_share_path, PACKAGE_MANIFEST_FILENAME)
if os.path.isfile(package_file_path):
try:
package = parse_package(package_file_path)
except InvalidPackage:
continue
for export in package.exports:
if export.tagname == 'gazebo_ros':
if 'gazebo_model_path' in export.attributes:
xml_path = export.attributes['gazebo_model_path']
xml_path = xml_path.replace('${prefix}', package_share_path)
gazebo_model_path.append(xml_path)
if 'plugin_path' in export.attributes:
xml_path = export.attributes['plugin_path']
xml_path = xml_path.replace('${prefix}', package_share_path)
gazebo_plugin_path.append(xml_path)
if 'gazebo_media_path' in export.attributes:
xml_path = export.attributes['gazebo_media_path']
xml_path = xml_path.replace('${prefix}', package_share_path)
gazebo_media_path.append(xml_path)
gazebo_model_path = os.pathsep.join(gazebo_model_path + gazebo_media_path)
gazebo_plugin_path = os.pathsep.join(gazebo_plugin_path)
return gazebo_model_path, gazebo_plugin_path
def launch_gz(context, *args, **kwargs):
model_paths, plugin_paths = GazeboRosPaths.get_paths()
env = {
"GZ_SIM_SYSTEM_PLUGIN_PATH": os.pathsep.join(
[
environ.get("GZ_SIM_SYSTEM_PLUGIN_PATH", default=""),
environ.get("LD_LIBRARY_PATH", default=""),
plugin_paths,
]
),
"GZ_SIM_RESOURCE_PATH": os.pathsep.join(
[
environ.get("GZ_SIM_RESOURCE_PATH", default=""),
model_paths,
]
),
}
gz_args = LaunchConfiguration('gz_args').perform(context)
gz_version = LaunchConfiguration('gz_version').perform(context)
ign_args = LaunchConfiguration('ign_args').perform(context)
ign_version = LaunchConfiguration('ign_version').perform(context)
debugger = LaunchConfiguration('debugger').perform(context)
on_exit_shutdown = LaunchConfiguration('on_exit_shutdown').perform(context)
if not len(gz_args) and len(ign_args):
print("ign_args is deprecated, migrate to gz_args!")
exec_args = ign_args
else:
exec_args = gz_args
if len(ign_version) or (ign_version == '' and int(gz_version) < 7):
exec = 'ruby $(which ign) gazebo'
if len(ign_version):
gz_version = ign_version
else:
exec = 'ruby $(which gz) sim'
if debugger != 'false':
debug_prefix = 'x-terminal-emulator -e gdb -ex run --args'
else:
debug_prefix = None
if on_exit_shutdown != 'false':
on_exit = Shutdown()
else:
on_exit = None
return [ExecuteProcess(
cmd=[exec, exec_args, '--force-version', gz_version],
name='gazebo',
output='screen',
additional_env=env,
shell=True,
prefix=debug_prefix,
on_exit=on_exit
)]
def generate_launch_description():
return LaunchDescription([
DeclareLaunchArgument('gz_args', default_value='',
description='Arguments to be passed to Gazebo Sim'),
# Gazebo Sim's major version
DeclareLaunchArgument('gz_version', default_value='@GZ_SIM_VER@',
description='Gazebo Sim\'s major version'),
# TODO(CH3): Deprecated. Remove on tock.
DeclareLaunchArgument(
'ign_args', default_value='',
description='Deprecated: Arguments to be passed to Gazebo Sim'
),
# Gazebo Sim's major version
DeclareLaunchArgument(
'ign_version', default_value='',
description='Deprecated: Gazebo Sim\'s major version'
),
DeclareLaunchArgument(
'debugger', default_value='false',
description='Run in Debugger'),
DeclareLaunchArgument(
'on_exit_shutdown', default_value='false',
description='Shutdown on gz-sim exit'),
OpaqueFunction(function = launch_gz),
])