Matlab加载栅格地图

实现二进制栅格地图

The object keeps track of three reference frames: world, local, and, grid. 世界坐标系的原点由GridLocationInWorld决定, which defines the bottom-left corner of the map relative to the world frame. LocalOriginInWorld property specifies the location of the origin of the local frame relative to the world frame. The first grid location with index (1,1) begins in the top-left corner of the grid.

map = binaryOccupancyMap creates a 2-D binary occupancy grid with a width and height of 10m. The default grid resolution is one cell per meter.

1
2
3
4
5
6
image = imread('F:\map.pgm');
%imshow(image)

imageBW = image < 100;
map = binaryOccupancyMap(imageBW);
show(map)

读取建好的pgm地图,如果直接imshow,结果就是pgm文件的本来样子

加下面几句后,结果是这样的
处理后的栅格地图.png

实现栅格地图

occupancyMap对象support local coordinates, world coordinates, and grid indices. The first grid location with index (1,1) begins in the top-left corner of the grid.

Use the occupancyMap class to create 2-D maps of an environment with probability values representing different obstacles in your world. You can specify exact probability values of cells or include observations from sensors such as laser scanners.

Probability values are stored using a binary Bayes filter to estimate the occupancy of each grid cell. A log-odds representation is used, with values stored as int16 to reduce the map storage size and allow for real-time applications.

1
2
3
4
5
image = imread('F:\map.pgm');
imageNorm = double(image)/255;
imageOccupancy = 1 - imageNorm;
map = occupancyMap(imageOccupancy,20);
show(map)

pgm文件中的值是0~255的uint8类型,将其归一化:先转为double类型,再除以255. 图片中的障碍物对应值为0,应该用1减去它,这样1就代表障碍物了. 否则图片显示出来是一团黑.

使用occupancyMap函数创建栅格地图,分辨率为1米20个cell,所支持的分辨率极限是±0.001

读取雷达扫描结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 建一个空地图,宽和高依次为10m,分辨率20,也就是1米有20个cell
map = occupancyMap(10,10,20);
# 机器人位姿[x,y,theta]
pose = [5,5,pi/2];
# ones函数是生成100行1列的矩阵, 元素全是1, 又都乘以3
# ones(n)函数是生成nXn的矩阵
ranges = 3*ones(100,1);
angles = linspace(-pi/2,pi/2,100);
maxrange = 20;
scan = lidarScan(ranges,angles);
# 把laser scan的数据插入栅格地图
insertRay(map,pose,scan,maxrange);

show(map)

linspace(x,y,n)是Matlab中的一个指令,用于产生x,y之间n点行矢量。其中x是起始值、y是中止值,n表示元素个数,如果缺省,则默认n为100。

linspace(1,10,2)为1,10. linspace(1,10,4)为1,4,7,10

laserScan对象

使用laserScan对象作为一些机器人算法的输入,例如matchScans, controllerVFH, monteCarloLocalization.

1
2
3
4
// ranges and angles inputs are vectors of the same length
scan = lidarScan(ranges,angles)
scan = lidarScan(cart)
scan = lidarScan(scanMsg) # 从ROS message中创建

plot(scan)可以显示雷达扫描的曲线

1
2
3
4
5
minRange = 0.1;
maxRange = 7;
scan2 = removeInvalidData(scan,'RangeLimits',[minRange maxRange]);
hold on
plot(scan2)

根据指定的范围,移除invalid数据

参考:Matlab occupancy map


全局路径规划(一) global_planner

概述

ROS 的navigation官方功能包提供了三种全局路径规划器:carrot_planner、global_planner、navfn,默认使用的是navfn。
继承关系 1.png

  • carrot_planner检查需要到达的目标是不是一个障碍物,如果是一个障碍物,它就将目标点替换成一个附近可接近的点。因此,这个模块其实并没有做任何全局规划的工作。在复杂的室内环境中,这个模块并不实用。

  • navfn使用Dijkstra算法找到最短路径。

  • global planner是navfn的升级版。它支持A*算法; 可以切换二次近似; 切换网格路径;

目前常用的是global_planner,需要先设定move_base的参数: base_global_planner: "global_planner/GlobalPlanner"global_planner根据给定的目标位置进行总体路径的规划,只处理全局代价地图中的数据。提供快速的、内插值的全局规划,目前已经取代navfn。遵循navcore::navcore 包中指定的BaseGlobalPlanner接口。它接受costmap生成的全局代价地图规划出从起始点到目标点的路径,为local_planner规划路径作出参考。

global_planner没有提供类似D*这样的动态方法,而是用了定时规划路径,ROS是启动了一个线程,在移动过程中对路径不断的重新规划。这个feature是可以去掉的,特别是当你的运算负载很高,处理器又有限的情况下。还有重新规划(当找不到路径,也就是走着走着新扫描到未知区域的障碍或者动态增加的障碍)两种办法。加上了定时规划和重新规划之后的A*D*几乎是一模一样的。

配置

move_base是通过plugin调用全局规划器的,文件bgp_plugin.xml

1
2
3
4
5
6
7
<library path="lib/libglobal_planner">
<class name="global_planner/GlobalPlanner" type="global_planner::GlobalPlanner" base_class_type="nav_core::BaseGlobalPlanner">
<description>
A implementation of a grid based planner using Dijkstras or A*
</description>
</class>
</library>

package.xml的配置中,加入如下行:
1
2
3
<export>
<nav_core plugin="${prefix}/bgp_plugin.xml" />
</export>

参数

  • allow_unknown: true    是否允许规划器规划在未知区域创建规划,只设置该参数为true还不行,还要在costmap_commons_params.yaml中设置 track_unknown_space 参数也为true才行,

  • default_tolerance: 0.0    当设置的目的地被障碍物占据时,需要以该参数为半径寻找到最近的点作为新目的地点.

  • visualize_potential: false    是否显示从PointCloud2计算得到的势区域. 这个参数可以让你看见potential array的图像,看计算出的cost是怎么样子(颜色深浅代表距离起始点的远近)

  • use_dijkstra: true    设置为true,将使用dijkstra算法, 否则使用A*算法

  • use_quadratic: true    设置为true,将使用二次函数近似函数计算potential,否则使用更加简单的计算方式,这样节省硬件资源

  • use_grid_path: false    默认使用梯度下降法,路径更为光滑,从周围八个栅格中找到下降梯度最大的点。 如果为true,使用栅格路径,从终点开始找上下或左右4个中最小的栅格直到起点,会规划一条沿着网格边界的路径,偏向于直线穿越网格

  • old_navfn_behavior: false    navfn是非常旧的ROS系统中使用的,现在已经都用global_planner代替navfn了,所以不建议设置为true.

  • lethal_cost: 253    致命代价值,默认是设置为253,可以动态来配置该参数.

  • neutral_cost: 50    中等代价值,默认设置是50,可以动态配置该参数.

  • cost_factor: 3.0    代价地图与每个代价值相乘的因子.

  • publish_potential: true    是否发布costmap的势函数.

  • orientation_mode: 0    如何设置每个点的方向(None = 0,Forward = 1,Interpolate = 2,ForwardThenInterpolate = 3,Backward = 4,Leftward = 5,Rightward = 6)(可动态重新配置)

  • orientation_window_size: 1    根据orientation_mode指定的位置积分来得到使用窗口的方向.默认值1,可以动态重新配置.

       A*和Dijkstra两种算法

两种算法的效果对比
A.png
Dijkstra.png

A*Dijkstra少计算很多,但可能不会产生相同路径。另外,在global_planner的A*里,the potentials are computed using 4-connected grid squares, while the path found by tracing the potential gradient from the goal back to the start uses the same grid in an 8-connected fashion. Thus, the actual path found may not be fully optimal in an 8-connected sense. (Also, no visited-state set is tracked while computing potentials, as in a more typical A* implementation, because such is unnecessary for 4-connected grids).

话题

发布的话题是~<name>/plan(nav_msgs/Path),即最新规划出的路径,每次规划出新路径就要发布一次,主要用于观测。

GlobalPlanner::initialize

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
if (!initialized_)
{
ros::NodeHandle private_nh("~/" + name);
costmap_ = costmap;
frame_id_ = frame_id; //costmap_ros->getGlobalFrameID()

unsigned int cx = costmap->getSizeInCellsX(), cy = costmap->getSizeInCellsY();
// 参数赋值, 默认false
private_nh.param("old_navfn_behavior", old_navfn_behavior_, false);
if(!old_navfn_behavior_)
convert_offset_ = 0.5;
else
convert_offset_ = 0.0;

bool use_quadratic; // 二次方的
private_nh.param("use_quadratic", use_quadratic, true);

// p_calc_声明为PotentialCalculator指针
// QuadraticCalculator是它的派生类, 唯一区别是覆盖了calculatePotential函数
if (use_quadratic) // 默认使用二次
p_calc_ = new QuadraticCalculator(cx, cy);
else
p_calc_ = new PotentialCalculator(cx, cy);

bool use_dijkstra;
private_nh.param("use_dijkstra", use_dijkstra, true);
//DijkstraExpansion 和 AStarExpansion 都继承 Expander类
if (use_dijkstra)
{
DijkstraExpansion* de = new DijkstraExpansion(p_calc_, cx, cy);
if(!old_navfn_behavior_)
de->setPreciseStart(true);
planner_ = de; // Expander* planner_;
}
else // 如果不用dijkstra,就用A星算法.
planner_ = new AStarExpansion(p_calc_, cx, cy);

bool use_grid_path;
private_nh.param("use_grid_path", use_grid_path, false);
// GradientPath 和 GridPath都继承了Traceback
if (use_grid_path)
path_maker_ = new GridPath(p_calc_);
else
path_maker_ = new GradientPath(p_calc_);

orientation_filter_ = new OrientationFilter();
// NavfnROS::initialize中也注册此话题
plan_pub_ = private_nh.advertise<nav_msgs::Path>("plan", 1);
potential_pub_ = private_nh.advertise<nav_msgs::OccupancyGrid>("potential", 1);
// 一系列参数赋值
private_nh.param("allow_unknown", allow_unknown_, true);
planner_->setHasUnknown(allow_unknown_);
private_nh.param("planner_window_x", planner_window_x_, 0.0);
private_nh.param("planner_window_y", planner_window_y_, 0.0);
private_nh.param("default_tolerance", default_tolerance_, 0.0);
private_nh.param("publish_scale", publish_scale_, 100);

double costmap_pub_freq;
private_nh.param("planner_costmap_publish_frequency", costmap_pub_freq, 0.0);

//get the tf prefix
ros::NodeHandle prefix_nh;
tf_prefix_ = tf::getPrefixParam(prefix_nh);

make_plan_srv_ = private_nh.advertiseService("make_plan", &GlobalPlanner::makePlanService, this);

dsrv_ = new dynamic_reconfigure::Server<global_planner::GlobalPlannerConfig>(ros::NodeHandle("~/" + name));
dynamic_reconfigure::Server<global_planner::GlobalPlannerConfig>::CallbackType cb = boost::bind(
&GlobalPlanner::reconfigureCB, this, _1, _2);
dsrv_->setCallback(cb);

initialized_ = true;
} else
ROS_WARN("This planner has already been initialized, you can't call it twice, doing nothing");

参考:
ROS- global_panner
global_planner历史背景和概述


Dijkstra算法

以下面的加权图举例,说明算法的过程
加权图
加权图中不可有负权边
伪代码.png
或者这样理解:

  1. 第一个核心步骤:找到当前未处理过的顶点中 最小的点 V,(由于起点到起点的消耗为0,所以算法开始时 V 必定代表起点);
  2. 第二个核心步骤:若V有邻居,则计算经过 V 的情况下起点到达各邻居的消耗 ,并选择是否更新 V 邻居的值。若没有邻居则对该点的处理结束
  3. 重复以上两个核心步骤,直到满足算法终止的条件:有向图中所有的点都被处理过。

算法每处理完一个顶点后,该顶点对应的 值就是起点到该点的最短路径长度,且在这之后不会被更改。这就是最短路径的原因
过程 1
过程 2

Dijkstra的特点是从起点开始,由近及远,层层扩展。越靠前处理的点离起点越近,最后一个处理的点离起点最远。
即使我只想找两个点之间的最短路径,Dijkstra还是需要遍历所有节点,因此时间复杂度为,所以不适合复杂路径等大规模场景。


A星算法

地图和起点,终点

算法的伪代码.png

1.jpg

2.jpg

3.jpg

公式表示为:f(n)=g(n)+h(n),其中f(n)是经过节点n从初始点到目标点的代价函数,g(n)表示从初始节点到节点n的代价,h(n)表示从节点n到目标点的启发式代价

  • Dijkstra是无目的的扩展,A星是启发式,选择离目标点最近的方向扩展,但不一定是最短路径。
  • Dijkstra算法的实质是广度优先搜索,是一种发散式的搜索,所以空间复杂度和时间复杂度都比较高。
  • 对路径上的当前点,A*算法不但记录其到源点的代价,还计算当前点到目标点的期望代价,是一种启发式算法,也可以认为是一种深度优先的算法。

Matlab中使用ROS

把Matlab编写的路径规划算法用于ROS中的自主导航,matlab也有ros的接口,可以通过话题直接把算法结果发给ROS环境,控制机器人移动

实现Matlab和ROS的通信

master可以在Matlab上,但这种需求很少,一般在Ubuntu上,下面演示从Matlab连接ROS的过程

执行setenv('ROS_MASTER_URI','http://192.168.1.7:11311'),IP为master

然后执行rosinit,正常的话会出现下面提示:

1
2
The value of the ROS_MASTER_URI environment variable, http://192.168.1.7:11311, will be used to connect to the ROS master.
Initializing global node /matlab_global_node_37037 with NodeURI http://192.168.1.8:20811/

现在二者通信成功了,可以使用rosnodel list等命令了。但是与标准的ROS命令有所不同,以下列出常用的几个:
常用命令.png

rosshutdown用于退出ROS网络,在此之后Matlab和ROS不再通信。
rosshutdown.png

参考:Matlab中的ROS


Matlab常用操作

matlab执行dos命令

dos函数: dos('ping 192.168.0.109')

for循环

1
2
3
4
5
6
7
% ii  ---循环变量,也就是循环次数
clc;clear;

for ii = 1:10
fprintf('value of a: %d\n', ii);
end
fprintf('跳出循环后,value of a: %d\n', ii);

控制表达式产生了一个1ⅹ10数组,所以语句1到n将会被重复执行10次。注意在循环体在最后一次执行后,循环系数将会一直为10。

randn 和 rand 函数

randn:产生正态分布的随机数或矩阵的函数

randn:产生均值为0,方差σ^2 = 1,标准差σ = 1的正态分布的随机数或矩阵的函数。

  • Y = randn(n):返回一个n*n的随机项的矩阵。如果n不是个数量,将返回错误信息。 n可以是0,此时为空矩阵

  • Y = randn(m,n) 或 Y = randn([m n]): 返回一个m*n的随机项矩阵。

产生一个随机分布的指定均值和方差的矩阵:将randn产生的结果乘以标准差,然后加上期望均值即可。例如:产生均值为0.6,方差为0.1的一个5*5的随机数方式如下:

1
x = .6 + sqrt(0.1) * randn(5)


rand函数产生由在(0, 1)之间均匀分布的随机数组成的数组

  • Y = rand(n): 返回一个n*n的随机矩阵如果n不是数量,则返回错误信息

  • Y = rand(m,n) 或 Y = rand([m n]): 返回一个m x n的随机矩阵


laser_filters

scan_tools提供了一系列用于激光SLAM的工具,在github的分支只到indigo,所以无法从ros直接安装,但可以编译源码安装. 其中重要的有:

  • laser_scan_matcher: an incremental laser scan matcher, using Andrea Censi’s Canonical Scan Matcher implementation. It downloads and installs Andrea Censi’s Canonical Scan Matcher [1] locally.

  • scan_to_cloud_converter: converts LaserScan to PointCloud messages.

laser_filters 过滤雷达数据

过滤器的机制和代价地图是类似的,节点scan_to_scan_filter_chain相当于代价地图,每一个filter相当于地图的每一层,通过加载yaml而加载filter。

以range过滤器为例,修改range_filter.yaml如下:

1
2
3
4
5
6
7
8
9
scan_filter_chain:
- name: box_filter
type: laser_filters/LaserScanRangeFilter
params:
# use_message_range_limits: false # if not specified defaults to false
lower_threshold: 0.18 # 默认0
upper_threshold: 0.22 # 默认100000.0
lower_replacement_value: 0 # 默认 NaN
upper_replacement_value: 999 # 默认 NaN

运行launch: roslaunch laser_filters range_filter_example.launch,里面就是节点scan_to_scan_filter_chain和yaml文件

结果查看scan_filtered话题,只显示出0.18~0.22距离的数据,太小的显示为0,太大的显示为999.

又比如使用LaserScanAngularBoundsFilter,只取-45°~45°内的scan,结果如下,要把laser坐标系的x轴放到水平向右的方向观察

-45°至45°.png

参考:
scan_filtered的使用
laser_filters


ICP系列算法

SLAM前端匹配,是构建方程的过程。后端优化是求解方程的过程。一个好的SLAM算法重点在前端。

做帧间的匹配是为了得到机器人前后的局部位姿变换关系,可以认为是一种里程计。其实标定了轮子直径跟间距我们就有了一个里程计了,但是因为轮子打滑等各种因素存在误差会比较大。再用雷达的数据做一次配准可以提高定位精度。

  • ICP:迭代最近点
  • PL-ICP:点到线的ICP
  • IMLS-ICP:implicit moving least square,隐式移动最小二乘
  • Generalized ICP (GICP),综合考虑 point-to-point、point-to-plane 和 plane-to-plane 策略,精度、鲁棒性都有所提高;
    Normal Iterative Closest Point (NICP)
  • NICP:考虑法向量和局部曲率,更进一步利用了点云的局部结构信息,其论文中实验结果比 GICP 的性能更好。

ICP

R,t就是相邻两帧激光对应的机器人位置的欧氏变换,以此类推,根据很多帧的点云,就能求出一系列的机器人位姿变换关系。
ICP有二维和三维。
由于两个点云对应同一个实体,那么理论上配准后的距离应该为0,由于误差的存在,我们要求距离的最小值。

对于已知对应点的情况(已知R),我们可以求出闭式解,而不必用迭代方法:

实际中不知道对应点匹配,不能直接算出R和t,要进行迭代计算:寻找对应点,根据对应点计算R,t;对点云转换再计算误差;不断迭代直至误差足够小。ICP算法里不能有nan的雷达数据

ICP方法存在以下缺点,所以不在实际SLAM中使用:

  1. 依赖初始值,初始值不好时,迭代次数增加;对于较大的初始误差,可能会出现错误的迭代结果
  2. ICP是一阶收敛,收敛速度慢。(所以会用kd-tree来加快搜索效率)例如我使用A2雷达,频率12.7Hz,两帧间隔0.079s。ICP时间在0.01s~0.02s,而超过0.015s的有一部分,这就了两帧scan间隔的%19。甚至有些雷达,ICP计算时间大于两帧间隔,这样算出来的位置始终是延迟的.
  3. 两帧激光点云中的点不可能表示的是空间中相同的位置,所以用点到点的距离作为误差方程势必会引入随机误差。
  4. 会有离群点及噪声

所以原始的ICP太粗糙,不足以应用实践。

PL-ICP

深蓝学院这一段又讲的不好,对censi论文中的PL-ICP示意图讲解不到位,对红色和蓝色的点,同心圆没有说明,而且数学公式怀疑有问题,看的云里雾里。

Point to Line-ICP修改的是误差尺度,思想和之后的ICP类似。

雷达的激光点是对实际环境中曲面的离散采样。重要的不是激光点,而是曲面。最好的误差尺度为当前激光点到实际
曲面的距离;所以关键的问题在于如何恢复曲面

PL-ICP:用分段线性的方法(折线)来对实际曲面进行近似,从而定义当前帧激光点到曲面的距离

  1. ICP对点对点的距离作为误差,PL-ICP为点到线的距离作为误差;PL-ICP的误差形式更符号实际情况。
  2. 收敛速度不同,ICP为一阶收敛,PL-ICP为二阶收敛。
  3. PL-ICP的求解精度高于ICP,特别是在结构化环境中,但不适合室外
  4. PL-ICP对初始值更敏感。不单独使用,其容易陷入局部循环。与里程计、CSM等一起使用,通常用里程计得到一个初始转换矩阵q0给到PL-ICP算法

经我测试,换成PL-ICP后,配准时间降了一个数量级,耗时比较长的是0.00274035s。但是静止时输出的帧间匹配结果的精度,没有明显提升,可能是雷达问题。


ros的csm包实现了ICP和PL-ICP算法。作者给出了一个该功能包的操作说明文件(csm_manual.pdf)。里面详细描述了各项配置参数的含义。其中sm/app文件夹中的sm0.c sm1.c sm2.c sm3.c 相当于是几个使用示例。 主要的算法实现是在csm/icp文件夹中的几个文件里。论文中的所有算法步骤完整的体现在了icp_loop.c文件中的icp_loop函数里。


AMCL的缺陷和遗留问题

缺陷

  1. 如果AMCL算法无法快速解决绑架问题,添加随机位姿的粒子可能会导致粒子集扩散。失效恢复机制有时能重定位成功(可能花十几秒时间),有时失败

  2. AMCL依赖里程计,所以误差不能太大,否则影响粒子的位姿;依赖扫描匹配,所以环境中缺乏明显的特征或有物体遮挡或者环境有明显变化时,扫描匹配结果有较大偏差,最终影响粒子权重。

  3. AMCL收敛速度不够快,在初值比较好的情况下,如果设置min_particles为300,max_particles为5000,重采样需要14~20次才能收敛到300个粒子。如果设置得更小,收敛速度就更慢。 重采样几次时,有的粒子权重还是另一些粒子权重的三四倍。

问题

  1. AMCL的运动模型是旋转——平移——旋转,但我的里程计实际是按两个时刻机器人走了一小段直线

  2. 似然域模型的高斯模糊图有什么意义,跟激光点到最近障碍的距离有什么关系

  3. AMCL出现定位抖动,很可能是里程计误差

  4. 如果出现机器人行走一段时间后,粒子集稀疏甚至彻底分散的情况。这可能是环境和地图不匹配,雷达扫描也就不匹配,测量模型出问题,结果w_diff比较大,AMCL向地图注入随机粒子

连接的master错误.png


源码分析(七)重采样

laserReceived 中的实现

以下内容在 if(lasers_update_[laser_index]) 的括号内

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
// 粒子重采样
if(!(++resample_count_ % resample_interval_))
{
pf_update_resample(pf_);
resampled = true;
}

pf_sample_set_t* set = pf_->sets + pf_->current_set;
ROS_DEBUG("Num samples: %d\n", set->sample_count);

// 发布话题particlecloud, 发布的是最终结果cloud,在机器人移动后执行
if (!m_force_update) //在运动模型那里赋值false
{
geometry_msgs::PoseArray cloud_msg;
cloud_msg.header.stamp = ros::Time::now();
cloud_msg.header.frame_id = global_frame_id_;
cloud_msg.poses.resize(set->sample_count);
for(int i=0;i<set->sample_count;i++) // 粒子个数
{
// tf::Pose转为geometry_msgs::Pose,就是用于后面发布消息
tf::poseTFToMsg(tf::Pose(tf::createQuaternionFromYaw(set->samples[i].pose.v[2]),
tf::Vector3(set->samples[i].pose.v[0],
set->samples[i].pose.v[1], 0) ),
cloud_msg.poses[i]);
}
// 构造函数中注册了话题
particlecloud_pub_.publish(cloud_msg);
}


以下内容紧接上面内容,不在if内,但还在laserReceived

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// 刚启动amcl时不执行,机器人移动后执行
if(resampled || force_publication)
{
// Read out the current hypotheses
double max_weight = 0.0;
int max_weight_hyp = -1;
std::vector<amcl_hyp_t> hyps;
hyps.resize(pf_->sets[pf_->current_set].cluster_count);
for(int hyp_count = 0; hyp_count < pf_->sets[pf_->current_set].cluster_count; hyp_count++)
{
double weight;
pf_vector_t pose_mean;
pf_matrix_t pose_cov;
if (!pf_get_cluster_stats(pf_, hyp_count, &weight, &pose_mean, &pose_cov))
{
ROS_ERROR("Couldn't get stats on cluster %d", hyp_count);
break;
}

hyps[hyp_count].weight = weight;
hyps[hyp_count].pf_pose_mean = pose_mean;
hyps[hyp_count].pf_pose_cov = pose_cov;

if(hyps[hyp_count].weight > max_weight)
{
max_weight = hyps[hyp_count].weight;
max_weight_hyp = hyp_count;
}
}

if(max_weight > 0.0)
{
ROS_DEBUG("Max weight pose: %.3f %.3f %.3f",
hyps[max_weight_hyp].pf_pose_mean.v[0],
hyps[max_weight_hyp].pf_pose_mean.v[1],
hyps[max_weight_hyp].pf_pose_mean.v[2] );
geometry_msgs::PoseWithCovarianceStamped p;
// Fill in the header
p.header.frame_id = global_frame_id_;
p.header.stamp = laser_scan->header.stamp;
// Copy in the pose
p.pose.pose.position.x = hyps[max_weight_hyp].pf_pose_mean.v[0];
p.pose.pose.position.y = hyps[max_weight_hyp].pf_pose_mean.v[1];
tf::quaternionTFToMsg(tf::createQuaternionFromYaw(hyps[max_weight_hyp].pf_pose_mean.v[2]),
p.pose.pose.orientation);
// Copy in the covariance, converting from 3-D to 6-D
pf_sample_set_t* set = pf_->sets + pf_->current_set;
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
// Report the overall filter covariance, rather than the
// covariance for the highest-weight cluster
//p.covariance[6*i+j] = hyps[max_weight_hyp].pf_pose_cov.m[i][j];
p.pose.covariance[6*i+j] = set->cov.m[i][j];
}
}
// Report the overall filter covariance, rather than the
// covariance for the highest-weight cluster
//p.covariance[6*5+5] = hyps[max_weight_hyp].pf_pose_cov.m[2][2];
p.pose.covariance[6*5+5] = set->cov.m[2][2];

pose_pub_.publish(p);
last_published_pose = p;

ROS_DEBUG("New pose: %6.3f %6.3f %6.3f",
hyps[max_weight_hyp].pf_pose_mean.v[0],
hyps[max_weight_hyp].pf_pose_mean.v[1],
hyps[max_weight_hyp].pf_pose_mean.v[2]);

// subtracting base to odom from map to base and send map to odom instead
tf::Stamped<tf::Pose> odom_to_map;
try
{
tf::Transform tmp_tf(tf::createQuaternionFromYaw(hyps[max_weight_hyp].pf_pose_mean.v[2]),
tf::Vector3(hyps[max_weight_hyp].pf_pose_mean.v[0],
hyps[max_weight_hyp].pf_pose_mean.v[1],
0.0));
tf::Stamped<tf::Pose> tmp_tf_stamped (tmp_tf.inverse(),
laser_scan->header.stamp,
base_frame_id_);
this->tf_->transformPose(odom_frame_id_,
tmp_tf_stamped,
odom_to_map);
}
catch(tf::TransformException)
{
ROS_DEBUG("Failed to subtract base to odom transform");
return;
}
latest_tf_ = tf::Transform(tf::Quaternion(odom_to_map.getRotation()),
tf::Point(odom_to_map.getOrigin()));
latest_tf_valid_ = true;
if (tf_broadcast_ == true)
{
// We want to send a transform that is good up until a
// tolerance time so that odom can be used
ros::Time transform_expiration = (laser_scan->header.stamp +
transform_tolerance_);
tf::StampedTransform tmp_tf_stamped(latest_tf_.inverse(),
transform_expiration,
global_frame_id_, odom_frame_id_);
this->tfb_->sendTransform(tmp_tf_stamped);
sent_first_transform_ = true;
}
}
else
{
ROS_ERROR("No pose!");
}
}

laserReceived到这里只剩一个else if(latest_tf_valid_)

pf_update_resample

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Resample 粒子分布
void pf_update_resample(pf_t *pf)
{
int i;
double total;
pf_sample_set_t *set_a, *set_b;
pf_sample_t *sample_a, *sample_b;

double* c;
double w_diff;
// 用来接收当前粒子集的信息
set_a = pf->sets + pf->current_set;
// 保存重采样后的粒子
set_b = pf->sets + (pf->current_set + 1) % 2;

// Build up cumulative probability table for resampling.
// TODO: Replace this with a more efficient procedure (e.g., GeneralDiscreteDistributions.html)
// 对粒子的权重进行积分,获得分布函数,后面用于重采样
c = (double*)malloc(sizeof(double)*(set_a->sample_count+1));
c[0] = 0.0;
for(i=0;i<set_a->sample_count;i++)
c[i+1] = c[i] + set_a->samples[i].weight;
// c[1] = 第一个粒子权重
// c[2] = 第一和第二粒子权重之和,以此类推 c[i] 是粒子集的 前i个粒子的权重之和
// Create the kd tree for adaptive sampling
pf_kdtree_clear(set_b->kdtree);

// Draw samples from set a to create set b.
total = 0;
set_b->sample_count = 0;
// 表示注入粒子的概率
w_diff = 1.0 - pf->w_fast / pf->w_slow;
if(w_diff < 0.0)
w_diff = 0.0;

// Can't (easily) combine low-variance sampler with KLD adaptive
// sampling, so we'll take the more traditional route.
/*
// Low-variance resampler, taken from Probabilistic Robotics, p110
count_inv = 1.0/set_a->sample_count;
r = drand48() * count_inv;
c = set_a->samples[0].weight;
i = 0;
m = 0;
*/
// 确保重采样生成的粒子集(set_b)的粒子数不超过规定的最大的粒子数
while(set_b->sample_count < pf->max_samples)
{
sample_b = set_b->samples + set_b->sample_count++;
// 产生的随机数小于w_diff时,将往set_b中随机注入粒子
if(drand48() < w_diff)
// pf->random_pose_fn 为一个函数指针,其返回一个随机位姿
sample_b->pose = (pf->random_pose_fn)(pf->random_pose_data);
else
{
// Can't (easily) combine low-variance sampler with KLD adaptive
// sampling, so we'll take the more traditional route.
/*
// Low-variance resampler, taken from Probabilistic Robotics, p110
U = r + m * count_inv;
while(U>c)
{
i++;
// Handle wrap-around by resetting counters and picking a new random
// number
if(i >= set_a->sample_count)
{
r = drand48() * count_inv;
c = set_a->samples[0].weight;
i = 0;
m = 0;
U = r + m * count_inv;
continue;
}
c += set_a->samples[i].weight;
}
m++;
*/

// Naive discrete event sampler
double r = drand48(); // drand48 返回服从均匀分布的[0.0, 1.0)之间的 double 随机数
// c[i]相当于把粒子权重以数轴距离的形式表现,查看r容易落在数轴哪个位置。
// 当set_a的第i个粒子权重很大时,r落在c[i]与c[i+1]之间的概率就很高,找符合条件的i
// 虽然这里不能保证每次都能从set_a中提取权重最大的粒子,因为r是一个随机数。但是由于粒子数量较大,
// 因此提取的大多数粒子权重还是高的,这是一个统计学的方法
for(i=0; i<set_a->sample_count; i++)
{
if((c[i] <= r) && (r < c[i+1]))
break;
}
assert(i<set_a->sample_count);

sample_a = set_a->samples + i;
assert(sample_a->weight > 0);
// 从set_a中挑选粒子赋给一个粒子,之后放进set_b
sample_b->pose = sample_a->pose;
}
sample_b->weight = 1.0;
// total之前是0
total += sample_b->weight;
// Add sample to histogram
pf_kdtree_insert(set_b->kdtree, sample_b->pose, sample_b->weight);
// 大的粒子数在定位初期能提高定位精度,但是当粒子集开始聚合后,程序便不再需要这么多的粒子
// 因此这里需要引入一个新的控制条件来节省计算资源
// pf_resample_limit根据时间与粒子集测量更新的结果,返回粒子集所需要的最佳粒子数量,
// 从而实现了粒子数量随着时间的变化而自我调整
if (set_b->sample_count > pf_resample_limit(pf, set_b->kdtree->leaf_count))
break;
}
// 重置,避免w_diff越来越大,导致大量插入随机位姿的粒子
if(w_diff > 0.0)
pf->w_slow = pf->w_fast = 0.0;

// 将set_b的新粒子插入Kd-Tree中并且对粒子的权重归一化,为了之后的聚类分析做准备
for (i = 0; i < set_b->sample_count; i++)
{
sample_b = set_b->samples + i;
sample_b->weight /= total; // 粒子集b的粒子权重都是 1/total
}
// 重新聚类分析
pf_cluster_stats(pf, set_b);
// 将set_b设为当前粒子集,参与下一次的更新循环
// 此时的set_b就对应rviz显示的粒子云
pf->current_set = (pf->current_set + 1) % 2;

pf_update_converged(pf);
free(c);
return;
}

如果w_diff在某次计算的比较大,那么就容易注入随机粒子,比如:

1
2
3
4
5
w_diff >0:  0.032770
random pose num: 160

w_diff >0: 0.040729
random pose num: 214

最后收敛后,set_b中的粒子都来自set_a,有些对应set_a中的相同的粒子;有些粒子对应set_a中权重相同的,但不是同一个的粒子