diff --git a/turtlebot4_navigation/launch/nav2.launch.py b/turtlebot4_navigation/launch/nav2.launch.py index a4a8d49..15f922f 100644 --- a/turtlebot4_navigation/launch/nav2.launch.py +++ b/turtlebot4_navigation/launch/nav2.launch.py @@ -18,52 +18,65 @@ from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription -from launch.actions import DeclareLaunchArgument, GroupAction, IncludeLaunchDescription +from launch.actions import ( + DeclareLaunchArgument, + GroupAction, + IncludeLaunchDescription, + OpaqueFunction +) from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration, PathJoinSubstitution -from launch_ros.actions import PushRosNamespace +from launch_ros.actions import PushRosNamespace, SetRemap ARGUMENTS = [ DeclareLaunchArgument('use_sim_time', default_value='false', choices=['true', 'false'], description='Use sim time'), + DeclareLaunchArgument('params_file', + default_value=PathJoinSubstitution([ + get_package_share_directory('turtlebot4_navigation'), + 'config', + 'nav2.yaml' + ]), + description='Nav2 parameters'), + DeclareLaunchArgument('namespace', default_value='', + description='Robot namespace') ] -def generate_launch_description(): - pkg_turtlebot4_navigation = get_package_share_directory('turtlebot4_navigation') +def launch_setup(context, *args, **kwargs): pkg_nav2_bringup = get_package_share_directory('nav2_bringup') - nav2_params_arg = DeclareLaunchArgument( - 'params_file', - default_value=PathJoinSubstitution( - [pkg_turtlebot4_navigation, 'config', 'nav2.yaml']), - description='Nav2 parameters') - - namespace_arg = DeclareLaunchArgument( - 'namespace', - default_value='', - description='Robot namespace') - + nav2_params = LaunchConfiguration('params_file') namespace = LaunchConfiguration('namespace') + namespace_str = namespace.perform(context) use_sim_time = LaunchConfiguration('use_sim_time') + launch_nav2 = PathJoinSubstitution( + [pkg_nav2_bringup, 'launch', 'navigation_launch.py']) + nav2 = GroupAction([ PushRosNamespace(namespace), + SetRemap('/' + namespace_str + '/global_costmap/scan', '/' + namespace_str + '/scan'), + SetRemap('/' + namespace_str + '/local_costmap/scan', '/' + namespace_str + '/scan'), IncludeLaunchDescription( - PythonLaunchDescriptionSource( - PathJoinSubstitution( - [pkg_nav2_bringup, 'launch', 'navigation_launch.py'])), - launch_arguments={'use_sim_time': use_sim_time, - 'use_composition': 'False', - 'params_file': LaunchConfiguration('params_file')}.items()), + PythonLaunchDescriptionSource(launch_nav2), + launch_arguments=[ + ('use_sim_time', use_sim_time), + ('params_file', nav2_params.perform(context)), + ('use_composition', 'False'), + ('namespace', namespace_str) + ] + ), ]) + return [nav2] + + +def generate_launch_description(): ld = LaunchDescription(ARGUMENTS) - ld.add_action(nav2_params_arg) - ld.add_action(namespace_arg) - ld.add_action(nav2) + ld.add_action(OpaqueFunction(function=launch_setup)) return ld