ROS2安装cartographer
2. 安装Cartographer和Cartographer ROS
使用apt安装(推荐):
bash
sudo apt install ros-humble-cartographer-ros
或者,从源代码安装:
bash
sudo apt-get update
sudo apt-get install -y python3-wstool python3-rosdep ninja-build stow
mkdir -p ~/cartographer_ws/src
cd ~/cartographer_ws
wstool init src
cd src
git clone -b ros2 https://github.com/ros2/cartographer.git
git clone -b ros2 https://github.com/ros2/cartographer_ros.git
wstool update -t src
sudo rosdep init
rosdep update
rosdep install --from-paths src --ignore-src --rosdistro=humble -y
colcon build
source install/setup.bash
3. 创建ROS 2工作空间
bash
mkdir -p ~/ros2_cartographer_ws/src
cd ~/ros2_cartographer_ws/src
4. 创建启动文件
在src文件夹中,创建一个名为cartographer_ros的包(如果还没有的话):
bash
ros2 pkg create --build-type ament_cmake cartographer_ros
在cartographer_ros包中,创建launch和config文件夹:
bash
mkdir launch config
在launch文件夹中创建启动文件demo_my_robot_2d.launch.py:
python
from launch import LaunchDescription
from launch_ros.actions import Node, IncludeLaunchDescription, ExecuteProcess
import launch
import launch_ros
def generate_launch_description():use_sim_time_arg = launch.actions.DeclareLaunchArgument('use_sim_time', default_value='false')bag_filename_arg = launch.actions.DeclareLaunchArgument('bag_filename')my_robot_2d_launch = IncludeLaunchDescription(PythonLaunchDescriptionSource([launch_ros.substitutions.TextSubstitution(text='~/cartographer_ros/launch/my_robot_2d.launch.py')]),launch_arguments={'use_sim_time': 'True'}.items())rviz_node = Node(package='rviz2',executable='rviz2',arguments=['-d', '~/cartographer_ros/config/demo_2d.rviz'],parameters=[{'use_sim_time': True}],)ros2_bag_play_cmd = ExecuteProcess(cmd=['ros2', 'bag', 'play', bag_filename_arg],name='rosbag_play',)return LaunchDescription([use_sim_time_arg,bag_filename_arg,my_robot_2d_launch,rviz_node,ros2_bag_play_cmd])
5. 创建配置文件
在config文件夹中创建一个名为my_robot_2d.lua的配置文件:
lua
options = {map_frame = "map",tracking_frame = "lidar_link",published_frame = "lidar_link",odom_frame = "odom",provide_odom_frame = false,publish_frame_projected_to_2d = true,use_pose_extrapolator = true,use_odometry = false,use_nav_sat = false,use_landmarks = false,num_laser_scans = 1,num_multi_echo_laser_scans = 0,num_subdivisions_per_laser_scan = 1,num_point_clouds = 0,lookup_transform_timeout_sec = 0.2,submap_publish_period_sec = 0.3,pose_publish_period_sec = 5e-3,trajectory_publish_period_sec = 30e-3,rangefinder_sampling_ratio = 1.,odometry_sampling_ratio = 1.,fixed_frame_pose_sampling_ratio = 1.,imu_sampling_ratio = 1.,landmarks_sampling_ratio = 1.,
}
MAP_BUILDER.use_trajectory_builder_2d = true
TRAJECTORY_BUILDER_2D.min_range = 0.5
TRAJECTORY_BUILDER_2D.max_range = 30
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 3.
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
TRAJECTORY_BUILDER_2D.motion_filter.max_distance_meters = 0.05
TRAJECTORY_BUILDER_2D.motion_filter.max_angle_radians = math.rad(0.1)
TRAJECTORY_BUILDER_2D.motion_filter.max_time_seconds = 0.2
POSE_GRAPH.constraint_builder.min_score = 0.65
POSE_GRAPH.constraint_builder.global_localization_min_score = 0.7
6. 构建工作空间
回到工作空间的根目录:
bash
cd ~/ros2_cartographer_ws
安装依赖并构建:
bash
rosdep install --from-paths src --ignore-src --rosdistro=humble -y
colcon build --packages-up-to cartographer_ros
source install/setup.bash
7. 启动Cartographer进行建图
运行启动文件:
bash
ros2 launch cartographer_ros demo_my_robot_2d.launch.py bag_filename:=/path/to/your/bagfile.bag
将/path/to/your/bagfile.bag替换为您的激光雷达数据包的实际路径。
8. 使用RViz可视化
启动RViz并加载配置文件:
bash
ros2 run rviz2 rviz2 -d ~/ros2_cartographer_ws/src/cartographer_ros/config/demo_2d.rviz
9. 保存地图
生成并保存地图:
bash
ros2 run map_server map_saver -f your_map
这将生成your_map.pgm和your_map.yaml文件。
以上步骤提供了一个完整的流程,从安装到配置再到运行Cartographer进行激光建图。请根据您的具体需求调整路径和参数。