Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
assisted_teleop_example_launch.py
1 # Copyright (c) 2021 Samsung Research America
2 # Copyright (c) 2022 Joshua Wallace
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 from pathlib import Path
18 import tempfile
19 
20 from ament_index_python.packages import get_package_share_directory
21 from launch import LaunchDescription
22 from launch.actions import (AppendEnvironmentVariable, DeclareLaunchArgument, ExecuteProcess,
23  IncludeLaunchDescription, OpaqueFunction, RegisterEventHandler)
24 from launch.conditions import IfCondition
25 from launch.event_handlers import OnShutdown
26 from launch.launch_description_sources import PythonLaunchDescriptionSource
27 from launch.substitutions import Command, LaunchConfiguration, PythonExpression
28 from launch_ros.actions import Node
29 
30 
31 def generate_launch_description() -> LaunchDescription:
32  nav2_bringup_dir = get_package_share_directory('nav2_bringup')
33  sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
34  desc_dir = get_package_share_directory('nav2_minimal_tb4_description')
35 
36  robot_sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro')
37  world = os.path.join(sim_dir, 'worlds', 'depot.sdf')
38  map_yaml_file = os.path.join(nav2_bringup_dir, 'maps', 'depot.yaml')
39 
40  # Launch configuration variables
41  use_rviz = LaunchConfiguration('use_rviz')
42  headless = LaunchConfiguration('headless')
43 
44  # Declare the launch arguments
45  declare_use_rviz_cmd = DeclareLaunchArgument(
46  'use_rviz', default_value='True', description='Whether to start RVIZ'
47  )
48 
49  declare_simulator_cmd = DeclareLaunchArgument(
50  'headless', default_value='False', description='Whether to execute gzclient)'
51  )
52 
53  # start the simulation
54  world_sdf = tempfile.mktemp(prefix='nav2_', suffix='.sdf')
55  world_sdf_xacro = ExecuteProcess(
56  cmd=['xacro', '-o', world_sdf, ['headless:=', headless], world])
57  start_gazebo_server_cmd = IncludeLaunchDescription(
58  PythonLaunchDescriptionSource(
59  os.path.join(get_package_share_directory('ros_gz_sim'), 'launch',
60  'gz_sim.launch.py')),
61  launch_arguments={'gz_args': ['-r -s ', world_sdf]}.items())
62 
63  remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown(
64  on_shutdown=[
65  OpaqueFunction(function=lambda _: os.remove(world_sdf))
66  ]))
67 
68  set_env_vars_resources = AppendEnvironmentVariable(
69  'GZ_SIM_RESOURCE_PATH',
70  os.path.join(sim_dir, 'worlds'))
71  start_gazebo_client_cmd = IncludeLaunchDescription(
72  PythonLaunchDescriptionSource(
73  os.path.join(get_package_share_directory('ros_gz_sim'),
74  'launch',
75  'gz_sim.launch.py')
76  ),
77  condition=IfCondition(PythonExpression(
78  ['not ', headless])),
79  launch_arguments={'gz_args': ['-v4 -g ']}.items(),
80  )
81 
82  spawn_robot_cmd = IncludeLaunchDescription(
83  PythonLaunchDescriptionSource(
84  os.path.join(sim_dir, 'launch', 'spawn_tb4.launch.py')),
85  launch_arguments={'use_sim_time': 'True',
86  'robot_sdf': robot_sdf,
87  'x_pose': '-8.0',
88  'y_pose': '0.0',
89  'z_pose': '0.0',
90  'roll': '0.0',
91  'pitch': '0.0',
92  'yaw': '0.0'}.items())
93 
94  start_robot_state_publisher_cmd = Node(
95  package='robot_state_publisher',
96  executable='robot_state_publisher',
97  name='robot_state_publisher',
98  output='screen',
99  parameters=[
100  {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', robot_sdf])}
101  ]
102  )
103 
104  # start the visualization
105  rviz_cmd = IncludeLaunchDescription(
106  PythonLaunchDescriptionSource(
107  os.path.join(nav2_bringup_dir, 'launch', 'rviz_launch.py')
108  ),
109  condition=IfCondition(use_rviz),
110  launch_arguments={'namespace': ''}.items(),
111  )
112 
113  # start navigation
114  bringup_cmd = IncludeLaunchDescription(
115  PythonLaunchDescriptionSource(
116  os.path.join(nav2_bringup_dir, 'launch', 'bringup_launch.py')
117  ),
118  launch_arguments={'map': map_yaml_file, 'use_sim_time': 'True'}.items(),
119  )
120 
121  # start the demo autonomy task
122  demo_cmd = Node(
123  package='nav2_simple_commander',
124  executable='example_assisted_teleop',
125  emulate_tty=True,
126  output='screen',
127  )
128 
129  set_env_vars_resources2 = AppendEnvironmentVariable(
130  'GZ_SIM_RESOURCE_PATH',
131  str(Path(os.path.join(desc_dir)).parent.resolve()))
132 
133  ld = LaunchDescription()
134  ld.add_action(declare_use_rviz_cmd)
135  ld.add_action(declare_simulator_cmd)
136  ld.add_action(world_sdf_xacro)
137  ld.add_action(remove_temp_sdf_file)
138  ld.add_action(set_env_vars_resources)
139  ld.add_action(set_env_vars_resources2)
140  ld.add_action(start_gazebo_server_cmd)
141  ld.add_action(start_gazebo_client_cmd)
142  ld.add_action(spawn_robot_cmd)
143  ld.add_action(start_robot_state_publisher_cmd)
144  ld.add_action(rviz_cmd)
145  ld.add_action(bringup_cmd)
146  ld.add_action(demo_cmd)
147  return ld