mars_lib 0.1.0.2abe2576fe7f
Modular and Robust Sensor-Fusion
Loading...
Searching...
No Matches
vision_sensor_class.h
Go to the documentation of this file.
1// Copyright (C) 2021 Christian Brommer, Control of Networked Systems, University of Klagenfurt, Austria.
2//
3// All rights reserved.
4//
5// This software is licensed under the terms of the BSD-2-Clause-License with
6// no commercial use allowed, the full terms of which are made available
7// in the LICENSE file. No license in patents is granted.
8//
9// You can contact the author at <christian.brommer@ieee.org>
10
11#ifndef VISIONSENSORCLASS_H
12#define VISIONSENSORCLASS_H
13
14#include <mars/core_state.h>
15#include <mars/ekf.h>
21#include <mars/time.h>
24#include <cmath>
25#include <iostream>
26#include <memory>
27#include <string>
28#include <utility>
29
30namespace mars
31{
33
35{
36public:
37 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
38
39 bool update_scale_{ true };
40
41 VisionSensorClass(const std::string& name, std::shared_ptr<CoreState> core_states, bool update_scale = true)
42 {
43 name_ = name;
44 core_states_ = std::move(core_states);
45 const_ref_to_nav_ = false;
47 update_scale_ = update_scale;
48
49 // chi2
50 chi2_.set_dof(6);
51
52 std::cout << "Created: [" << this->name_ << "] Sensor" << std::endl;
53 }
54
55 virtual ~VisionSensorClass() = default;
56
57 VisionSensorStateType get_state(const std::shared_ptr<void>& sensor_data)
58 {
59 VisionSensorData data = *static_cast<VisionSensorData*>(sensor_data.get());
60 return data.state_;
61 }
62
63 Eigen::MatrixXd get_covariance(const std::shared_ptr<void>& sensor_data)
64 {
65 VisionSensorData data = *static_cast<VisionSensorData*>(sensor_data.get());
66 return data.get_full_cov();
67 }
68
69 void set_initial_calib(std::shared_ptr<void> calibration)
70 {
71 initial_calib_ = calibration;
73 }
74
75 BufferDataType Initialize(const Time& timestamp, std::shared_ptr<void> sensor_data,
76 std::shared_ptr<CoreType> latest_core_data)
77 {
78 VisionMeasurementType measurement = *static_cast<VisionMeasurementType*>(sensor_data.get());
79
80 VisionSensorData sensor_state;
81 std::string calibration_type;
82
84 {
85 calibration_type = "Given";
86
87 VisionSensorData calib = *static_cast<VisionSensorData*>(initial_calib_.get());
88
89 sensor_state.state_ = calib.state_;
90 sensor_state.sensor_cov_ = calib.sensor_cov_;
91
92 // Overwrite the calibration between the reference world and navigation world in given sensor_state
93 if (!this->ref_to_nav_given_)
94 {
95 // The calibration between reference world and navigation world is not given.
96 // Calculate it given the current estimate and measurement
97
98 // Orientation Vision World R_vw
99
100 Eigen::Quaterniond q_wi(latest_core_data->state_.q_wi_);
101 Eigen::Quaterniond q_ic(calib.state_.q_ic_);
102 Eigen::Quaterniond q_vc(measurement.orientation_);
103 Eigen::Quaterniond q_vw = (q_wi * q_ic * q_vc.inverse()).inverse();
104
105 Eigen::Matrix3d R_wi(q_wi.toRotationMatrix());
106 Eigen::Matrix3d R_ic(q_ic.toRotationMatrix());
107
108 Eigen::Matrix3d R_vw(q_vw.toRotationMatrix());
109
110 Eigen::Vector3d p_wi(latest_core_data->state_.p_wi_);
111 Eigen::Vector3d p_ic(calib.state_.p_ic_);
112 Eigen::Vector3d p_vc(measurement.position_);
113
114 Eigen::Vector3d p_vw = -(R_vw * (p_wi + (R_wi * p_ic)) - p_vc);
115
116 sensor_state.state_.q_vw_ = q_vw;
117 sensor_state.state_.p_vw_ = p_vw;
118 }
119 std::cout << "Info: [" << name_ << "] Reference Frame initialized to:" << std::endl;
120 std::cout << "\tP_vw[m]: [" << sensor_state.state_.p_vw_.transpose() << " ]" << std::endl;
121
122 Eigen::Vector4d q_vw_out(sensor_state.state_.q_vw_.w(), sensor_state.state_.q_vw_.x(),
123 sensor_state.state_.q_vw_.y(), sensor_state.state_.q_vw_.z());
124
125 std::cout << "\tq_vw: [" << q_vw_out.transpose() << " ]" << std::endl;
126 std::cout << "\tR_vw[deg]: ["
127 << sensor_state.state_.q_vw_.toRotationMatrix().eulerAngles(0, 1, 2).transpose() * (180 / M_PI) << " ]"
128 << std::endl;
129 }
130 else
131 {
132 calibration_type = "Auto";
133
134 // Eigen::Vector3d p_wp(measurement.position_);
135 // Eigen::Quaterniond q_wp(measurement.orientation_);
136
137 // Eigen::Vector3d p_wi(latest_core_data->state_.p_wi_);
138 // Eigen::Quaterniond q_wi(latest_core_data->state_.q_wi_);
139 // Eigen::Matrix3d r_wi(q_wi.toRotationMatrix());
140
141 // Eigen::Vector3d p_ip = r_wi.transpose() * (p_wp - p_wi);
142 // Eigen::Quaterniond q_ip = q_wi.conjugate() * q_wp;
143
144 // // Calibration, position / rotation imu-pose
145 // sensor_state.state_.p_vw_ = p_ip;
146 // sensor_state.state_.q_vw_ = q_ip;
147 // sensor_state.state_.p_ic_ = p_ip;
148 // sensor_state.state_.q_ic_ = q_ip;
149 // sensor_state.state_.lambda_ = 1;
150
151 // // The covariance should enclose the initialization with a 3 Sigma bound
152 // Eigen::Matrix<double, 13, 1> std;
153 // std << 1, 1, 1, (35 * M_PI / 180), (35 * M_PI / 180), (35 * M_PI / 180);
154 // sensor_state.sensor_cov_ = std.cwiseProduct(std).asDiagonal();
155 std::cout << "Vision calibration AUTO init not implemented yet" << std::endl;
156 exit(EXIT_FAILURE);
157 }
158
159 // Bypass core state for the returned object
160 BufferDataType result(std::make_shared<CoreType>(*latest_core_data.get()),
161 std::make_shared<VisionSensorData>(sensor_state));
162
163 is_initialized_ = true;
164
165 std::cout << "Info: Initialized [" << name_ << "] with [" << calibration_type << "] Calibration at t=" << timestamp
166 << std::endl;
167
169 {
170 std::cout << "Info: [" << name_ << "] Calibration(rounded):" << std::endl;
171 std::cout << "\tP_vw[m]: [" << sensor_state.state_.p_vw_.transpose() << " ]" << std::endl;
172 std::cout << "\tR_vw[deg]: ["
173 << sensor_state.state_.q_vw_.toRotationMatrix().eulerAngles(0, 1, 2).transpose() * (180 / M_PI) << " ]"
174 << std::endl;
175 std::cout << "\tP_ic[m]: [" << sensor_state.state_.p_ic_.transpose() << " ]" << std::endl;
176 std::cout << "\tR_ic[deg]: ["
177 << sensor_state.state_.q_ic_.toRotationMatrix().eulerAngles(0, 1, 2).transpose() * (180 / M_PI) << " ]"
178 << std::endl;
179 std::cout << "\tLambda[%]: [" << sensor_state.state_.lambda_ * 100 << " ]" << std::endl;
180 }
181
182 return result;
183 }
184
185 bool CalcUpdate(const Time& /*timestamp*/, std::shared_ptr<void> measurement, const CoreStateType& prior_core_state,
186 std::shared_ptr<void> latest_sensor_data, const Eigen::MatrixXd& prior_cov,
187 BufferDataType* new_state_data)
188 {
189 // Check if updates should be performed with the sensor
190 if (!do_update_)
191 {
192 return false;
193 }
194
195 // Cast the sensor measurement and prior state information
196 VisionMeasurementType* meas = static_cast<VisionMeasurementType*>(measurement.get());
197 VisionSensorData* prior_sensor_data = static_cast<VisionSensorData*>(latest_sensor_data.get());
198
199 // Decompose sensor measurement
200 Eigen::Vector3d p_meas = meas->position_;
201 Eigen::Quaternion<double> q_meas = meas->orientation_;
202
203 // Extract sensor state
204 VisionSensorStateType prior_sensor_state(prior_sensor_data->state_);
205
206 // Generate measurement noise matrix and check
207 // if noisevalues from the measurement object should be used
208 Eigen::MatrixXd R_meas_dyn;
210 {
211 meas->get_meas_noise(&R_meas_dyn);
212 }
213 else
214 {
215 R_meas_dyn = this->R_.asDiagonal();
216 }
217 const Eigen::Matrix<double, 6, 6> R_meas = R_meas_dyn;
218
219 const int size_of_core_state = CoreStateType::size_error_;
220 const int size_of_sensor_state = prior_sensor_state.cov_size_;
221 const int size_of_full_error_state = size_of_core_state + size_of_sensor_state;
222 const Eigen::MatrixXd P = prior_cov;
223 assert(P.size() == size_of_full_error_state * size_of_full_error_state);
224
225 // Calculate the measurement jacobian H
226 const Eigen::Matrix3d I_3 = Eigen::Matrix3d::Identity();
227 const Eigen::Vector3d P_wi = prior_core_state.p_wi_;
228 const Eigen::Matrix3d R_wi = prior_core_state.q_wi_.toRotationMatrix();
229 const Eigen::Vector3d P_vw = prior_sensor_state.p_vw_;
230 const Eigen::Matrix3d R_vw = prior_sensor_state.q_vw_.toRotationMatrix();
231 const Eigen::Vector3d P_ic = prior_sensor_state.p_ic_;
232 const Eigen::Matrix3d R_ic = prior_sensor_state.q_ic_.toRotationMatrix();
233 const double L = prior_sensor_state.lambda_;
234
235 // Position
236 const Eigen::Matrix3d Hp_pwi = L * R_vw;
237 const Eigen::Matrix3d Hp_vwi = Eigen::Matrix3d::Zero();
238 const Eigen::Matrix3d Hp_rwi = -L * R_vw * R_wi * Utils::Skew(P_ic);
239 const Eigen::Matrix3d Hp_bw = Eigen::Matrix3d::Zero();
240 const Eigen::Matrix3d Hp_ba = Eigen::Matrix3d::Zero();
241
242 const Eigen::Matrix3d Hp_pvw = I_3 * L;
243 const Eigen::Matrix3d Hp_rvw = -L * R_vw * Utils::Skew(P_wi + R_wi * P_ic);
244 const Eigen::Matrix3d Hp_pic = L * R_vw * R_wi;
245 const Eigen::Matrix3d Hp_ric = Eigen::Matrix3d::Zero();
246 Eigen::Vector3d Hp_lambda;
247 if (update_scale_)
248 {
249 Hp_lambda = P_vw + R_vw * (P_wi + R_wi * P_ic);
250 }
251 else
252 {
253 Hp_lambda = Eigen::Vector3d::Zero();
254 }
255 // Assemble the jacobian for the position (horizontal)
256 // H_p = [Hp_pwi Hp_vwi Hp_rwi Hp_bw Hp_ba Hp_ip Hp_rip];
257 Eigen::MatrixXd H_p(3, Hp_pwi.cols() + Hp_vwi.cols() + Hp_rwi.cols() + Hp_bw.cols() + Hp_ba.cols() + Hp_pvw.cols() +
258 Hp_rvw.cols() + Hp_pic.cols() + Hp_ric.cols() + Hp_lambda.cols());
259
260 H_p << Hp_pwi, Hp_vwi, Hp_rwi, Hp_bw, Hp_ba, Hp_pvw, Hp_rvw, Hp_pic, Hp_ric, Hp_lambda;
261
262 // Orientation
263 const Eigen::Matrix3d Hr_pwi = Eigen::Matrix3d::Zero();
264 const Eigen::Matrix3d Hr_vwi = Eigen::Matrix3d::Zero();
265 const Eigen::Matrix3d Hr_rwi = R_ic.transpose();
266 const Eigen::Matrix3d Hr_bw = Eigen::Matrix3d::Zero();
267 const Eigen::Matrix3d Hr_ba = Eigen::Matrix3d::Zero();
268
269 const Eigen::Matrix3d Hr_pvw = Eigen::Matrix3d::Zero();
270 const Eigen::Matrix3d Hr_rvw = R_ic.transpose() * R_wi.transpose();
271 const Eigen::Matrix3d Hr_pic = Eigen::Matrix3d::Zero();
272 const Eigen::Matrix3d Hr_ric = I_3;
273 const Eigen::Vector3d Hr_lambda = Eigen::Vector3d::Zero();
274
275 // Assemble the jacobian for the orientation (horizontal)
276 // H_r = [Hr_pwi Hr_vwi Hr_rwi Hr_bw Hr_ba Hr_pip Hr_rip];
277 Eigen::MatrixXd H_r(3, Hr_pwi.cols() + Hr_vwi.cols() + Hr_rwi.cols() + Hr_bw.cols() + Hr_ba.cols() + Hr_pvw.cols() +
278 Hr_rvw.cols() + Hr_pic.cols() + Hr_ric.cols() + Hr_lambda.cols());
279 H_r << Hr_pwi, Hr_vwi, Hr_rwi, Hr_bw, Hr_ba, Hr_pvw, Hr_rvw, Hr_pic, Hr_ric, Hr_lambda;
280
281 // Combine all jacobians (vertical)
282 Eigen::MatrixXd H(H_p.rows() + H_r.rows(), H_r.cols());
283 H << H_p, H_r;
284
285 // Calculate the residual z = z~ - (estimate)
286 // Position
287 const Eigen::Vector3d p_est = (P_vw + R_vw * (P_wi + R_wi * P_ic)) * L;
288 const Eigen::Vector3d res_p = p_meas - p_est;
289
290 // Orientation
291 const Eigen::Quaternion<double> q_est =
292 prior_sensor_state.q_vw_ * prior_core_state.q_wi_ * prior_sensor_state.q_ic_;
293 const Eigen::Quaternion<double> res_q = q_est.inverse() * q_meas;
294 const Eigen::Vector3d res_r = 2 * res_q.vec() / res_q.w();
295
296 // Combine residuals (vertical)
297 residual_ = Eigen::MatrixXd(res_p.rows() + res_r.rows(), 1);
298 residual_ << res_p, res_r;
299
300 // Perform EKF calculations
301 mars::Ekf ekf(H, R_meas, residual_, P);
302 const Eigen::MatrixXd correction = ekf.CalculateCorrection(&chi2_);
303 assert(correction.size() == size_of_full_error_state * 1);
304
305 // Perform Chi2 test
306 if (!chi2_.passed_ && chi2_.do_test_)
307 {
309 return false;
310 }
311
312 Eigen::MatrixXd P_updated = ekf.CalculateCovUpdate();
313 assert(P_updated.size() == size_of_full_error_state * size_of_full_error_state);
314 P_updated = Utils::EnforceMatrixSymmetry(P_updated);
315
316 // Apply Core Correction
317 CoreStateVector core_correction = correction.block(0, 0, CoreStateType::size_error_, 1);
318 CoreStateType corrected_core_state = CoreStateType::ApplyCorrection(prior_core_state, core_correction);
319
320 // Apply Sensor Correction
321 const Eigen::MatrixXd sensor_correction = correction.block(size_of_core_state, 0, size_of_sensor_state, 1);
322 const VisionSensorStateType corrected_sensor_state = ApplyCorrection(prior_sensor_state, sensor_correction);
323
324 // Return Results
325 // CoreState data
326 CoreType core_data;
327 core_data.cov_ = P_updated.block(0, 0, CoreStateType::size_error_, CoreStateType::size_error_);
328 core_data.state_ = corrected_core_state;
329
330 // SensorState data
331 std::shared_ptr<VisionSensorData> sensor_data(std::make_shared<VisionSensorData>());
332 sensor_data->set_cov(P_updated);
333 sensor_data->state_ = corrected_sensor_state;
334
335 BufferDataType state_entry(std::make_shared<CoreType>(core_data), sensor_data);
336
338 {
339 // corrected_sensor_data.ref_to_nav = prior_ref_to_nav;
340 }
341 else
342 {
343 // TODO(chb) also estimate ref to nav
344 }
345
346 *new_state_data = state_entry;
347
348 return true;
349 }
350
352 const Eigen::MatrixXd& correction)
353 {
354 // state + error state correction
355 // with quaternion from small angle approx -> new state
356
357 // p_vw [0,1,2] 0:2
358 // q_vw [3,4,5] 3:5
359 // p_ic [6,7,8] 6:8
360 // q_ic [9,10,11] 9:11
361 // lambda [12] 12
362
363 VisionSensorStateType corrected_sensor_state;
364 corrected_sensor_state.p_vw_ = prior_sensor_state.p_vw_ + correction.block(0, 0, 3, 1);
365 corrected_sensor_state.q_vw_ =
366 Utils::ApplySmallAngleQuatCorr(prior_sensor_state.q_vw_, correction.block(3, 0, 3, 1));
367
368 corrected_sensor_state.p_ic_ = prior_sensor_state.p_ic_ + correction.block(6, 0, 3, 1);
369 corrected_sensor_state.q_ic_ =
370 Utils::ApplySmallAngleQuatCorr(prior_sensor_state.q_ic_, correction.block(9, 0, 3, 1));
371
372 corrected_sensor_state.lambda_ = prior_sensor_state.lambda_ + correction(12);
373
374 return corrected_sensor_state;
375 }
376};
377} // namespace mars
378
379#endif // VISIONSENSORCLASS_H
bool has_meas_noise
Definition measurement_base_class.h:23
bool get_meas_noise(Eigen::MatrixXd *meas_noise)
get the measurement noise associated with the current sensor measurement
Definition measurement_base_class.h:25
int cov_size_
Definition base_states.h:25
The BaseSensorData class binds the sensor state and covariance matrix.
Definition bind_sensor_data.h:29
EIGEN_MAKE_ALIGNED_OPERATOR_NEW T state_
Definition bind_sensor_data.h:35
Eigen::MatrixXd get_full_cov() const
get_full_cov builds the full covariance matrix
Definition bind_sensor_data.h:63
Eigen::MatrixXd sensor_cov_
covariance of the sensor states
Definition bind_sensor_data.h:37
The BufferDataType binds the core and sensor state in form of a shared void pointer.
Definition buffer_data_type.h:36
bool passed_
Determine if the test is performed or not.
Definition ekf.h:84
bool do_test_
Upper critival value.
Definition ekf.h:83
void set_dof(const int &value)
set_dof Set degree of freedom for the X2 distribution
void PrintReport(const std::string &name)
PrintReport Print a formated report e.g. if the test did not pass.
Definition core_state_type.h:21
static constexpr int size_error_
Definition core_state_type.h:38
Eigen::Vector3d p_wi_
Definition core_state_type.h:27
Eigen::Quaternion< double > q_wi_
Definition core_state_type.h:29
static CoreStateType ApplyCorrection(CoreStateType state_prior, Eigen::Matrix< double, CoreStateType::size_error_, 1 > correction)
ApplyCorrection.
Definition core_state_type.h:46
Definition core_type.h:19
CoreStateMatrix cov_
Definition core_type.h:22
CoreStateType state_
Definition core_type.h:21
Definition ekf.h:92
Eigen::MatrixXd CalculateCovUpdate()
CalculateCovUpdate Updating the state covariance after the state update.
Eigen::MatrixXd CalculateCorrection()
Kalman gain.
std::string name_
Name of the individual sensor instance.
Definition sensor_abs_class.h:23
bool is_initialized_
True if the sensor has been initialized.
Definition sensor_abs_class.h:24
bool do_update_
True if updates should be performed with the sensor.
Definition sensor_abs_class.h:25
bool const_ref_to_nav_
True if the reference should not be estimated.
Definition sensor_abs_class.h:27
bool ref_to_nav_given_
True if the reference to the navigation frame is given by initial calibration.
Definition sensor_abs_class.h:28
bool use_dynamic_meas_noise_
True if dynamic noise values from measurements should be used.
Definition sensor_abs_class.h:29
Definition time.h:20
Definition update_sensor_abs_class.h:24
bool initial_calib_provided_
True if an initial calibration was provided.
Definition update_sensor_abs_class.h:38
std::shared_ptr< CoreState > core_states_
Definition update_sensor_abs_class.h:42
Eigen::VectorXd R_
Measurement noise "squared".
Definition update_sensor_abs_class.h:32
std::shared_ptr< void > initial_calib_
Definition update_sensor_abs_class.h:37
Chi2 chi2_
Definition update_sensor_abs_class.h:40
Eigen::MatrixXd residual_
Definition update_sensor_abs_class.h:31
static Eigen::MatrixXd EnforceMatrixSymmetry(const Eigen::Ref< const Eigen::MatrixXd > &mat_in)
EnforceMatrixSymmetry.
static Eigen::Matrix3d Skew(const Eigen::Vector3d &v)
skew generate the skew symmetric matrix of v
static Eigen::Quaterniond ApplySmallAngleQuatCorr(const Eigen::Quaterniond &q_prior, const Eigen::Vector3d &correction)
ApplySmallAngleQuatCorr.
Definition vision_measurement_type.h:21
Eigen::Quaternion< double > orientation_
Quaternion [w x y z].
Definition vision_measurement_type.h:26
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Vector3d position_
Position [x y z].
Definition vision_measurement_type.h:25
Definition vision_sensor_class.h:35
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool update_scale_
Definition vision_sensor_class.h:39
Eigen::MatrixXd get_covariance(const std::shared_ptr< void > &sensor_data)
get_covariance Resolves a void pointer to the covariance matrix of the corresponding sensor type Each...
Definition vision_sensor_class.h:63
VisionSensorClass(const std::string &name, std::shared_ptr< CoreState > core_states, bool update_scale=true)
Definition vision_sensor_class.h:41
VisionSensorStateType get_state(const std::shared_ptr< void > &sensor_data)
Definition vision_sensor_class.h:57
bool CalcUpdate(const Time &, std::shared_ptr< void > measurement, const CoreStateType &prior_core_state, std::shared_ptr< void > latest_sensor_data, const Eigen::MatrixXd &prior_cov, BufferDataType *new_state_data)
CalcUpdate Calculates the update for an individual sensor definition.
Definition vision_sensor_class.h:185
void set_initial_calib(std::shared_ptr< void > calibration)
set_initial_calib Sets the calibration of an individual sensor
Definition vision_sensor_class.h:69
VisionSensorStateType ApplyCorrection(const VisionSensorStateType &prior_sensor_state, const Eigen::MatrixXd &correction)
Definition vision_sensor_class.h:351
BufferDataType Initialize(const Time &timestamp, std::shared_ptr< void > sensor_data, std::shared_ptr< CoreType > latest_core_data)
Initialize the state of an individual sensor.
Definition vision_sensor_class.h:75
virtual ~VisionSensorClass()=default
Definition vision_sensor_state_type.h:20
double lambda_
Definition vision_sensor_state_type.h:28
Eigen::Quaternion< double > q_vw_
Definition vision_sensor_state_type.h:25
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Eigen::Vector3d p_vw_
Definition vision_sensor_state_type.h:24
Eigen::Quaternion< double > q_ic_
Definition vision_sensor_state_type.h:27
Eigen::Vector3d p_ic_
Definition vision_sensor_state_type.h:26
Definition buffer.h:27
Eigen::Matrix< double, CoreStateType::size_error_, 1 > CoreStateVector
Definition core_state_type.h:135