gtsam
Loading...
Searching...
No Matches
AHRSFactor.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
19
20#pragma once
21
22/* GTSAM includes */
23#include <gtsam/geometry/Rot3.h>
26
27#include <iostream>
28
29#if GTSAM_ENABLE_BOOST_SERIALIZATION
30#include <boost/serialization/version.hpp>
31#endif
32
33namespace gtsam {
34
35namespace internal {
36
38template <class PIM>
39Rot3 predictAhrs(const PIM& pim, const Rot3& Ri, const Vector3& bias,
41 OptionalJacobian<3, 3> H2 = {}) {
42 Rot3 biasCorrected =
43 pim.biascorrectedDeltaRij(bias - pim.biasHat(), H2);
44
45 // The common case needs no Earth-rotation correction. The compose Jacobian
46 // with respect to its second argument is identity, so H2 is already final.
47 if (!pim.p().omegaCoriolis || pim.p().omegaCoriolis->isZero(0.0))
48 return Ri.compose(biasCorrected, H1);
49
50 // Exact rotating-frame attitude transition from Brossard et al.:
51 // Rj = Exp(-Omega * dt) * Ri * DeltaR.
52 const Rot3 gammaRotation =
53 Rot3::Expmap(-(*pim.p().omegaCoriolis) * pim.deltaTij());
54 Matrix3 D_gammaRi_Ri, D_Rj_gammaRi, D_Rj_delta;
55 const Rot3 gammaRi =
56 gammaRotation.compose(Ri, {}, H1 ? &D_gammaRi_Ri : nullptr);
57 const Rot3 Rj = gammaRi.compose(biasCorrected, H1 ? &D_Rj_gammaRi : nullptr,
58 H2 ? &D_Rj_delta : nullptr);
59
60 if (H1) *H1 = D_Rj_gammaRi * D_gammaRi_Ri;
61 if (H2) *H2 = D_Rj_delta * (*H2);
62 return Rj;
63}
64
65} // namespace internal
66
88 : public PreintegratedRotation {
89 protected:
90 Vector3 biasHat_;
94
95 public:
98
103 PreintegratedAhrsMeasurements(const std::shared_ptr<Params>& p,
104 const Vector3& biasHat = Vector3::Zero())
105 : PreintegratedRotation(p), biasHat_(biasHat) {
107 }
108
118 PreintegratedAhrsMeasurements(const std::shared_ptr<Params>& p,
119 const Vector3& bias_hat, double deltaTij,
120 const Rot3& deltaRij,
121 const Matrix3& delRdelBiasOmega,
122 const Matrix3& preint_meas_cov)
123 : PreintegratedRotation(p, deltaTij, deltaRij, delRdelBiasOmega),
124 biasHat_(bias_hat),
125 preintMeasCov_(preint_meas_cov) {}
126
127 Params& p() const { return *std::static_pointer_cast<Params>(p_); }
128 const Vector3& biasHat() const { return biasHat_; }
129 const Matrix3& preintMeasCov() const { return preintMeasCov_; }
130
132 void print(const std::string& s = "Preintegrated Measurements: ") const;
133
135 bool equals(const PreintegratedAhrsMeasurements& expected,
136 double tol = 1e-9) const;
137
139 void resetIntegration();
140
150 void integrateMeasurement(const Vector3& measuredOmega, double deltaT);
151
160 Rot3 predict(const Rot3& Ri, const Vector3& bias,
162 gtsam::OptionalJacobian<3, 3> H2 = {}) const {
163 return internal::predictAhrs(*this, Ri, bias, H1, H2);
164 }
165
166 private:
167#if GTSAM_ENABLE_BOOST_SERIALIZATION
169 friend class boost::serialization::access;
170 template <class ARCHIVE>
171 void serialize(ARCHIVE& ar, const unsigned int version) {
172 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(PreintegratedRotation);
173 ar& BOOST_SERIALIZATION_NVP(p_);
174 ar& BOOST_SERIALIZATION_NVP(biasHat_);
175 if (version > 0) {
176 ar& BOOST_SERIALIZATION_NVP(preintMeasCov_);
177 } else if (ARCHIVE::is_loading::value) {
178 preintMeasCov_.setZero();
179 }
180 }
181#endif
182};
183
212template <class PIM>
214 : public NoiseModelFactorT<Vector3, Rot3, Rot3, Vector3> {
215 using This = AHRSFactorT<PIM>;
217
218 PIM pim_;
219
220 public:
221 // Provide access to the Matrix& version of evaluateError:
223
225#if !defined(_MSC_VER) && __GNUC__ == 4 && __GNUC_MINOR__ > 5
226 typedef typename std::shared_ptr<This> shared_ptr;
227#else
228 typedef std::shared_ptr<This> shared_ptr;
229#endif
230
232 AHRSFactorT() = default;
233
241 AHRSFactorT(Key rot_i, Key rot_j, Key bias, const PIM& pim)
242 : Base(noiseModel::Gaussian::Covariance(pim.preintMeasCov()), rot_i,
243 rot_j, bias),
244 pim_(pim) {}
245
246 ~AHRSFactorT() override = default;
247
249 gtsam::NonlinearFactor::shared_ptr clone() const override {
250 return std::make_shared<This>(*this);
251 }
252
254 void print(const std::string& s,
255 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
256 std::cout << s << "AHRSFactor(" << keyFormatter(this->template key<1>())
257 << "," << keyFormatter(this->template key<2>()) << ","
258 << keyFormatter(this->template key<3>()) << ")\n";
259 pim_.print(" preintegrated measurements:");
260 this->noiseModel_->print(" noise model: ");
261 }
262
264 bool equals(const NonlinearFactor& other, double tol = 1e-9) const override {
265 const auto* expected = dynamic_cast<const This*>(&other);
266 return expected != nullptr && Base::equals(*expected, tol) &&
267 pim_.equals(expected->pim_, tol);
268 }
269
271 const PIM& preintegratedMeasurements() const { return pim_; }
272
274
276 Vector3 evaluateError(const Rot3& Ri, const Rot3& Rj, const Vector3& bias,
278 OptionalMatrixType H3) const override {
279 Matrix3 D_predict_Ri, D_predict_bias;
280 const Rot3 predictedRj = internal::predictAhrs(
281 pim_, Ri, bias, H1 ? &D_predict_Ri : nullptr,
282 H3 ? &D_predict_bias : nullptr);
283
284 Matrix3 D_error_Rj, D_error_predict;
285 const Vector3 error = Rj.logmap(
286 predictedRj, H2 ? &D_error_Rj : nullptr,
287 H1 || H3 ? &D_error_predict : nullptr);
288
289 if (H1) *H1 = D_error_predict * D_predict_Ri;
290 if (H2) *H2 = D_error_Rj;
291 if (H3) *H3 = D_error_predict * D_predict_bias;
292 return error;
293 }
294
295 private:
296#if GTSAM_ENABLE_BOOST_SERIALIZATION
298 friend class boost::serialization::access;
299 template <class ARCHIVE>
300 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
301 // NoiseModelFactor3 instead of NoiseModelFactorN for backward compatibility
302 ar& boost::serialization::make_nvp(
303 "NoiseModelFactor3", boost::serialization::base_object<Base>(*this));
304 ar& boost::serialization::make_nvp("_PIM_", pim_);
305 }
306#endif
307};
309
311
312template <>
315
316template <class PIM>
318
319} // namespace gtsam
320
321#if GTSAM_ENABLE_BOOST_SERIALIZATION
322BOOST_CLASS_VERSION(gtsam::PreintegratedAhrsMeasurements, 1)
323#endif
3D rotation represented as a rotation matrix or quaternion
Rot3 predictAhrs(const PIM &pim, const Rot3 &Ri, const Vector3 &bias, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={})
Shared AHRS orientation prediction for compatible preintegration types.
Definition AHRSFactor.h:39
Base class for noise model factors with N variables.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
All noise models live in the noiseModel namespace.
Definition LossFunctions.cpp:33
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
TangentVector logmap(const Class &g) const
logmap as required by manifold concept Applies logarithmic map to group element that takes *this to g
Definition Lie.h:161
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition Rot3.h:65
static Rot3 Expmap(const Vector3 &v, OptionalJacobian< 3, 3 > H={})
Exponential map - create a rotation from canonical coordinates using Rodrigues' formula.
Definition Rot3M.cpp:173
bool equals(const This &other, double tol=1e-9) const
check equality
Definition Factor.cpp:42
PreintegratedAHRSMeasurements accumulates (integrates) the gyroscope measurements (rotation rates) an...
Definition AHRSFactor.h:88
Rot3 predict(const Rot3 &Ri, const Vector3 &bias, gtsam::OptionalJacobian< 3, 3 > H1={}, gtsam::OptionalJacobian< 3, 3 > H2={}) const
Predict the orientation at time j, given orientation and bias at time i.
Definition AHRSFactor.h:160
Vector3 biasHat_
Angular rate bias values used during preintegration.
Definition AHRSFactor.h:90
PreintegratedAhrsMeasurements(const std::shared_ptr< Params > &p, const Vector3 &bias_hat, double deltaTij, const Rot3 &deltaRij, const Matrix3 &delRdelBiasOmega, const Matrix3 &preint_meas_cov)
Non-Default constructor, initialize with measurements.
Definition AHRSFactor.h:118
void resetIntegration()
Reset integrated quantities to zero.
Definition AHRSFactor.cpp:49
PreintegratedAhrsMeasurements(const std::shared_ptr< Params > &p, const Vector3 &biasHat=Vector3::Zero())
Default constructor, initialize with no measurements.
Definition AHRSFactor.h:103
Matrix3 preintMeasCov_
Covariance matrix of the preintegrated measurements (first-order propagation from measurementCovarian...
Definition AHRSFactor.h:91
PreintegratedAhrsMeasurements()
Default constructor, only for serialization and wrappers.
Definition AHRSFactor.h:97
An AHRSFactor is a three-way factor that is based on the preintegrated gyroscope measurements.
Definition AHRSFactor.h:214
std::shared_ptr< This > shared_ptr
Shorthand for a smart pointer to a factor.
Definition AHRSFactor.h:228
void print(const std::string &s, const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition AHRSFactor.h:254
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition AHRSFactor.h:249
AHRSFactorT()=default
Default constructor - only use for serialization.
Vector3 evaluateError(const Rot3 &Ri, const Rot3 &Rj, const Vector3 &bias, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
implement functions needed to derive from Factor
Definition AHRSFactor.h:276
AHRSFactorT(Key rot_i, Key rot_j, Key bias, const PIM &pim)
Constructor.
Definition AHRSFactor.h:241
const PIM & preintegratedMeasurements() const
Access the preintegrated measurements.
Definition AHRSFactor.h:271
bool equals(const NonlinearFactor &other, double tol=1e-9) const override
equals
Definition AHRSFactor.h:264
PreintegratedRotation()
Default constructor for serialization.
Definition PreintegratedRotation.h:173
NoiseModelFactorT()
Definition NoiseModelFactorN.h:257
virtual Vector3 evaluateError(const ValueTypes &... x, OptionalMatrixTypeT< ValueTypes >... H) const=0
Key key() const
Definition NoiseModelFactorN.h:307
Nonlinear factor base class.
Definition NonlinearFactor.h:70
double error(const Values &c) const override
Calculate the error of the factor.
Definition NonlinearFactor.cpp:146
Gaussian implements the mathematical model |R*x|^2 = |y|^2 with R'*R=inv(Sigma) where y = whiten(x) =...
Definition NoiseModel.h:192