Nav2 Navigation Stack - jazzy  jazzy
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 
22 from launch import LaunchDescription
23 from launch.actions import (
24  AppendEnvironmentVariable,
25  DeclareLaunchArgument,
26  ExecuteProcess,
27  IncludeLaunchDescription,
28  OpaqueFunction,
29  RegisterEventHandler,
30 )
31 from launch.conditions import IfCondition
32 from launch.event_handlers import OnShutdown
33 from launch.launch_description_sources import PythonLaunchDescriptionSource
34 from launch.substitutions import Command, LaunchConfiguration, PythonExpression
35 from launch_ros.actions import Node
36 
37 
38 def generate_launch_description():
39  nav2_bringup_dir = get_package_share_directory('nav2_bringup')
40  sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
41  desc_dir = get_package_share_directory('nav2_minimal_tb4_description')
42 
43  robot_sdf = os.path.join(desc_dir, 'urdf', 'standard', 'turtlebot4.urdf.xacro')
44  world = os.path.join(sim_dir, 'worlds', 'depot.sdf')
45  map_yaml_file = os.path.join(nav2_bringup_dir, 'maps', 'depot.yaml')
46 
47  # Launch configuration variables
48  use_rviz = LaunchConfiguration('use_rviz')
49  headless = LaunchConfiguration('headless')
50 
51  # Declare the launch arguments
52  declare_use_rviz_cmd = DeclareLaunchArgument(
53  'use_rviz', default_value='True', description='Whether to start RVIZ'
54  )
55 
56  declare_simulator_cmd = DeclareLaunchArgument(
57  'headless', default_value='False', description='Whether to execute gzclient)'
58  )
59 
60  # start the simulation
61  world_sdf = tempfile.mktemp(prefix='nav2_', suffix='.sdf')
62  world_sdf_xacro = ExecuteProcess(
63  cmd=['xacro', '-o', world_sdf, ['headless:=', headless], world])
64  start_gazebo_server_cmd = IncludeLaunchDescription(
65  PythonLaunchDescriptionSource(
66  os.path.join(get_package_share_directory('ros_gz_sim'), 'launch',
67  'gz_sim.launch.py')),
68  launch_arguments={'gz_args': ['-r -s ', world_sdf]}.items())
69 
70  remove_temp_sdf_file = RegisterEventHandler(event_handler=OnShutdown(
71  on_shutdown=[
72  OpaqueFunction(function=lambda _: os.remove(world_sdf))
73  ]))
74 
75  set_env_vars_resources = AppendEnvironmentVariable(
76  'GZ_SIM_RESOURCE_PATH',
77  os.path.join(sim_dir, 'worlds'))
78  start_gazebo_client_cmd = IncludeLaunchDescription(
79  PythonLaunchDescriptionSource(
80  os.path.join(get_package_share_directory('ros_gz_sim'),
81  'launch',
82  'gz_sim.launch.py')
83  ),
84  condition=IfCondition(PythonExpression(
85  ['not ', headless])),
86  launch_arguments={'gz_args': ['-v4 -g ']}.items(),
87  )
88 
89  spawn_robot_cmd = IncludeLaunchDescription(
90  PythonLaunchDescriptionSource(
91  os.path.join(sim_dir, 'launch', 'spawn_tb4.launch.py')),
92  launch_arguments={'use_sim_time': 'True',
93  'robot_sdf': robot_sdf,
94  'x_pose': '-8.0',
95  'y_pose': '0.0',
96  'z_pose': '0.0',
97  'roll': '0.0',
98  'pitch': '0.0',
99  'yaw': '0.0'}.items())
100 
101  start_robot_state_publisher_cmd = Node(
102  package='robot_state_publisher',
103  executable='robot_state_publisher',
104  name='robot_state_publisher',
105  output='screen',
106  parameters=[
107  {'use_sim_time': True, 'robot_description': Command(['xacro', ' ', robot_sdf])}
108  ]
109  )
110 
111  # start the visualization
112  rviz_cmd = IncludeLaunchDescription(
113  PythonLaunchDescriptionSource(
114  os.path.join(nav2_bringup_dir, 'launch', 'rviz_launch.py')
115  ),
116  condition=IfCondition(use_rviz),
117  launch_arguments={'namespace': '', 'use_namespace': 'False'}.items(),
118  )
119 
120  # start navigation
121  bringup_cmd = IncludeLaunchDescription(
122  PythonLaunchDescriptionSource(
123  os.path.join(nav2_bringup_dir, 'launch', 'bringup_launch.py')
124  ),
125  launch_arguments={'map': map_yaml_file, 'use_sim_time': 'True'}.items(),
126  )
127 
128  # start the demo autonomy task
129  demo_cmd = Node(
130  package='nav2_simple_commander',
131  executable='example_assisted_teleop',
132  emulate_tty=True,
133  output='screen',
134  )
135 
136  set_env_vars_resources2 = AppendEnvironmentVariable(
137  'GZ_SIM_RESOURCE_PATH',
138  str(Path(os.path.join(desc_dir)).parent.resolve()))
139 
140  ld = LaunchDescription()
141  ld.add_action(declare_use_rviz_cmd)
142  ld.add_action(declare_simulator_cmd)
143  ld.add_action(world_sdf_xacro)
144  ld.add_action(remove_temp_sdf_file)
145  ld.add_action(set_env_vars_resources)
146  ld.add_action(set_env_vars_resources2)
147  ld.add_action(start_gazebo_server_cmd)
148  ld.add_action(start_gazebo_client_cmd)
149  ld.add_action(spawn_robot_cmd)
150  ld.add_action(start_robot_state_publisher_cmd)
151  ld.add_action(rviz_cmd)
152  ld.add_action(bringup_cmd)
153  ld.add_action(demo_cmd)
154  return ld