use-ikfom.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef USE_IKFOM_H
  2. #define USE_IKFOM_H
  3. #include <IKFoM_toolkit/esekfom/esekfom.hpp>
  4. typedef MTK::vect<3, double> vect3;
  5. typedef MTK::SO3<double> SO3;
  6. typedef MTK::S2<double, 98090, 10000, 1> S2; //S2 流形
  7. typedef MTK::vect<1, double> vect1;
  8. typedef MTK::vect<2, double> vect2;
  9. MTK_BUILD_MANIFOLD(state_ikfom,
  10. ((vect3, pos))
  11. ((SO3, rot))
  12. ((SO3, offset_R_L_I))
  13. ((vect3, offset_T_L_I))
  14. ((vect3, vel))
  15. ((vect3, bg))
  16. ((vect3, ba))
  17. ((S2, grav)) //S2流形,grav为负值
  18. );
  19. MTK_BUILD_MANIFOLD(input_ikfom,
  20. ((vect3, acc))
  21. ((vect3, gyro))
  22. );
  23. MTK_BUILD_MANIFOLD(process_noise_ikfom,
  24. ((vect3, ng))
  25. ((vect3, na))
  26. ((vect3, nbg))
  27. ((vect3, nba))
  28. );
  29. MTK::get_cov<process_noise_ikfom>::type process_noise_cov()
  30. {
  31. MTK::get_cov<process_noise_ikfom>::type cov = MTK::get_cov<process_noise_ikfom>::type::Zero();
  32. MTK::setDiagonal<process_noise_ikfom, vect3, 0>(cov, &process_noise_ikfom::ng, 0.0001);// 0.03
  33. MTK::setDiagonal<process_noise_ikfom, vect3, 3>(cov, &process_noise_ikfom::na, 0.0001); // *dt 0.01 0.01 * dt * dt 0.05
  34. MTK::setDiagonal<process_noise_ikfom, vect3, 6>(cov, &process_noise_ikfom::nbg, 0.00001); // *dt 0.00001 0.00001 * dt *dt 0.3 //0.001 0.0001 0.01
  35. MTK::setDiagonal<process_noise_ikfom, vect3, 9>(cov, &process_noise_ikfom::nba, 0.00001); //0.001 0.05 0.0001/out 0.01
  36. return cov;
  37. }
  38. //double L_offset_to_I[3] = {0.04165, 0.02326, -0.0284}; // Avia
  39. //vect3 Lidar_offset_to_IMU(L_offset_to_I, 3);
  40. Eigen::Matrix<double, 24, 1> get_f(state_ikfom &s, const input_ikfom &in)
  41. {
  42. Eigen::Matrix<double, 24, 1> res = Eigen::Matrix<double, 24, 1>::Zero();
  43. vect3 omega;
  44. in.gyro.boxminus(omega, s.bg);
  45. vect3 a_inertial = s.rot * (in.acc-s.ba);
  46. for(int i = 0; i < 3; i++ ){
  47. res(i) = s.vel[i];
  48. res(i + 3) = omega[i];
  49. res(i + 12) = a_inertial[i] + s.grav[i];
  50. }
  51. return res;
  52. }
  53. Eigen::Matrix<double, 24, 23> df_dx(state_ikfom &s, const input_ikfom &in)
  54. {
  55. Eigen::Matrix<double, 24, 23> cov = Eigen::Matrix<double, 24, 23>::Zero();
  56. cov.template block<3, 3>(0, 12) = Eigen::Matrix3d::Identity();
  57. vect3 acc_;
  58. in.acc.boxminus(acc_, s.ba);
  59. vect3 omega;
  60. in.gyro.boxminus(omega, s.bg);
  61. cov.template block<3, 3>(12, 3) = -s.rot.toRotationMatrix()*MTK::hat(acc_);
  62. cov.template block<3, 3>(12, 18) = -s.rot.toRotationMatrix();
  63. Eigen::Matrix<state_ikfom::scalar, 2, 1> vec = Eigen::Matrix<state_ikfom::scalar, 2, 1>::Zero();
  64. Eigen::Matrix<state_ikfom::scalar, 3, 2> grav_matrix;
  65. s.S2_Mx(grav_matrix, vec, 21);
  66. cov.template block<3, 2>(12, 21) = grav_matrix;
  67. cov.template block<3, 3>(3, 15) = -Eigen::Matrix3d::Identity();
  68. return cov;
  69. }
  70. Eigen::Matrix<double, 24, 12> df_dw(state_ikfom &s, const input_ikfom &in)
  71. {
  72. Eigen::Matrix<double, 24, 12> cov = Eigen::Matrix<double, 24, 12>::Zero();
  73. cov.template block<3, 3>(12, 3) = -s.rot.toRotationMatrix();
  74. cov.template block<3, 3>(3, 0) = -Eigen::Matrix3d::Identity();
  75. cov.template block<3, 3>(15, 6) = Eigen::Matrix3d::Identity();
  76. cov.template block<3, 3>(18, 9) = Eigen::Matrix3d::Identity();
  77. return cov;
  78. }
  79. vect3 SO3ToEuler(const SO3 &orient)
  80. {
  81. Eigen::Matrix<double, 3, 1> _ang;
  82. Eigen::Vector4d q_data = orient.coeffs().transpose();
  83. //scalar w=orient.coeffs[3], x=orient.coeffs[0], y=orient.coeffs[1], z=orient.coeffs[2];
  84. double sqw = q_data[3]*q_data[3];
  85. double sqx = q_data[0]*q_data[0];
  86. double sqy = q_data[1]*q_data[1];
  87. double sqz = q_data[2]*q_data[2];
  88. double unit = sqx + sqy + sqz + sqw; // if normalized is one, otherwise is correction factor
  89. double test = q_data[3]*q_data[1] - q_data[2]*q_data[0];
  90. if (test > 0.49999*unit) { // singularity at north pole
  91. _ang << 2 * std::atan2(q_data[0], q_data[3]), M_PI/2, 0;
  92. double temp[3] = {_ang[0] * 57.3, _ang[1] * 57.3, _ang[2] * 57.3};
  93. vect3 euler_ang(temp, 3);
  94. return euler_ang;
  95. }
  96. if (test < -0.49999*unit) { // singularity at south pole
  97. _ang << -2 * std::atan2(q_data[0], q_data[3]), -M_PI/2, 0;
  98. double temp[3] = {_ang[0] * 57.3, _ang[1] * 57.3, _ang[2] * 57.3};
  99. vect3 euler_ang(temp, 3);
  100. return euler_ang;
  101. }
  102. _ang <<
  103. std::atan2(2*q_data[0]*q_data[3]+2*q_data[1]*q_data[2] , -sqx - sqy + sqz + sqw),
  104. std::asin (2*test/unit),
  105. std::atan2(2*q_data[2]*q_data[3]+2*q_data[1]*q_data[0] , sqx - sqy - sqz + sqw);
  106. double temp[3] = {_ang[0] * 57.3, _ang[1] * 57.3, _ang[2] * 57.3};
  107. vect3 euler_ang(temp, 3);
  108. // euler_ang[0] = roll, euler_ang[1] = pitch, euler_ang[2] = yaw
  109. return euler_ang;
  110. }
  111. #endif