-
Notifications
You must be signed in to change notification settings - Fork 62
/
simulation.launch.py
180 lines (164 loc) · 6.26 KB
/
simulation.launch.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
# Copyright 2024 Husarion sp. z o.o.
#
# 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.
from launch import LaunchDescription
from launch.actions import (
DeclareLaunchArgument,
GroupAction,
IncludeLaunchDescription,
LogInfo,
OpaqueFunction,
)
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import (
EnvironmentVariable,
LaunchConfiguration,
PathJoinSubstitution,
TextSubstitution,
)
from launch_ros.actions import SetParameter
from launch_ros.substitutions import FindPackageShare
from nav2_common.launch import ParseMultiRobotPose
def launch_setup(context, *args, **kwargs):
namespace = LaunchConfiguration("namespace").perform(context)
mecanum = LaunchConfiguration("mecanum").perform(context)
world = LaunchConfiguration("world").perform(context)
headless = LaunchConfiguration("headless").perform(context)
use_gpu = LaunchConfiguration("use_gpu").perform(context)
x = LaunchConfiguration("x", default="0.0").perform(context)
y = LaunchConfiguration("y", default="2.0").perform(context)
z = LaunchConfiguration("z", default="0.0").perform(context)
roll = LaunchConfiguration("roll", default="0.0").perform(context)
pitch = LaunchConfiguration("pitch", default="0.0").perform(context)
yaw = LaunchConfiguration("yaw", default="0.0").perform(context)
gz_args = f"--headless-rendering -s -v 4 -r {world}" if eval(headless) else f"-r {world}"
gz_sim = IncludeLaunchDescription(
PythonLaunchDescriptionSource(
PathJoinSubstitution(
[
FindPackageShare("ros_gz_sim"),
"launch",
"gz_sim.launch.py",
]
)
),
launch_arguments={
"gz_args": gz_args,
"on_exit_shutdown": "True",
}.items(),
)
robots_list = ParseMultiRobotPose("robots").value()
if len(robots_list) == 0:
robots_list = {
namespace: {
"x": x,
"y": y,
"z": z,
"roll": roll,
"pitch": pitch,
"yaw": yaw,
}
}
spawn_group = []
for robot_name in robots_list:
init_pose = robots_list[robot_name]
group = GroupAction(
[
LogInfo(
msg=[
"Launching namespace=",
robot_name,
" init_pose=",
str(init_pose),
]
),
IncludeLaunchDescription(
PythonLaunchDescriptionSource(
PathJoinSubstitution(
[
FindPackageShare("rosbot_gazebo"),
"launch",
"spawn.launch.py",
]
)
),
launch_arguments={
"mecanum": mecanum,
"use_sim": "True",
"use_gpu": use_gpu,
"simulation_engine": "ignition-gazebo",
"namespace": TextSubstitution(text=robot_name),
"x": TextSubstitution(text=str(init_pose["x"])),
"y": TextSubstitution(text=str(init_pose["y"])),
"z": TextSubstitution(text=str(init_pose["z"])),
"roll": TextSubstitution(text=str(init_pose["roll"])),
"pitch": TextSubstitution(text=str(init_pose["pitch"])),
"yaw": TextSubstitution(text=str(init_pose["yaw"])),
}.items(),
),
]
)
spawn_group.append(group)
return [gz_sim, *spawn_group]
def generate_launch_description():
declare_namespace_arg = DeclareLaunchArgument(
"namespace",
default_value=EnvironmentVariable("ROBOT_NAMESPACE", default_value=""),
description="Namespace for all topics and tfs",
)
declare_mecanum_arg = DeclareLaunchArgument(
"mecanum",
default_value="False",
description=(
"Whether to use mecanum drive controller (otherwise diff drive controller is used)"
),
)
world_package = FindPackageShare("husarion_gz_worlds")
world_file = PathJoinSubstitution([world_package, "worlds", "husarion_world.sdf"])
declare_world_arg = DeclareLaunchArgument(
"world", default_value=world_file, description="SDF world file"
)
declare_headless_arg = DeclareLaunchArgument(
"headless",
default_value="False",
description="Run Gazebo Ignition in the headless mode",
choices=["True", "False"],
)
declare_use_gpu_arg = DeclareLaunchArgument(
"use_gpu",
default_value="True",
description="Whether GPU acceleration is used",
)
declare_robots_arg = DeclareLaunchArgument(
"robots",
default_value="",
description=(
"Spawning multiple robots at positions with yaw orientations e. g. robots:='robot1={x:"
" 0.0, y: -1.0}; robot2={x: 1.0, y: -1.0}; robot3={x: 2.0, y: -1.0}; robot4={x: 3.0,"
" y: -1.0}'"
),
)
return LaunchDescription(
[
declare_namespace_arg,
declare_mecanum_arg,
declare_world_arg,
declare_headless_arg,
declare_use_gpu_arg,
declare_robots_arg,
# Sets use_sim_time for all nodes started below
# (doesn't work for nodes started from ignition gazebo)
SetParameter(name="use_sim_time", value=True),
OpaqueFunction(function=launch_setup),
]
)