IMUKittiExampleVO.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. close all
  2. clc
  3. import gtsam.*;
  4. disp('Example of application of ISAM2 for visual-inertial navigation on the KITTI VISION BENCHMARK SUITE (http://www.computervisiononline.com/dataset/kitti-vision-benchmark-suite)')
  5. %% Read metadata and compute relative sensor pose transforms
  6. % IMU metadata
  7. disp('-- Reading sensor metadata')
  8. IMU_metadata = importdata('KittiEquivBiasedImu_metadata.txt');
  9. IMU_metadata = cell2struct(num2cell(IMU_metadata.data), IMU_metadata.colheaders, 2);
  10. IMUinBody = Pose3.Expmap([IMU_metadata.BodyPtx; IMU_metadata.BodyPty; IMU_metadata.BodyPtz;
  11. IMU_metadata.BodyPrx; IMU_metadata.BodyPry; IMU_metadata.BodyPrz; ]);
  12. if ~IMUinBody.equals(Pose3, 1e-5)
  13. error 'Currently only support IMUinBody is identity, i.e. IMU and body frame are the same';
  14. end
  15. % VO metadata
  16. VO_metadata = importdata('KittiRelativePose_metadata.txt');
  17. VO_metadata = cell2struct(num2cell(VO_metadata.data), VO_metadata.colheaders, 2);
  18. VOinBody = Pose3.Expmap([VO_metadata.BodyPtx; VO_metadata.BodyPty; VO_metadata.BodyPtz;
  19. VO_metadata.BodyPrx; VO_metadata.BodyPry; VO_metadata.BodyPrz; ]);
  20. VOinIMU = IMUinBody.inverse().compose(VOinBody);
  21. %% Read data and change coordinate frame of GPS and VO measurements to IMU frame
  22. disp('-- Reading sensor data from file')
  23. % IMU data
  24. IMU_data = importdata('KittiEquivBiasedImu.txt');
  25. IMU_data = cell2struct(num2cell(IMU_data.data), IMU_data.colheaders, 2);
  26. imum = cellfun(@(x) x', num2cell([ [IMU_data.accelX]' [IMU_data.accelY]' [IMU_data.accelZ]' [IMU_data.omegaX]' [IMU_data.omegaY]' [IMU_data.omegaZ]' ], 2), 'UniformOutput', false);
  27. [IMU_data.acc_omega] = deal(imum{:});
  28. clear imum
  29. % VO data
  30. VO_data = importdata('KittiRelativePose.txt');
  31. VO_data = cell2struct(num2cell(VO_data.data), VO_data.colheaders, 2);
  32. % Merge relative pose fields and convert to Pose3
  33. logposes = [ [VO_data.dtx]' [VO_data.dty]' [VO_data.dtz]' [VO_data.drx]' [VO_data.dry]' [VO_data.drz]' ];
  34. logposes = num2cell(logposes, 2);
  35. relposes = arrayfun(@(x) {gtsam.Pose3.Expmap(x{:}')}, logposes);
  36. relposes = arrayfun(@(x) {VOinIMU.compose(x{:}).compose(VOinIMU.inverse())}, relposes);
  37. [VO_data.RelativePose] = deal(relposes{:});
  38. VO_data = rmfield(VO_data, { 'dtx' 'dty' 'dtz' 'drx' 'dry' 'drz' });
  39. noiseModelVO = noiseModel.Diagonal.Sigmas([ VO_metadata.RotationSigma * [1;1;1]; VO_metadata.TranslationSigma * [1;1;1] ]);
  40. clear logposes relposes
  41. %% Get initial conditions for the estimated trajectory
  42. currentPoseGlobal = Pose3;
  43. currentVelocityGlobal = LieVector([0;0;0]); % the vehicle is stationary at the beginning
  44. currentBias = imuBias.ConstantBias(zeros(3,1), zeros(3,1));
  45. sigma_init_x = noiseModel.Isotropic.Sigmas([ 1.0; 1.0; 0.01; 0.01; 0.01; 0.01 ]);
  46. sigma_init_v = noiseModel.Isotropic.Sigma(3, 1000.0);
  47. sigma_init_b = noiseModel.Isotropic.Sigmas([ 0.100; 0.100; 0.100; 5.00e-05; 5.00e-05; 5.00e-05 ]);
  48. sigma_between_b = [ IMU_metadata.AccelerometerBiasSigma * ones(3,1); IMU_metadata.GyroscopeBiasSigma * ones(3,1) ];
  49. g = [0;0;-9.8];
  50. w_coriolis = [0;0;0];
  51. %% Solver object
  52. isamParams = ISAM2Params;
  53. isamParams.setFactorization('CHOLESKY');
  54. isamParams.setRelinearizeSkip(10);
  55. isam = gtsam.ISAM2(isamParams);
  56. newFactors = NonlinearFactorGraph;
  57. newValues = Values;
  58. %% Main loop:
  59. % (1) we read the measurements
  60. % (2) we create the corresponding factors in the graph
  61. % (3) we solve the graph to obtain and optimal estimate of robot trajectory
  62. timestamps = [VO_data.Time]';
  63. timestamps = timestamps(15:end,:); % there seem to be issues with the initial IMU measurements
  64. IMUtimes = [IMU_data.Time];
  65. disp('-- Starting main loop: inference is performed at each time step, but we plot trajectory every 100 steps')
  66. for measurementIndex = 1:length(timestamps)
  67. % At each non=IMU measurement we initialize a new node in the graph
  68. currentPoseKey = symbol('x',measurementIndex);
  69. currentVelKey = symbol('v',measurementIndex);
  70. currentBiasKey = symbol('b',measurementIndex);
  71. t = timestamps(measurementIndex, 1);
  72. if measurementIndex == 1
  73. %% Create initial estimate and prior on initial pose, velocity, and biases
  74. newValues.insert(currentPoseKey, currentPoseGlobal);
  75. newValues.insert(currentVelKey, currentVelocityGlobal);
  76. newValues.insert(currentBiasKey, currentBias);
  77. newFactors.add(PriorFactorPose3(currentPoseKey, currentPoseGlobal, sigma_init_x));
  78. newFactors.add(PriorFactorLieVector(currentVelKey, currentVelocityGlobal, sigma_init_v));
  79. newFactors.add(PriorFactorConstantBias(currentBiasKey, currentBias, sigma_init_b));
  80. else
  81. t_previous = timestamps(measurementIndex-1, 1);
  82. %% Summarize IMU data between the previous GPS measurement and now
  83. IMUindices = find(IMUtimes >= t_previous & IMUtimes <= t);
  84. currentSummarizedMeasurement = gtsam.ImuFactorPreintegratedMeasurements( ...
  85. currentBias, IMU_metadata.AccelerometerSigma.^2 * eye(3), ...
  86. IMU_metadata.GyroscopeSigma.^2 * eye(3), IMU_metadata.IntegrationSigma.^2 * eye(3));
  87. for imuIndex = IMUindices
  88. accMeas = [ IMU_data(imuIndex).accelX; IMU_data(imuIndex).accelY; IMU_data(imuIndex).accelZ ];
  89. omegaMeas = [ IMU_data(imuIndex).omegaX; IMU_data(imuIndex).omegaY; IMU_data(imuIndex).omegaZ ];
  90. deltaT = IMU_data(imuIndex).dt;
  91. currentSummarizedMeasurement.integrateMeasurement(accMeas, omegaMeas, deltaT);
  92. end
  93. % Create IMU factor
  94. newFactors.add(ImuFactor( ...
  95. currentPoseKey-1, currentVelKey-1, ...
  96. currentPoseKey, currentVelKey, ...
  97. currentBiasKey, currentSummarizedMeasurement, g, w_coriolis));
  98. % LC: sigma_init_b is wrong: this should be some uncertainty on bias evolution given in the IMU metadata
  99. newFactors.add(BetweenFactorConstantBias(currentBiasKey-1, currentBiasKey, imuBias.ConstantBias(zeros(3,1), zeros(3,1)), ...
  100. noiseModel.Diagonal.Sigmas(sqrt(numel(IMUindices)) * sigma_between_b)));
  101. %% Create VO factor
  102. VOpose = VO_data(measurementIndex).RelativePose;
  103. newFactors.add(BetweenFactorPose3(currentPoseKey - 1, currentPoseKey, VOpose, noiseModelVO));
  104. % Add initial value
  105. newValues.insert(currentPoseKey, currentPoseGlobal.compose(VOpose));
  106. newValues.insert(currentVelKey, currentVelocityGlobal);
  107. newValues.insert(currentBiasKey, currentBias);
  108. % Update solver
  109. % =======================================================================
  110. isam.update(newFactors, newValues);
  111. newFactors = NonlinearFactorGraph;
  112. newValues = Values;
  113. if rem(measurementIndex,100)==0 % plot every 100 time steps
  114. cla;
  115. plot3DTrajectory(isam.calculateEstimate, 'g-');
  116. title('Estimated trajectory using ISAM2 (IMU+VO)')
  117. xlabel('[m]')
  118. ylabel('[m]')
  119. zlabel('[m]')
  120. axis equal
  121. drawnow;
  122. end
  123. % =======================================================================
  124. currentPoseGlobal = isam.calculateEstimate(currentPoseKey);
  125. currentVelocityGlobal = isam.calculateEstimate(currentVelKey);
  126. currentBias = isam.calculateEstimate(currentBiasKey);
  127. end
  128. end % end main loop
  129. disp('-- Reached end of sensor data')