SimpleHelicopter.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * @file SimpleHelicopter.h
  3. * @brief Implement SimpleHelicopter discrete dynamics model and variational integrator,
  4. * following [Kobilarov09siggraph]
  5. * @author Duy-Nguyen Ta
  6. */
  7. #pragma once
  8. #include <gtsam/nonlinear/NonlinearFactor.h>
  9. #include <gtsam/geometry/Pose3.h>
  10. #include <gtsam/base/numericalDerivative.h>
  11. #include <cmath>
  12. namespace gtsam {
  13. /**
  14. * Implement the Reconstruction equation: \f$ g_{k+1} = g_k \exp (h\xi_k) \f$, where
  15. * \f$ h \f$: timestep (parameter)
  16. * \f$ g_{k+1}, g_{k} \f$: poses at the current and the next timestep
  17. * \f$ \xi_k \f$: the body-fixed velocity (Lie algebra)
  18. * It is somewhat similar to BetweenFactor, but treats the body-fixed velocity
  19. * \f$ \xi_k \f$ as a variable. So it is a three-way factor.
  20. * Note: this factor is necessary if one needs to smooth the entire graph. It's not needed
  21. * in sequential update method.
  22. */
  23. class Reconstruction : public NoiseModelFactor3<Pose3, Pose3, Vector6> {
  24. double h_; // time step
  25. typedef NoiseModelFactor3<Pose3, Pose3, Vector6> Base;
  26. public:
  27. Reconstruction(Key gKey1, Key gKey, Key xiKey, double h, double mu = 1000.0) :
  28. Base(noiseModel::Constrained::All(6, std::abs(mu)), gKey1, gKey,
  29. xiKey), h_(h) {
  30. }
  31. ~Reconstruction() override {}
  32. /// @return a deep copy of this factor
  33. gtsam::NonlinearFactor::shared_ptr clone() const override {
  34. return boost::static_pointer_cast<gtsam::NonlinearFactor>(
  35. gtsam::NonlinearFactor::shared_ptr(new Reconstruction(*this))); }
  36. /** \f$ log((g_k\exp(h\xi_k))^{-1}g_{k+1}) = 0, with optional derivatives */
  37. Vector evaluateError(const Pose3& gk1, const Pose3& gk, const Vector6& xik,
  38. boost::optional<Matrix&> H1 = boost::none,
  39. boost::optional<Matrix&> H2 = boost::none,
  40. boost::optional<Matrix&> H3 = boost::none) const override {
  41. Matrix6 D_exphxi_xi;
  42. Pose3 exphxi = Pose3::Expmap(h_ * xik, H3 ? &D_exphxi_xi : 0);
  43. Matrix6 D_gkxi_gk, D_gkxi_exphxi;
  44. Pose3 gkxi = gk.compose(exphxi, D_gkxi_gk, H3 ? &D_gkxi_exphxi : 0);
  45. Matrix6 D_hx_gk1, D_hx_gkxi;
  46. Pose3 hx = gkxi.between(gk1, (H2 || H3) ? &D_hx_gkxi : 0, H1 ? &D_hx_gk1 : 0);
  47. Matrix6 D_log_hx;
  48. Vector error = Pose3::Logmap(hx, D_log_hx);
  49. if (H1) *H1 = D_log_hx * D_hx_gk1;
  50. if (H2 || H3) {
  51. Matrix6 D_log_gkxi = D_log_hx * D_hx_gkxi;
  52. if (H2) *H2 = D_log_gkxi * D_gkxi_gk;
  53. if (H3) *H3 = D_log_gkxi * D_gkxi_exphxi * D_exphxi_xi * h_;
  54. }
  55. return error;
  56. }
  57. };
  58. /**
  59. * Implement the Discrete Euler-Poincare' equation:
  60. */
  61. class DiscreteEulerPoincareHelicopter : public NoiseModelFactor3<Vector6, Vector6, Pose3> {
  62. double h_; /// time step
  63. Matrix Inertia_; /// Inertia tensors Inertia = [ J 0; 0 M ]
  64. Vector Fu_; /// F is the 6xc Control matrix, where c is the number of control variables uk, which directly change the vehicle pose (e.g., gas/brake/speed)
  65. /// F(.) is actually a function of the shape variables, which do not change the pose, but affect the vehicle's shape, e.g. steering wheel.
  66. /// Fu_ encodes everything we need to know about the vehicle's dynamics.
  67. double m_; /// mass. For gravity external force f_ext, which has a fixed formula in this case.
  68. // TODO: Fk_ and f_ext should be generalized as functions (factor nodes) on control signals and poses/velocities.
  69. // This might be needed in control or system identification problems.
  70. // We treat them as constant here, since the control inputs are to specify.
  71. typedef NoiseModelFactor3<Vector6, Vector6, Pose3> Base;
  72. public:
  73. DiscreteEulerPoincareHelicopter(Key xiKey1, Key xiKey_1, Key gKey,
  74. double h, const Matrix& Inertia, const Vector& Fu, double m,
  75. double mu = 1000.0) :
  76. Base(noiseModel::Constrained::All(6, std::abs(mu)), xiKey1, xiKey_1, gKey),
  77. h_(h), Inertia_(Inertia), Fu_(Fu), m_(m) {
  78. }
  79. ~DiscreteEulerPoincareHelicopter() override {}
  80. /// @return a deep copy of this factor
  81. gtsam::NonlinearFactor::shared_ptr clone() const override {
  82. return boost::static_pointer_cast<gtsam::NonlinearFactor>(
  83. gtsam::NonlinearFactor::shared_ptr(new DiscreteEulerPoincareHelicopter(*this))); }
  84. /** DEP, with optional derivatives
  85. * pk - pk_1 - h_*Fu_ - h_*f_ext = 0
  86. * where pk = CT_TLN(h*xi_k)*Inertia*xi_k
  87. * pk_1 = CT_TLN(-h*xi_k_1)*Inertia*xi_k_1
  88. * */
  89. Vector evaluateError(const Vector6& xik, const Vector6& xik_1, const Pose3& gk,
  90. boost::optional<Matrix&> H1 = boost::none,
  91. boost::optional<Matrix&> H2 = boost::none,
  92. boost::optional<Matrix&> H3 = boost::none) const override {
  93. Vector muk = Inertia_*xik;
  94. Vector muk_1 = Inertia_*xik_1;
  95. // Apply the inverse right-trivialized tangent (derivative) map of the exponential map,
  96. // using the trapezoidal Lie-Newmark (TLN) scheme, to a vector.
  97. // TLN is just a first order approximation of the dExpInv_exp above, detailed in [Kobilarov09siggraph]
  98. // C_TLN formula: I6 - 1/2 ad[xi].
  99. Matrix D_adjThxik_muk, D_adjThxik1_muk1;
  100. Vector pk = muk - 0.5*Pose3::adjointTranspose(h_*xik, muk, D_adjThxik_muk);
  101. Vector pk_1 = muk_1 - 0.5*Pose3::adjointTranspose(-h_*xik_1, muk_1, D_adjThxik1_muk1);
  102. Matrix D_gravityBody_gk;
  103. Point3 gravityBody = gk.rotation().unrotate(Point3(0.0, 0.0, -9.81*m_), D_gravityBody_gk, boost::none);
  104. Vector f_ext = (Vector(6) << 0.0, 0.0, 0.0, gravityBody.x(), gravityBody.y(), gravityBody.z()).finished();
  105. Vector hx = pk - pk_1 - h_*Fu_ - h_*f_ext;
  106. if (H1) {
  107. Matrix D_pik_xi = Inertia_-0.5*(h_*D_adjThxik_muk + Pose3::adjointMap(h_*xik).transpose()*Inertia_);
  108. *H1 = D_pik_xi;
  109. }
  110. if (H2) {
  111. Matrix D_pik1_xik1 = Inertia_-0.5*(-h_*D_adjThxik1_muk1 + Pose3::adjointMap(-h_*xik_1).transpose()*Inertia_);
  112. *H2 = -D_pik1_xik1;
  113. }
  114. if (H3) {
  115. *H3 = Z_6x6;
  116. insertSub(*H3, -h_*D_gravityBody_gk, 3, 0);
  117. }
  118. return hx;
  119. }
  120. #if 0
  121. Vector computeError(const Vector6& xik, const Vector6& xik_1, const Pose3& gk) const {
  122. Vector pk = Pose3::dExpInv_exp(h_*xik).transpose()*Inertia_*xik;
  123. Vector pk_1 = Pose3::dExpInv_exp(-h_*xik_1).transpose()*Inertia_*xik_1;
  124. Point3 gravityBody = gk.rotation().unrotate(Point3(0.0, 0.0, -9.81*m_));
  125. Vector f_ext = (Vector(6) << 0.0, 0.0, 0.0, gravityBody.x(), gravityBody.y(), gravityBody.z());
  126. Vector hx = pk - pk_1 - h_*Fu_ - h_*f_ext;
  127. return hx;
  128. }
  129. Vector evaluateError(const Vector6& xik, const Vector6& xik_1, const Pose3& gk,
  130. boost::optional<Matrix&> H1 = boost::none,
  131. boost::optional<Matrix&> H2 = boost::none,
  132. boost::optional<Matrix&> H3 = boost::none) const {
  133. if (H1) {
  134. (*H1) = numericalDerivative31(
  135. std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
  136. std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
  137. ),
  138. xik, xik_1, gk, 1e-5
  139. );
  140. }
  141. if (H2) {
  142. (*H2) = numericalDerivative32(
  143. std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
  144. std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
  145. ),
  146. xik, xik_1, gk, 1e-5
  147. );
  148. }
  149. if (H3) {
  150. (*H3) = numericalDerivative33(
  151. std::function<Vector(const Vector6&, const Vector6&, const Pose3&)>(
  152. std::bind(&DiscreteEulerPoincareHelicopter::computeError, *this, _1, _2, _3)
  153. ),
  154. xik, xik_1, gk, 1e-5
  155. );
  156. }
  157. return computeError(xik, xik_1, gk);
  158. }
  159. #endif
  160. };
  161. } /* namespace gtsam */