写一个mapper_params_localization.yaml配置文件
slam_toolbox:
ros__parameters:
# TF 相关
odom_frame: odom
map_frame: map
base_frame: base_footprint
scan_topic: /scan
mode: localization # 关键:纯定位
map_file_name: /home/xxx/slam_toolbox_map # 不写后缀,自动找 .posegraph + .data
# 可选:启动时初始位姿
# map_start_pose: [x, y, yaw]
编写slam_toolbox_localization.launch.py
# slam_toolbox_localization.launch.py
# 放在你的包 my_slam_pkg/launch/ 目录下
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from pathlib import Path
def generate_launch_description():
# 支持从命令行传入参数(比如 map_file / use_sim_time)
use_sim_time = LaunchConfiguration('use_sim_time')
params_file = LaunchConfiguration('slam_params_file')
declare_use_sim_time = DeclareLaunchArgument(
'use_sim_time',
default_value='false',
description='Use simulation (Gazebo) clock if true'
)
declare_params_file = DeclareLaunchArgument(
'slam_params_file',
default_value=str(
Path(__file__).parent.parent / 'config' / 'mapper_params_localization.yaml'
),
description='Full path to the slam_toolbox localization parameter file'
)
slam_toolbox_node = Node(
package='slam_toolbox',
executable='sync_slam_toolbox_node', # 或 online_async_slam_toolbox_node,看你喜欢哪种
name='slam_toolbox',
output='screen',
parameters=[
params_file,
{'use_sim_time': use_sim_time}
]
)
return LaunchDescription([
declare_use_sim_time,
declare_params_file,
slam_toolbox_node
])
启动服务
ros2 launch my_slam_pkg slam_toolbox_localization.launch.py \
use_sim_time:=True \
slam_params_file:=/home/your_name/my_slam_pkg/config/mapper_params_localization.yaml