Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
rviz_launch.py
1 # Copyright (c) 2018 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import os
16 
17 from ament_index_python.packages import get_package_share_directory
18 from launch import LaunchDescription
19 from launch.actions import DeclareLaunchArgument, EmitEvent, RegisterEventHandler
20 from launch.event_handlers import OnProcessExit
21 from launch.events import Shutdown
22 from launch.substitutions import LaunchConfiguration
23 from launch_ros.actions import Node
24 from nav2_common.launch import LaunchConfigAsBool
25 
26 
27 def generate_launch_description() -> LaunchDescription:
28  # Get the launch directory
29  bringup_dir = get_package_share_directory('nav2_bringup')
30 
31  # Create the launch configuration variables
32  namespace = LaunchConfiguration('namespace')
33  rviz_config_file = LaunchConfiguration('rviz_config')
34  use_sim_time = LaunchConfigAsBool('use_sim_time')
35 
36  # Declare the launch arguments
37  declare_namespace_cmd = DeclareLaunchArgument(
38  'namespace',
39  default_value='navigation',
40  description=(
41  'Top-level namespace. The value will be used to replace the '
42  '<robot_namespace> keyword on the rviz config file.'
43  ),
44  )
45 
46  declare_rviz_config_file_cmd = DeclareLaunchArgument(
47  'rviz_config',
48  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
49  description='Full path to the RVIZ config file to use',
50  )
51 
52  declare_use_sim_time_cmd = DeclareLaunchArgument(
53  'use_sim_time',
54  default_value='false',
55  description='Use simulation (Gazebo) clock if true')
56 
57  # Launch rviz
58  start_rviz_cmd = Node(
59  package='rviz2',
60  executable='rviz2',
61  namespace=namespace,
62  arguments=['-d', rviz_config_file, '--ros-args', '--log-level', 'warn'],
63  output='screen',
64  parameters=[{'use_sim_time': use_sim_time}],
65  remappings=[
66  ('/tf', 'tf'),
67  ('/tf_static', 'tf_static'),
68  ],
69  )
70 
71  exit_event_handler = RegisterEventHandler(
72  event_handler=OnProcessExit(
73  target_action=start_rviz_cmd,
74  on_exit=EmitEvent(event=Shutdown(reason='rviz exited')),
75  ),
76  )
77 
78  # Create the launch description and populate
79  ld = LaunchDescription()
80 
81  # Declare the launch options
82  ld.add_action(declare_namespace_cmd)
83  ld.add_action(declare_rviz_config_file_cmd)
84  ld.add_action(declare_use_sim_time_cmd)
85 
86  # Add any conditioned actions
87  ld.add_action(start_rviz_cmd)
88 
89  # Add other nodes and processes we need
90  ld.add_action(exit_event_handler)
91 
92  return ld