17 from ament_index_python.packages
import get_package_share_directory
19 from launch
import LaunchDescription
20 from launch.actions
import DeclareLaunchArgument, EmitEvent, RegisterEventHandler
21 from launch.conditions
import IfCondition, UnlessCondition
22 from launch.event_handlers
import OnProcessExit
23 from launch.events
import Shutdown
24 from launch.substitutions
import LaunchConfiguration
25 from launch_ros.actions
import Node
29 def generate_launch_description():
31 bringup_dir = get_package_share_directory(
'nav2_bringup')
34 namespace = LaunchConfiguration(
'namespace')
35 use_namespace = LaunchConfiguration(
'use_namespace')
36 rviz_config_file = LaunchConfiguration(
'rviz_config')
39 declare_namespace_cmd = DeclareLaunchArgument(
41 default_value=
'navigation',
42 description=(
'Top-level namespace. The value will be used to replace the '
43 '<robot_namespace> keyword on the rviz config file.'))
45 declare_use_namespace_cmd = DeclareLaunchArgument(
47 default_value=
'false',
48 description=
'Whether to apply a namespace to the navigation stack')
50 declare_rviz_config_file_cmd = DeclareLaunchArgument(
52 default_value=os.path.join(bringup_dir,
'rviz',
'nav2_default_view.rviz'),
53 description=
'Full path to the RVIZ config file to use')
56 start_rviz_cmd = Node(
57 condition=UnlessCondition(use_namespace),
60 arguments=[
'-d', rviz_config_file],
63 namespaced_rviz_config_file = ReplaceString(
64 source_file=rviz_config_file,
65 replacements={
'<robot_namespace>': (
'/', namespace)})
67 start_namespaced_rviz_cmd = Node(
68 condition=IfCondition(use_namespace),
72 arguments=[
'-d', namespaced_rviz_config_file],
74 remappings=[(
'/map',
'map'),
76 (
'/tf_static',
'tf_static'),
77 (
'/goal_pose',
'goal_pose'),
78 (
'/clicked_point',
'clicked_point'),
79 (
'/initialpose',
'initialpose')])
81 exit_event_handler = RegisterEventHandler(
82 condition=UnlessCondition(use_namespace),
83 event_handler=OnProcessExit(
84 target_action=start_rviz_cmd,
85 on_exit=EmitEvent(event=Shutdown(reason=
'rviz exited'))))
87 exit_event_handler_namespaced = RegisterEventHandler(
88 condition=IfCondition(use_namespace),
89 event_handler=OnProcessExit(
90 target_action=start_namespaced_rviz_cmd,
91 on_exit=EmitEvent(event=Shutdown(reason=
'rviz exited'))))
94 ld = LaunchDescription()
97 ld.add_action(declare_namespace_cmd)
98 ld.add_action(declare_use_namespace_cmd)
99 ld.add_action(declare_rviz_config_file_cmd)
102 ld.add_action(start_rviz_cmd)
103 ld.add_action(start_namespaced_rviz_cmd)
106 ld.add_action(exit_event_handler)
107 ld.add_action(exit_event_handler_namespaced)