Cartographer 的前端算法思路

2024-05-11 11:08

本文主要是介绍Cartographer 的前端算法思路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前一篇博客里面提到的是 Cartographer 前端实现中非常小的一个部分的算法思路,参照了《Real time correlative scan matching》里的算法实现了一部分实时scan match 的功能,不过这并不是Cartographer中前端的全部,甚至是可以通过参数disable的一部分功能。 
在 Cartographer 对应的论文《Real-Time Loop Closure in 2D LIDAR SLAM》中提到的前端算法中只有Ceres scan matching,其实就是基于ceres solver实现的非线性优化模型,今天我们看一下具体的算法模型。同样,在读懂算法和代码之前需要一些基础知识:

Ceres Solver tutorial & 最小二乘求解非线性优化问题 
Cartographer Git 
双三次插值

论文内容简单翻译
这里我们主要看论文的”IV. Local 2D SLAM” —— “C. ceres scan matching”部分:

Prior to inserting a scan into a submap, the scan pose ξ is optimized relative to the current local submap using a Ceres-based [14] scan matcher. The scan matcher is responsible for finding a scan pose that maximizes the probabilities at the scan points in the submap. We cast this as a nonlinear least squares problem 
我们用一个基于 ceres solver 的 scan matcher 优化获得当前的 scan pose ξξ,这个 scan matcher 负责找到一个位姿使得 scan 中的所有点在当前 local map 中的概率和最大,于是我们定义一下最小二乘问题: 
argminξ∑k=1K(1−Msmooth(Tξhk))2(CS)
(CS)arg⁡minξ∑k=1K(1−Msmooth(Tξhk))2

where TξTξ transforms hkhk from the scan frame to the submap frame according to the scan pose. The function Msmooth:R2→RMsmooth:R2→R is a smooth version of the probability values in the local submap. We use bicubic interpolation. As a result, values outside the interval [0, 1] can occur but are considered harmless. ‘ 
式中 TξTξ 将 hkhk 中的 scan 点全部转化到 local map 坐标系下,Msmooth:R2→RMsmooth:R2→R 则是一个将 local map 中的各点概率进行一个平滑处理的函数,这里我们用双三次插值。这样可能会出现概率小于0或者大于1的情况,不过这种情况并不会产生错误。 
Mathematical optimization of this smooth function usually gives better precision than the resolution of the grid. Since this is a local optimization, good initial estimates are required. An IMU capable of measuring angular velocities can be used to estimate the rotational component θθ of the pose between scan matches. A higher frequency of scan matches or a pixel-accurate scan matching approach, although more computationally intensive, can be used in the absence of an IMU. 
数学优化问题通常会提供一个比网格地图的分辨率精度更高的优化结果。由于这是一个实时的局部优化,需要一个好的初始位姿估计。我们可以用 IMU 来估计 scan match 中的旋转角度 θθ,当然如果 scan matching 的频率很高,是可以不使用IMU的。
算法与代码分析
Cartographer 的 ceres scan matcher 将上面的最小二成问题分解成了三个 Cost Function: 
- Translation delta cost function 
- Rotational drlta cost function 
- ★★★ Occupied space cost function ★★★

Ceres Scan Matcher
由于代码量很小而且不难懂,我们直接从代码来看算法可能比较直观(用注释来简单解释算法结构,下面算法的Cost functions的解释也都放在代码注释里):

/*
 * input:
 * 1.上一个 scan 的位姿 previous_pose
 * 2.当前的 scan 的位姿的初始估计 initial_pose_estimate
 * 3.当前 scan 点云(2D)point_cloud
 * 4.local map 概率分布栅格图 probability_grid
 * output
 * 1. 计算得到的位姿估计 pose_estimate
 * 2. ceres solver 计算的总结 summary
*/
void CeresScanMatcher::Match(const transform::Rigid2d& previous_pose,
                             const transform::Rigid2d& initial_pose_estimate,
                             const sensor::PointCloud& point_cloud,
                             const ProbabilityGrid& probability_grid,
                             transform::Rigid2d* const pose_estimate,
                             ceres::Solver::Summary* const summary) const 
{
    // 优化的变量(3个)
    double ceres_pose_estimate[3] = {initial_pose_estimate.translation().x(),
                                    initial_pose_estimate.translation().y(),
                                    initial_pose_estimate.rotation().angle()};
    ceres::Problem problem;
    CHECK_GT(options_.occupied_space_weight(), 0.);

    // 下面分别加入了三个 Cost Function
    // 这里的 ceres 相关的只是需要读者自行阅读 ceres solver的教程,教程写的很详细也很好理解
    problem.AddResidualBlock(
            new ceres::AutoDiffCostFunction<OccupiedSpaceCostFunctor, ceres::DYNAMIC, 3>(
                new OccupiedSpaceCostFunctor(
                    options_.occupied_space_weight() / std::sqrt(static_cast<double>(point_cloud.size())),
                    point_cloud, 
                    probability_grid),
                point_cloud.size()),
            nullptr, 
            ceres_pose_estimate);
    CHECK_GT(options_.translation_weight(), 0.);

    problem.AddResidualBlock(
            new ceres::AutoDiffCostFunction<TranslationDeltaCostFunctor, 2, 3>(
                new TranslationDeltaCostFunctor(options_.translation_weight(),previous_pose)),
            nullptr,
            ceres_pose_estimate);
    CHECK_GT(options_.rotation_weight(), 0.);

    problem.AddResidualBlock(
            new ceres::AutoDiffCostFunction<RotationDeltaCostFunctor, 1, 3>(
                new RotationDeltaCostFunctor(options_.rotation_weight(),ceres_pose_estimate[2])),
            nullptr,
            ceres_pose_estimate);

    ceres::Solve(ceres_solver_options_, &problem, summary);

    *pose_estimate = transform::Rigid2d({ceres_pose_estimate[0], ceres_pose_estimate[1]}, ceres_pose_estimate[2]);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Translation & Rotational Cost Function
下面是两个Cost Function 的实现,就是保证 translation 和 rotation 最小的 Cost Function,很简单易懂不多做解释:

class TranslationDeltaCostFunctor {
 public:
  // Constructs a new TranslationDeltaCostFunctor from the given
  // 'initial_pose_estimate' (x, y, theta).
  explicit TranslationDeltaCostFunctor(
      const double scaling_factor,
      const transform::Rigid2d& initial_pose_estimate)
      : scaling_factor_(scaling_factor),
        x_(initial_pose_estimate.translation().x()),
        y_(initial_pose_estimate.translation().y()) {}

  TranslationDeltaCostFunctor(const TranslationDeltaCostFunctor&) = delete;
  TranslationDeltaCostFunctor& operator=(const TranslationDeltaCostFunctor&) =
      delete;

  template <typename T>
  bool operator()(const T* const pose, T* residual) const {
    // 获得一个 2 维的残差,即 x,y 方向上的位移
    residual[0] = scaling_factor_ * (pose[0] - x_);
    residual[1] = scaling_factor_ * (pose[1] - y_);
    return true;
  }

 private:
  const double scaling_factor_;
  const double x_;
  const double y_;
};

class RotationDeltaCostFunctor {
 public:
  // Constructs a new RotationDeltaCostFunctor for the given 'angle'.
  explicit RotationDeltaCostFunctor(const double scaling_factor,
                                    const double angle)
      : scaling_factor_(scaling_factor), angle_(angle) {}

  RotationDeltaCostFunctor(const RotationDeltaCostFunctor&) = delete;
  RotationDeltaCostFunctor& operator=(const RotationDeltaCostFunctor&) = delete;

  template <typename T>
  bool operator()(const T* const pose, T* residual) const {
    // 获得一个 1 维的残差,即旋转角度
    residual[0] = scaling_factor_ * (pose[2] - angle_);
    return true;
  }

 private:
  const double scaling_factor_;
  const double angle_;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Occupied Space Cost Funtion
class OccupiedSpaceCostFunctor {
 public:
  // Creates an OccupiedSpaceCostFunctor using the specified map, resolution
  // level, and point cloud.
  OccupiedSpaceCostFunctor(const double scaling_factor,
                           const sensor::PointCloud& point_cloud,
                           const ProbabilityGrid& probability_grid)
      : scaling_factor_(scaling_factor),
        point_cloud_(point_cloud),
        probability_grid_(probability_grid) {}

  OccupiedSpaceCostFunctor(const OccupiedSpaceCostFunctor&) = delete;
  OccupiedSpaceCostFunctor& operator=(const OccupiedSpaceCostFunctor&) = delete;

  template <typename T>
  bool operator()(const T* const pose, T* residual) const {
    // 第一步,将 pose 转换成一个 3 × 3 的变换矩阵
    Eigen::Matrix<T, 2, 1> translation(pose[0], pose[1]);
    Eigen::Rotation2D<T> rotation(pose[2]);
    Eigen::Matrix<T, 2, 2> rotation_matrix = rotation.toRotationMatrix();
    Eigen::Matrix<T, 3, 3> transform;
    transform << rotation_matrix, translation, T(0.), T(0.), T(1.);

    // 这里将构造时传入的概率栅格图(local submap)加载到一个双三次插值器中
    // GridArrayAdapter 的实现这里省略了,想了解具体实现的可以在 Cartographer 的代码里找到
    // 功能主要是在概率栅格图对应的 index 中取出相应的概率值
    const GridArrayAdapter adapter(probability_grid_);
    ceres::BiCubicInterpolator<GridArrayAdapter> interpolator(adapter);
    const MapLimits& limits = probability_grid_.limits();

    // 遍历 point_cloud_(当前 scan)中的所有点,用变换矩阵将其变换到 local map 的坐标系中
    // 取出每个点对应的栅格地图概率(双三次插值之后的)p
    // 我们要求的是这个 p 最大的情况,也就是 1-p 最小的情况,所以最后残差是 factor*(1-p)
    // 这个残差的纬度就是 point_cloud_ 中的点数
    for (size_t i = 0; i < point_cloud_.size(); ++i) {
      // Note that this is a 2D point. The third component is a scaling factor.
      const Eigen::Matrix<T, 3, 1> point((T(point_cloud_[i].x())),
                                         (T(point_cloud_[i].y())), T(1.));
      const Eigen::Matrix<T, 3, 1> world = transform * point;
      interpolator.Evaluate(
          (limits.max().x() - world[0]) / limits.resolution() - 0.5 +
              T(kPadding),
          (limits.max().y() - world[1]) / limits.resolution() - 0.5 +
              T(kPadding),
          &residual[i]);
      residual[i] = scaling_factor_ * (1. - residual[i]);
    }
    return true;
  }
  ...
  };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
其实不难发现,这个Occupied Space Cost Function 的模型和上一篇博客中的 Real time correlative scan matching 的思路基本上一致,只是求解方法变成了最小二乘问题的求解,这样求解过程我们无需关心,只需要关心建模本身。

参考资料
[1] Ceres Solver tutorial 
[2] Real-Time Loop Closure in 2D LIDAR SLAM
--------------------- 
作者:ashleyliuyc 
来源:CSDN 
原文:https://blog.csdn.net/u012209790/article/details/82735923 
版权声明:本文为博主原创文章,转载请附上博文链接!

这篇关于Cartographer 的前端算法思路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979363

相关文章

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下: