GCOP
1.0
|
00001 #ifndef GCOP_KALMANCORRECTOR_H 00002 #define GCOP_KALMANCORRECTOR_H 00003 00004 #include "corrector.h" 00005 00006 namespace gcop { 00007 00008 template <typename T = VectorXd, 00009 int _nx = Dynamic, 00010 int _nu = Dynamic, 00011 int _np = Dynamic, 00012 typename Tz = VectorXd, 00013 int _nz = Dynamic> class KalmanCorrector : public Corrector<T, _nx, _nu, _np, Tz, _nz> { 00014 public: 00015 00016 typedef Matrix<double, _nx, 1> Vectornd; 00017 typedef Matrix<double, _nu, 1> Vectorcd; 00018 typedef Matrix<double, _np, 1> Vectormd; 00019 00020 typedef Matrix<double, _nx, _nx> Matrixnd; 00021 typedef Matrix<double, _nx, _nu> Matrixncd; 00022 typedef Matrix<double, _nx, _nz> Matrixnrd; 00023 typedef Matrix<double, _nu, _nx> Matrixcnd; 00024 typedef Matrix<double, _nu, _nu> Matrixcd; 00025 00026 typedef Matrix<double, _np, _np> Matrixmd; 00027 typedef Matrix<double, _nx, _np> Matrixnmd; 00028 typedef Matrix<double, _np, _nx> Matrixmnd; 00029 00030 typedef Matrix<double, _nz, 1> Vectorrd; 00031 typedef Matrix<double, _nz, _nz> Matrixrd; 00032 typedef Matrix<double, _nz, _nx> Matrixrnd; 00033 typedef Matrix<double, _nz, _nu> Matrixrcd; 00034 typedef Matrix<double, _nz, _np> Matrixrmd; 00035 00036 KalmanCorrector(Manifold<T, _nx> &X, 00037 Sensor<T, _nx, _nu, _np, Tz, _nz> &sensor); 00038 00039 virtual ~KalmanCorrector(); 00040 00041 virtual bool Correct(T& xb, double t, const T &xa, 00042 const Vectorcd &u, const Tz &z, 00043 const Vectormd *p = 0, bool cov = true); 00044 00045 Tz y; 00046 Vectornd dx; 00047 Vectorrd dz; 00048 00049 Matrixrnd H; 00050 Matrixnrd K; 00051 00052 }; 00053 00054 00055 template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 00056 KalmanCorrector<T, _nx, _nu, _np, Tz, _nz>::KalmanCorrector(Manifold<T, _nx> &X, 00057 Sensor<T, _nx, _nu, _np, Tz, _nz> &sensor) : 00058 Corrector<T, _nx, _nu, _np, Tz, _nz>(X, sensor) { 00059 00060 if (_nz == Dynamic) { 00061 y.resize(sensor.Z.n); 00062 dz.resize(sensor.Z.n); 00063 } 00064 if (_nx == Dynamic) { 00065 dx.resize(X.n); 00066 } 00067 if (_nx == Dynamic || _nz == Dynamic) { 00068 K.resize(X.n, sensor.Z.n); 00069 H.resize(sensor.Z.n, X.n); 00070 } 00071 00072 H.setZero(); 00073 } 00074 00075 template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 00076 KalmanCorrector<T, _nx, _nu, _np, Tz, _nz>::~KalmanCorrector() { 00077 } 00078 00079 00080 template <typename T, int _nx, int _nu, int _np, typename Tz, int _nz> 00081 bool KalmanCorrector<T, _nx, _nu, _np, Tz, _nz>::Correct(T& xb, double t, const T &xa, 00082 const Vectorcd &u, const Tz &z, 00083 const Vectormd *p, bool cov) { 00084 this->sensor(y, t, xa, u, p, &H); 00085 00086 // std::cout << "y=" << y << std::endl; 00087 // std::cout << "Pa=" << xa.P << std::endl; 00088 // std::cout << "S=" << H*xa.P*H.transpose() + this->sensor.R << std::endl; 00089 // std::cout << "xa.P*H.transpose()=" << xa.P*H.transpose() << std::endl; 00090 00091 K = xa.P*H.transpose()*(H*xa.P*H.transpose() + this->sensor.R).inverse(); 00092 00093 00094 // std::cout <<"K=" << K<<std::endl; 00095 if (cov) 00096 xb.P = (MatrixXd::Identity(this->X.n, this->X.n) - K*H)*xa.P; 00097 00098 // std::cout << "K=" << K << std::endl; 00099 // std::cout << "z-y=" << z-y << std::endl; 00100 // %x = x*S.exp(K*(z - y)); 00101 //%K = inv(inv(P) + H'*inv(S.R)*H)*H'*inv(S.R); 00102 //%P = (eye(length(x)) - K*H)*P*(eye(length(x)) - K*H)' + K*S.R*K'; 00103 00104 this->sensor.Z.Lift(dz, y, z); 00105 dx = K*dz; 00106 this->X.Retract(xb, xa, dx); 00107 } 00108 } 00109 00110 00111 #endif 00112