imu_ekf.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2011, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Sebastian Klose
00036  *********************************************************************/
00037 
00038 #ifndef IMU_FILTER_IMU_EKF_H
00039 #define IMU_FILTER_IMU_EKF_H
00040 
00041 #include <Eigen/Core>
00042 #include <Eigen/Geometry>
00043 
00044 #include <opencv/cv.hpp>
00045 
00046 #include <ros/ros.h>
00047 
00048 #include <imu_filter/imu_measurement.h>
00049 #include <imu_filter/imu_state.h>
00050 #include <imu_filter/observation.h>
00051 
00052 namespace imu_filter 
00053 {
00054         //* A Extended Kalman Filter class for IMUs
00060         class ImuEkf
00061         {
00062                 public:
00063                         typedef Eigen::Matrix<double, 12, 12> ProcessNoiseMatrix;
00064 
00069                         ImuEkf();
00070 
00074                         ~ImuEkf();
00075 
00090                         void propagate( const ImuMeasurement & measurement,     double deltaT );
00091 
00099                         void correction( const Observation & observation );
00100 
00106                         const ImuState & state() const { return state_; }
00107                         ImuState & state() { return state_; }
00108 
00121                         void initialize( const Eigen::Quaterniond & rot, 
00122                                                          const Eigen::Vector3d & pos,
00123                                                          const Eigen::Quaterniond & calibRot, 
00124                                                          const Eigen::Vector3d & calibPos,
00125                                                          const Eigen::Vector3d & measuredAcceleration,
00126                                                          const ImuState::CovarianceMatrix & covMat,
00127                                                          bool constrainGravity ); 
00128 
00133                         void setStateCovariance( const ImuState::CovarianceMatrix & covMat ) { state_.covariance() = covMat; }
00134 
00139                         void setStateCovariance( const ImuState::StateVector & diag ) { state_.covariance() = diag.asDiagonal(); }
00140 
00147                         void setProcessNoise( const ProcessNoiseMatrix & noiseMat ) { Q_ = noiseMat; }
00148 
00155                         void setExtrinsicTransform( const Eigen::Quaterniond & rot, const Eigen::Vector3d & trans );
00156 
00157 
00166                         void objectTransformation( Eigen::Quaterniond & rot, Eigen::Vector3d & trans, Eigen::Vector3d & grav );
00167 
00172                         void setEstimateGravity( bool v )       { state_.setEstimateGravity( v ); }
00173 
00178                         void setEstimateGyroBias( bool v )      { state_.setEstimateGyroBias( v ); }
00179 
00184                         void setEstimateAccBias( bool v )       { state_.setEstimateAccBias( v ); }
00185 
00190                         void setVelocityDamping( double v )     { velocity_damping_ = v; }
00191 
00192                 private:
00193                         ImuState        state_;
00194                         cv::RNG         rng_;
00195 
00196                         // reserve space for the jacobians:
00197                         // F = dpropagate/dstate = [19x19]
00198                         Eigen::Matrix<double, ImuState::Dimension, ImuState::Dimension> F_;
00199 
00200                         // G = dpropagate/dnoise = [19x12]
00201                         Eigen::Matrix<double, ImuState::Dimension, 12>  G_;
00202 
00203                         // Q = process noise matrix; 
00204                         ProcessNoiseMatrix      Q_;
00205 
00206                         // Kalman vars
00207                         // error between prediction and measurement
00208                         Observation::Vector                                                             measResidual_;
00209                         ImuState::StateVector                                                   stateUpdate_;
00210                         Eigen::Matrix<double, ImuState::Dimension, 7>   kalmanGain_;
00211                         ImuState::CovarianceMatrix                                              covarianceUpdate_;
00212                         double                                                                                  velocity_damping_;
00213 
00214 
00215                         // the process is continuous time -> update the process noise matrix according to time step
00216                         void updateProcessNoise( const ImuMeasurement & measurment, double deltaT );
00217 
00218                         void propagateCovariance();
00219 
00220                         void propagateOrientation( Eigen::Quaterniond & qnew,
00221                                                                            const ImuState & state,
00222                                                                            const ImuMeasurement & measurement,
00223                                                                            double deltaT );
00224 
00225                         void propagateTranslation( Eigen::Vector3d & tnew,
00226                                                                            const Eigen::Quaterniond & rot,
00227                                                                            const ImuState & state,
00228                                                                            const ImuMeasurement & measurement,
00229                                                                            double deltaT );
00230 
00231                         void propagateVelocity( Eigen::Vector3d & vnew,
00232                                                                     const Eigen::Quaterniond & rot,
00233                                                                     const ImuState & state,
00234                                                                     const ImuMeasurement & measurement,
00235                                                                     double deltaT );
00236 
00237                         // update of perturbation jacobian G
00238                         void updatePerturbationJacobian( const Eigen::Quaterniond & d, double deltaT );
00239 
00240                         void numericalJacobians( const ImuState & state,
00241                                                                          const ImuMeasurement & measurement,
00242                                                                          double deltaT,
00243                                                                          Eigen::Matrix<double, 19, 19> & F,
00244                                                                          Eigen::Matrix<double, 19, 12> & G );
00245 
00246                         void numericalJacobians2( const ImuState & state,
00247                                                                           const ImuMeasurement & measurement,
00248                                                                           double deltaT,
00249                                                                           Eigen::Matrix<double, 19, 19> & F,
00250                                                                           Eigen::Matrix<double, 19, 12> & G );
00251 
00252                         void noiseJacobian( const ImuState & state, 
00253                                                             const ImuMeasurement & meas,
00254                                                             double deltaT,
00255                                                             double step,        
00256                                                             Eigen::Matrix<double, 19,12> & G );
00257 
00258                         void stateJacobian( const ImuState & state, 
00259                                                             const ImuMeasurement & meas, 
00260                                                                 double deltaT,
00261                                                             double step,        
00262                                                             Eigen::Matrix<double, 19, 19> & F );
00263 
00264                         void propagateState( const ImuState & oldState, 
00265                                                                  const ImuMeasurement & meas, 
00266                                                                  double deltaT, ImuState & newState );
00267 
00268                         void normalizeOrientationObservation( Eigen::Quaterniond & q )
00269                         {
00270                                 q.normalize();
00271 
00272                                 // find closest version (in sign) of quaternion w.r.t. to current estimate
00273                                 Eigen::Vector4d a, b;
00274                                 a =  state_.orientation().coeffs();
00275                                 b =  q.coeffs();
00276                                 if( ( a - b ).norm() > ( a + b ).norm() ) {
00277                                         q.coeffs() *= -1;
00278                                 }
00279                         }
00280         };
00281 
00282 }
00283 
00284 #endif


imu_filter
Author(s): Sebastian Klose
autogenerated on Thu Dec 12 2013 11:24:42