State.cpp
Go to the documentation of this file.
1 /*
2  * OpenVINS: An Open Platform for Visual-Inertial Research
3  * Copyright (C) 2018-2023 Patrick Geneva
4  * Copyright (C) 2018-2023 Guoquan Huang
5  * Copyright (C) 2018-2023 OpenVINS Contributors
6  * Copyright (C) 2018-2019 Kevin Eckenhoff
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include "State.h"
23 
24 using namespace ov_core;
25 using namespace ov_type;
26 using namespace ov_msckf;
27 
28 State::State(StateOptions &options) {
29 
30  // Save our options
31  _options = options;
32 
33  // Append the imu to the state and covariance
34  int current_id = 0;
35  _imu = std::make_shared<IMU>();
36  _imu->set_local_id(current_id);
37  _variables.push_back(_imu);
38  current_id += _imu->size();
39 
40  // Append the imu intrinsics to the state and covariance
41  // NOTE: these need to be right "next" to the IMU state in the covariance
42  // NOTE: since if calibrating these will evolve / be correlated during propagation
43  _calib_imu_dw = std::make_shared<Vec>(6);
44  _calib_imu_da = std::make_shared<Vec>(6);
45  if (options.imu_model == StateOptions::ImuModel::KALIBR) {
46  // lower triangular of the matrix (column-wise)
47  Eigen::Matrix<double, 6, 1> _imu_default = Eigen::Matrix<double, 6, 1>::Zero();
48  _imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;
49  _calib_imu_dw->set_value(_imu_default);
50  _calib_imu_dw->set_fej(_imu_default);
51  _calib_imu_da->set_value(_imu_default);
52  _calib_imu_da->set_fej(_imu_default);
53  } else {
54  // upper triangular of the matrix (column-wise)
55  Eigen::Matrix<double, 6, 1> _imu_default = Eigen::Matrix<double, 6, 1>::Zero();
56  _imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;
57  _calib_imu_dw->set_value(_imu_default);
58  _calib_imu_dw->set_fej(_imu_default);
59  _calib_imu_da->set_value(_imu_default);
60  _calib_imu_da->set_fej(_imu_default);
61  }
62  _calib_imu_tg = std::make_shared<Vec>(9);
63  _calib_imu_GYROtoIMU = std::make_shared<JPLQuat>();
64  _calib_imu_ACCtoIMU = std::make_shared<JPLQuat>();
65  if (options.do_calib_imu_intrinsics) {
66 
67  // Gyroscope dw
68  _calib_imu_dw->set_local_id(current_id);
69  _variables.push_back(_calib_imu_dw);
70  current_id += _calib_imu_dw->size();
71 
72  // Accelerometer da
73  _calib_imu_da->set_local_id(current_id);
74  _variables.push_back(_calib_imu_da);
75  current_id += _calib_imu_da->size();
76 
77  // Gyroscope gravity sensitivity
78  if (options.do_calib_imu_g_sensitivity) {
79  _calib_imu_tg->set_local_id(current_id);
80  _variables.push_back(_calib_imu_tg);
81  current_id += _calib_imu_tg->size();
82  }
83 
84  // If kalibr model, R_GYROtoIMU is calibrated
85  // If rpng model, R_ACCtoIMU is calibrated
86  if (options.imu_model == StateOptions::ImuModel::KALIBR) {
87  _calib_imu_GYROtoIMU->set_local_id(current_id);
88  _variables.push_back(_calib_imu_GYROtoIMU);
89  current_id += _calib_imu_GYROtoIMU->size();
90  } else {
91  _calib_imu_ACCtoIMU->set_local_id(current_id);
92  _variables.push_back(_calib_imu_ACCtoIMU);
93  current_id += _calib_imu_ACCtoIMU->size();
94  }
95  }
96 
97  // Camera to IMU time offset
98  _calib_dt_CAMtoIMU = std::make_shared<Vec>(1);
99  if (_options.do_calib_camera_timeoffset) {
100  _calib_dt_CAMtoIMU->set_local_id(current_id);
101  _variables.push_back(_calib_dt_CAMtoIMU);
102  current_id += _calib_dt_CAMtoIMU->size();
103  }
104 
105  // Loop through each camera and create extrinsic and intrinsics
106  for (int i = 0; i < _options.num_cameras; i++) {
107 
108  // Allocate extrinsic transform
109  auto pose = std::make_shared<PoseJPL>();
110 
111  // Allocate intrinsics for this camera
112  auto intrin = std::make_shared<Vec>(8);
113 
114  // Add these to the corresponding maps
115  _calib_IMUtoCAM.insert({i, pose});
116  _cam_intrinsics.insert({i, intrin});
117 
118  // If calibrating camera-imu pose, add to variables
119  if (_options.do_calib_camera_pose) {
120  pose->set_local_id(current_id);
121  _variables.push_back(pose);
122  current_id += pose->size();
123  }
124 
125  // If calibrating camera intrinsics, add to variables
126  if (_options.do_calib_camera_intrinsics) {
127  intrin->set_local_id(current_id);
128  _variables.push_back(intrin);
129  current_id += intrin->size();
130  }
131  }
132 
133  // Finally initialize our covariance to small value
134  _Cov = std::pow(1e-3, 2) * Eigen::MatrixXd::Identity(current_id, current_id);
135 
136  // Finally, set some of our priors for our calibration parameters
137  if (_options.do_calib_imu_intrinsics) {
138  _Cov.block(_calib_imu_dw->id(), _calib_imu_dw->id(), 6, 6) = std::pow(0.005, 2) * Eigen::Matrix<double, 6, 6>::Identity();
139  _Cov.block(_calib_imu_da->id(), _calib_imu_da->id(), 6, 6) = std::pow(0.008, 2) * Eigen::Matrix<double, 6, 6>::Identity();
140  if (_options.do_calib_imu_g_sensitivity) {
141  _Cov.block(_calib_imu_tg->id(), _calib_imu_tg->id(), 9, 9) = std::pow(0.005, 2) * Eigen::Matrix<double, 9, 9>::Identity();
142  }
143  if (_options.imu_model == StateOptions::ImuModel::KALIBR) {
144  _Cov.block(_calib_imu_GYROtoIMU->id(), _calib_imu_GYROtoIMU->id(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();
145  } else {
146  _Cov.block(_calib_imu_ACCtoIMU->id(), _calib_imu_ACCtoIMU->id(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();
147  }
148  }
149  if (_options.do_calib_camera_timeoffset) {
150  _Cov(_calib_dt_CAMtoIMU->id(), _calib_dt_CAMtoIMU->id()) = std::pow(0.01, 2);
151  }
152  if (_options.do_calib_camera_pose) {
153  for (int i = 0; i < _options.num_cameras; i++) {
154  _Cov.block(_calib_IMUtoCAM.at(i)->id(), _calib_IMUtoCAM.at(i)->id(), 3, 3) = std::pow(0.005, 2) * Eigen::MatrixXd::Identity(3, 3);
155  _Cov.block(_calib_IMUtoCAM.at(i)->id() + 3, _calib_IMUtoCAM.at(i)->id() + 3, 3, 3) =
156  std::pow(0.015, 2) * Eigen::MatrixXd::Identity(3, 3);
157  }
158  }
159  if (_options.do_calib_camera_intrinsics) {
160  for (int i = 0; i < _options.num_cameras; i++) {
161  _Cov.block(_cam_intrinsics.at(i)->id(), _cam_intrinsics.at(i)->id(), 4, 4) = std::pow(1.0, 2) * Eigen::MatrixXd::Identity(4, 4);
162  _Cov.block(_cam_intrinsics.at(i)->id() + 4, _cam_intrinsics.at(i)->id() + 4, 4, 4) =
163  std::pow(0.005, 2) * Eigen::MatrixXd::Identity(4, 4);
164  }
165  }
166 }
ov_msckf
Extended Kalman Filter estimator.
Definition: VioManager.h:46
ov_msckf::StateOptions::do_calib_imu_intrinsics
bool do_calib_imu_intrinsics
Bool to determine whether or not to calibrate the IMU intrinsics.
Definition: StateOptions.h:56
ov_msckf::StateOptions
Struct which stores all our filter options.
Definition: StateOptions.h:35
ov_msckf::StateOptions::imu_model
ImuModel imu_model
What model our IMU intrinsics are.
Definition: StateOptions.h:65
ov_type
State.h
ov_msckf::StateOptions::do_calib_imu_g_sensitivity
bool do_calib_imu_g_sensitivity
Bool to determine whether or not to calibrate the Gravity sensitivity.
Definition: StateOptions.h:59
ov_core


ov_msckf
Author(s): Patrick Geneva , Kevin Eckenhoff , Guoquan Huang
autogenerated on Mon Dec 16 2024 03:06:54