当前位置:   article > 正文

经典激光雷达SLAM系统:LeGO-LOAM

lego-loam

作者 | 密斯特李  编辑 | 汽车人

原文链接:https://zhuanlan.zhihu.com/p/511968459

点击下方卡片,关注“自动驾驶之心”公众号

ADAS巨卷干货,即可获取

点击进入→自动驾驶之心技术交流群

后台回复【IROS2022】获取IROS2022所有自动驾驶方向论文!

0、引子

上期文章介绍了F-LOAM,它重点针对LOAM计算效率问题进行了优化,旨在不影响精度的情况下尽量缩减其对平台算力的需求,使之能够轻量化地部署于算力有限的无人平台上(例如:无人机、移动机器人等)。F-LOAM所采用的优化思路是舍弃LOAM中不必要的laserOodmetry线程并以匀速运动模型估计的位姿预测代替。代码实现整体与A-LOAM一脉相承,除结构优化外在实现细节方面并没有什么差异。

本文介绍的LeGO-LOAM同样是针对LOAM计算效率问题的优化,针对地面移动机器人在室内外环境下运行时的特点,针对性的对LOAM进行优化和改进,实现了一套轻量级的激光雷达SLAM系统。该工作由Shan Tixiao完成,论文LeGO-LOAM: Lightweight and Ground-Optimized LiDAR Odometry and Mapping on Variance Terrain发表于2018年IROS会议,代码源生于LOAM的原始版本,开源于RobustFieldAutonomyLab/LeGO-LOAM,优化版本的代码见facontidavide/LeGO-LOAM-BOR。

相比于F-LOAM, LeGO-LOAM不仅整合了LOAM的系统结构,同时对LOAM中的特征提取、位姿估计计算都进行了优化改进,此外还加入了闭环检测和全局优化,将LOAM这一LO系统构建为完整的SLAM系统,整体工作的创新性和完整性都更加突出。

1、主要创新点及系统架构

1.1 主要创新点

根据论文内容和代码,笔者将这一工作的主要创新点归纳为以下几点:

  • 相较于LOAM的第一个改进:增加基于柱面投影特征分割,对原始点云进行地面点分割和目标聚类。

  • 相较于LOAM的第二个改进:基于分割点云的线面特征提取,从地面点和目标聚类点云中提取线、面特征,特征点更精确。

  • 相较于LOAM的第三个改进:采用两步法实现laserOdometry线程的位姿优化计算,在优化估计精度保持不变的情况下收敛速度极大提升。

  • 相较于LOAM的第四个改进:采用关键帧进行局部地图和全局地图的管理,邻近点云的索引更为便捷。

  • 相较于LOAM的第五个改进:增加基于距离的闭环检测和全局优化,构成完整的激光SLAM解决方案。

1.2 系统架构

LeGO-LOAM的系统架构在其论文中展示的很清晰,ROS的节点关系也能清晰地展示系统各模块间的关系,如下图:

b721794fd4dc1df285edd4fe4e7df11e.png
LeGO-LOAM系统架构
1a368f33fd371e06fa771853228dcebd.png
LeGO-LOAM的ROS节点关系图

与LOAM类似,系统整体分为四个部分,对应四个ROS节点,在四个不同进程中运行。不同的是,LeGO-LOAM将LOAM中负责点云特征提取的scanRegistration节点和负责scan-to-scan匹配的laserOdometry节点整合为featureAssociation节点,增加imageProjection节点,同时在mapOptimization节点中开辟闭环检测与全局优化线程。各节点的具体功能如下:

0074e5c194986d581b794717572fa7f2.png

2、主要方法及代码实现

2.1 点云投影与目标分割

LiDAR直接输出的原始点云是无序的,即无法像图像一样能够明确的知道点云中某一点上下左右的点是哪个,因此在数据处理时通常需要建立树型结构(例如KD树、八叉树)对点云数据进行数据组织和管理。点云投影实际上是一种将无序点云有序化的过程,我们可以直接从投影的点云深度图(depth image 或 range image)中索引到某一点的上下或两侧的点,这也将极大地提高目标分割的效率。

254dcbefb730b5a11838de870e797832.png de018e3cd38b7ec4e98a13524805bd9f.png
点云与深度图投影示意图

在LeGO-LOAM代码中,实现部分在imageProjection.cpp中的projectPointCloud()函数中实现,具体说明见代码中注释:

  1. void projectPointCloud(){
  2.         // range image projection
  3.         float verticalAngle, horizonAngle, range;
  4.         size_t rowIdn, columnIdn, index, cloudSize; 
  5.         PointType thisPoint;
  6.         cloudSize = laserCloudIn->points.size(); // 点云中点个数
  7.         for (size_t i = 0; i < cloudSize; ++i){ // 逐点计算
  8.             thisPoint.x = laserCloudIn->points[i].x;
  9.             thisPoint.y = laserCloudIn->points[i].y;
  10.             thisPoint.z = laserCloudIn->points[i].z;
  11.             // find the row and column index in the iamge for this point
  12.             if (useCloudRing == true){ // 当LiDAR驱动能够输出scan line信息时,直接读取
  13.                 rowIdn = laserCloudInRing->points[i].ring;
  14.             }
  15.             else// 否则,需要计算俯仰角
  16.                 verticalAngle = atan2(thisPoint.z, sqrt(thisPoint.x * thisPoint.x + thisPoint.y * thisPoint.y)) * 180 / M_PI; // 单位为度
  17.                 rowIdn = (verticalAngle + ang_bottom) / ang_res_y; // 计算行号,见公式(1)
  18.             }
  19.             if (rowIdn < 0 || rowIdn >= N_SCAN)
  20.                 continue;
  21.             horizonAngle = atan2(thisPoint.x, thisPoint.y) * 180 / M_PI; // 计算列号,见公式(2)
  22.             // 由于atan2返回的是(-pi,pi)的一个值,为了保证正前方在深度图的中间,需要进行如下角度变换。
  23.             columnIdn = -round((horizonAngle-90.0)/ang_res_x) + Horizon_SCAN/2;
  24.             if (columnIdn >= Horizon_SCAN)
  25.                 columnIdn -= Horizon_SCAN;
  26.             if (columnIdn < 0 || columnIdn >= Horizon_SCAN)
  27.                 continue;
  28.             // 
  29.             range = sqrt(thisPoint.x * thisPoint.x + thisPoint.y * thisPoint.y + thisPoint.z * thisPoint.z);
  30.             if (range < sensorMinimumRange) // 舍弃深度小于最小阈值的点
  31.                 continue;
  32.             
  33.             rangeMat.at<float>(rowIdn, columnIdn) = range// 构建深度图
  34.             thisPoint.intensity = (float)rowIdn + (float)columnIdn / 10000.0;
  35.             index = columnIdn  + rowIdn * Horizon_SCAN; // 深度图像素坐标对应的索引号
  36.             fullCloud->points[index] = thisPoint; // 构建点云
  37.             fullInfoCloud->points[index] = thisPoint;
  38.             fullInfoCloud->points[index].intensity = range// the corresponding range of a point is saved as "intensity"
  39.         }
  40.     }

而后,利用投影深度图进行地面点分割。该方法的前提是默认了深度图下部大部分区域为地面点,且由于无人车位于地面上,车载LiDAR的  平面与地面大致水平,  与地面大致垂直。因此,若深度图上两像素点所构成的向量与LiDAR坐标系  平面的夹角很小,则该向量就近似位于地面上。具体实现部分见imageProjection.cppgroundRemoval()函数,具体说明见代码中的注释:

  1. void groundRemoval(){
  2.         size_t lowerInd, upperInd;
  3.         float diffX, diffY, diffZ, angle;
  4.         // 构建地面点深度图groundMat
  5.         // -1, 深度图中的无效区域
  6.         //  0, 非地面点标记
  7.         //  1, 地面点标记
  8.         for (size_t j = 0; j < Horizon_SCAN; ++j){
  9.             for (size_t i = 0; i < groundScanInd; ++i){
  10.                 lowerInd = j + ( i )*Horizon_SCAN; // 当前像素点下方像素的索引号
  11.                 upperInd = j + (i+1)*Horizon_SCAN; // 当前像素点上方像素的索引号
  12.                 if (fullCloud->points[lowerInd].intensity == -1 ||
  13.                     fullCloud->points[upperInd].intensity == -1){ // 对应像素无深度值
  14.                     // no info to check, invalid points
  15.                     groundMat.at<int8_t>(i,j) = -1;
  16.                     continue;
  17.                 }
  18.                 // 构建向量    
  19.                 diffX = fullCloud->points[upperInd].x - fullCloud->points[lowerInd].x;
  20.                 diffY = fullCloud->points[upperInd].y - fullCloud->points[lowerInd].y;
  21.                 diffZ = fullCloud->points[upperInd].z - fullCloud->points[lowerInd].z;
  22.                 angle = atan2(diffZ, sqrt(diffX*diffX + diffY*diffY) ) * 180 / M_PI; // 计算向量与x-y平面的夹角
  23.                 if (abs(angle - sensorMountAngle) <= 10){ // sensorMountAngle表示LiDAR安装时与地面的夹角,通俗来说头朝下时夹角为正,头朝上扬起时夹角为负
  24.                     groundMat.at<int8_t>(i,j) = 1;
  25.                     groundMat.at<int8_t>(i+1,j) = 1// 若夹角小于10°,则将构成向量的两个深度图像素点标记为地面点
  26.                 }
  27.             }
  28.         }
  29.         // extract ground cloud (groundMat == 1)
  30.         // mark entry that doesn't need to label (ground and invalid point) for segmentation
  31.         // note that ground remove is from 0~N_SCAN-1, need rangeMat for mark label matrix for the 16th scan
  32.         for (size_t i = 0; i < N_SCAN; ++i){
  33.             for (size_t j = 0; j < Horizon_SCAN; ++j){
  34.                 if (groundMat.at<int8_t>(i,j) == 1 || rangeMat.at<float>(i,j) == FLT_MAX){
  35.                     labelMat.at<int>(i,j) = -1;
  36.                 }
  37.             }
  38.         }
  39.         if (pubGroundCloud.getNumSubscribers() != 0){
  40.             for (size_t i = 0; i <= groundScanInd; ++i){
  41.                 for (size_t j = 0; j < Horizon_SCAN; ++j){
  42.                     if (groundMat.at<int8_t>(i,j) == 1)
  43.                         groundCloud->push_back(fullCloud->points[j + i*Horizon_SCAN]); // 插入地面点点云
  44.                 }
  45.             }
  46.         }
  47.     }

地面点分割后,对非地面点进行进一步的目标聚类分割。采用广度优先搜索算法(Breadth-First Search,BFS)在非地面深度图上对所有深度像素进行聚类分割,原则是:某一像素点与其邻域点及LiDAR坐标系原点O构成三角型,另深度值较大的点为A,较小的点为B。当  较大时则认为A和B位于同一目标。分割算法的具体细节可参考文献[1]。LeGO-LOAM中代码实现见imageProjection.cpplabelComponents(int row, int col)函数:

  1. void labelComponents(int row, int col){
  2.         // use std::queue std::vector std::deque will slow the program down greatly
  3.         float d1, d2, alpha, angle;
  4.         int fromIndX, fromIndY, thisIndX, thisIndY; 
  5.         bool lineCountFlag[N_SCAN] = {false};
  6.         queueIndX[0] = row;
  7.         queueIndY[0] = col;
  8.         int queueSize = 1;
  9.         int queueStartInd = 0;
  10.         int queueEndInd = 1;
  11.         allPushedIndX[0] = row;
  12.         allPushedIndY[0] = col;
  13.         int allPushedIndSize = 1;
  14.         
  15.         while(queueSize > 0){ // BFS搜索的主循环
  16.             // Pop point
  17.             fromIndX = queueIndX[queueStartInd];
  18.             fromIndY = queueIndY[queueStartInd];
  19.             --queueSize;
  20.             ++queueStartInd;
  21.             // Mark popped point
  22.             labelMat.at<int>(fromIndX, fromIndY) = labelCount; // 对目标类别标号
  23.             // Loop through all the neighboring grids of popped grid
  24.             for (auto iter = neighborIterator.begin(); iter != neighborIterator.end(); ++iter){ // 邻域搜索
  25.                 // new index
  26.                 thisIndX = fromIndX + (*iter).first;
  27.                 thisIndY = fromIndY + (*iter).second;
  28.                 // index should be within the boundary
  29.                 if (thisIndX < 0 || thisIndX >= N_SCAN)
  30.                     continue;
  31.                 // at range image margin (left or right side)
  32.                 if (thisIndY < 0)
  33.                     thisIndY = Horizon_SCAN - 1;
  34.                 if (thisIndY >= Horizon_SCAN)
  35.                     thisIndY = 0;
  36.                 // prevent infinite loop (caused by put already examined point back)
  37.                 if (labelMat.at<int>(thisIndX, thisIndY) != 0)
  38.                     continue;
  39.                 d1 = std::max(rangeMat.at<float>(fromIndX, fromIndY), 
  40.                               rangeMat.at<float>(thisIndX, thisIndY)); // 深度较大的点,即A
  41.                 d2 = std::min(rangeMat.at<float>(fromIndX, fromIndY), 
  42.                               rangeMat.at<float>(thisIndX, thisIndY)); // 深度较小的点,即B
  43.                 // 角AOB实际上就是深度图像素点划分时所设定的角度分辨率   
  44.                 if ((*iter).first == 0)
  45.                     alpha = segmentAlphaX; // 若邻域点为同一行的点,则角AOB为水平方向的角度分辨率
  46.                 else
  47.                     alpha = segmentAlphaY; // 若邻域点为同一列的点,则角AOB为垂直方向的角度分辨率
  48.                 angle = atan2(d2*sin(alpha), (d1 -d2*cos(alpha))); // 计算角OAB
  49.                 if (angle > segmentTheta){ // 若角OAB大于所设阈值,则认为A和B位于同一目标
  50.                     queueIndX[queueEndInd] = thisIndX;
  51.                     queueIndY[queueEndInd] = thisIndY; // 重置搜索起点,以当前邻域点为起点继续搜索
  52.                     ++queueSize;
  53.                     ++queueEndInd;
  54.                     labelMat.at<int>(thisIndX, thisIndY) = labelCount;
  55.                     lineCountFlag[thisIndX] = true;
  56.                     allPushedIndX[allPushedIndSize] = thisIndX;
  57.                     allPushedIndY[allPushedIndSize] = thisIndY;
  58.                     ++allPushedIndSize;
  59.                 }
  60.             }
  61.         }
  62.         // check if this segment is valid
  63.         bool feasibleSegment = false;
  64.         if (allPushedIndSize >= 30)
  65.             feasibleSegment = true// 若当前目标点个数大于30个,则该目标为有效目标
  66.         else if (allPushedIndSize >= segmentValidPointNum){ 
  67.             // 若不足30但大于所设最小有效阈值,则判断该目标是否跨越足够多的行。若是,则同样标记为有效目标
  68.             // 这一判断是避免一些垂直地面的柱状目标(如:路标杆、树干)因聚类目标点个数过少而被错误的标记为无效
  69.             int lineCount = 0;
  70.             for (size_t i = 0; i < N_SCAN; ++i)
  71.                 if (lineCountFlag[i] == true)
  72.                     ++lineCount;
  73.             if (lineCount >= segmentValidLineNum)
  74.                 feasibleSegment = true;            
  75.         }
  76.         // segment is valid, mark these points
  77.         if (feasibleSegment == true){
  78.             ++labelCount;
  79.         }else// segment is invalid, mark these points
  80.             for (size_t i = 0; i < allPushedIndSize; ++i){
  81.                 labelMat.at<int>(allPushedIndX[i], allPushedIndY[i]) = 999999// 无效目标点的标记
  82.             }
  83.         }
  84.     }

2.2 特征提取与激光里程计

LeGO-LOAM采用与LOAM中相同的特征提取方法,即根据邻域点平滑度筛选线和面特征,本文不再赘述。主要区别是LeGO-LOAM特征提取时兼顾了地面分割和目标聚类的结果。筛选出地面点作为平面特征,并从非地面点且有效聚类目标中提取线特征。代码实现参考featureAssociation.cppcalculateSmoothness()、markOccludedPints()extractFeatures()等函数。

激光里程计通过scan-to-scan匹配估计LiDAR在到间的相对运动,特征匹配方法与LOAM中相同,唯一区别在于LeGO-LOAM采用两步法实现6自由度的位姿状态估计。根据作者的测试,采用两步法能够在获得相同精度的前提下降低35%的计算时间消耗,大大提升了运算效率。具体而言,先利用地面特征估计相对位姿的俯仰角ry、横滚角rx和z轴平移量三个自由度;而后再利用线特征估计相对位姿的偏航角rz、x轴平移量和y轴平移量。代码实现见featureAssociation.cppupdateTransformation()函数:

  1. void updateTransformation(){
  2.         if (laserCloudCornerLastNum < 10 || laserCloudSurfLastNum < 100)
  3.             return;
  4.         for (int iterCount1 = 0; iterCount1 < 25; iterCount1++) { // 第一步: 根据地面特征估计三个自由度
  5.             laserCloudOri->clear();
  6.             coeffSel->clear();
  7.             findCorrespondingSurfFeatures(iterCount1);
  8.             if (laserCloudOri->points.size() < 10)
  9.                 continue;
  10.             if (calculateTransformationSurf(iterCount1) == false)
  11.                 break;
  12.         }
  13.         for (int iterCount2 = 0; iterCount2 < 25; iterCount2++) { // 第二步: 根据线特征估计其余的三个自由度
  14.             laserCloudOri->clear();
  15.             coeffSel->clear();
  16.             findCorrespondingCornerFeatures(iterCount2);
  17.             if (laserCloudOri->points.size() < 10)
  18.                 continue;
  19.             if (calculateTransformationCorner(iterCount2) == false)
  20.                 break;
  21.         }
  22.     }

通过构建非线性优化方程,采用L-M算法求解相对位姿变换。L-M优化迭代部分的实现在上述代码的calculateTransformationSurf()calculateTransformationCorner() 函数中。这里要填一下上一篇文章提到的“采用ceres等优化计算库改进LOAM代码后会产生一定问题”这个坑。在LOAM的优化迭代实现中,引入了退化因子(degeneracy factor)的概念,通过退化因子判断特征退化方向。

那么什么是特征退化?这里我通俗的解释一下:对于点云特征匹配而言,我们将它构建为了一个非线性优化(Nonlinear Optimization)问题,这个非线性优化问题的解是一个六自由度的位姿变换。平行于x-y平面的特征点云可以为估计rx,ry,z提供充足的约束;而平行于z-y和z-x平面的特征点云则可以为估计rz,x,y提供充足的约束。但如果实在一个看不到头尾的长直隧道中,显然平行于平面z-x的点云是严重不足的,导致难以估计x平移量,这就是所谓的退化问题,专业点讲即观测约束在状态空间中是不完备的。对于特征退化因子的理论和推导可以参看LOAM作者Ji Zhang的另一篇论文[2],本文不再深入,若感兴趣的同学比较多我们再单独出一篇解析。具体实现也比较简单,说白了L-M迭代就是在给定初值的基础上增量式地朝着无偏最优估计逼近。若状态空间的某一个方向发现了退化,那就直接不更新这个方向的值就好了。通过将对应状态方向的权阵置0,使这个方向的估计增量不会被加在优化初值上。这里以calculateTransformationCorner()函数为例:

  1. bool calculateTransformationCorner(int iterCount){
  2.         int pointSelNum = laserCloudOri->points.size();
  3.         cv::Mat matA(pointSelNum, 3, CV_32F, cv::Scalar::all(0));
  4.         cv::Mat matAt(3, pointSelNum, CV_32F, cv::Scalar::all(0));
  5.         cv::Mat matAtA(33, CV_32F, cv::Scalar::all(0));
  6.         cv::Mat matB(pointSelNum, 1, CV_32F, cv::Scalar::all(0));
  7.         cv::Mat matAtB(31, CV_32F, cv::Scalar::all(0));
  8.         cv::Mat matX(31, CV_32F, cv::Scalar::all(0));
  9.         float srx = sin(transformCur[0]);
  10.         float crx = cos(transformCur[0]);
  11.         float sry = sin(transformCur[1]);
  12.         float cry = cos(transformCur[1]);
  13.         float srz = sin(transformCur[2]);
  14.         float crz = cos(transformCur[2]);
  15.         float tx = transformCur[3];
  16.         float ty = transformCur[4];
  17.         float tz = transformCur[5];
  18.         float b1 = -crz*sry - cry*srx*srz; float b2 = cry*crz*srx - sry*srz; float b3 = crx*cry; float b4 = tx*-b1 + ty*-b2 + tz*b3;
  19.         float b5 = cry*crz - srx*sry*srz; float b6 = cry*srz + crz*srx*sry; float b7 = crx*sry; float b8 = tz*b7 - ty*b6 - tx*b5;
  20.         float c5 = crx*srz;
  21.         // 利用观测到的线特征构建优化方程的系数矩阵A和残差向量B
  22.         for (int i = 0; i < pointSelNum; i++) {
  23.             pointOri = laserCloudOri->points[i];
  24.             coeff = coeffSel->points[i];
  25.             float ary = (b1*pointOri.x + b2*pointOri.y - b3*pointOri.z + b4) * coeff.x
  26.                       + (b5*pointOri.x + b6*pointOri.y - b7*pointOri.z + b8) * coeff.z;
  27.             float atx = -b5 * coeff.x + c5 * coeff.y + b1 * coeff.z;
  28.             float atz = b7 * coeff.x - srx * coeff.y - b3 * coeff.z;
  29.             float d2 = coeff.intensity;
  30.             matA.at<float>(i, 0) = ary;
  31.             matA.at<float>(i, 1) = atx;
  32.             matA.at<float>(i, 2) = atz;
  33.             matB.at<float>(i, 0) = -0.05 * d2;
  34.         }
  35.         cv::transpose(matA, matAt);
  36.         matAtA = matAt * matA;
  37.         matAtB = matAt * matB;
  38.         cv::solve(matAtA, matAtB, matX, cv::DECOMP_QR); // QR分解法求解 AtAX = AtB
  39.         if (iterCount == 0) {
  40.             cv::Mat matE(13, CV_32F, cv::Scalar::all(0));
  41.             cv::Mat matV(33, CV_32F, cv::Scalar::all(0));
  42.             cv::Mat matV2(33, CV_32F, cv::Scalar::all(0));
  43.             cv::eigen(matAtA, matE, matV); // 对AtA这一对称矩阵进行特征值分解
  44.             matV.copyTo(matV2); // 特征向量,一行对应一个状态方向
  45.             isDegenerate = false;
  46.             float eignThre[3] = {101010};
  47.             for (int i = 2; i >= 0; i--) { // 从最小特征值开始
  48.                 if (matE.at<float>(0, i) < eignThre[i]) {
  49.                     for (int j = 0; j < 3; j++) {
  50.                         matV2.at<float>(i, j) = 0// 若出现退化,将这一行置零
  51.                     }
  52.                     isDegenerate = true;
  53.                 } else {
  54.                     break;
  55.                 }
  56.             }
  57.             matP = matV.inv() * matV2; // 由于AtA是方阵,根据对称对角化定理V^-1 = Vt
  58.         }
  59.         if (isDegenerate) {
  60.             cv::Mat matX2(31, CV_32F, cv::Scalar::all(0));
  61.             matX.copyTo(matX2);
  62.             matX = matP * matX2; 
  63.         }
  64.         transformCur[1] += matX.at<float>(00);
  65.         transformCur[3] += matX.at<float>(10);
  66.         transformCur[5] += matX.at<float>(20);
  67.         for(int i=0; i<6; i++){
  68.             if(isnan(transformCur[i]))
  69.                 transformCur[i]=0;
  70.         }
  71.         float deltaR = sqrt(
  72.                             pow(rad2deg(matX.at<float>(00)), 2));
  73.         float deltaT = sqrt(
  74.                             pow(matX.at<float>(10) * 1002) +
  75.                             pow(matX.at<float>(20) * 1002));
  76.         if (deltaR < 0.1 && deltaT < 0.1) { // 判断收敛
  77.             return false;
  78.         }
  79.         return true;
  80.     }

2.3 地图优化

在这一节点中,共并行了3个线程,分别是:

  • 主线程:scan-to-map匹配以优化激光雷达里程计估计结果,采用基于因子图优化的增量式平滑算法(incremental smoothing and mapping,iSAM)进行整体的位姿图优化

  • 分线程:基于距离的闭环检测,对可能产生闭环的关键帧进行ICP配准,获得闭环帧之间的相对变换加入全局位姿图,并采用iSAM进行整体优化

  • 分线程:对点云、轨迹和构建的位姿图网络进行可视化,并保存点云和离散轨迹。

主线程的scan-to-map匹配与LOAM完全一致,主要改进在于局部地图的管理。LOAM中采用栅格图对局部点云进行管理,栅格图大小固定,随着LiDAR的运动而移动。在LeGO-LOAM中则采用基于关键帧的地图管理方式,这种方法的优点一方面是便于全局优化更新,另一方面是能够更加真实的反映LiDAR邻域环境情况,局部地图更丰富;缺点也很显然,内存占用随运行时间而不断增加,占用内存更大。局部地图的构建同样距离判断,具体实现在mapOptimization.cppextractSurroundingKeyFrames()函数中:

  1. surroundingKeyPoses->clear();
  2.             surroundingKeyPosesDS->clear();
  3.          // extract all the nearby key poses and downsample them
  4.          kdtreeSurroundingKeyPoses->setInputCloud(cloudKeyPoses3D); // 利用KD数搜索邻近的关键帧
  5.          kdtreeSurroundingKeyPoses->radiusSearch(currentRobotPosPoint, (double)surroundingKeyframeSearchRadius, pointSearchInd, pointSearchSqDis, 0);
  6.          for (int i = 0; i < pointSearchInd.size(); ++i)
  7.                 surroundingKeyPoses->points.push_back(cloudKeyPoses3D->points[pointSearchInd[i]]);
  8.          downSizeFilterSurroundingKeyPoses.setInputCloud(surroundingKeyPoses);
  9.          downSizeFilterSurroundingKeyPoses.filter(*surroundingKeyPosesDS); // 对搜索到的临近关键帧降采样,避免过多重复的点
  10.          // delete key frames that are not in surrounding region
  11.             int numSurroundingPosesDS = surroundingKeyPosesDS->points.size();
  12.             for (int i = 0; i < surroundingExistingKeyPosesID.size(); ++i){
  13.                 bool existingFlag = false;
  14.                 for (int j = 0; j < numSurroundingPosesDS; ++j){
  15.                     if (surroundingExistingKeyPosesID[i] == (int)surroundingKeyPosesDS->points[j].intensity){
  16.                         existingFlag = true;
  17.                         break;
  18.                     }
  19.                 }
  20.                 if (existingFlag == false){
  21.                     surroundingExistingKeyPosesID.   erase(surroundingExistingKeyPosesID.   begin() + i);
  22.                     surroundingCornerCloudKeyFrames. erase(surroundingCornerCloudKeyFrames. begin() + i);
  23.                     surroundingSurfCloudKeyFrames.   erase(surroundingSurfCloudKeyFrames.   begin() + i);
  24.                     surroundingOutlierCloudKeyFrames.erase(surroundingOutlierCloudKeyFrames.begin() + i);
  25.                     --i;
  26.                 }
  27.             }
  28.          // add new key frames that are not in calculated existing key frames
  29.             for (int i = 0; i < numSurroundingPosesDS; ++i) {
  30.                 bool existingFlag = false;
  31.                 for (auto iter = surroundingExistingKeyPosesID.begin(); iter != surroundingExistingKeyPosesID.end(); ++iter){
  32.                     if ((*iter) == (int)surroundingKeyPosesDS->points[i].intensity){
  33.                         existingFlag = true;
  34.                         break;
  35.                     }
  36.                 }
  37.                 if (existingFlag == true){
  38.                     continue;
  39.                 }else{
  40.                     int thisKeyInd = (int)surroundingKeyPosesDS->points[i].intensity;
  41.                     PointTypePose thisTransformation = cloudKeyPoses6D->points[thisKeyInd];
  42.                     updateTransformPointCloudSinCos(&thisTransformation);
  43.                     surroundingExistingKeyPosesID.   push_back(thisKeyInd);
  44.                     surroundingCornerCloudKeyFrames. push_back(transformPointCloud(cornerCloudKeyFrames[thisKeyInd]));
  45.                     surroundingSurfCloudKeyFrames.   push_back(transformPointCloud(surfCloudKeyFrames[thisKeyInd]));
  46.                     surroundingOutlierCloudKeyFrames.push_back(transformPointCloud(outlierCloudKeyFrames[thisKeyInd]));
  47.                 }
  48.             }
  49.             // 构建局部点云地图
  50.             for (int i = 0; i < surroundingExistingKeyPosesID.size(); ++i) {
  51.                 *laserCloudCornerFromMap += *surroundingCornerCloudKeyFrames[i];
  52.                 *laserCloudSurfFromMap   += *surroundingSurfCloudKeyFrames[i];
  53.                 *laserCloudSurfFromMap   += *surroundingOutlierCloudKeyFrames[i];
  54.             }

而后在scan2MapOptimization()函数中进行scan-to-map的匹配,优化LiDAR位姿。然后,判断当前帧是否为关键帧,若是关键帧则更新因子图并采用iSAM算法进行全局优化,具体如下:

  1. void saveKeyFramesAndFactor(){
  2.         currentRobotPosPoint.x = transformAftMapped[3];
  3.         currentRobotPosPoint.y = transformAftMapped[4];
  4.         currentRobotPosPoint.z = transformAftMapped[5]; // scan-to-map后的LiDAR位姿
  5.         bool saveThisKeyFrame = true// 关键帧筛选
  6.         if (sqrt((previousRobotPosPoint.x-currentRobotPosPoint.x)*(previousRobotPosPoint.x-currentRobotPosPoint.x)
  7.                 +(previousRobotPosPoint.y-currentRobotPosPoint.y)*(previousRobotPosPoint.y-currentRobotPosPoint.y)
  8.                 +(previousRobotPosPoint.z-currentRobotPosPoint.z)*(previousRobotPosPoint.z-currentRobotPosPoint.z)) < 0.3){
  9.             saveThisKeyFrame = false// 当平移距离超过0.3m时添加关键帧
  10.         }
  11.         
  12.         if (saveThisKeyFrame == false && !cloudKeyPoses3D->points.empty())
  13.          return;
  14.         previousRobotPosPoint = currentRobotPosPoint;
  15.         /**
  16.          * update grsam graph
  17.          */
  18.         if (cloudKeyPoses3D->points.empty()){
  19.             gtSAMgraph.add(PriorFactor<Pose3>(0, Pose3(Rot3::RzRyRx(transformTobeMapped[2], transformTobeMapped[0], transformTobeMapped[1]),
  20.                                                           Point3(transformTobeMapped[5], transformTobeMapped[3], transformTobeMapped[4])), priorNoise)); // 先验因子,即明确状态的因子,不随优化而改变
  21.             initialEstimate.insert(0, Pose3(Rot3::RzRyRx(transformTobeMapped[2], transformTobeMapped[0], transformTobeMapped[1]),
  22.                                                   Point3(transformTobeMapped[5], transformTobeMapped[3], transformTobeMapped[4]))); // 状态量初值
  23.             for (int i = 0; i < 6; ++i)
  24.              transformLast[i] = transformTobeMapped[i];
  25.         }
  26.         else{
  27.             gtsam::Pose3 poseFrom = Pose3(Rot3::RzRyRx(transformLast[2], transformLast[0], transformLast[1]),
  28.                                                 Point3(transformLast[5], transformLast[3], transformLast[4]));
  29.             gtsam::Pose3 poseTo   = Pose3(Rot3::RzRyRx(transformAftMapped[2], transformAftMapped[0], transformAftMapped[1]),
  30.                                                 Point3(transformAftMapped[5], transformAftMapped[3], transformAftMapped[4]));
  31.             gtSAMgraph.add(BetweenFactor<Pose3>(cloudKeyPoses3D->points.size()-1, cloudKeyPoses3D->points.size(), poseFrom.between(poseTo), odometryNoise)); // 由相对位姿变换构成的二元因子
  32.             initialEstimate.insert(cloudKeyPoses3D->points.size(), Pose3(Rot3::RzRyRx(transformAftMapped[2], transformAftMapped[0], transformAftMapped[1]),
  33.                                                                           Point3(transformAftMapped[5], transformAftMapped[3], transformAftMapped[4]))); // 状态量初值
  34.         }
  35.         /**
  36.          * update iSAM
  37.          */
  38.         isam->update(gtSAMgraph, initialEstimate);
  39.         isam->update(); // isam的整体优化
  40.         
  41.         gtSAMgraph.resize(0);
  42.         initialEstimate.clear();
  43.         /**
  44.          * save key poses
  45.          */
  46.         PointType thisPose3D;
  47.         PointTypePose thisPose6D;
  48.         Pose3 latestEstimate;
  49.         isamCurrentEstimate = isam->calculateEstimate(); // 整体优化后的位姿
  50.         latestEstimate = isamCurrentEstimate.at<Pose3>(isamCurrentEstimate.size()-1);
  51.         thisPose3D.x = latestEstimate.translation().y();
  52.         thisPose3D.y = latestEstimate.translation().z();
  53.         thisPose3D.z = latestEstimate.translation().x();
  54.         thisPose3D.intensity = cloudKeyPoses3D->points.size(); // this can be used as index
  55.         cloudKeyPoses3D->push_back(thisPose3D);
  56.         thisPose6D.x = thisPose3D.x;
  57.         thisPose6D.y = thisPose3D.y;
  58.         thisPose6D.z = thisPose3D.z;
  59.         thisPose6D.intensity = thisPose3D.intensity; // this can be used as index
  60.         thisPose6D.roll  = latestEstimate.rotation().pitch();
  61.         thisPose6D.pitch = latestEstimate.rotation().yaw();
  62.         thisPose6D.yaw   = latestEstimate.rotation().roll(); // in camera frame
  63.         thisPose6D.time = timeLaserOdometry;
  64.         cloudKeyPoses6D->push_back(thisPose6D);
  65.         /**
  66.          * save updated transform
  67.          */
  68.         if (cloudKeyPoses3D->points.size() > 1){
  69.             transformAftMapped[0] = latestEstimate.rotation().pitch();
  70.             transformAftMapped[1] = latestEstimate.rotation().yaw();
  71.             transformAftMapped[2] = latestEstimate.rotation().roll();
  72.             transformAftMapped[3] = latestEstimate.translation().y();
  73.             transformAftMapped[4] = latestEstimate.translation().z();
  74.             transformAftMapped[5] = latestEstimate.translation().x();
  75.             for (int i = 0; i < 6; ++i){
  76.              transformLast[i] = transformAftMapped[i];
  77.              transformTobeMapped[i] = transformAftMapped[i];
  78.             }
  79.         }
  80.         pcl::PointCloud<PointType>::Ptr thisCornerKeyFrame(new pcl::PointCloud<PointType>());
  81.         pcl::PointCloud<PointType>::Ptr thisSurfKeyFrame(new pcl::PointCloud<PointType>());
  82.         pcl::PointCloud<PointType>::Ptr thisOutlierKeyFrame(new pcl::PointCloud<PointType>());
  83.         pcl::copyPointCloud(*laserCloudCornerLastDS,  *thisCornerKeyFrame);
  84.         pcl::copyPointCloud(*laserCloudSurfLastDS,    *thisSurfKeyFrame);
  85.         pcl::copyPointCloud(*laserCloudOutlierLastDS, *thisOutlierKeyFrame);
  86.         cornerCloudKeyFrames.push_back(thisCornerKeyFrame);
  87.         surfCloudKeyFrames.push_back(thisSurfKeyFrame);
  88.         outlierCloudKeyFrames.push_back(thisOutlierKeyFrame);
  89.     }

闭环检测部分的代码实现也很直观,就不再详述了。

3、总结

实验结果在KITTI和自采集的移动机器人室外数据集上进行了测试,从结果上看在算力有限的小机器人平台上精度较LOAM有较大提升,同时时间消耗上大大缩减,证实了LeGO-LOAM能够轻量化的部署在算力有限的无人平台上,且实现非常好的定位和建图效果。

参考文献

[1] Bogoslavskyi I, Stachniss C. Efficient online segmentation for sparse 3D laser scans[J]. PFG–Journal of Photogrammetry, Remote Sensing and Geoinformation Science, 2017, 85(1): 41-52.

[2] Zhang J, Kaess M, Singh S. On degeneracy of optimization-based state estimation problems[C]//2016 IEEE International Conference on Robotics and Automation (ICRA). IEEE, 2016: 809-816.

自动驾驶之心】全栈技术交流群

自动驾驶之心是首个自动驾驶开发者社区,聚焦目标检测、语义分割、全景分割、实例分割、关键点检测、车道线、目标跟踪、3D目标检测、多传感器融合、SLAM、光流估计、轨迹预测、高精地图、规划控制、AI模型部署落地等方向;

加入我们:自动驾驶之心技术交流群汇总!

自动驾驶之心【知识星球】

想要了解更多自动驾驶感知(分类、检测、分割、关键点、车道线、3D目标检测、多传感器融合、目标跟踪、光流估计、轨迹预测)、自动驾驶定位建图(SLAM、高精地图)、自动驾驶规划控制、领域技术方案、AI模型部署落地实战、行业动态、岗位发布,欢迎扫描下方二维码,加入自动驾驶之心知识星球(三天内无条件退款),日常分享论文+代码,这里汇聚行业和学术界大佬,前沿技术方向尽在掌握中,期待交流!

1fc1ed04f234393523627f1c44804f8f.jpeg

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/932421
推荐阅读
相关标签
  

闽ICP备14008679号