环境:
Ubuntu22.04
ROS2 humble
激光雷达:镭神激光雷达N10_P
首先安装cartographer
安装这两个humble版本的包
sudo apt install ros-humble-cartographer ros-humble-cartographer-ros
等待安装完之后,查看是否有安装好
ros2 pkg list | grep cartogrpaher
返回即代表安装好
cartographer_ros
cartographer_ros_msgs
接着激活激光雷达驱动(以N10_P为例)
首先在自己的工作区使用git克隆命令将镭神工具包克隆到自己的工作区
git clone -b M10P/N10P https://github.com/Lslidar/Lslidar_ROS2_driver.git
克隆后的文件结构为
.
├── build
├── install
├── lslidar_driver
├── lslidar_msgs
├── README.md
└── version.txt
首先进入Lslidar_ROS2_driver文件夹
cd Lslidar_ROS2_driver
接着输入
colcon build
source install/setup.bash
ros2 launch lslidar_driver lslidar_launch.py
就可以看到激光雷达已经激活拥有了点云图像
激光雷达启动后会发布一个节点,然后cartographer订阅激光雷达节点发布的消息
接着我们来配置 cartographer的.lua文件和launch.py文件
这两个文件的路径分别在/opt/ros/humble/share/cartographer_ros/configuration_files
和/opt/ros/humble/share/cartographer_ros/launch下
首先我是修改了官方的backpack_2d.lua为mylaser.lua代码如下
include "map_builder.lua"
include "trajectory_builder.lua"
options = {
map_builder = MAP_BUILDER,
trajectory_builder = TRAJECTORY_BUILDER,
map_frame = "map",
tracking_frame = "laser_link",
published_frame = "laser_link",
odom_frame = "odom",
provide_odom_frame = false,
publish_frame_projected_to_2d = false,
use_pose_extrapolator = false,
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.submaps.num_range_data = 35
TRAJECTORY_BUILDER_2D.min_range = 0.
TRAJECTORY_BUILDER_2D.max_range = 200.
TRAJECTORY_BUILDER_2D.missing_data_ray_length = 5.
TRAJECTORY_BUILDER_2D.use_imu_data = false
TRAJECTORY_BUILDER_2D.use_online_correlative_scan_matching = true
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.linear_search_window = 0.1
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.translation_delta_cost_weight = 10.
TRAJECTORY_BUILDER_2D.real_time_correlative_scan_matcher.rotation_delta_cost_weight = 1e-1
POSE_GRAPH.optimization_problem.huber_scale = 1e2
POSE_GRAPH.optimize_every_n_nodes = 35
POSE_GRAPH.constraint_builder.min_score = 0.65
return options
这里需要修改的就是tracking_frame改成你的雷达frame_id 我这里是laser_link
published_frame 参数是发布信息的坐标一般为base_link但是这里单纯雷达建图也设置为雷达的id就可以
odom没有的话 provide_odom_frame设置为false
接着是launch.py文件的配置我修改了修改了官方的backpack_2d.launch.py为mylaser.lua代码如下mylaser.launch.py代码如下
"""
Copyright 2018 The Cartographer Authors
Copyright 2022 Wyca Robotics (for the ros2 conversion)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.conditions import IfCondition, UnlessCondition
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node, SetRemap
from launch_ros.substitutions import FindPackageShare
from launch.launch_description_sources import PythonLaunchDescriptionSource
import os
def generate_launch_description():
## ***** Launch arguments *****
use_sim_time_arg = DeclareLaunchArgument('use_sim_time', default_value = 'False')
## ***** File paths ******
pkg_share = FindPackageShare('cartographer_ros').find('cartographer_ros')
# urdf_dir = os.path.join(pkg_share, 'urdf')
# urdf_file = os.path.join(urdf_dir, 'backpack_2d.urdf')
# with open(urdf_file, 'r') as infp:
# robot_desc = infp.read()
## ***** Nodes *****
# robot_state_publisher_node = Node(
# package = 'robot_state_publisher',
# executable = 'robot_state_publisher',
# parameters=[
# {'robot_description': robot_desc},
# {'use_sim_time': LaunchConfiguration('use_sim_time')}],
# output = 'screen'
# )
cartographer_node = Node(
package = 'cartographer_ros',
executable = 'cartographer_node',
arguments = [
'-configuration_directory', FindPackageShare('cartographer_ros').find('cartographer_ros') + '/configuration_files',
'-configuration_basename', 'mylaser.lua'],
remappings = [
('scan', 'scan')],
output = 'screen'
)
cartographer_occupancy_grid_node = Node(
package = 'cartographer_ros',
executable = 'cartographer_occupancy_grid_node',
parameters = [
{'use_sim_time': False},
{'resolution': 0.05}],
)
rviz_node = Node(
package='rviz2',
namespace='rviz2',
executable='rviz2',
name='rviz2',
output='screen')
return LaunchDescription([
use_sim_time_arg,
# Nodes
# robot_state_publisher_node,
rviz_node,
cartographer_node,
cartographer_occupancy_grid_node,
])
真实的机器人不需要所以设置为false,urdf注释掉,主要需要修改的find(‘cartographer_ros’) + ‘/configuration_files’,## ‘-configuration_basename’, ‘backpack_2d.lua’]要对应自己的.lua文件,然后 remappings = [ (‘scan’, ‘scan’)]映射改为scan话题。
配置好后启动.launch文件来接受雷达信息建图
ros2 launch cartographer_ros mylaser.launch.py
启动后会看到空的一个网格
点击左下角Add加入By topic的 Map话题
就可以看到建图了
最后可以通过rqt来监控通信
sudo apt update
sudo apt install ros-humble-rqt*
# 运行
rqt_graph
建图之后进行保存
先安装地图管理功能包
udo apt install ros-humble-nav2-map-server
运行地图保存命令最后一个参数为地图路径及名字。
ros2 run nav2_map_server map_saver_cli -f map