commit
ecc736667b
|
@ -26,16 +26,16 @@ namespace gtsam {
|
||||||
Vector AttitudeFactor::attitudeError(const Rot3& nRb,
|
Vector AttitudeFactor::attitudeError(const Rot3& nRb,
|
||||||
OptionalJacobian<2, 3> H) const {
|
OptionalJacobian<2, 3> H) const {
|
||||||
if (H) {
|
if (H) {
|
||||||
Matrix23 D_nRef_R;
|
Matrix23 D_nRotated_R;
|
||||||
Matrix22 D_e_nRef;
|
Matrix22 D_e_nRotated;
|
||||||
Unit3 nRef = nRb.rotate(bRef_, D_nRef_R);
|
Unit3 nRotated = nRb.rotate(bMeasured_, D_nRotated_R);
|
||||||
Vector e = nZ_.error(nRef, D_e_nRef);
|
Vector e = nRef_.error(nRotated, D_e_nRotated);
|
||||||
|
|
||||||
(*H) = D_e_nRef * D_nRef_R;
|
(*H) = D_e_nRotated * D_nRotated_R;
|
||||||
return e;
|
return e;
|
||||||
} else {
|
} else {
|
||||||
Unit3 nRef = nRb * bRef_;
|
Unit3 nRotated = nRb * bMeasured_;
|
||||||
return nZ_.error(nRef);
|
return nRef_.error(nRotated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,8 +44,8 @@ void Rot3AttitudeFactor::print(const string& s,
|
||||||
const KeyFormatter& keyFormatter) const {
|
const KeyFormatter& keyFormatter) const {
|
||||||
cout << (s.empty() ? "" : s + " ") << "Rot3AttitudeFactor on "
|
cout << (s.empty() ? "" : s + " ") << "Rot3AttitudeFactor on "
|
||||||
<< keyFormatter(this->key()) << "\n";
|
<< keyFormatter(this->key()) << "\n";
|
||||||
nZ_.print(" measured direction in nav frame: ");
|
nRef_.print(" reference direction in nav frame: ");
|
||||||
bRef_.print(" reference direction in body frame: ");
|
bMeasured_.print(" measured direction in body frame: ");
|
||||||
this->noiseModel_->print(" noise model: ");
|
this->noiseModel_->print(" noise model: ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,16 +53,16 @@ void Rot3AttitudeFactor::print(const string& s,
|
||||||
bool Rot3AttitudeFactor::equals(const NonlinearFactor& expected,
|
bool Rot3AttitudeFactor::equals(const NonlinearFactor& expected,
|
||||||
double tol) const {
|
double tol) const {
|
||||||
const This* e = dynamic_cast<const This*>(&expected);
|
const This* e = dynamic_cast<const This*>(&expected);
|
||||||
return e != nullptr && Base::equals(*e, tol) && this->nZ_.equals(e->nZ_, tol)
|
return e != nullptr && Base::equals(*e, tol) && this->nRef_.equals(e->nRef_, tol)
|
||||||
&& this->bRef_.equals(e->bRef_, tol);
|
&& this->bMeasured_.equals(e->bMeasured_, tol);
|
||||||
}
|
}
|
||||||
|
|
||||||
//***************************************************************************
|
//***************************************************************************
|
||||||
void Pose3AttitudeFactor::print(const string& s,
|
void Pose3AttitudeFactor::print(const string& s,
|
||||||
const KeyFormatter& keyFormatter) const {
|
const KeyFormatter& keyFormatter) const {
|
||||||
cout << s << "Pose3AttitudeFactor on " << keyFormatter(this->key()) << "\n";
|
cout << s << "Pose3AttitudeFactor on " << keyFormatter(this->key()) << "\n";
|
||||||
nZ_.print(" measured direction in nav frame: ");
|
nRef_.print(" reference direction in nav frame: ");
|
||||||
bRef_.print(" reference direction in body frame: ");
|
bMeasured_.print(" measured direction in body frame: ");
|
||||||
this->noiseModel_->print(" noise model: ");
|
this->noiseModel_->print(" noise model: ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,8 +70,8 @@ void Pose3AttitudeFactor::print(const string& s,
|
||||||
bool Pose3AttitudeFactor::equals(const NonlinearFactor& expected,
|
bool Pose3AttitudeFactor::equals(const NonlinearFactor& expected,
|
||||||
double tol) const {
|
double tol) const {
|
||||||
const This* e = dynamic_cast<const This*>(&expected);
|
const This* e = dynamic_cast<const This*>(&expected);
|
||||||
return e != nullptr && Base::equals(*e, tol) && this->nZ_.equals(e->nZ_, tol)
|
return e != nullptr && Base::equals(*e, tol) && this->nRef_.equals(e->nRef_, tol)
|
||||||
&& this->bRef_.equals(e->bRef_, tol);
|
&& this->bMeasured_.equals(e->bMeasured_, tol);
|
||||||
}
|
}
|
||||||
|
|
||||||
//***************************************************************************
|
//***************************************************************************
|
||||||
|
|
|
@ -17,59 +17,69 @@
|
||||||
**/
|
**/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <gtsam/nonlinear/NonlinearFactor.h>
|
|
||||||
#include <gtsam/geometry/Pose3.h>
|
#include <gtsam/geometry/Pose3.h>
|
||||||
#include <gtsam/geometry/Unit3.h>
|
#include <gtsam/geometry/Unit3.h>
|
||||||
|
#include <gtsam/nonlinear/NonlinearFactor.h>
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for prior on attitude
|
* @class AttitudeFactor
|
||||||
* Example:
|
*
|
||||||
* - measurement is direction of gravity in body frame bF
|
* @brief Base class for an attitude factor that constrains the rotation between
|
||||||
* - reference is direction of gravity in navigation frame nG
|
* body and navigation frames.
|
||||||
* This factor will give zero error if nRb * bF == nG
|
*
|
||||||
|
* This factor enforces that when the measured direction in the body frame
|
||||||
|
* (e.g., from an IMU accelerometer) is rotated into the navigation frame using the
|
||||||
|
* rotation variable, it should align with a known reference direction in the
|
||||||
|
* navigation frame (e.g., gravity vector).
|
||||||
|
*
|
||||||
|
* Mathematically, the error is zero when:
|
||||||
|
* nRb * bMeasured == nRef
|
||||||
|
*
|
||||||
|
* This is useful for incorporating absolute orientation measurements into the
|
||||||
|
* factor graph.
|
||||||
|
*
|
||||||
* @ingroup navigation
|
* @ingroup navigation
|
||||||
*/
|
*/
|
||||||
class AttitudeFactor {
|
class AttitudeFactor {
|
||||||
|
protected:
|
||||||
|
Unit3 nRef_, bMeasured_; ///< Position measurement in
|
||||||
|
|
||||||
protected:
|
public:
|
||||||
|
|
||||||
Unit3 nZ_, bRef_; ///< Position measurement in
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
/** default constructor - only use for serialization */
|
/** default constructor - only use for serialization */
|
||||||
AttitudeFactor() {
|
AttitudeFactor() {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Constructor
|
* @brief Constructor
|
||||||
* @param nZ measured direction in navigation frame
|
* @param nRef Reference direction in the navigation frame (e.g., gravity).
|
||||||
* @param bRef reference direction in body frame (default Z-axis in NED frame, i.e., [0; 0; 1])
|
* @param bMeasured Measured direction in the body frame (e.g., from IMU
|
||||||
|
* accelerometer). Default is Unit3(0, 0, 1).
|
||||||
*/
|
*/
|
||||||
AttitudeFactor(const Unit3& nZ, const Unit3& bRef = Unit3(0, 0, 1)) :
|
AttitudeFactor(const Unit3& nRef, const Unit3& bMeasured = Unit3(0, 0, 1))
|
||||||
nZ_(nZ), bRef_(bRef) {
|
: nRef_(nRef), bMeasured_(bMeasured) {}
|
||||||
}
|
|
||||||
|
|
||||||
/** vector of errors */
|
/** vector of errors */
|
||||||
Vector attitudeError(const Rot3& p,
|
Vector attitudeError(const Rot3& p, OptionalJacobian<2, 3> H = {}) const;
|
||||||
OptionalJacobian<2,3> H = {}) const;
|
|
||||||
|
|
||||||
const Unit3& nZ() const {
|
const Unit3& nRef() const { return nRef_; }
|
||||||
return nZ_;
|
const Unit3& bMeasured() const { return bMeasured_; }
|
||||||
}
|
|
||||||
const Unit3& bRef() const {
|
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
|
||||||
return bRef_;
|
[[deprecated("Use nRef() instead")]]
|
||||||
}
|
const Unit3& nZ() const { return nRef_; }
|
||||||
|
|
||||||
|
[[deprecated("Use bMeasured() instead")]]
|
||||||
|
const Unit3& bRef() const { return bMeasured_; }
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
||||||
/** Serialization function */
|
/** Serialization function */
|
||||||
friend class boost::serialization::access;
|
friend class boost::serialization::access;
|
||||||
template<class ARCHIVE>
|
template <class ARCHIVE>
|
||||||
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
|
void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
|
||||||
ar & boost::serialization::make_nvp("nZ_", nZ_);
|
ar& boost::serialization::make_nvp("nRef_", nRef_);
|
||||||
ar & boost::serialization::make_nvp("bRef_", bRef_);
|
ar& boost::serialization::make_nvp("bMeasured_", bMeasured_);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
@ -78,12 +88,11 @@ public:
|
||||||
* Version of AttitudeFactor for Rot3
|
* Version of AttitudeFactor for Rot3
|
||||||
* @ingroup navigation
|
* @ingroup navigation
|
||||||
*/
|
*/
|
||||||
class GTSAM_EXPORT Rot3AttitudeFactor: public NoiseModelFactorN<Rot3>, public AttitudeFactor {
|
class GTSAM_EXPORT Rot3AttitudeFactor : public NoiseModelFactorN<Rot3>,
|
||||||
|
public AttitudeFactor {
|
||||||
typedef NoiseModelFactorN<Rot3> Base;
|
typedef NoiseModelFactorN<Rot3> Base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Provide access to the Matrix& version of evaluateError:
|
// Provide access to the Matrix& version of evaluateError:
|
||||||
using Base::evaluateError;
|
using Base::evaluateError;
|
||||||
|
|
||||||
|
@ -94,23 +103,20 @@ public:
|
||||||
typedef Rot3AttitudeFactor This;
|
typedef Rot3AttitudeFactor This;
|
||||||
|
|
||||||
/** default constructor - only use for serialization */
|
/** default constructor - only use for serialization */
|
||||||
Rot3AttitudeFactor() {
|
Rot3AttitudeFactor() {}
|
||||||
}
|
|
||||||
|
|
||||||
~Rot3AttitudeFactor() override {
|
~Rot3AttitudeFactor() override {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Constructor
|
* @brief Constructor
|
||||||
* @param key of the Rot3 variable that will be constrained
|
* @param key of the Rot3 variable that will be constrained
|
||||||
* @param nZ measured direction in navigation frame
|
* @param nRef reference direction in navigation frame
|
||||||
* @param model Gaussian noise model
|
* @param model Gaussian noise model
|
||||||
* @param bRef reference direction in body frame (default Z-axis)
|
* @param bMeasured measured direction in body frame (default Z-axis)
|
||||||
*/
|
*/
|
||||||
Rot3AttitudeFactor(Key key, const Unit3& nZ, const SharedNoiseModel& model,
|
Rot3AttitudeFactor(Key key, const Unit3& nRef, const SharedNoiseModel& model,
|
||||||
const Unit3& bRef = Unit3(0, 0, 1)) :
|
const Unit3& bMeasured = Unit3(0, 0, 1))
|
||||||
Base(model, key), AttitudeFactor(nZ, bRef) {
|
: Base(model, key), AttitudeFactor(nRef, bMeasured) {}
|
||||||
}
|
|
||||||
|
|
||||||
/// @return a deep copy of this factor
|
/// @return a deep copy of this factor
|
||||||
gtsam::NonlinearFactor::shared_ptr clone() const override {
|
gtsam::NonlinearFactor::shared_ptr clone() const override {
|
||||||
|
@ -123,46 +129,46 @@ public:
|
||||||
DefaultKeyFormatter) const override;
|
DefaultKeyFormatter) const override;
|
||||||
|
|
||||||
/** equals */
|
/** equals */
|
||||||
bool equals(const NonlinearFactor& expected, double tol = 1e-9) const override;
|
bool equals(const NonlinearFactor& expected,
|
||||||
|
double tol = 1e-9) const override;
|
||||||
|
|
||||||
/** vector of errors */
|
/** vector of errors */
|
||||||
Vector evaluateError(const Rot3& nRb, OptionalMatrixType H) const override {
|
Vector evaluateError(const Rot3& nRb, OptionalMatrixType H) const override {
|
||||||
return attitudeError(nRb, H);
|
return attitudeError(nRb, H);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
||||||
/** Serialization function */
|
/** Serialization function */
|
||||||
friend class boost::serialization::access;
|
friend class boost::serialization::access;
|
||||||
template<class ARCHIVE>
|
template <class ARCHIVE>
|
||||||
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
|
void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
|
||||||
// NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
|
// NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
|
||||||
ar & boost::serialization::make_nvp("NoiseModelFactor1",
|
ar& boost::serialization::make_nvp(
|
||||||
boost::serialization::base_object<Base>(*this));
|
"NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
|
||||||
ar & boost::serialization::make_nvp("AttitudeFactor",
|
ar& boost::serialization::make_nvp(
|
||||||
|
"AttitudeFactor",
|
||||||
boost::serialization::base_object<AttitudeFactor>(*this));
|
boost::serialization::base_object<AttitudeFactor>(*this));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GTSAM_MAKE_ALIGNED_OPERATOR_NEW
|
GTSAM_MAKE_ALIGNED_OPERATOR_NEW
|
||||||
};
|
};
|
||||||
|
|
||||||
/// traits
|
/// traits
|
||||||
template<> struct traits<Rot3AttitudeFactor> : public Testable<Rot3AttitudeFactor> {};
|
template <>
|
||||||
|
struct traits<Rot3AttitudeFactor> : public Testable<Rot3AttitudeFactor> {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Version of AttitudeFactor for Pose3
|
* Version of AttitudeFactor for Pose3
|
||||||
* @ingroup navigation
|
* @ingroup navigation
|
||||||
*/
|
*/
|
||||||
class GTSAM_EXPORT Pose3AttitudeFactor: public NoiseModelFactorN<Pose3>,
|
class GTSAM_EXPORT Pose3AttitudeFactor : public NoiseModelFactorN<Pose3>,
|
||||||
public AttitudeFactor {
|
public AttitudeFactor {
|
||||||
|
|
||||||
typedef NoiseModelFactorN<Pose3> Base;
|
typedef NoiseModelFactorN<Pose3> Base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Provide access to the Matrix& version of evaluateError:
|
// Provide access to the Matrix& version of evaluateError:
|
||||||
using Base::evaluateError;
|
using Base::evaluateError;
|
||||||
|
|
||||||
|
@ -173,23 +179,20 @@ public:
|
||||||
typedef Pose3AttitudeFactor This;
|
typedef Pose3AttitudeFactor This;
|
||||||
|
|
||||||
/** default constructor - only use for serialization */
|
/** default constructor - only use for serialization */
|
||||||
Pose3AttitudeFactor() {
|
Pose3AttitudeFactor() {}
|
||||||
}
|
|
||||||
|
|
||||||
~Pose3AttitudeFactor() override {
|
~Pose3AttitudeFactor() override {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Constructor
|
* @brief Constructor
|
||||||
* @param key of the Pose3 variable that will be constrained
|
* @param key of the Pose3 variable that will be constrained
|
||||||
* @param nZ measured direction in navigation frame
|
* @param nRef reference direction in navigation frame
|
||||||
* @param model Gaussian noise model
|
* @param model Gaussian noise model
|
||||||
* @param bRef reference direction in body frame (default Z-axis)
|
* @param bMeasured measured direction in body frame (default Z-axis)
|
||||||
*/
|
*/
|
||||||
Pose3AttitudeFactor(Key key, const Unit3& nZ, const SharedNoiseModel& model,
|
Pose3AttitudeFactor(Key key, const Unit3& nRef, const SharedNoiseModel& model,
|
||||||
const Unit3& bRef = Unit3(0, 0, 1)) :
|
const Unit3& bMeasured = Unit3(0, 0, 1))
|
||||||
Base(model, key), AttitudeFactor(nZ, bRef) {
|
: Base(model, key), AttitudeFactor(nRef, bMeasured) {}
|
||||||
}
|
|
||||||
|
|
||||||
/// @return a deep copy of this factor
|
/// @return a deep copy of this factor
|
||||||
gtsam::NonlinearFactor::shared_ptr clone() const override {
|
gtsam::NonlinearFactor::shared_ptr clone() const override {
|
||||||
|
@ -202,40 +205,41 @@ public:
|
||||||
DefaultKeyFormatter) const override;
|
DefaultKeyFormatter) const override;
|
||||||
|
|
||||||
/** equals */
|
/** equals */
|
||||||
bool equals(const NonlinearFactor& expected, double tol = 1e-9) const override;
|
bool equals(const NonlinearFactor& expected,
|
||||||
|
double tol = 1e-9) const override;
|
||||||
|
|
||||||
/** vector of errors */
|
/** vector of errors */
|
||||||
Vector evaluateError(const Pose3& nTb, OptionalMatrixType H) const override {
|
Vector evaluateError(const Pose3& nTb, OptionalMatrixType H) const override {
|
||||||
Vector e = attitudeError(nTb.rotation(), H);
|
Vector e = attitudeError(nTb.rotation(), H);
|
||||||
if (H) {
|
if (H) {
|
||||||
Matrix H23 = *H;
|
Matrix H23 = *H;
|
||||||
*H = Matrix::Zero(2,6);
|
*H = Matrix::Zero(2, 6);
|
||||||
H->block<2,3>(0,0) = H23;
|
H->block<2, 3>(0, 0) = H23;
|
||||||
}
|
}
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
#ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
|
||||||
/** Serialization function */
|
/** Serialization function */
|
||||||
friend class boost::serialization::access;
|
friend class boost::serialization::access;
|
||||||
template<class ARCHIVE>
|
template <class ARCHIVE>
|
||||||
void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
|
void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
|
||||||
// NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
|
// NoiseModelFactor1 instead of NoiseModelFactorN for backward compatibility
|
||||||
ar & boost::serialization::make_nvp("NoiseModelFactor1",
|
ar& boost::serialization::make_nvp(
|
||||||
boost::serialization::base_object<Base>(*this));
|
"NoiseModelFactor1", boost::serialization::base_object<Base>(*this));
|
||||||
ar & boost::serialization::make_nvp("AttitudeFactor",
|
ar& boost::serialization::make_nvp(
|
||||||
|
"AttitudeFactor",
|
||||||
boost::serialization::base_object<AttitudeFactor>(*this));
|
boost::serialization::base_object<AttitudeFactor>(*this));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GTSAM_MAKE_ALIGNED_OPERATOR_NEW
|
GTSAM_MAKE_ALIGNED_OPERATOR_NEW
|
||||||
};
|
};
|
||||||
|
|
||||||
/// traits
|
/// traits
|
||||||
template<> struct traits<Pose3AttitudeFactor> : public Testable<Pose3AttitudeFactor> {};
|
template <>
|
||||||
|
struct traits<Pose3AttitudeFactor> : public Testable<Pose3AttitudeFactor> {};
|
||||||
} /// namespace gtsam
|
|
||||||
|
|
||||||
|
} // namespace gtsam
|
||||||
|
|
|
@ -0,0 +1,136 @@
|
||||||
|
# AttitudeFactor in GTSAM
|
||||||
|
|
||||||
|
[Cautionary note: this was generated from the source using ChatGPT but edited by Frank]
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
The `AttitudeFactor` in GTSAM is a factor that constrains the orientation (attitude) of a robot or sensor platform based on directional measurements. This is particularly useful in GPS-denied navigation contexts where orientation must be estimated from inertial sensors like accelerometers or magnetometers.
|
||||||
|
|
||||||
|
This document explains the mathematical foundation of the `AttitudeFactor` and guides users on how to use this factor effectively in GTSAM.
|
||||||
|
|
||||||
|
## Mathematical Foundation
|
||||||
|
|
||||||
|
### Concept
|
||||||
|
|
||||||
|
The `AttitudeFactor` constrains the rotation $\mathbf{R}_{nb}$ (from body frame $b$ to navigation frame $n$) such that a known reference direction in the navigation frame aligns with a measured direction in the body frame, when rotated. The factor enforces that:
|
||||||
|
|
||||||
|
$$
|
||||||
|
\text{nRef} \approx \mathbf{R}_{nb} \cdot \text{bMeasured}
|
||||||
|
$$
|
||||||
|
|
||||||
|
where:
|
||||||
|
|
||||||
|
- $\mathbf{R}_{nb}$ is the rotation matrix representing the orientation from body to navigation frame.
|
||||||
|
- $\text{bMeasured}$ is the measured direction in the body frame (e.g., the accelerometer reading).
|
||||||
|
- $\text{nRef}$ is the known reference direction in the navigation frame (e.g., the gravity vector in nav).
|
||||||
|
|
||||||
|
### Error Function
|
||||||
|
|
||||||
|
The error function computes the angular difference between the rotated reference direction and the measured direction:
|
||||||
|
|
||||||
|
$$
|
||||||
|
\mathbf{e} = \text{error}(\text{nRef}, \mathbf{R}_{nb} \cdot \text{bMeasured})
|
||||||
|
$$
|
||||||
|
|
||||||
|
This error is minimal (zero) when the rotated body reference direction aligns perfectly with the measured navigation direction.
|
||||||
|
|
||||||
|
The error is computed internally using the `attitudeError` function:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
Vector AttitudeFactor::attitudeError(const Rot3& nRb) const {
|
||||||
|
Unit3 nRotated = nRb * bMeasured_;
|
||||||
|
return nRef_.error(nRotated);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Explanation:
|
||||||
|
- The function computes the rotated measurement `nRotated` and then the error between `nRef` and `nRotated`.
|
||||||
|
- `nRef_.error(nRotated)` is a 2D vector-valued error between two directions, defined in [Unit3.h](../geometry/Unit3.h).
|
||||||
|
|
||||||
|
### Jacobians
|
||||||
|
|
||||||
|
For optimization, the $2 \times 3$ Jacobian of the error function with respect to the rotation parameters is required. The Jacobian is computed using chain rule differentiation, involving the derivative of the rotated vector with respect to the rotation parameters and the derivative of the error with respect to the rotated vector.
|
||||||
|
|
||||||
|
Note the Jacobian for this specific error function vanishes 180 degrees away from the true direction, and the factor is only expected to behave well when the `nRb` value is initialized in the correct hemisphere.
|
||||||
|
|
||||||
|
## Usage in GTSAM
|
||||||
|
|
||||||
|
### Including the Header
|
||||||
|
|
||||||
|
Include the `AttitudeFactor.h` header in your code:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <gtsam/navigation/AttitudeFactor.h>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Creating an Attitude Factor
|
||||||
|
|
||||||
|
You can create an attitude factor for either a `Rot3` (rotation only) or a `Pose3` (position and rotation) variable.
|
||||||
|
|
||||||
|
#### For `Rot3` Variables
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// Define keys
|
||||||
|
gtsam::Key rotKey = ...;
|
||||||
|
|
||||||
|
// Known reference direction in navigation frame (e.g., gravity)
|
||||||
|
gtsam::Unit3 nRef(0, 0, -1); // Assuming gravity points down in navigation frame
|
||||||
|
|
||||||
|
// Measured direction in body frame (e.g., accelerometer direction)
|
||||||
|
gtsam::Unit3 bMeasured(0, 0, 9.8); // will be normalized automatically
|
||||||
|
|
||||||
|
// Noise model
|
||||||
|
auto noiseModel = gtsam::noiseModel::Isotropic::Sigma(2, 0.1); // 2D error, sigma = 0.1
|
||||||
|
|
||||||
|
// Add to factor graph
|
||||||
|
gtsam::NonlinearFactorGraph graph;
|
||||||
|
graph.emplace_shared<Rot3AttitudeFactor>(rotKey, nRef, noiseModel, bMeasured);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### For `Pose3` Variables
|
||||||
|
|
||||||
|
There is also a `Pose3AttitudeFactor` that automatically extracts the rotation from the pose, taking into account the chain rule for this operation so the Jacobians with respect to pose are correct.
|
||||||
|
The same caveat about vanishing Jacobian holds.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// Define keys
|
||||||
|
gtsam::Key poseKey = ...;
|
||||||
|
|
||||||
|
// Known reference direction in navigation frame (e.g., gravity)
|
||||||
|
gtsam::Unit3 nRef(0, 0, -1); // Assuming gravity points down in navigation frame
|
||||||
|
|
||||||
|
// Measured direction in body frame (e.g., accelerometer direction)
|
||||||
|
gtsam::Unit3 bMeasured(0, 0, 9.8); // will be normalized automatically
|
||||||
|
|
||||||
|
// Noise model
|
||||||
|
auto noiseModel = gtsam::noiseModel::Isotropic::Sigma(2, 0.1);
|
||||||
|
|
||||||
|
// Add to factor graph
|
||||||
|
gtsam::NonlinearFactorGraph graph;
|
||||||
|
graph.emplace_shared<Pose3AttitudeFactor>(poseKey, nRef, noiseModel, bMeasured);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Explanation of Parameters
|
||||||
|
|
||||||
|
- **Key**: The variable key in the factor graph corresponding to the `Rot3` or `Pose3` variable you are constraining.
|
||||||
|
- **nRef**: The known direction in the navigation frame. This is typically obtained from sensors like accelerometers or magnetometers.
|
||||||
|
- **bMeasured**: The measured direction in the body frame. By default, this is set to the Z-axis `[0; 0; 1]`, but it should match the direction your sensor measures. When constructing a `Unit3`, will be automatically normalized.
|
||||||
|
- **noiseModel**: The noise model representing the uncertainty in the measurement. Adjust the standard deviation based on the confidence in your measurements.
|
||||||
|
|
||||||
|
## Example in GPS-Denied Navigation
|
||||||
|
|
||||||
|
In GPS-denied environments, orientation estimation relies heavily on inertial measurements. By incorporating the `AttitudeFactor`, you can:
|
||||||
|
|
||||||
|
- Constrain the roll and pitch angles using gravity measurements from an accelerometer.
|
||||||
|
- Constrain the yaw angle using magnetic field measurements from a magnetometer (with caution due to magnetic disturbances).
|
||||||
|
|
||||||
|
This factor helps maintain an accurate orientation estimate over time, which is crucial for applications like drone flight, underwater vehicles, or indoor robotics.
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The `AttitudeFactor` is a useful tool in GTSAM for incorporating orientation measurements into your factor graph. By aligning a measured direction in the body frame with a known reference direction in the navigation frame, it provides a constraint that improves the estimation of the rotation variable. Correct understanding and usage of this factor enhance the performance of navigation algorithms, especially in challenging environments where GPS is unavailable.
|
||||||
|
|
||||||
|
# References
|
||||||
|
|
||||||
|
- [GTSAM Documentation](https://gtsam.org/)
|
||||||
|
- [Unit3 Class Reference](https://gtsam.org/doxygen/)
|
|
@ -37,13 +37,9 @@ typedef ManifoldPreintegration PreintegrationType;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If you are using the factor, please cite:
|
* If you are using the factor, please cite:
|
||||||
* L. Carlone, Z. Kira, C. Beall, V. Indelman, F. Dellaert, "Eliminating
|
* Christian Forster, Luca Carlone, Frank Dellaert, and Davide Scaramuzza,
|
||||||
* conditionally independent sets in factor graphs: a unifying perspective based
|
* "On-Manifold Preintegration for Real-Time Visual-Inertial Odometry", IEEE
|
||||||
* on smart factors", Int. Conf. on Robotics and Automation (ICRA), 2014.
|
* Transactions on Robotics, 2017.
|
||||||
*
|
|
||||||
* C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration on
|
|
||||||
* Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
|
|
||||||
* Robotics: Science and Systems (RSS), 2015.
|
|
||||||
*
|
*
|
||||||
* REFERENCES:
|
* REFERENCES:
|
||||||
* [1] G.S. Chirikjian, "Stochastic Models, Information Theory, and Lie Groups",
|
* [1] G.S. Chirikjian, "Stochastic Models, Information Theory, and Lie Groups",
|
||||||
|
@ -54,8 +50,8 @@ typedef ManifoldPreintegration PreintegrationType;
|
||||||
* [3] L. Carlone, S. Williams, R. Roberts, "Preintegrated IMU factor:
|
* [3] L. Carlone, S. Williams, R. Roberts, "Preintegrated IMU factor:
|
||||||
* Computation of the Jacobian Matrices", Tech. Report, 2013.
|
* Computation of the Jacobian Matrices", Tech. Report, 2013.
|
||||||
* Available in this repo as "PreintegratedIMUJacobians.pdf".
|
* Available in this repo as "PreintegratedIMUJacobians.pdf".
|
||||||
* [4] C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration on
|
* [4] C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration
|
||||||
* Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
|
* on Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
|
||||||
* Robotics: Science and Systems (RSS), 2015.
|
* Robotics: Science and Systems (RSS), 2015.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
# Navigation Factors
|
||||||
|
|
||||||
|
This directory contains factors related to navigation, including various IMU factors.
|
||||||
|
|
||||||
|
## IMU Factor:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
The `ImuFactor` is a 5-ways factor involving previous state (pose and velocity of
|
||||||
|
the vehicle at previous time step), current state (pose and velocity at
|
||||||
|
current time step), and the bias estimate.
|
||||||
|
Following the preintegration
|
||||||
|
scheme proposed in [2], the `ImuFactor` includes many IMU measurements, which
|
||||||
|
are "summarized" using the PreintegratedIMUMeasurements class.
|
||||||
|
The figure above, courtesy of [Mathworks' navigation toolbox](https://www.mathworks.com/help/nav/index.html), which are also using our work, shows the factor graph fragment for two time slices.
|
||||||
|
|
||||||
|
Note that this factor does not model "temporal consistency" of the biases
|
||||||
|
(which are usually slowly varying quantities), which is up to the caller.
|
||||||
|
See also `CombinedImuFactor` for a class that does this for you.
|
||||||
|
|
||||||
|
If you are using the factor, please cite:
|
||||||
|
> Christian Forster, Luca Carlone, Frank Dellaert, and Davide Scaramuzza, "On-Manifold Preintegration for Real-Time Visual-Inertial Odometry", IEEE Transactions on Robotics, 2017.
|
||||||
|
|
||||||
|
## REFERENCES:
|
||||||
|
1. G.S. Chirikjian, "Stochastic Models, Information Theory, and Lie Groups",
|
||||||
|
Volume 2, 2008.
|
||||||
|
2. T. Lupton and S.Sukkarieh, "Visual-Inertial-Aided Navigation for
|
||||||
|
High-Dynamic Motion in Built Environments Without Initial Conditions",
|
||||||
|
TRO, 28(1):61-76, 2012.
|
||||||
|
3. L. Carlone, S. Williams, R. Roberts, "Preintegrated IMU factor:
|
||||||
|
Computation of the Jacobian Matrices", Tech. Report, 2013.
|
||||||
|
Available in this repo as "PreintegratedIMUJacobians.pdf".
|
||||||
|
4. C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration on
|
||||||
|
Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
|
||||||
|
Robotics: Science and Systems (RSS), 2015.
|
||||||
|
|
||||||
|
## The Attitude Factor
|
||||||
|
|
||||||
|
The `AttitudeFactor` in GTSAM is a factor that constrains the orientation (attitude) of a robot or sensor platform based on directional measurements. Both `Rot3` and `Pose3` versions are available.
|
||||||
|
|
||||||
|
Written up in detail with the help of ChatGPT [here](AttitudeFactor.md).
|
||||||
|
|
|
@ -256,34 +256,30 @@ virtual class AHRSFactor : gtsam::NonlinearFactor {
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <gtsam/navigation/AttitudeFactor.h>
|
#include <gtsam/navigation/AttitudeFactor.h>
|
||||||
//virtual class AttitudeFactor : gtsam::NonlinearFactor {
|
virtual class Rot3AttitudeFactor : gtsam::NoiseModelFactor {
|
||||||
// AttitudeFactor(const Unit3& nZ, const Unit3& bRef);
|
Rot3AttitudeFactor(size_t key, const gtsam::Unit3& nRef, const gtsam::noiseModel::Diagonal* model,
|
||||||
// AttitudeFactor();
|
const gtsam::Unit3& bMeasured);
|
||||||
//};
|
Rot3AttitudeFactor(size_t key, const gtsam::Unit3& nRef, const gtsam::noiseModel::Diagonal* model);
|
||||||
virtual class Rot3AttitudeFactor : gtsam::NonlinearFactor{
|
|
||||||
Rot3AttitudeFactor(size_t key, const gtsam::Unit3& nZ, const gtsam::noiseModel::Diagonal* model,
|
|
||||||
const gtsam::Unit3& bRef);
|
|
||||||
Rot3AttitudeFactor(size_t key, const gtsam::Unit3& nZ, const gtsam::noiseModel::Diagonal* model);
|
|
||||||
Rot3AttitudeFactor();
|
Rot3AttitudeFactor();
|
||||||
void print(string s = "", const gtsam::KeyFormatter& keyFormatter =
|
void print(string s = "", const gtsam::KeyFormatter& keyFormatter =
|
||||||
gtsam::DefaultKeyFormatter) const;
|
gtsam::DefaultKeyFormatter) const;
|
||||||
bool equals(const gtsam::NonlinearFactor& expected, double tol) const;
|
bool equals(const gtsam::NonlinearFactor& expected, double tol) const;
|
||||||
gtsam::Unit3 nZ() const;
|
gtsam::Unit3 nRef() const;
|
||||||
gtsam::Unit3 bRef() const;
|
gtsam::Unit3 bMeasured() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual class Pose3AttitudeFactor : gtsam::NonlinearFactor {
|
virtual class Pose3AttitudeFactor : gtsam::NoiseModelFactor {
|
||||||
Pose3AttitudeFactor(size_t key, const gtsam::Unit3& nZ,
|
Pose3AttitudeFactor(size_t key, const gtsam::Unit3& nRef,
|
||||||
const gtsam::noiseModel::Diagonal* model,
|
const gtsam::noiseModel::Diagonal* model,
|
||||||
const gtsam::Unit3& bRef);
|
const gtsam::Unit3& bMeasured);
|
||||||
Pose3AttitudeFactor(size_t key, const gtsam::Unit3& nZ,
|
Pose3AttitudeFactor(size_t key, const gtsam::Unit3& nRef,
|
||||||
const gtsam::noiseModel::Diagonal* model);
|
const gtsam::noiseModel::Diagonal* model);
|
||||||
Pose3AttitudeFactor();
|
Pose3AttitudeFactor();
|
||||||
void print(string s = "", const gtsam::KeyFormatter& keyFormatter =
|
void print(string s = "", const gtsam::KeyFormatter& keyFormatter =
|
||||||
gtsam::DefaultKeyFormatter) const;
|
gtsam::DefaultKeyFormatter) const;
|
||||||
bool equals(const gtsam::NonlinearFactor& expected, double tol) const;
|
bool equals(const gtsam::NonlinearFactor& expected, double tol) const;
|
||||||
gtsam::Unit3 nZ() const;
|
gtsam::Unit3 nRef() const;
|
||||||
gtsam::Unit3 bRef() const;
|
gtsam::Unit3 bMeasured() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <gtsam/navigation/GPSFactor.h>
|
#include <gtsam/navigation/GPSFactor.h>
|
||||||
|
|
|
@ -11,44 +11,44 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file testAttitudeFactor.cpp
|
* @file testAttitudeFactor.cpp
|
||||||
* @brief Unit test for Rot3AttitudeFactor
|
* @brief Unit test for AttitudeFactors (rot3 and Pose3 versions)
|
||||||
* @author Frank Dellaert
|
* @author Frank Dellaert
|
||||||
* @date January 22, 2014
|
* @date January 22, 2014
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <gtsam/navigation/AttitudeFactor.h>
|
#include <CppUnitLite/TestHarness.h>
|
||||||
#include <gtsam/base/Testable.h>
|
#include <gtsam/base/Testable.h>
|
||||||
#include <gtsam/base/numericalDerivative.h>
|
#include <gtsam/base/numericalDerivative.h>
|
||||||
|
#include <gtsam/navigation/AttitudeFactor.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "gtsam/base/Matrix.h"
|
#include "gtsam/base/Matrix.h"
|
||||||
#include <CppUnitLite/TestHarness.h>
|
|
||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gtsam;
|
using namespace gtsam;
|
||||||
|
|
||||||
// *************************************************************************
|
// *************************************************************************
|
||||||
TEST( Rot3AttitudeFactor, Constructor ) {
|
TEST(Rot3AttitudeFactor, Constructor) {
|
||||||
|
|
||||||
// Example: pitch and roll of aircraft in an ENU Cartesian frame.
|
// Example: pitch and roll of aircraft in an ENU Cartesian frame.
|
||||||
// If pitch and roll are zero for an aerospace frame,
|
// If pitch and roll are zero for an aerospace frame,
|
||||||
// that means Z is pointing down, i.e., direction of Z = (0,0,-1)
|
// that means Z is pointing down, i.e., direction of Z = (0,0,-1)
|
||||||
Unit3 bZ(0, 0, 1); // reference direction is body Z axis
|
Unit3 bMeasured(0, 0, 1); // measured direction is body Z axis
|
||||||
Unit3 nDown(0, 0, -1); // down, in ENU navigation frame, is "measurement"
|
Unit3 nDown(0, 0, -1); // down, in ENU navigation frame, is "reference"
|
||||||
|
|
||||||
// Factor
|
// Factor
|
||||||
Key key(1);
|
Key key(1);
|
||||||
SharedNoiseModel model = noiseModel::Isotropic::Sigma(2, 0.25);
|
SharedNoiseModel model = noiseModel::Isotropic::Sigma(2, 0.25);
|
||||||
Rot3AttitudeFactor factor0(key, nDown, model);
|
Rot3AttitudeFactor factor0(key, nDown, model);
|
||||||
Rot3AttitudeFactor factor(key, nDown, model, bZ);
|
Rot3AttitudeFactor factor(key, nDown, model, bMeasured);
|
||||||
EXPECT(assert_equal(factor0,factor,1e-5));
|
EXPECT(assert_equal(factor0, factor, 1e-5));
|
||||||
|
|
||||||
// Create a linearization point at the zero-error point
|
// Create a linearization point at the zero-error point
|
||||||
Rot3 nRb;
|
Rot3 nRb;
|
||||||
EXPECT(assert_equal((Vector) Z_2x1,factor.evaluateError(nRb),1e-5));
|
EXPECT(assert_equal((Vector)Z_2x1, factor.evaluateError(nRb), 1e-5));
|
||||||
|
|
||||||
auto err_fn = [&factor](const Rot3& r){
|
auto err_fn = [&factor](const Rot3& r) {
|
||||||
return factor.evaluateError(r, OptionalNone);
|
return factor.evaluateError(r, OptionalNone);
|
||||||
};
|
};
|
||||||
// Calculate numerical derivatives
|
// Calculate numerical derivatives
|
||||||
|
@ -80,32 +80,31 @@ TEST(Rot3AttitudeFactor, CopyAndMove) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// *************************************************************************
|
// *************************************************************************
|
||||||
TEST( Pose3AttitudeFactor, Constructor ) {
|
TEST(Pose3AttitudeFactor, Constructor) {
|
||||||
|
|
||||||
// Example: pitch and roll of aircraft in an ENU Cartesian frame.
|
// Example: pitch and roll of aircraft in an ENU Cartesian frame.
|
||||||
// If pitch and roll are zero for an aerospace frame,
|
// If pitch and roll are zero for an aerospace frame,
|
||||||
// that means Z is pointing down, i.e., direction of Z = (0,0,-1)
|
// that means Z is pointing down, i.e., direction of Z = (0,0,-1)
|
||||||
Unit3 bZ(0, 0, 1); // reference direction is body Z axis
|
Unit3 bMeasured(0, 0, 1); // measured direction is body Z axis
|
||||||
Unit3 nDown(0, 0, -1); // down, in ENU navigation frame, is "measurement"
|
Unit3 nDown(0, 0, -1); // down, in ENU navigation frame, is "reference"
|
||||||
|
|
||||||
// Factor
|
// Factor
|
||||||
Key key(1);
|
Key key(1);
|
||||||
SharedNoiseModel model = noiseModel::Isotropic::Sigma(2, 0.25);
|
SharedNoiseModel model = noiseModel::Isotropic::Sigma(2, 0.25);
|
||||||
Pose3AttitudeFactor factor0(key, nDown, model);
|
Pose3AttitudeFactor factor0(key, nDown, model);
|
||||||
Pose3AttitudeFactor factor(key, nDown, model, bZ);
|
Pose3AttitudeFactor factor(key, nDown, model, bMeasured);
|
||||||
EXPECT(assert_equal(factor0,factor,1e-5));
|
EXPECT(assert_equal(factor0, factor, 1e-5));
|
||||||
|
|
||||||
// Create a linearization point at the zero-error point
|
// Create a linearization point at the zero-error point
|
||||||
Pose3 T(Rot3(), Point3(-5.0, 8.0, -11.0));
|
Pose3 T(Rot3(), Point3(-5.0, 8.0, -11.0));
|
||||||
EXPECT(assert_equal((Vector) Z_2x1,factor.evaluateError(T),1e-5));
|
EXPECT(assert_equal((Vector)Z_2x1, factor.evaluateError(T), 1e-5));
|
||||||
|
|
||||||
Matrix actualH1;
|
Matrix actualH1;
|
||||||
|
|
||||||
auto err_fn = [&factor](const Pose3& p){
|
auto err_fn = [&factor](const Pose3& p) {
|
||||||
return factor.evaluateError(p, OptionalNone);
|
return factor.evaluateError(p, OptionalNone);
|
||||||
};
|
};
|
||||||
// Calculate numerical derivatives
|
// Calculate numerical derivatives
|
||||||
Matrix expectedH = numericalDerivative11<Vector,Pose3>(err_fn, T);
|
Matrix expectedH = numericalDerivative11<Vector, Pose3>(err_fn, T);
|
||||||
|
|
||||||
// Use the factor to calculate the derivative
|
// Use the factor to calculate the derivative
|
||||||
Matrix actualH;
|
Matrix actualH;
|
||||||
|
|
Loading…
Reference in New Issue