testSimpleHelicopter.cpp
Go to the documentation of this file.
1 
10 #include "gtsam/base/Vector.h"
11 #include "gtsam/geometry/Pose3.h"
12 
13 /* ************************************************************************* */
14 using namespace std::placeholders;
15 using namespace gtsam;
16 using namespace gtsam::symbol_shorthand;
17 
18 const double tol=1e-5;
19 const double h = 0.01;
20 
21 //const double deg2rad = M_PI/180.0;
22 //Pose3 g1(Rot3::Ypr(deg2rad*10.0, deg2rad*20.0, deg2rad*30.0), Point3(100.0, 200.0, 300.0));
23 Pose3 g1(Rot3(), Point3(100.0, 0.0, 300.0));
24 //Vector6 v1((Vector(6) << 0.1, 0.05, 0.02, 10.0, 20.0, 30.0).finished());
25 Vector6 V1_w((Vector(6) << 0.0, 0.0, M_PI/3, 0.0, 0.0, 30.0).finished());
26 Vector6 V1_g1 = g1.inverse().Adjoint(V1_w);
28 //Vector6 v2 = Pose3::Logmap(g1.between(g2));
29 
30 double mass = 100.0;
31 Vector gamma2 = Vector2(0.0, 0.0); // no shape
32 Vector u2 = Vector2(0.0, 0.0); // no control at time 2
33 double distT = 1.0; // distance from the body-centered x axis to the big top motor
34 double distR = 5.0; // distance from the body-centered z axis to the small motor
35 Matrix Mass = ((Vector(3) << mass, mass, mass).finished()).asDiagonal();
36 Matrix Inertia = (Vector(6) << 2.0/5.0*mass*distR*distR, 2.0/5.0*mass*distR*distR, 2.0/5.0*mass*distR*distR, mass, mass, mass).finished().asDiagonal();
37 
38 Vector computeFu(const Vector& gamma, const Vector& control) {
39  double gamma_r = gamma(0), gamma_p = gamma(1);
40 
41  Matrix F = (Matrix(6, 2) << distT*sin(gamma_r), 0.0,
42  distT*sin(gamma_p*cos(gamma_r)), 0.0,
43  0.0, distR,
44  sin(gamma_p)*cos(gamma_r), 0.0,
45  -sin(gamma_r), -1.0,
46  cos(gamma_p)*sin(gamma_r), 0.0
47  ).finished();
48  return F*control;
49 }
50 
51 /* ************************************************************************* */
52 TEST( Reconstruction, evaluateError) {
53  // hard constraints don't need a noise model
54  Reconstruction constraint(G(2), G(1), V(1), h);
55 
56  // verify error function
57  Matrix H1, H2, H3;
58  EXPECT(
59  assert_equal(Z_6x1, constraint.evaluateError(g2, g1, V1_g1, H1, H2, H3), tol));
60 
61  std::function<Vector(const Pose3&, const Pose3&, const Vector6&)> f =
62  [&constraint](const Pose3& a1, const Pose3& a2, const Vector6& a3) {
63  return constraint.evaluateError(a1, a2, a3);
64  };
65 
66  Matrix numericalH1 = numericalDerivative31(f, g2, g1, V1_g1, 1e-5);
67 
68  Matrix numericalH2 = numericalDerivative32(f, g2, g1, V1_g1, 1e-5);
69 
70  Matrix numericalH3 = numericalDerivative33(f, g2, g1, V1_g1, 1e-5);
71 
72  EXPECT(assert_equal(numericalH1,H1,1e-5));
73  EXPECT(assert_equal(numericalH2,H2,1e-5));
74 #ifdef GTSAM_USE_QUATERNIONS // TODO: why is the quaternion version much less accurate??
75  EXPECT(assert_equal(numericalH3,H3,1e-3));
76 #else
77  EXPECT(assert_equal(numericalH3,H3,1e-3));
78 #endif
79 }
80 
81 /* ************************************************************************* */
82 // Implement Newton-Euler equation for rigid body dynamics
83 Vector newtonEuler(const Vector& Vb, const Vector& Fb, const Matrix& Inertia) {
84  Matrix W = Pose3::adjointMap((Vector(6) << Vb(0), Vb(1), Vb(2), 0., 0., 0.).finished());
85  Vector dV = Inertia.inverse()*(Fb - W*Inertia*Vb);
86  return dV;
87 }
88 
90  Vector Fu = computeFu(gamma2, u2);
91  Vector fGravity_g1 = Z_6x1;
92  fGravity_g1.segment<3>(3) = g1.rotation().unrotate(Vector3(0, 0, -mass*9.81)); // gravity force in g1 frame
93  Vector Fb = Fu+fGravity_g1;
94 
95  Vector dV = newtonEuler(V1_g1, Fb, Inertia);
96  Vector V2_g1 = dV*h + V1_g1;
97  Pose3 g21 = g2.between(g1);
98  Vector V2_g2 = g21.Adjoint(V2_g1); // convert the new velocity to g2's frame
99 
100  Vector6 expectedv2(V2_g2);
101 
102  // hard constraints don't need a noise model
103  DiscreteEulerPoincareHelicopter constraint(V(2), V(1), G(2), h,
104  Inertia, Fu, mass);
105 
106  // verify error function
107  Matrix H1, H2, H3;
108  EXPECT(assert_equal(Z_6x1, constraint.evaluateError(expectedv2, V1_g1, g2, H1, H2, H3), 1e0));
109 
110  std::function<Vector(const Vector6&, const Vector6&, const Pose3&)> f =
111  [&constraint](const Vector6& a1, const Vector6& a2, const Pose3& a3) {
112  return constraint.evaluateError(a1, a2, a3);
113  };
114 
115  Matrix numericalH1 = numericalDerivative31(f, expectedv2, V1_g1, g2, 1e-5);
116 
117  Matrix numericalH2 = numericalDerivative32(f, expectedv2, V1_g1, g2, 1e-5);
118 
119  Matrix numericalH3 = numericalDerivative33(f, expectedv2, V1_g1, g2, 1e-5);
120 
121  EXPECT(assert_equal(numericalH1,H1,1e-5));
122  EXPECT(assert_equal(numericalH2,H2,1e-5));
123  EXPECT(assert_equal(numericalH3,H3,5e-5));
124 }
125 
126 /* ************************************************************************* */
127 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
128 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::LieGroup::between
Class between(const Class &g) const
Definition: Lie.h:52
gtsam::numericalDerivative33
internal::FixedSizeMatrix< Y, X3 >::type numericalDerivative33(std::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Definition: numericalDerivative.h:292
main
int main()
Definition: testSimpleHelicopter.cpp:127
mass
double mass
Definition: testSimpleHelicopter.cpp:30
Vector.h
typedef and functions to augment Eigen's VectorXd
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
ceres::sin
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:439
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
gtsam::Vector2
Eigen::Vector2d Vector2
Definition: Vector.h:43
TestHarness.h
g1
Pose3 g1(Rot3(), Point3(100.0, 0.0, 300.0))
Inertia
Matrix Inertia
Definition: testSimpleHelicopter.cpp:36
SimpleHelicopter.h
V1_w
Vector6 V1_w((Vector(6)<< 0.0, 0.0, M_PI/3, 0.0, 0.0, 30.0).finished())
gtsam::symbol_shorthand
Definition: inference/Symbol.h:147
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:44
h
const double h
Definition: testSimpleHelicopter.cpp:19
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:39
gtsam::Rot3::unrotate
Point3 unrotate(const Point3 &p, OptionalJacobian< 3, 3 > H1={}, OptionalJacobian< 3, 3 > H2={}) const
rotate point from world to rotated frame
Definition: Rot3.cpp:136
ceres::cos
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:426
TEST
TEST(Reconstruction, evaluateError)
Definition: testSimpleHelicopter.cpp:52
gtsam::DiscreteEulerPoincareHelicopter
Definition: SimpleHelicopter.h:79
align_3::a1
Point2 a1
Definition: testPose2.cpp:769
ceres::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Definition: gtsam/3rdparty/ceres/eigen.h:42
gtsam::Pose3::Adjoint
Vector6 Adjoint(const Vector6 &xi_b, OptionalJacobian< 6, 6 > H_this={}, OptionalJacobian< 6, 6 > H_xib={}) const
Definition: Pose3.cpp:65
align_3::a3
Point2 a3
Definition: testPose2.cpp:771
numericalDerivative.h
Some functions to compute numerical derivatives.
gtsam::Rot3
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
gtsam::numericalDerivative32
internal::FixedSizeMatrix< Y, X2 >::type numericalDerivative32(std::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Definition: numericalDerivative.h:259
gtsam::Pose3
Definition: Pose3.h:37
g2
Pose3 g2(g1.expmap(h *V1_g1))
newtonEuler
Vector newtonEuler(const Vector &Vb, const Vector &Fb, const Matrix &Inertia)
Definition: testSimpleHelicopter.cpp:83
gtsam::Pose3::inverse
Pose3 inverse() const
inverse transformation with derivatives
Definition: Pose3.cpp:47
gtsam::numericalDerivative31
internal::FixedSizeMatrix< Y, X1 >::type numericalDerivative31(std::function< Y(const X1 &, const X2 &, const X3 &)> h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Definition: numericalDerivative.h:226
u2
Vector u2
Definition: testSimpleHelicopter.cpp:32
Symbol.h
distR
double distR
Definition: testSimpleHelicopter.cpp:34
gamma
#define gamma
Definition: mconf.h:85
gtsam::symbol_shorthand::F
Key F(std::uint64_t j)
Definition: inference/Symbol.h:153
V1_g1
Vector6 V1_g1
Definition: testSimpleHelicopter.cpp:26
gtsam::Reconstruction
Definition: SimpleHelicopter.h:27
computeFu
Vector computeFu(const Vector &gamma, const Vector &control)
Definition: testSimpleHelicopter.cpp:38
TestResult
Definition: TestResult.h:26
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
tol
const double tol
Definition: testSimpleHelicopter.cpp:18
gtsam::Pose3::rotation
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself={}) const
get rotation
Definition: Pose3.cpp:316
gtsam::DiscreteEulerPoincareHelicopter::evaluateError
Vector evaluateError(const Vector6 &xik, const Vector6 &xik_1, const Pose3 &gk, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Definition: SimpleHelicopter.h:117
Mass
Matrix Mass
Definition: testSimpleHelicopter.cpp:35
gtsam
traits
Definition: SFMdata.h:40
gtsam::Reconstruction::evaluateError
Vector evaluateError(const Pose3 &gk1, const Pose3 &gk, const Vector6 &xik, OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3) const override
Definition: SimpleHelicopter.h:48
gtsam::symbol_shorthand::W
Key W(std::uint64_t j)
Definition: inference/Symbol.h:170
align_3::a2
Point2 a2
Definition: testPose2.cpp:770
G
JacobiRotation< float > G
Definition: Jacobi_makeGivens.cpp:2
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
V
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
gtsam::LieGroup::expmap
Class expmap(const TangentVector &v) const
Definition: Lie.h:78
M_PI
#define M_PI
Definition: mconf.h:117
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
Pose3.h
3D Pose manifold SO(3) x R^3 and group SE(3)
gamma2
Vector gamma2
Definition: testSimpleHelicopter.cpp:31
distT
double distT
Definition: testSimpleHelicopter.cpp:33


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:07:26