|
| 1 | +/** |
| 2 | + * @file GNSSDCSFactor.h |
| 3 | + * @author Ryan Watson |
| 4 | + * @brief Header file for GNSS Factor with multimodal uncert. model |
| 5 | + **/ |
| 6 | + |
| 7 | +#pragma once |
| 8 | +#include <gtsam/base/Vector.h> |
| 9 | +#include <gtsam/base/Matrix.h> |
| 10 | +#include <gtsam/base/Testable.h> |
| 11 | +#include <gtsam/geometry/Pose3.h> |
| 12 | +#include <gtsam/geometry/Point2.h> |
| 13 | +#include <gtsam/geometry/Point3.h> |
| 14 | +#include <gtsam/linear/GaussianFactor.h> |
| 15 | +#include <gtsam/gnssNavigation/GnssTools.h> |
| 16 | +#include <gtsam/nonlinear/NonlinearFactor.h> |
| 17 | +#include <gtsam/gnssNavigation/nonBiasStates.h> |
| 18 | + |
| 19 | + |
| 20 | +#include <libcluster/merge.h> |
| 21 | +#include <libcluster/probutils.h> |
| 22 | +#include <libcluster/libcluster.h> |
| 23 | +#include <libcluster/distributions.h> |
| 24 | + |
| 25 | +#include <boost/tuple/tuple.hpp> |
| 26 | +#include <boost/math/special_functions.hpp> |
| 27 | + |
| 28 | + |
| 29 | +namespace gtsam { |
| 30 | + |
| 31 | + |
| 32 | +class GTSAM_EXPORT GNSSDCSFactor : public NonlinearFactor { |
| 33 | + |
| 34 | +private: |
| 35 | + |
| 36 | +typedef gtsam::NonlinearFactor Base; |
| 37 | +typedef GNSSDCSFactor This; |
| 38 | + |
| 39 | +mutable int iter_count_; |
| 40 | +Key k1_,k2_; |
| 41 | +Point3 satXYZ_; |
| 42 | +Point3 nomXYZ_; |
| 43 | +nonBiasStates h_; |
| 44 | +Vector2 measured_, k_; |
| 45 | +Eigen::MatrixXd model_; |
| 46 | + |
| 47 | +public: |
| 48 | + |
| 49 | +typedef boost::shared_ptr<GNSSDCSFactor> shared_ptr; |
| 50 | + |
| 51 | +GNSSDCSFactor() : measured_() { |
| 52 | + h_=Matrix(2,5); |
| 53 | +} |
| 54 | + |
| 55 | +GNSSDCSFactor(Key deltaStates, Key bias, const Vector2 measurement, |
| 56 | + const Point3 satXYZ, const Point3 nomXYZ, Eigen::MatrixXd &model, const Vector2 k) : |
| 57 | + Base(cref_list_of<2>(deltaStates)(bias)), k1_(deltaStates), k2_(bias), k_(k), measured_(measurement), satXYZ_(satXYZ), nomXYZ_(nomXYZ), model_(model), iter_count_(0) { |
| 58 | +} |
| 59 | + |
| 60 | +virtual ~GNSSDCSFactor() { |
| 61 | +} |
| 62 | + |
| 63 | +/** print */ |
| 64 | +virtual void print(const std::string& s, const KeyFormatter& keyFormatter = DefaultKeyFormatter) const { |
| 65 | + std::cout << s << "GNSS Factor(" |
| 66 | + << keyFormatter(k1_) << "," |
| 67 | + << keyFormatter(k2_) << ")\n" |
| 68 | + << " measured: " << measured_.transpose(); |
| 69 | +} |
| 70 | + |
| 71 | +virtual bool equals(const NonlinearFactor& f, double tol = 1e-9) const { |
| 72 | + const This *t = dynamic_cast<const This*>(&f); |
| 73 | + |
| 74 | + if (t && Base::equals(f)) { return k1_ == t->k1_ && k2_ == t->k2_; } |
| 75 | + else{ return false; } |
| 76 | +} |
| 77 | + |
| 78 | +virtual gtsam::NonlinearFactor::shared_ptr clone() const { |
| 79 | + return boost::static_pointer_cast<gtsam::NonlinearFactor>( |
| 80 | + gtsam::NonlinearFactor::shared_ptr(new GNSSDCSFactor(*this))); |
| 81 | +} |
| 82 | + |
| 83 | +virtual double error(const gtsam::Values& x) const { |
| 84 | + return whitenedError(x).squaredNorm(); |
| 85 | +} |
| 86 | + |
| 87 | +Vector unwhitenedError(const gtsam::Values& x, |
| 88 | + boost::optional<std::vector<Matrix>&> H = boost::none) const; |
| 89 | + |
| 90 | +Vector whitenedError(const gtsam::Values& x, |
| 91 | + boost::optional<std::vector<Matrix>&> H = boost::none) const; |
| 92 | + |
| 93 | +virtual Vector residual(const gtsam::Values& x) const { |
| 94 | + const Vector b = unwhitenedError(x); |
| 95 | + return b; |
| 96 | +} |
| 97 | + |
| 98 | +virtual size_t dim() const { |
| 99 | + return 5; |
| 100 | +} |
| 101 | + |
| 102 | +std::size_t size() const { |
| 103 | + return 2; |
| 104 | +} |
| 105 | + |
| 106 | +bool active(const gtsam::Values& x) const { |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +/* ************************************************************************* */ |
| 113 | +/** |
| 114 | + * Linearize a non-linearFactorN to get a gtsam::GaussianFactor, |
| 115 | + * \f$ Ax-b \approx h(x+\delta x)-z = h(x) + A \delta x - z \f$ |
| 116 | + * Hence \f$ b = z - h(x) = - \mathtt{error\_vector}(x) \f$ |
| 117 | + */ |
| 118 | +/* This version of linearize recalculates the noise model each time */ |
| 119 | +virtual boost::shared_ptr<gtsam::GaussianFactor> linearize( |
| 120 | + const gtsam::Values& x) const { |
| 121 | + |
| 122 | + iter_count_ += 1; |
| 123 | + |
| 124 | + if (!active(x)) |
| 125 | + return boost::shared_ptr<JacobianFactor>(); |
| 126 | + |
| 127 | + // Call evaluate error to get Jacobians and RHS vector b |
| 128 | + std::vector<Matrix> A(this->size()); |
| 129 | + Vector b = unwhitenedError(x, A); |
| 130 | + |
| 131 | + // Fill in terms, needed to create JacobianFactor below |
| 132 | + std::vector<std::pair<Key, Matrix> > terms(size()); |
| 133 | + for (size_t j = 0; j < size(); ++j) { |
| 134 | + terms[j].first = keys()[j]; |
| 135 | + terms[j].second.swap(A[j]); |
| 136 | + } |
| 137 | + |
| 138 | + // DCS Base scaling of cov. |
| 139 | + double v_range, v_phase; |
| 140 | + |
| 141 | + // check range residaul |
| 142 | + if (std::pow(b(0),2) < k_(0) || iter_count_ < 2) |
| 143 | + { |
| 144 | + v_range = model_(0,0); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + double scale = (4*std::pow(k_(0),2)) / (std::pow( std::pow(b(0),2) + k_(0),2)); |
| 149 | + v_range = model_(0,0)/scale; |
| 150 | + } |
| 151 | + |
| 152 | + // check phase residaul |
| 153 | + if (std::pow(b(1),2) < k_(1) || iter_count_ < 2) |
| 154 | + { |
| 155 | + v_phase = model_(1,1); |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + double scale = (4*std::pow(k_(1),2)) / (std::pow( std::pow(b(1),2) + k_(1),2)); |
| 160 | + v_phase = model_(1,1)/scale; |
| 161 | + } |
| 162 | + |
| 163 | + auto jacobianFactor = GaussianFactor::shared_ptr( new JacobianFactor(terms, -b, noiseModel::Diagonal::Variances((gtsam::Vector(2) << v_range, v_phase).finished()) )); |
| 164 | + |
| 165 | + return jacobianFactor; |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +private: |
| 170 | + |
| 171 | +/** Serialization function */ |
| 172 | +friend class boost::serialization::access; |
| 173 | +template<class ARCHIVE> |
| 174 | +void serialize(ARCHIVE & ar, const unsigned int /*version*/) { |
| 175 | + ar |
| 176 | + & boost::serialization::make_nvp("NonlinearFactor", |
| 177 | + boost::serialization::base_object<Base>(*this)); |
| 178 | + ar & BOOST_SERIALIZATION_NVP(measured_); |
| 179 | +} |
| 180 | + |
| 181 | +}; // GNSSDCSFactor Factor |
| 182 | +} // namespace |
0 commit comments