实战指南:从零搭建ROS2 + Cartographer 2D激光SLAM系统
1. 环境准备与ROS2安装
第一次接触ROS2和Cartographer的朋友可能会觉得这个组合有点复杂,但别担心,我会用最直白的方式带你走通整个流程。咱们先从最基础的环境搭建开始,就像盖房子要先打地基一样。
我推荐使用Ubuntu 22.04 LTS系统,这是目前ROS2 Humble版本最稳定的运行环境。安装系统后,记得先执行sudo apt update && sudo apt upgrade更新系统,这个步骤能避免很多奇怪的依赖问题。
ROS2的安装其实比想象中简单:
sudo apt install ros-humble-desktop echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc source ~/.bashrc这三行命令就完成了核心安装。我建议再装几个常用工具:
sudo apt install python3-colcon-common-extensions python3-rosdep2 sudo rosdep init rosdep update遇到过最头疼的问题是网络连接导致的rosdep初始化失败。如果遇到这种情况,可以尝试更换软件源或者使用手机热点。实测下来,清华和中科大的镜像源速度比较稳定。
2. 激光雷达驱动配置
手头的LD14雷达需要先搞定驱动。这个雷达性价比不错,但官方驱动有时候会抽风。我推荐使用社区维护的ldlidar_sl_ros2驱动:
cd ~/ros2_ws/src git clone https://github.com/ldrobotSensorTeam/ldlidar_sl_ros2.git连接雷达时有个坑要注意:USB转串口的权限问题。我习惯用这条命令一劳永逸:
sudo usermod -a -G dialout $USER sudo chmod 666 /dev/ttyACM0修改launch文件时重点关注这几个参数:
parameters=[ {'product_name': 'LDLiDAR_LD14'}, {'laser_scan_topic_name': 'scan'}, # 话题名后面建图要用 {'frame_id': 'base_laser'}, # 坐标系名称 {'port_name': '/dev/ttyACM0'}, # 实际设备号 {'serial_baudrate': 115200} ]启动雷达节点后,用ros2 topic echo /scan能看到数据就说明驱动工作正常。如果没数据,先检查USB连接是否松动,这是新手最容易忽略的问题。
3. Cartographer安装与配置
安装Cartographer其实就两行命令:
sudo apt install ros-humble-cartographer ros-humble-cartographer-ros但这里有个版本匹配的坑:Cartographer的ROS2版本更新较慢,最好指定安装版本。我测试过ros-humble-cartographer=2.0.9000这个版本最稳定。
配置文件是核心难点,我提炼了关键参数:
options = { map_frame = "map", tracking_frame = "base_link", # 雷达坐标系 published_frame = "base_link", # 底盘坐标系 odom_frame = "odom", provide_odom_frame = true, use_odometry = false, # 纯激光建图设为false num_laser_scans = 1, # 单雷达设为1 lookup_transform_timeout_sec = 0.2 }TF树配置是另一个容易出错的地方。正确的坐标系关系应该是:
map -> odom -> base_link -> base_laser可以用ros2 run tf2_tools view_frames生成坐标系关系图检查。如果发现断链,就需要调整published_frame参数。
4. 实战建图与调优
启动建图的完整命令序列:
ros2 launch ldlidar_sl_ros2 ld14.launch.py ros2 launch cartographer_ros my_robot_2d.launch.py ros2 run rviz2 rviz2 -d $(ros2 pkg list | grep cartographer_ros)/configuration_files/demo_2d.rviz建图质量主要看这三个参数:
TRAJECTORY_BUILDER_2D.submaps.num_range_data- 子图包含的扫描数(建议30-50)POSE_GRAPH.optimize_every_n_nodes- 优化频率(建议35)TRAJECTORY_BUILDER_2D.min_range/max_range- 有效测量范围
遇到地图扭曲时,可以尝试:
- 降低移动速度
- 调大
real_time_correlative_scan_matcher.linear_search_window - 增加
POSE_GRAPH.constraint_builder.min_score
保存地图的正确姿势:
ros2 run nav2_map_server map_saver_cli -f ~/map这个命令会生成.pgm和.yaml文件。记得在保存前让机器人停在已建图区域,这样地图完整性最好。
5. 常见问题排查
问题1:雷达数据时有时无
- 检查USB供电是否充足
- 尝试更换USB线材
- 降低串口波特率到9600测试
问题2:TF树报错
[ERROR] [tf2_ros]: Could not find a connection between 'base_link' and 'base_laser'- 确认launch文件中的frame_id命名一致
- 检查static_transform_publisher参数顺序
问题3:建图漂移严重
- 调大
TRAJECTORY_BUILDER_2D.max_range - 减小
POSE_GRAPH.optimize_every_n_nodes - 考虑添加IMU或里程计数据
问题4:RViz中看不到地图
- 检查topic名称是否为
/map - 确认Grid的Color Scheme设置为
costmap - 尝试重置RViz显示配置
6. 进阶技巧与优化
想让建图效果更专业?试试这些技巧:
- 多轨迹优化:在大型场景中,可以分段建图后合并
ros2 run cartographer_ros cartographer_assets_writer -configuration_directory $(ros2 pkg list | grep cartographer_ros)/configuration_files -configuration_basename my_robot_2d.lua -bag_filenames input.bag -pose_graph_filename optimized.pbstream- 参数自动调优:使用cartographer_tuning工具
sudo apt install ros-humble-cartographer-tools ros2 run cartographer_ros configuration_files_tester --input-dir=./config- 性能监控:实时查看计算负载
ros2 run cartographer_ros cartographer_occupancy_grid_node --resolution 0.05 --publish_period_sec 1.0- 地图后处理:用Python脚本清理噪点
import numpy as np from PIL import Image img = Image.open('map.pgm').convert('L') arr = np.array(img) arr[arr > 220] = 255 # 清除游离噪点 Image.fromarray(arr).save('cleaned_map.pgm')这套系统我在多个机器人项目上实际应用过,最大的体会是:参数没有绝对的最优值,需要根据实际环境和硬件反复调整。建议每次只修改一个参数,记录下变化效果。建图时保持匀速运动,急转弯容易造成特征点匹配失败。
