gtsam
Loading...
Searching...
No Matches
SimpleHelicopter.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
15
16/*
17 * @file SimpleHelicopter.h
18 * @brief Implement SimpleHelicopter discrete dynamics model and variational integrator,
19 * following [Kobilarov09siggraph]
20 * @author Duy-Nguyen Ta
21 */
22
23#pragma once
24
25#include <gtsam/config.h>
26
27#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
28
34
35#include <cmath>
36
37namespace gtsam {
38
50class Reconstruction
51 : public NoiseModelFactorT<Vector6, Pose3, Pose3, Vector6> {
52
53 double h_; // time step
54 typedef NoiseModelFactorT<Vector6, Pose3, Pose3, Vector6> Base;
55public:
56
57 // Provide access to the Matrix& version of evaluateError:
58 using Base::evaluateError;
59
60 Reconstruction(Key gKey1, Key gKey, Key xiKey, double h, double mu = 1000.0) :
61 Base(noiseModel::Constrained::All(6, std::abs(mu)), gKey1, gKey,
62 xiKey), h_(h) {
63 }
64 ~Reconstruction() override {}
65
67 gtsam::NonlinearFactor::shared_ptr clone() const override {
68 return std::static_pointer_cast<gtsam::NonlinearFactor>(
69 gtsam::NonlinearFactor::shared_ptr(new Reconstruction(*this))); }
70
72 Vector6 evaluateError(const Pose3& gk1, const Pose3& gk,
73 const Vector6& xik, OptionalMatrixType H1,
74 OptionalMatrixType H2,
75 OptionalMatrixType H3) const override {
76
77 Matrix6 D_exphxi_xi;
78 Pose3 exphxi = Pose3::Expmap(h_ * xik, H3 ? &D_exphxi_xi : 0);
79
80 Matrix6 D_gkxi_gk, D_gkxi_exphxi;
81 Pose3 gkxi = gk.compose(exphxi, D_gkxi_gk, H3 ? &D_gkxi_exphxi : 0);
82
83 Matrix6 D_hx_gk1, D_hx_gkxi;
84 Pose3 hx = gkxi.between(gk1, (H2 || H3) ? &D_hx_gkxi : 0, H1 ? &D_hx_gk1 : 0);
85
86 Matrix6 D_log_hx;
87 Vector error = Pose3::Logmap(hx, D_log_hx);
88
89 if (H1) *H1 = D_log_hx * D_hx_gk1;
90 if (H2 || H3) {
91 Matrix6 D_log_gkxi = D_log_hx * D_hx_gkxi;
92 if (H2) *H2 = D_log_gkxi * D_gkxi_gk;
93 if (H3) *H3 = D_log_gkxi * D_gkxi_exphxi * D_exphxi_xi * h_;
94 }
95
96 return error;
97 }
98
99};
100
105class DiscreteEulerPoincareHelicopter
106 : public NoiseModelFactorT<Vector6, Vector6, Vector6, Pose3> {
107
108 double h_;
109 Matrix Inertia_;
110 Vector Fu_;
113 double m_;
114
115 // TODO: Fk_ and f_ext should be generalized as functions (factor nodes) on control signals and poses/velocities.
116 // This might be needed in control or system identification problems.
117 // We treat them as constant here, since the control inputs are to specify.
118
119 typedef NoiseModelFactorT<Vector6, Vector6, Vector6, Pose3> Base;
120
121public:
122
123 // Provide access to the Matrix& version of evaluateError:
124 using Base::evaluateError;
125
126 DiscreteEulerPoincareHelicopter(Key xiKey1, Key xiKey_1, Key gKey,
127 double h, const Matrix& Inertia, const Vector& Fu, double m,
128 double mu = 1000.0) :
129 Base(noiseModel::Constrained::All(6, std::abs(mu)), xiKey1, xiKey_1, gKey),
130 h_(h), Inertia_(Inertia), Fu_(Fu), m_(m) {
131 }
132 ~DiscreteEulerPoincareHelicopter() override {}
133
135 gtsam::NonlinearFactor::shared_ptr clone() const override {
136 return std::static_pointer_cast<gtsam::NonlinearFactor>(
137 gtsam::NonlinearFactor::shared_ptr(new DiscreteEulerPoincareHelicopter(*this))); }
138
144 Vector6 evaluateError(const Vector6& xik, const Vector6& xik_1,
145 const Pose3& gk, OptionalMatrixType H1,
146 OptionalMatrixType H2,
147 OptionalMatrixType H3) const override {
148
149 Vector muk = Inertia_*xik;
150 Vector muk_1 = Inertia_*xik_1;
151
152 // Apply the transpose of a first-order inverse left Jacobian using the
153 // trapezoidal Lie-Newmark (TLN) scheme.
154 // TLN is just a first order approximation of the dExpInv_exp above, detailed in [Kobilarov09siggraph]
155 // C_TLN formula: I6 - 1/2 ad[xi].
156 Matrix D_adjThxik_muk, D_adjThxik1_muk1;
157 Vector pk = muk - 0.5*Pose3::adjointTranspose(h_*xik, muk, D_adjThxik_muk);
158 Vector pk_1 = muk_1 - 0.5*Pose3::adjointTranspose(-h_*xik_1, muk_1, D_adjThxik1_muk1);
159
160 Matrix D_gravityBody_gk;
161 Point3 gravityBody = gk.rotation().unrotate(Point3(0.0, 0.0, -9.81*m_), D_gravityBody_gk, {});
162 Vector f_ext{
163 {0.0, 0.0, 0.0, gravityBody.x(), gravityBody.y(), gravityBody.z()}};
164
165 Vector hx = pk - pk_1 - h_*Fu_ - h_*f_ext;
166
167 if (H1) {
168 Matrix D_pik_xi = Inertia_-0.5*(h_*D_adjThxik_muk + Pose3::adjointMap(h_*xik).transpose()*Inertia_);
169 *H1 = D_pik_xi;
170 }
171
172 if (H2) {
173 Matrix D_pik1_xik1 = Inertia_-0.5*(-h_*D_adjThxik1_muk1 + Pose3::adjointMap(-h_*xik_1).transpose()*Inertia_);
174 *H2 = -D_pik1_xik1;
175 }
176
177 if (H3) {
178 *H3 = Z_6x6;
179 H3->block<3, 3>(3, 0) = -h_ * D_gravityBody_gk;
180 }
181
182 return hx;
183 }
184
185#if 0
186 Vector computeError(const Vector6& xik, const Vector6& xik_1, const Pose3& gk) const {
187 Vector pk = Pose3::dExpInv_exp(h_*xik).transpose()*Inertia_*xik;
188 Vector pk_1 = Pose3::dExpInv_exp(-h_*xik_1).transpose()*Inertia_*xik_1;
189
190 Point3 gravityBody = gk.rotation().unrotate(Point3(0.0, 0.0, -9.81*m_));
191 Vector f_ext = (Vector(6) << 0.0, 0.0, 0.0, gravityBody.x(), gravityBody.y(), gravityBody.z());
192
193 Vector hx = pk - pk_1 - h_*Fu_ - h_*f_ext;
194
195 return hx;
196 }
197
198 Vector evaluateError(const Vector6& xik, const Vector6& xik_1, const Pose3& gk,
199 OptionalMatrixType H1, OptionalMatrixType H2,
200 OptionalMatrixType H3) const {
201 if (H1) {
202 (*H1) = numericalDerivative31(
203 std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
204 std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
205 ),
206 xik, xik_1, gk, 1e-5
207 );
208 }
209 if (H2) {
210 (*H2) = numericalDerivative32(
211 std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
212 std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
213 ),
214 xik, xik_1, gk, 1e-5
215 );
216 }
217 if (H3) {
218 (*H3) = numericalDerivative33(
219 std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
220 std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
221 ),
222 xik, xik_1, gk, 1e-5
223 );
224 }
225
226 return computeError(xik, xik_1, gk);
227 }
228#endif
229
230};
231
232} // namespace gtsam
233
234#endif // GTSAM_ALLOW_DEPRECATED_SINCE_V43
Macros for Matrix constants to avoid excessive template instantiation.
Numerical derivative helpers for manifold-valued functions.
3D Pose manifold SO(3) x R^3 and group SE(3)
Base class for noise model factors with N variables.
Non-linear factor base classes.
internal::MatrixMN< traits< internal::OutputType< Y, F, X1, X2, X3 > >::dimension, N >::type numericalDerivative31(F &&h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Compute numerical derivative in argument 1 of ternary function.
Definition numericalDerivative.h:294
internal::MatrixMN< traits< internal::OutputType< Y, F, X1, X2, X3 > >::dimension, N >::type numericalDerivative32(F &&h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Compute numerical derivative in argument 2 of ternary function.
Definition numericalDerivative.h:324
internal::MatrixMN< traits< internal::OutputType< Y, F, X1, X2, X3 > >::dimension, N >::type numericalDerivative33(F &&h, const X1 &x1, const X2 &x2, const X3 &x3, double delta=1e-5)
Compute numerical derivative in argument 3 of ternary function.
Definition numericalDerivative.h:354
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Vector3 Point3
As of GTSAM 4, in order to make GTSAM more lean, it is now possible to just typedef Point3 to Vector3...
Definition Point3.h:38
A convenient base class for creating your own NoiseModelFactor with n variables.
Definition NoiseModelFactorN.h:155