Nav2 Navigation Stack - kilted  kilted
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 
25 
26 def generate_launch_description() -> LaunchDescription:
27  # Get the launch directory
28  bringup_dir = get_package_share_directory('nav2_bringup')
29 
30  # Create the launch configuration variables
31  namespace = LaunchConfiguration('namespace')
32  rviz_config_file = LaunchConfiguration('rviz_config')
33  use_sim_time = LaunchConfiguration('use_sim_time')
34 
35  # Declare the launch arguments
36  declare_namespace_cmd = DeclareLaunchArgument(
37  'namespace',
38  default_value='navigation',
39  description=(
40  'Top-level namespace. The value will be used to replace the '
41  '<robot_namespace> keyword on the rviz config file.'
42  ),
43  )
44 
45  declare_rviz_config_file_cmd = DeclareLaunchArgument(
46  'rviz_config',
47  default_value=os.path.join(bringup_dir, 'rviz', 'nav2_default_view.rviz'),
48  description='Full path to the RVIZ config file to use',
49  )
50 
51  declare_use_sim_time_cmd = DeclareLaunchArgument(
52  'use_sim_time',
53  default_value='false',
54  description='Use simulation (Gazebo) clock if true')
55 
56  # Launch rviz
57  start_rviz_cmd = Node(
58  package='rviz2',
59  executable='rviz2',
60  namespace=namespace,
61  arguments=['-d', rviz_config_file, '--ros-args', '--log-level', 'warn'],
62  output='screen',
63  parameters=[{'use_sim_time': use_sim_time}],
64  remappings=[
65  ('/tf', 'tf'),
66  ('/tf_static', 'tf_static'),
67  ],
68  )
69 
70  exit_event_handler = RegisterEventHandler(
71  event_handler=OnProcessExit(
72  target_action=start_rviz_cmd,
73  on_exit=EmitEvent(event=Shutdown(reason='rviz exited')),
74  ),
75  )
76 
77  # Create the launch description and populate
78  ld = LaunchDescription()
79 
80  # Declare the launch options
81  ld.add_action(declare_namespace_cmd)
82  ld.add_action(declare_rviz_config_file_cmd)
83  ld.add_action(declare_use_sim_time_cmd)
84 
85  # Add any conditioned actions
86  ld.add_action(start_rviz_cmd)
87 
88  # Add other nodes and processes we need
89  ld.add_action(exit_event_handler)
90 
91  return ld