参考资料:
[1]VINS-Mono源码解析(五)后端: 紧耦合优化https://blog.csdn.net/q597967420/article/details/76099443
[2]http://zhehangt.win/2018/04/24/SLAM/VINS/VINSVIO/
[3]https://www.zhihu.com/question/63754583/answer/259699612
[4]中文注释:https://github.com/castiel520/VINS-Mono/blob/master/vins_estimator/src/estimator.cpp
整体思路:
Step1:添加待优化的状态量
1.1添加p,q,speed,ba,bg
1.2添加相机和IMU外参p_cb,q_cb
1.3将优化变量存入数组.因为ceres用的是double类型的数组,所以要做vector到double类型的变换 < WINDOW_SIZE - 2))
Step2:添加残差
2.1添加边缘化残差
2.2添加IMU残差.滑动窗口中的相邻两帧之间都有一个IMU残差.滑动窗口的大小是10.共有10个IMU残差项.注意:这里的IMU项和camera项之间是有一个系数的,这个系数就是他们各自的协方差矩阵,IMU的协方差就是预计分的协方差,视觉的协方差就是一个固定的系数,f/1.5.(1.5是特征点追踪的方差)
2.3添加视觉残差.针对滑动窗口中的所有特征点.只要该特征点被观测的次数大于2次并且观测到该特征点的首帧在滑动窗口的前7才行.然后通过观测该特征点的两帧建立残差.
这里忽略闭环校正的情况
2.4然后设置求解器属性,进行求解问题.这里设置的最大迭代次数是8,最大求解时间是0.04ms,为了保证实时.
Step3:marg部分
3.1对于边缘化首帧
3.1.1把之前存的残差部分加进来
3.1.2把与首帧相关的残差项加进来,包含IMU,vision.
3.1.3计算所有残差项的残差和雅克比
3.1.4多线程构造Hx=b的结构,(需要细看)
3.1.5marg结束,调整参数块在下一次window的位置
3.2对于边缘化倒数第二帧
3.2.1如果倒数第二帧不是关键帧,保留该帧的IMU测量,去掉该帧的visual,代码中都没写.
3.2.2计算所有残差项的残差和雅克比
3.2.3多线程构造Hx=b的结构,(需要细看)
3.2.4marg结束,调整参数块在下一次window的位置
相关代码:
void Estimator::optimization()
{
ceres::Problem problem;
ceres::LossFunction *loss_function;
//loss_function = new ceres::HuberLoss(1.0);
//!设置柯西损失函数因子
loss_function = new ceres::CauchyLoss(1.0);
//!Step1:添加待优化状态量
//!Step1.1:添加[p,q](7),[speed,ba,bg](9)
for (int i = 0; i < WINDOW_SIZE + 1; i++)
{
ceres::LocalParameterization *local_parameterization = new PoseLocalParameterization();
problem.AddParameterBlock(para_Pose[i], SIZE_POSE, local_parameterization);
problem.AddParameterBlock(para_SpeedBias[i], SIZE_SPEEDBIAS);
}
//!Step1.2:添加相机与IMU的外参[p_cb,q_cb](7)
for (int i = 0; i < NUM_OF_CAM; i++)
{
ceres::LocalParameterization *local_parameterization = new PoseLocalParameterization();
problem.AddParameterBlock(para_Ex_Pose[i], SIZE_POSE, local_parameterization);
if (!ESTIMATE_EXTRINSIC)
{
ROS_DEBUG("fix extinsic param");
problem.SetParameterBlockConstant(para_Ex_Pose[i]);
}
else
ROS_DEBUG("estimate extinsic param");
}
//!将优化量存入数组
TicToc t_whole, t_prepare;
// 因为ceres用的是double数组,所以下面用vector2double做类型转换
vector2double();
//!Step2.1:添加边缘化的残差
// last_marginalization_parameter_blocks指的就是和被边缘化的变量有约束关系的变量,也就是heyijia博客中的Xb.
// 这个marginalization的结构是始终存在的,随着marg结构的不断更新,last_marginalization_parameter_blocks对应的还是滑动窗口中的变量
// last_marginalization_info 就是Xb对应的测量Zb,将这个约束作为Xb的先验,
if (last_marginalization_info)
{
// construct new marginlization_factor
MarginalizationFactor *marginalization_factor = new MarginalizationFactor(last_marginalization_info);
problem.AddResidualBlock(marginalization_factor, NULL,
last_marginalization_parameter_blocks);
}
//!Step2.2:添加IMU的residual
// 这里IMU项和camera项之间是有一个系数的,这个系数就是他们各自的协方差矩阵,IMU的协方差就是预积分的协方差(IMUFacotor::Evaluate,中添加IMU协方差,求解jacibian矩阵),
// 而camera的测量残差则是一个固定的系数.
for (int i = 0; i < WINDOW_SIZE; i++)
{
int j = i + 1;
if (pre_integrations[j]->sum_dt > 10.0)
continue;
//!添加代价函数
IMUFactor* imu_factor = new IMUFactor(pre_integrations[j]);
//!注意在添加残差的组成部分,由前后两帧的[p,q,v,b]组成,在计算雅克比的时候[p,q](7),[v,b](9)分开计算
problem.AddResidualBlock(imu_factor, NULL, para_Pose[i], para_SpeedBias[i], para_Pose[j], para_SpeedBias[j]);
}
//!Step2.3:添加视觉的residual
int f_m_cnt = 0;
int feature_index = -1;
//feature是滑动窗口内所有的特征点的集合
for (auto &it_per_id : f_manager.feature)
{
it_per_id.used_num = it_per_id.feature_per_frame.size();
// 如果这个特征点被观测的次数大于等于2 并且首次观测到该特征点的帧小于滑动窗口倒数第3帧,这个特征点就可以建立一个残差
if (!(it_per_id.used_num >= 2 && it_per_id.start_frame < WINDOW_SIZE - 2))
continue;
++feature_index;
//!得到观测到该特征点的首帧
int imu_i = it_per_id.start_frame, imu_j = imu_i - 1;
//!得到首帧观测到的特征点的归一化相机坐标
Vector3d pts_i = it_per_id.feature_per_frame[0].point;
for (auto &it_per_frame : it_per_id.feature_per_frame)
{
imu_j++;
if (imu_i == imu_j)
{
continue;
}
//!得到第二个特征点
Vector3d pts_j = it_per_frame.point;
ProjectionFactor *f = new ProjectionFactor(pts_i, pts_j);
problem.AddResidualBlock(f, loss_function, para_Pose[imu_i], para_Pose[imu_j], para_Ex_Pose[0], para_Feature[feature_index]);
f_m_cnt++;
}
}
relocalize = false;
//!Step2.3:添加视觉的residual
//!添加闭环校正时候的状态量和残差
//loop close factor
if(LOOP_CLOSURE)
{
int loop_constraint_num = 0;
//!遍历闭环检测 database
for (int k = 0; k < (int)retrive_data_vector.size(); k++)
{
//!遍历滑窗内的Keyframe
for(int i = 0; i < WINDOW_SIZE; i++)
{
//!为什么这样就闭环成功了呢?这个地方通过时间戳就闭环了?
//!建立闭环约束
if(retrive_data_vector[k].header == Headers[i].stamp.toSec())
{
relocalize = true;
ceres::LocalParameterization *local_parameterization = new PoseLocalParameterization();
problem.AddParameterBlock(retrive_data_vector[k].loop_pose, SIZE_POSE, local_parameterization);
loop_window_index = i;
loop_constraint_num++;
int retrive_feature_index = 0;
int feature_index = -1;
//!遍历滑窗内的特征点
for (auto &it_per_id : f_manager.feature)
{
it_per_id.used_num = it_per_id.feature_per_frame.size();
//!至少有两帧图像观测到该特征点且不是滑窗内的最后两帧
if (!(it_per_id.used_num >= 2 && it_per_id.start_frame < WINDOW_SIZE - 2))
continue;
++feature_index;
int start = it_per_id.start_frame;
//!如果该Feature是被滑窗中本帧之前观测到
if(start <= i)
{
while(retrive_data_vector[k].features_ids[retrive_feature_index] < it_per_id.feature_id)
{
retrive_feature_index++;
}
//!将拥有固定位姿的闭环帧加入到Visual-Inertail BA中
if(retrive_data_vector[k].features_ids[retrive_feature_index] == it_per_id.feature_id)
{
Vector3d pts_j = Vector3d(retrive_data_vector[k].measurements[retrive_feature_index].x, retrive_data_vector[k].measurements[retrive_feature_index].y, 1.0);
Vector3d pts_i = it_per_id.feature_per_frame[0].point;
ProjectionFactor *f = new ProjectionFactor(pts_i, pts_j);
problem.AddResidualBlock(f, loss_function, para_Pose[start], retrive_data_vector[k].loop_pose, para_Ex_Pose[0], para_Feature[feature_index]);
retrive_feature_index++;
}
}
}
}
}
}
ROS_DEBUG("loop constraint num: %d", loop_constraint_num);
}
ROS_DEBUG("visual measurement count: %d", f_m_cnt);
ROS_DEBUG("prepare for ceres: %f", t_prepare.toc());
//!设置求解器的属性
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_SCHUR;
//options.num_threads = 2;
options.trust_region_strategy_type = ceres::DOGLEG;
options.max_num_iterations = NUM_ITERATIONS;//8
//options.use_explicit_schur_complement = true;
//options.minimizer_progress_to_stdout = true;
//options.use_nonmonotonic_steps = true;
if (marginalization_flag == MARGIN_OLD)
options.max_solver_time_in_seconds = SOLVER_TIME * 4.0 / 5.0;
else
options.max_solver_time_in_seconds = SOLVER_TIME;//0.04ms
//!求解problem
TicToc t_solver;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
//cout << summary.BriefReport() << endl;
ROS_DEBUG("Iterations : %d", static_cast<int>(summary.iterations.size()));
ROS_DEBUG("solver costs: %f", t_solver.toc());
/*****************优化后的内容********************/
//!求解两个闭环帧之间的关系
// relative info between two loop frame
if(LOOP_CLOSURE && relocalize)
{
for (int k = 0; k < (int)retrive_data_vector.size(); k++)
{
for(int i = 0; i< WINDOW_SIZE; i++)
{
//!闭环检测成功
if(retrive_data_vector[k].header == Headers[i].stamp.toSec())
{
retrive_data_vector[k].relative_pose = true;
Matrix3d Rs_i = Quaterniond(para_Pose[i][6], para_Pose[i][3], para_Pose[i][4], para_Pose[i][5]).normalized().toRotationMatrix();
Vector3d Ps_i = Vector3d(para_Pose[i][0], para_Pose[i][1], para_Pose[i][2]);
//!闭环帧的位姿
Quaterniond Qs_loop;
Qs_loop = Quaterniond(retrive_data_vector[k].loop_pose[6], retrive_data_vector[k].loop_pose[3], retrive_data_vector[k].loop_pose[4], retrive_data_vector[k].loop_pose[5]).normalized().toRotationMatrix();
Matrix3d Rs_loop = Qs_loop.toRotationMatrix();
Vector3d Ps_loop = Vector3d( retrive_data_vector[k].loop_pose[0], retrive_data_vector[k].loop_pose[1], retrive_data_vector[k].loop_pose[2]);
//!求匹配帧到闭环帧之间的相对位姿
retrive_data_vector[k].relative_t = Rs_loop.transpose() * (Ps_i - Ps_loop);
retrive_data_vector[k].relative_q = Rs_loop.transpose() * Rs_i;
//!因为pitch和roll可观,所以仅考虑在yaw上的漂移
retrive_data_vector[k].relative_yaw = Utility::normalizeAngle(Utility::R2ypr(Rs_i).x() - Utility::R2ypr(Rs_loop).x());
//!
if (abs(retrive_data_vector[k].relative_yaw) > 30.0 || retrive_data_vector[k].relative_t.norm() > 20.0)
retrive_data_vector[k].relative_pose = false;
}
}
}
}
double2vector();
//!Step3:marg部分
//1.把之前存的残差部分加进来
//2.把与当前要marg掉的帧的所有相关残差项加进来,IMU,vision.
//3.preMarginalize-> 调用Evaluate计算所有ResidualBlock的残差和雅克比,parameter_block_data是margniliazation中存参数块的容器
//4.多线程构造Hx=b的结构,H是边缘化后的结果,First Estimate Jacobian,在X0处进行线性化,需要去看!!!!???????????????????????????
//5.marg结束,调整参数块在下一次window中对应的位置
TicToc t_whole_marginalization;
if (marginalization_flag == MARGIN_OLD)
{
MarginalizationInfo *marginalization_info = new MarginalizationInfo();
vector2double();
//! 先验误差会一直保存,而不是只使用一次
//! 如果上一次边缘化的信息存在
//! 要边缘化的参数块是 para_Pose[0] para_SpeedBias[0] 以及 para_Feature[feature_index](滑窗内的第feature_index个点的逆深度)
if (last_marginalization_info)
{
vector<int> drop_set;
for (int i = 0; i < static_cast<int>(last_marginalization_parameter_blocks.size()); i++)
{
//!查询last_marginalization_parameter_blocks中是首帧状态量的序号
if (last_marginalization_parameter_blocks[i] == para_Pose[0] ||
last_marginalization_parameter_blocks[i] == para_SpeedBias[0])
drop_set.push_back(i);
}
//! 构造边缘化的的Factor
// construct new marginlization_factor
MarginalizationFactor *marginalization_factor = new MarginalizationFactor(last_marginalization_info);
//! 添加上一次边缘化的参数块
//!cost_function, loss_function, 待估计参数(last_marginalization_parameter_blocks, drop_set)
ResidualBlockInfo *residual_block_info = new ResidualBlockInfo(marginalization_factor, NULL,
last_marginalization_parameter_blocks,
drop_set);
marginalization_info->addResidualBlockInfo(residual_block_info);
}
//!添加IMU的先验,只包含边缘化帧的IMU测量残差
//!Question:不应该是pre_integrations[0]么
//!
{
if (pre_integrations[1]->sum_dt < 10.0)
{
IMUFactor* imu_factor = new IMUFactor(pre_integrations[1]);
ResidualBlockInfo *residual_block_info = new ResidualBlockInfo(imu_factor, NULL,
vector<double *>{para_Pose[0], para_SpeedBias[0], para_Pose[1], para_SpeedBias[1]},
vector<int>{0, 1});
marginalization_info->addResidualBlockInfo(residual_block_info);
}
}
//!添加视觉的先验,只添加起始帧是旧帧且观测次数大于2的Features
{
int feature_index = -1;
//! 遍历滑窗内所有的Features
for (auto &it_per_id : f_manager.feature)
{
//! 该特征点被观测到的次数
it_per_id.used_num = it_per_id.feature_per_frame.size();
//! Feature的观测次数不小于2次,且起始帧不属于最后两帧
if (!(it_per_id.used_num >= 2 && it_per_id.start_frame < WINDOW_SIZE - 2))
continue;
++feature_index;
int imu_i = it_per_id.start_frame, imu_j = imu_i - 1;
//! 只选择被边缘化的帧的Features
if (imu_i != 0)
continue;
//! 得到该Feature在起始下的归一化坐标
Vector3d pts_i = it_per_id.feature_per_frame[0].point;
for (auto &it_per_frame : it_per_id.feature_per_frame)
{
imu_j++;
//! 不需要起始观测帧
if (imu_i == imu_j)
continue;
Vector3d pts_j = it_per_frame.point;
ProjectionFactor *f = new ProjectionFactor(pts_i, pts_j);
ResidualBlockInfo *residual_block_info = new ResidualBlockInfo(f, loss_function,
vector<double *>{para_Pose[imu_i], para_Pose[imu_j], para_Ex_Pose[0], para_Feature[feature_index]},
vector<int>{0, 3});
marginalization_info->addResidualBlockInfo(residual_block_info);
}
}
}
//! 将三个ResidualBlockInfo中的参数块综合到marginalization_info中
// 计算所有ResidualBlock(残差项)的残差和雅克比,parameter_block_data是参数块的容器
TicToc t_pre_margin;
marginalization_info->preMarginalize();
ROS_DEBUG("pre marginalization %f ms", t_pre_margin.toc());
//!
TicToc t_margin;
marginalization_info->marginalize();
ROS_DEBUG("marginalization %f ms", t_margin.toc());
//!将滑窗里关键帧位姿移位,为什么是向右移位了呢?
//! 这里是保存了所有状态量的信息,为什么没有保存逆深度的状态量呢
std::unordered_map<long, double *> addr_shift;
for (int i = 1; i <= WINDOW_SIZE; i++)
{
addr_shift[reinterpret_cast<long>(para_Pose[i])] = para_Pose[i - 1];
addr_shift[reinterpret_cast<long>(para_SpeedBias[i])] = para_SpeedBias[i - 1];
}
for (int i = 0; i < NUM_OF_CAM; i++)
addr_shift[reinterpret_cast<long>(para_Ex_Pose[i])] = para_Ex_Pose[i];
vector<double *> parameter_blocks = marginalization_info->getParameterBlocks(addr_shift);
if (last_marginalization_info)
delete last_marginalization_info;
last_marginalization_info = marginalization_info;
last_marginalization_parameter_blocks = parameter_blocks;
}
//!边缘化倒数第二帧
//如果倒数第二帧不是关键帧
//1.保留该帧的IMU测量,去掉该帧的visual,代码中都没有写.
//2.premarg
//3.marg
//4.滑动窗口移动
else
{
if (last_marginalization_info &&
std::count(std::begin(last_marginalization_parameter_blocks), std::end(last_marginalization_parameter_blocks), para_Pose[WINDOW_SIZE - 1]))
{
MarginalizationInfo *marginalization_info = new MarginalizationInfo();
vector2double();
if (last_marginalization_info)
{
vector<int> drop_set;
for (int i = 0; i < static_cast<int>(last_marginalization_parameter_blocks.size()); i++)
{
//!寻找导数第二帧的位姿
ROS_ASSERT(last_marginalization_parameter_blocks[i] != para_SpeedBias[WINDOW_SIZE - 1]);
if (last_marginalization_parameter_blocks[i] == para_Pose[WINDOW_SIZE - 1])
drop_set.push_back(i);
}
// construct new marginlization_factor
MarginalizationFactor *marginalization_factor = new MarginalizationFactor(last_marginalization_info);
ResidualBlockInfo *residual_block_info = new ResidualBlockInfo(marginalization_factor, NULL,
last_marginalization_parameter_blocks,
drop_set);
marginalization_info->addResidualBlockInfo(residual_block_info);
}
TicToc t_pre_margin;
ROS_DEBUG("begin marginalization");
marginalization_info->preMarginalize();
ROS_DEBUG("end pre marginalization, %f ms", t_pre_margin.toc());
TicToc t_margin;
ROS_DEBUG("begin marginalization");
marginalization_info->marginalize();
ROS_DEBUG("end marginalization, %f ms", t_margin.toc());
std::unordered_map<long, double *> addr_shift;
for (int i = 0; i <= WINDOW_SIZE; i++)
{
if (i == WINDOW_SIZE - 1)
continue;
else if (i == WINDOW_SIZE)
{
addr_shift[reinterpret_cast<long>(para_Pose[i])] = para_Pose[i - 1];
addr_shift[reinterpret_cast<long>(para_SpeedBias[i])] = para_SpeedBias[i - 1];
}
else
{
addr_shift[reinterpret_cast<long>(para_Pose[i])] = para_Pose[i];
addr_shift[reinterpret_cast<long>(para_SpeedBias[i])] = para_SpeedBias[i];
}
}
for (int i = 0; i < NUM_OF_CAM; i++)
addr_shift[reinterpret_cast<long>(para_Ex_Pose[i])] = para_Ex_Pose[i];
vector<double *> parameter_blocks = marginalization_info->getParameterBlocks(addr_shift);
if (last_marginalization_info)
delete last_marginalization_info;
last_marginalization_info = marginalization_info;
last_marginalization_parameter_blocks = parameter_blocks;
}
}
ROS_DEBUG("whole marginalization costs: %f", t_whole_marginalization.toc());
ROS_DEBUG("whole time for ceres: %f", t_whole.toc());
}