testMagFactor.cpp
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 
20 #include <gtsam/base/Testable.h>
22 
24 
26 
27 using namespace std;
28 using namespace gtsam;
29 using namespace GeographicLib;
30 
31 namespace {
32 // Convert from Mag to ENU
33 // ENU Origin is where the plane was in hold next to runway
34 // const double lat0 = 33.86998, lon0 = -84.30626, h0 = 274;
35 
36 // Get field from http://www.ngdc.noaa.gov/geomag-web/#igrfwmm
37 // Declination = -4.94 degrees (West), Inclination = 62.78 degrees Down
38 // As NED vector, in nT:
39 Point3 nM(22653.29982, -1956.83010, 44202.47862);
40 // Let's assume scale factor,
41 double scale = 255.0 / 50000.0;
42 // ...ground truth orientation,
43 Rot3 nRb = Rot3::Yaw(-0.1);
44 Rot2 theta = nRb.yaw();
45 // ...and bias
46 Point3 bias(10, -10, 50);
47 // ... then we measure
48 Point3 scaled = scale * nM;
49 Point3 measured = nRb.inverse() * (scale * nM) + bias;
50 
51 double s(scale* nM.norm());
52 Unit3 dir(nM);
53 
54 SharedNoiseModel model = noiseModel::Isotropic::Sigma(3, 0.25);
55 } // namespace
56 
57 // *************************************************************************
59  Matrix H;
60  Point3 expected(22735.5, 314.502, 44202.5);
61  EXPECT( assert_equal(expected, MagFactor::unrotate(theta,nM,&H),1e-1));
62  EXPECT(assert_equal(numericalDerivative11<Point3, Rot2> //
63  (std::bind(&MagFactor::unrotate, std::placeholders::_1, nM, OptionalNone), theta), H, 1e-6));
64 }
65 
66 // *************************************************************************
67 TEST( MagFactor, Factors ) {
68 
69  Matrix H1, H2, H3;
70 
71  // MagFactor
72  MagFactor f(1, measured, s, dir, bias, model);
73  EXPECT(assert_equal(Z_3x1,f.evaluateError(theta,H1),1e-5));
74  EXPECT(assert_equal((Matrix)numericalDerivative11<Vector, Rot2>
75  ([&f] (const Rot2& theta) { return f.evaluateError(theta); }, theta), H1, 1e-7));
76 
77  // MagFactor1
78  MagFactor1 f1(1, measured, s, dir, bias, model);
79  EXPECT(assert_equal(Z_3x1,f1.evaluateError(nRb,H1),1e-5));
80  EXPECT(assert_equal(numericalDerivative11<Vector, Rot3>
81  ([&f1] (const Rot3& nRb) { return f1.evaluateError(nRb); }, nRb), H1, 1e-7));
82 
83  // MagFactor2
84  MagFactor2 f2(1, 2, measured, nRb, model);
85  EXPECT(assert_equal(Z_3x1,f2.evaluateError(scaled,bias,H1,H2),1e-5));
86  EXPECT(assert_equal(numericalDerivative11<Vector, Point3> //
87  ([&f2] (const Point3& scaled) { return f2.evaluateError(scaled,bias); }, scaled), H1, 1e-7));
88  EXPECT(assert_equal(numericalDerivative11<Vector, Point3> //
89  ([&f2] (const Point3& bias) { return f2.evaluateError(scaled,bias); }, bias), H2, 1e-7));
90 
91  // MagFactor3
92  MagFactor3 f3(1, 2, 3, measured, nRb, model);
93  EXPECT(assert_equal(Z_3x1,f3.evaluateError(s,dir,bias,H1,H2,H3),1e-5));
94  EXPECT(assert_equal((Matrix)numericalDerivative11<Vector,double> //
95  ([&f3] (double s) { return f3.evaluateError(s,dir,bias); }, s), H1, 1e-7));
96  EXPECT(assert_equal(numericalDerivative11<Vector,Unit3> //
97  ([&f3] (const Unit3& dir) { return f3.evaluateError(s,dir,bias); }, dir), H2, 1e-7));
98  EXPECT(assert_equal(numericalDerivative11<Vector,Point3> //
99  ([&f3] (const Point3& bias) { return f3.evaluateError(s,dir,bias); }, bias), H3, 1e-7));
100 }
101 
102 // *************************************************************************
103 int main() {
104  TestResult tr;
105  return TestRegistry::runAllTests(tr);
106 }
107 // *************************************************************************
Point2 measured(-17, 30)
static const Eigen::MatrixBase< Vector3 >::ConstantReturnType Z_3x1
Definition: Vector.h:46
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
Header for GeographicLib::LocalCartesian class.
noiseModel::Diagonal::shared_ptr model
Matrix expected
Definition: testMatrix.cpp:971
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Definition: BFloat16.h:88
Some functions to compute numerical derivatives.
double f2(const Vector2 &x)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange [*:*] noreverse nowriteback set trange [*:*] noreverse nowriteback set urange [*:*] noreverse nowriteback set vrange [*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
Rot3 inverse() const
inverse of a rotation
Definition: Rot3.h:308
#define OptionalNone
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
const Rot3 nRb
Factors involving magnetometers.
P unrotate(const T &r, const P &pt)
Definition: lieProxies.h:50
#define EXPECT(condition)
Definition: Test.h:150
Vector evaluateError(const Rot2 &nRb, OptionalMatrixType H) const override
vector of errors
Definition: MagFactor.h:80
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Namespace for GeographicLib.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
traits
Definition: chartTesting.h:28
Point2 f1(const Point3 &p, OptionalJacobian< 2, 3 > H)
double f3(double x1, double x2)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
double yaw(OptionalJacobian< 1, 3 > H={}) const
Definition: Rot3.cpp:218
Vector3 Point3
Definition: Point3.h:38
TEST(MagFactor, unrotate)
int main()
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:741


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:42