注解:
功能:优化Lidar的位姿,在此基础上完成低频的环境建图
解释:经过前两个节点的处理可以完成一个完整激光里程计,可以概略地估计出Lidar的相对运动,可以直接利用估计的Lidar位姿和对应时刻的量测值完成建图。但量测噪声是不可避免的,因此Lidar位姿估计偏差一定存在。通过与地图匹配的方式来优化激光的位姿,利用已构建地图对位姿估计结果进行修正。当前扫描的点云和地图中所有点云去配准,这个计算消耗太大,因此为了保证实时性,作者在这里采用了一种低频处理方法,即调用建图节点的频率仅为调用里程计节点频率的十分之一。
备注:绿色的函数指在下面解释,红色的函数指本文单独的函数解释
代码流程:
1、主函数 main
/** Main node entry point. */
int main(int argc, char **argv)
{
ros::init(argc, argv, "laserMapping");
ros::NodeHandle node;
ros::NodeHandle privateNode("~");
loam::LaserMapping laserMapping(0.1);
if (laserMapping.setup(node, privateNode)) {
// initialization successful
laserMapping.spin();
}
return 0;
}
1.构造 初始化mapping里程计和里程计tf信息
explicit LaserMapping(const float& scanPeriod = 0.1, const size_t& maxIterations = 10);
LaserMapping::LaserMapping(const float& scanPeriod, const size_t& maxIterations)
{
// initialize mapping odometry and odometry tf messages
_odomAftMapped.header.frame_id = "/camera_init";
_odomAftMapped.child_frame_id = "/aft_mapped";
_aftMappedTrans.frame_id_ = "/camera_init";
_aftMappedTrans.child_frame_id_ = "/aft_mapped";
}
2.LaserMapping类中的setup函数
1)获取激光mapping参数
scanPeriod, maxIterations ,deltaTAbort deltaRAbort ,cornerFilterSize surfaceFilterSize mapFilterSize
激光周期 , 最大迭代次数 , 优化中止阈值,【T R】用于瘦身 角点/平面点/地图点 的体素过滤器
2)发布一些话题
_pubLaserCloudSurround = node.advertise<sensor_msgs::PointCloud2>("/laser_cloud_surround", 1);
_pubLaserCloudFullRes = node.advertise<sensor_msgs::PointCloud2>("/velodyne_cloud_registered", 2);
_pubOdomAftMapped = node.advertise<nav_msgs::Odometry>("/aft_mapped_to_init", 5);
3)订阅一些话题
_subLaserCloudCornerLast = node.subscribe<sensor_msgs::PointCloud2>
("/laser_cloud_corner_last", 2, &LaserMapping::laserCloudCornerLastHandler, this);
_subLaserCloudSurfLast = node.subscribe<sensor_msgs::PointCloud2>
("/laser_cloud_surf_last", 2, &LaserMapping::laserCloudSurfLastHandler, this);
_subLaserOdometry = node.subscribe<nav_msgs::Odometry>
("/laser_odom_to_init", 5, &LaserMapping::laserOdometryHandler, this);
_subLaserCloudFullRes = node.subscribe<sensor_msgs::PointCloud2>
("/velodyne_cloud_3", 2, &LaserMapping::laserCloudFullResHandler, this);
// subscribe to IMU topic
_subImu = node.subscribe<sensor_msgs::Imu>("/imu/data", 50, &LaserMapping::imuHandler, this);
3.LaserMapping类中的spin函数
void LaserMapping::spin()
{
ros::Rate rate(100);
bool status = ros::ok();
while (status)
{
ros::spinOnce();
// try processing buffered data
process();
status = ros::ok();
rate.sleep();
}
}
void LaserMapping::process() 函数
1)无新数据时返回
hasNewData 根据时间以及flags判断
bool LaserMapping::hasNewData()
{
return _newLaserCloudCornerLast && _newLaserCloudSurfLast &&
_newLaserCloudFullRes && _newLaserOdometry &&
fabs((_timeLaserCloudCornerLast - _timeLaserOdometry).toSec()) < 0.005 &&
fabs((_timeLaserCloudSurfLast - _timeLaserOdometry).toSec()) < 0.005 &&
fabs((_timeLaserCloudFullRes - _timeLaserOdometry).toSec()) < 0.005;
}
2)重置flags标志位 reset();
void LaserMapping::reset()
{
_newLaserCloudCornerLast = false;
_newLaserCloudSurfLast = false;
_newLaserCloudFullRes = false;
_newLaserOdometry = false;
}
3)程序的主要执行 BasicLaserMapping::process
if (!BasicLaserMapping::process(fromROSTime(_timeLaserOdometry)))
return;
4)返回结果 publishResult();
根据输入输出比率发布新的地图云
if (hasFreshMap()) // publish new map cloud
publishCloudMsg(_pubLaserCloudSurround, laserCloudSurroundDS(), _timeLaserOdometry, "/camera_init");
发布转化后全分辨率输入点云的地图
publishCloudMsg(_pubLaserCloudFullRes, laserCloud(), _timeLaserOdometry, "/camera_init");
在地图匹配后发布里程计 话题/tf
geometry_msgs::Quaternion geoQuat = tf::createQuaternionMsgFromRollPitchYaw
(transformAftMapped().rot_z.rad(), -transformAftMapped().rot_x.rad(), -transformAftMapped().rot_y.rad());
_odomAftMapped.header.stamp = _timeLaserOdometry;
_odomAftMapped.pose.pose.orientation.x = -geoQuat.y;
_odomAftMapped.pose.pose.orientation.y = -geoQuat.z;
_odomAftMapped.pose.pose.orientation.z = geoQuat.x;
_odomAftMapped.pose.pose.orientation.w = geoQuat.w;
_odomAftMapped.pose.pose.position.x = transformAftMapped().pos.x();
_odomAftMapped.pose.pose.position.y = transformAftMapped().pos.y();
_odomAftMapped.pose.pose.position.z = transformAftMapped().pos.z();
_odomAftMapped.twist.twist.angular.x = transformBefMapped().rot_x.rad();
_odomAftMapped.twist.twist.angular.y = transformBefMapped().rot_y.rad();
_odomAftMapped.twist.twist.angular.z = transformBefMapped().rot_z.rad();
_odomAftMapped.twist.twist.linear.x = transformBefMapped().pos.x();
_odomAftMapped.twist.twist.linear.y = transformBefMapped().pos.y();
_odomAftMapped.twist.twist.linear.z = transformBefMapped().pos.z();
_pubOdomAftMapped.publish(_odomAftMapped);
_aftMappedTrans.stamp_ = _timeLaserOdometry;
_aftMappedTrans.setRotation(tf::Quaternion(-geoQuat.y, -geoQuat.z, geoQuat.x, geoQuat.w));
_aftMappedTrans.setOrigin(tf::Vector3(transformAftMapped().pos.x(),
transformAftMapped().pos.y(),
transformAftMapped().pos.z()));
_tfBroadcaster.sendTransform(_aftMappedTrans);
2、订阅话题
1.激光角点订阅
将时间跟新,清除上次角点,将点云数据转化为pcl, 新数据跟新标志 true
_timeLaserCloudCornerLast = cornerPointsLastMsg->header.stamp;
laserCloudCornerLast().clear();
pcl::fromROSMsg(*cornerPointsLastMsg, laserCloudCornerLast());
_newLaserCloudCornerLast = true;
2.激光平面点订阅
将时间跟新,清除上次角点,将点云数据转化为pcl, 新数据跟新标志 true
_timeLaserCloudSurfLast = surfacePointsLastMsg->header.stamp;
laserCloudSurfLast().clear();
pcl::fromROSMsg(*surfacePointsLastMsg, laserCloudSurfLast());
_newLaserCloudSurfLast = true;
3.激光所有点云 订阅
_timeLaserCloudFullRes = laserCloudFullResMsg->header.stamp;
laserCloud().clear();
pcl::fromROSMsg(*laserCloudFullResMsg, laserCloud());
_newLaserCloudFullRes = true;
4.激光里程计订阅
将时间跟新,跟新里程计,新数据跟新标志 true
_timeLaserOdometry = laserOdometry->header.stamp;
double roll, pitch, yaw;
geometry_msgs::Quaternion geoQuat = laserOdometry->pose.pose.orientation;
tf::Matrix3x3(tf::Quaternion(geoQuat.z, -geoQuat.x, -geoQuat.y, geoQuat.w)).getRPY(roll, pitch, yaw);
updateOdometry(-pitch, -yaw, roll,
laserOdometry->pose.pose.position.x,
laserOdometry->pose.pose.position.y,
laserOdometry->pose.pose.position.z);
_newLaserOdometry = true;
5.IMU订阅
跟新IMU数据,时间,roll, pitch (滚动,俯仰)
double roll, pitch, yaw;
tf::Quaternion orientation;
tf::quaternionMsgToTF(imuIn->orientation, orientation);
tf::Matrix3x3(orientation).getRPY(roll, pitch, yaw);
updateIMU({ fromROSTime(imuIn->header.stamp) , roll, pitch });
3、BasicLaserMapping类中 process 函数
1.跳过一些帧, _stackFrameNum =1,_frameCount =0 跳过一帧数据
_frameCount++;
if (_frameCount < _stackFrameNum)
{
return false;
}
_frameCount = 0;
_stackFrameNum(1), _frameCount(0), _frameCount = _stackFrameNum - 1;
2.将传入数据关联到地图坐标系 将相关坐标转移到世界坐标系下->得到可用于建图的Lidar坐标
transformAssociateToMap();
3.将上一时刻所有 角(边)特征 和 面特征 转到世界坐标系下
for (auto const& pt : _laserCloudCornerLast->points)
{
pointAssociateToMap(pt, pointSel);
_laserCloudCornerStack->push_back(pointSel);
}
for (auto const& pt : _laserCloudSurfLast->points)
{
pointAssociateToMap(pt, pointSel);
_laserCloudSurfStack->push_back(pointSel);
}
_laserCloudCornerStack _laserCloudSurfStack 存放世界坐标系下的边、面特征
4.以下为优化处理
To find correspondences for the feature points, we store the point cloud on the map, , in 10m cubic areas. The points in the cubes that intersect with are extracted and stored in a 3D KD-tree in {W}. We find the points in within a certain region (10cm × 10cm × 10cm) around the feature points.
就是说:将地图点云 保存在一个10m的立方体中,若cube中的点与当前帧中的点云 有重叠部分就把他们提取出来保存在KD树中。我们找地图 中的点时,要在特征点附近宽为10cm的立方体邻域内搜索(实际代码中是10cm×10cm×5cm)。
实际处理中,我们首先将地图点云 保存在一个大cube中,并将其分割为一些子cube。
int laserCloudCenWidth = 10; // 邻域宽度, cm为单位
int laserCloudCenHeight = 5; // 邻域高度
int laserCloudCenDepth = 10; // 邻域深度
const int laserCloudWidth = 21; // 子cube沿宽方向的分割个数
const int laserCloudHeight = 11; // 高方向个数
const int laserCloudDepth = 21; // 深度方向个数
const int laserCloudNum = laserCloudWidth * laserCloudHeight * laserCloudDepth; // 子cube总数
而后我们就要找当前估计的Lidar位姿 属于哪个子cube。I、J、K对应了cube的索引。可以看出,当坐标属于[-25,25]时,cube对应与(10,5,10)即正中心的那个cube。
5.当前Lidar坐标系{L}y轴上的一点(0,10,0) 转到世界坐标系{W}下 pointOnYAxis
pcl::PointXYZI pointOnYAxis;
pointOnYAxis.x = 0.0;
pointOnYAxis.y = 10.0;
pointOnYAxis.z = 0.0;
pointAssociateToMap(pointOnYAxis, pointOnYAxis);
6.cube中心位置索引
auto const CUBE_SIZE = 50.0;
auto const CUBE_HALF = CUBE_SIZE / 2;
int centerCubeI = int((_transformTobeMapped.pos.x() + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenWidth;
int centerCubeJ = int((_transformTobeMapped.pos.y() + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenHeight;
int centerCubeK = int((_transformTobeMapped.pos.z() + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenDepth;
if (_transformTobeMapped.pos.x() + CUBE_HALF < 0) centerCubeI--;
if (_transformTobeMapped.pos.y() + CUBE_HALF < 0) centerCubeJ--;
if (_transformTobeMapped.pos.z() + CUBE_HALF < 0) centerCubeK--;
7.如果取到的子cube在整个大cube的边缘则将点对应的cube的索引向中心方向挪动一个单位,这样做主要是截取边沿cube
while (centerCubeI < 3)
{
for (int j = 0; j < _laserCloudHeight; j++)
{
for (int k = 0; k < _laserCloudDepth; k++)
{
for (int i = _laserCloudWidth - 1; i >= 1; i--)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i - 1, j, k);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeI++;
_laserCloudCenWidth++;
}
while (centerCubeI >= _laserCloudWidth - 3)
{
for (int j = 0; j < _laserCloudHeight; j++)
{
for (int k = 0; k < _laserCloudDepth; k++)
{
for (int i = 0; i < _laserCloudWidth - 1; i++)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i + 1, j, k);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeI--;
_laserCloudCenWidth--;
}
while (centerCubeJ < 3)
{
for (int i = 0; i < _laserCloudWidth; i++)
{
for (int k = 0; k < _laserCloudDepth; k++)
{
for (int j = _laserCloudHeight - 1; j >= 1; j--)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i, j - 1, k);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeJ++;
_laserCloudCenHeight++;
}
while (centerCubeJ >= _laserCloudHeight - 3)
{
for (int i = 0; i < _laserCloudWidth; i++)
{
for (int k = 0; k < _laserCloudDepth; k++)
{
for (int j = 0; j < _laserCloudHeight - 1; j++)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i, j + 1, k);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeJ--;
_laserCloudCenHeight--;
}
while (centerCubeK < 3)
{
for (int i = 0; i < _laserCloudWidth; i++)
{
for (int j = 0; j < _laserCloudHeight; j++)
{
for (int k = _laserCloudDepth - 1; k >= 1; k--)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i, j, k - 1);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeK++;
_laserCloudCenDepth++;
}
while (centerCubeK >= _laserCloudDepth - 3)
{
for (int i = 0; i < _laserCloudWidth; i++)
{
for (int j = 0; j < _laserCloudHeight; j++)
{
for (int k = 0; k < _laserCloudDepth - 1; k++)
{
const size_t indexA = toIndex(i, j, k);
const size_t indexB = toIndex(i, j, k + 1);
std::swap(_laserCloudCornerArray[indexA], _laserCloudCornerArray[indexB]);
std::swap(_laserCloudSurfArray[indexA], _laserCloudSurfArray[indexB]);
}
}
}
centerCubeK--;
_laserCloudCenDepth--;
}
8.处理完毕边沿点,接下来就是在取到的子cube的5*5*5的邻域内找对应的配准点了。
1)首先遍历该区域
_laserCloudValidInd.clear();
_laserCloudSurroundInd.clear();
for (int i = centerCubeI - 2; i <= centerCubeI + 2; i++)
{
for (int j = centerCubeJ - 2; j <= centerCubeJ + 2; j++)
{
for (int k = centerCubeK - 2; k <= centerCubeK + 2; k++)
{
if (i >= 0 && i < _laserCloudWidth &&
j >= 0 && j < _laserCloudHeight &&
k >= 0 && k < _laserCloudDepth)
{
2)计算子cube对应的点坐标,NOTE: 由于ijk均为整数,坐标取值为中心点坐标
float centerX = 50.0f * (i - _laserCloudCenWidth);
float centerY = 50.0f * (j - _laserCloudCenHeight);
float centerZ = 50.0f * (k - _laserCloudCenDepth);
3)取邻近的8个点坐标
bool isInLaserFOV = false;
for (int ii = -1; ii <= 1; ii += 2)
{
for (int jj = -1; jj <= 1; jj += 2)
{
for (int kk = -1; kk <= 1; kk += 2)
{
pcl::PointXYZI corner;
corner.x = centerX + 25.0f * ii;
corner.y = centerY + 25.0f * jj;
corner.z = centerZ + 25.0f * kk;
float squaredSide1 = calcSquaredDiff(transform_pos, corner);
float squaredSide2 = calcSquaredDiff(pointOnYAxis, corner);
float check1 = 100.0f + squaredSide1 - squaredSide2
- 10.0f * sqrt(3.0f) * sqrt(squaredSide1);
float check2 = 100.0f + squaredSide1 - squaredSide2
+ 10.0f * sqrt(3.0f) * sqrt(squaredSide1);
if (check1 < 0 && check2 > 0)
{
isInLaserFOV = true;
}
}
}
}
4)得到 激光云有效数量 和 激光云环绕
size_t cubeIdx = i + _laserCloudWidth * j + _laserCloudWidth * _laserCloudHeight * k;
if (isInLaserFOV)
{
_laserCloudValidInd.push_back(cubeIdx);
}
_laserCloudSurroundInd.push_back(cubeIdx);
}
}
}
}
这里还需要判断一下该点是否属于当前Lidar的可视范围内,可以根据余弦公式对距离范围进行推导。根据代码中的式子,只要点在x轴±60°的范围内都认为是FOV中的点(作者这么做是因为Lidar里程计的估计结果抬不准确了,只能概略的取一个较大的范围)。于是我们就得到了在当前Lidar位置的邻域内有效的地图特征点。
所以,我们就不需要对庞大的所有地图点云进行处理了,只需要处理这些邻域cube内的地图特征点即可,可以节省大量的运算资源。为了保证当前帧的点云足够平滑,还对点云进行了滤波处理。
9.接下来就准备精度更加高的配准了,首先是准备工作,我们需要两堆进行配准的点云
_laserCloudCornerFromMap->clear();
_laserCloudSurfFromMap->clear();
for (auto const& ind : _laserCloudValidInd)
{
*_laserCloudCornerFromMap += *_laserCloudCornerArray[ind];
*_laserCloudSurfFromMap += *_laserCloudSurfArray[ind];
}
// prepare feature stack clouds for pose optimization
for (auto& pt : *_laserCloudCornerStack) //有效的特征边上的点的个数
pointAssociateTobeMapped(pt, pt);
for (auto& pt : *_laserCloudSurfStack) //所有特征面上的点的个数
pointAssociateTobeMapped(pt, pt);
// 对所有当前帧特征点进行滤波处理
// down sample feature stack clouds
_laserCloudCornerStackDS->clear();
_downSizeFilterCorner.setInputCloud(_laserCloudCornerStack);
_downSizeFilterCorner.filter(*_laserCloudCornerStackDS);
size_t laserCloudCornerStackNum = _laserCloudCornerStackDS->size();
_laserCloudSurfStackDS->clear();
_downSizeFilterSurf.setInputCloud(_laserCloudSurfStack);
_downSizeFilterSurf.filter(*_laserCloudSurfStackDS);
size_t laserCloudSurfStackNum = _laserCloudSurfStackDS->size();
_laserCloudCornerStack->clear();
_laserCloudSurfStack->clear();
获得在当前Lidar所在位置附近的所有地图特征点以及当前帧的点云特征点,接下来匹配,我们再次拿出KD树,来寻找最邻近的5个点。对点云协方差矩阵进行主成分分析:若这五个点分布在直线上,协方差矩阵的特征值包含一个元素显著大于其余两个,与该特征值相关的特征向量表示所处直线的方向;若这五个点分布在平面上,协方差矩阵的特征值存在一个显著小的元素,与该特征值相关的特征向量表示所处平面的法线方向。
因此我们可以很轻易的根据特征向量找到直线上两点从而利用论文中点到直线的距离公式构建优化问题。平面特征也是相同的思路。完成了优化问题的构建之后就可以对它进行求解了,求解方法还是L-M迭代。
9.运行位姿优化 optimizeTransformTobeMappedoptimizeTransformTobeMapped();
10.存储相应的角点/边界点
// store down sized corner stack points in corresponding cube clouds
for (int i = 0; i < laserCloudCornerStackNum; i++)
{
pointAssociateToMap(_laserCloudCornerStackDS->points[i], pointSel);
int cubeI = int((pointSel.x + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenWidth;
int cubeJ = int((pointSel.y + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenHeight;
int cubeK = int((pointSel.z + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenDepth;
if (pointSel.x + CUBE_HALF < 0) cubeI--;
if (pointSel.y + CUBE_HALF < 0) cubeJ--;
if (pointSel.z + CUBE_HALF < 0) cubeK--;
if (cubeI >= 0 && cubeI < _laserCloudWidth &&
cubeJ >= 0 && cubeJ < _laserCloudHeight &&
cubeK >= 0 && cubeK < _laserCloudDepth)
{
size_t cubeInd = cubeI + _laserCloudWidth * cubeJ + _laserCloudWidth * _laserCloudHeight * cubeK;
_laserCloudCornerArray[cubeInd]->push_back(pointSel);
}
}
// store down sized surface stack points in corresponding cube clouds
for (int i = 0; i < laserCloudSurfStackNum; i++)
{
pointAssociateToMap(_laserCloudSurfStackDS->points[i], pointSel);
int cubeI = int((pointSel.x + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenWidth;
int cubeJ = int((pointSel.y + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenHeight;
int cubeK = int((pointSel.z + CUBE_HALF) / CUBE_SIZE) + _laserCloudCenDepth;
if (pointSel.x + CUBE_HALF < 0) cubeI--;
if (pointSel.y + CUBE_HALF < 0) cubeJ--;
if (pointSel.z + CUBE_HALF < 0) cubeK--;
if (cubeI >= 0 && cubeI < _laserCloudWidth &&
cubeJ >= 0 && cubeJ < _laserCloudHeight &&
cubeK >= 0 && cubeK < _laserCloudDepth)
{
size_t cubeInd = cubeI + _laserCloudWidth * cubeJ + _laserCloudWidth * _laserCloudHeight * cubeK;
_laserCloudSurfArray[cubeInd]->push_back(pointSel);
}
}
11.缩小所有有效(在视野内)功能立方体云
for (auto const& ind : _laserCloudValidInd)
{
_laserCloudCornerDSArray[ind]->clear();
_downSizeFilterCorner.setInputCloud(_laserCloudCornerArray[ind]);
_downSizeFilterCorner.filter(*_laserCloudCornerDSArray[ind]);
_laserCloudSurfDSArray[ind]->clear();
_downSizeFilterSurf.setInputCloud(_laserCloudSurfArray[ind]);
_downSizeFilterSurf.filter(*_laserCloudSurfDSArray[ind]);
// swap cube clouds for next processing
_laserCloudCornerArray[ind].swap(_laserCloudCornerDSArray[ind]);
_laserCloudSurfArray[ind].swap(_laserCloudSurfDSArray[ind]);
}
12.将全分辨率输入云转换为map坐标系
for (auto& pt : *_laserCloudFullRes)
pointAssociateToMap(pt, pt);
13.根据输入输出比率创建新的地图云
_downsizedMapCreated = createDownsizedMap();
4、optimizeTransformTobeMapped
1.特征点过少时返回
if (_laserCloudCornerFromMap->size() <= 10 || _laserCloudSurfFromMap->size() <= 100)
return;
2.利用KD树查找最邻近的5个点,对点云协方差矩阵进行主成分分析:若这五个点分布在直线上,协方差矩阵的特征值包含一个元素显著大于其余两个,与该特征值相关的特征向量表示所处直线的方向;若这五个点分布在平面上,协方差矩阵的特征值存在一个显著小的元素,与该特征值相关的特征向量表示所处平面的法线方向。
kd树寻找最近点
std::vector<int> pointSearchInd(5, 0);
std::vector<float> pointSearchSqDis(5, 0);
kdtreeCornerFromMap.setInputCloud(_laserCloudCornerFromMap);
kdtreeSurfFromMap.setInputCloud(_laserCloudSurfFromMap);
Eigen::Matrix<float, 5, 3> matA0;
Eigen::Matrix<float, 5, 1> matB0;
Eigen::Vector3f matX0;
Eigen::Matrix3f matA1;
Eigen::Matrix<float, 1, 3> matD1;
Eigen::Matrix3f matV1;
matA0.setZero();
matB0.setConstant(-1);
matX0.setZero();
matA1.setZero();
matD1.setZero();
matV1.setZero();
bool isDegenerate = false;
Eigen::Matrix<float, 6, 6> matP;
size_t laserCloudCornerStackNum = _laserCloudCornerStackDS->size();
size_t laserCloudSurfStackNum = _laserCloudSurfStackDS->size();
3.根据迭代次数进行迭代 默认为_maxIterations =10
for (size_t iterCount = 0; iterCount < _maxIterations; iterCount++)
{
_laserCloudOri.clear();
_coeffSel.clear();
4.分别对角点(边缘点)、平面点进行优化。
直线匹配,
1>根据索引找到该特征的位置(xyz),然后将其转化到世界坐标系下,接着找到最近的5个点,记住索引和距离。
for (int i = 0; i < laserCloudCornerStackNum; i++)
{
pointOri = _laserCloudCornerStackDS->points[i];
pointAssociateToMap(pointOri, pointSel);
kdtreeCornerFromMap.nearestKSearch(pointSel, 5, pointSearchInd, pointSearchSqDis);
2>5个最近点距离 小于1m时,取5个点在xyz的均值,并且根据这些点求取直线方程
对协方差矩阵进行特征值分解,最大特征值对应的特征向量即为直线的方向向量,该直线通过边缘点的集合中心
if (pointSearchSqDis[4] < 1.0)
{
Vector3 vc(0, 0, 0);
for (int j = 0; j < 5; j++)
vc += Vector3(_laserCloudCornerFromMap->points[pointSearchInd[j]]);
vc /= 5.0;
Eigen::Matrix3f mat_a;
mat_a.setZero();
for (int j = 0; j < 5; j++)
{
Vector3 a = Vector3(_laserCloudCornerFromMap->points[pointSearchInd[j]]) - vc;
mat_a(0, 0) += a.x() * a.x();
mat_a(0, 1) += a.x() * a.y();
mat_a(0, 2) += a.x() * a.z();
mat_a(1, 1) += a.y() * a.y();
mat_a(1, 2) += a.y() * a.z();
mat_a(2, 2) += a.z() * a.z();
}
matA1 = mat_a / 5.0;
Eigen::SelfAdjointEigenSolver<Eigen::Matrix3f> esolver(matA1);
//返回给定矩阵的特征值。返回包含特征值的列向量的const引用。
matD1 = esolver.eigenvalues().real();
//返回给定矩阵的特征向量。对矩阵的const引用,该矩阵的列是特征向量。
matV1 = esolver.eigenvectors().real();
3>优化,跟激光里程计类似,平面点处理时几个点在一个平面上计算点到面的距离
if (matD1(0, 2) > 3 * matD1(0, 1))
{
float x0 = pointSel.x;
float y0 = pointSel.y;
float z0 = pointSel.z;
float x1 = vc.x() + 0.1 * matV1(0, 2);
float y1 = vc.y() + 0.1 * matV1(1, 2);
float z1 = vc.z() + 0.1 * matV1(2, 2);
float x2 = vc.x() - 0.1 * matV1(0, 2);
float y2 = vc.y() - 0.1 * matV1(1, 2);
float z2 = vc.z() - 0.1 * matV1(2, 2);
float a012 = sqrt(((x0 - x1)*(y0 - y2) - (x0 - x2)*(y0 - y1))
* ((x0 - x1)*(y0 - y2) - (x0 - x2)*(y0 - y1))
+ ((x0 - x1)*(z0 - z2) - (x0 - x2)*(z0 - z1))
* ((x0 - x1)*(z0 - z2) - (x0 - x2)*(z0 - z1))
+ ((y0 - y1)*(z0 - z2) - (y0 - y2)*(z0 - z1))
* ((y0 - y1)*(z0 - z2) - (y0 - y2)*(z0 - z1)));
float l12 = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2) + (z1 - z2)*(z1 - z2));
float la = ((y1 - y2)*((x0 - x1)*(y0 - y2) - (x0 - x2)*(y0 - y1))
+ (z1 - z2)*((x0 - x1)*(z0 - z2) - (x0 - x2)*(z0 - z1))) / a012 / l12;
float lb = -((x1 - x2)*((x0 - x1)*(y0 - y2) - (x0 - x2)*(y0 - y1))
- (z1 - z2)*((y0 - y1)*(z0 - z2) - (y0 - y2)*(z0 - z1))) / a012 / l12;
float lc = -((x1 - x2)*((x0 - x1)*(z0 - z2) - (x0 - x2)*(z0 - z1))
+ (y1 - y2)*((y0 - y1)*(z0 - z2) - (y0 - y2)*(z0 - z1))) / a012 / l12;
float ld2 = a012 / l12;
// // TODO: Why writing to a variable that's never read? Maybe it should be used afterwards?
// pointProj = pointSel;
// pointProj.x -= la * ld2;
// pointProj.y -= lb * ld2;
// pointProj.z -= lc * ld2;
float s = 1 - 0.9f * fabs(ld2);
coeff.x = s * la;
coeff.y = s * lb;
coeff.z = s * lc;
coeff.intensity = s * ld2;
if (s > 0.1)
{
_laserCloudOri.push_back(pointOri);
_coeffSel.push_back(coeff);
}
}
}
4>进行优化
float srx = _transformTobeMapped.rot_x.sin();
float crx = _transformTobeMapped.rot_x.cos();
float sry = _transformTobeMapped.rot_y.sin();
float cry = _transformTobeMapped.rot_y.cos();
float srz = _transformTobeMapped.rot_z.sin();
float crz = _transformTobeMapped.rot_z.cos();
size_t laserCloudSelNum = _laserCloudOri.size();
if (laserCloudSelNum < 50)
continue;
Eigen::Matrix<float, Eigen::Dynamic, 6> matA(laserCloudSelNum, 6);
Eigen::Matrix<float, 6, Eigen::Dynamic> matAt(6, laserCloudSelNum);
Eigen::Matrix<float, 6, 6> matAtA;
Eigen::VectorXf matB(laserCloudSelNum);
Eigen::VectorXf matAtB;
Eigen::VectorXf matX;
for (int i = 0; i < laserCloudSelNum; i++)
{
pointOri = _laserCloudOri.points[i];
coeff = _coeffSel.points[i];
float arx = (crx*sry*srz*pointOri.x + crx * crz*sry*pointOri.y - srx * sry*pointOri.z) * coeff.x
+ (-srx * srz*pointOri.x - crz * srx*pointOri.y - crx * pointOri.z) * coeff.y
+ (crx*cry*srz*pointOri.x + crx * cry*crz*pointOri.y - cry * srx*pointOri.z) * coeff.z;
float ary = ((cry*srx*srz - crz * sry)*pointOri.x
+ (sry*srz + cry * crz*srx)*pointOri.y + crx * cry*pointOri.z) * coeff.x
+ ((-cry * crz - srx * sry*srz)*pointOri.x
+ (cry*srz - crz * srx*sry)*pointOri.y - crx * sry*pointOri.z) * coeff.z;
float arz = ((crz*srx*sry - cry * srz)*pointOri.x + (-cry * crz - srx * sry*srz)*pointOri.y)*coeff.x
+ (crx*crz*pointOri.x - crx * srz*pointOri.y) * coeff.y
+ ((sry*srz + cry * crz*srx)*pointOri.x + (crz*sry - cry * srx*srz)*pointOri.y)*coeff.z;
matA(i, 0) = arx;
matA(i, 1) = ary;
matA(i, 2) = arz;
matA(i, 3) = coeff.x;
matA(i, 4) = coeff.y;
matA(i, 5) = coeff.z;
matB(i, 0) = -coeff.intensity;
}
matAt = matA.transpose();
matAtA = matAt * matA;
matAtB = matAt * matB;
matX = matAtA.colPivHouseholderQr().solve(matAtB);
if (iterCount == 0)
{
Eigen::Matrix<float, 1, 6> matE;
Eigen::Matrix<float, 6, 6> matV;
Eigen::Matrix<float, 6, 6> matV2;
Eigen::SelfAdjointEigenSolver< Eigen::Matrix<float, 6, 6> > esolver(matAtA);
matE = esolver.eigenvalues().real();
matV = esolver.eigenvectors().real();
matV2 = matV;
isDegenerate = false;
float eignThre[6] = { 100, 100, 100, 100, 100, 100 };
for (int i = 0; i < 6; i++)
{
if (matE(0, i) < eignThre[i])
{
for (int j = 0; j < 6; j++)
{
matV2(i, j) = 0;
}
isDegenerate = true;
}
else
{
break;
}
}
matP = matV.inverse() * matV2;
}
if (isDegenerate)
{
Eigen::Matrix<float, 6, 1> matX2(matX);
matX = matP * matX2;
}
_transformTobeMapped.rot_x += matX(0, 0);
_transformTobeMapped.rot_y += matX(1, 0);
_transformTobeMapped.rot_z += matX(2, 0);
_transformTobeMapped.pos.x() += matX(3, 0);
_transformTobeMapped.pos.y() += matX(4, 0);
_transformTobeMapped.pos.z() += matX(5, 0);
float deltaR = sqrt(pow(rad2deg(matX(0, 0)), 2) +
pow(rad2deg(matX(1, 0)), 2) +
pow(rad2deg(matX(2, 0)), 2));
float deltaT = sqrt(pow(matX(3, 0) * 100, 2) +
pow(matX(4, 0) * 100, 2) +
pow(matX(5, 0) * 100, 2));
if (deltaR < _deltaRAbort && deltaT < _deltaTAbort)
break;
}
评论(0)
您还未登录,请登录后发表或查看评论