complementary_filter.h
Go to the documentation of this file.
00001 /*
00002   @author Roberto G. Valenti <robertogl.valenti@gmail.com>
00003 
00004         @section LICENSE
00005   Copyright (c) 2015, City University of New York
00006   CCNY Robotics Lab <http://robotics.ccny.cuny.edu>
00007         All rights reserved.
00008 
00009         Redistribution and use in source and binary forms, with or without
00010         modification, are permitted provided that the following conditions are met:
00011      1. Redistributions of source code must retain the above copyright
00012         notice, this list of conditions and the following disclaimer.
00013      2. Redistributions in binary form must reproduce the above copyright
00014         notice, this list of conditions and the following disclaimer in the
00015         documentation and/or other materials provided with the distribution.
00016      3. Neither the name of the City College of New York nor the
00017         names of its contributors may be used to endorse or promote products
00018         derived from this software without specific prior written permission.
00019 
00020         THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021         ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022         WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023         DISCLAIMED. IN NO EVENT SHALL the CCNY ROBOTICS LAB BE LIABLE FOR ANY
00024         DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025         (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026         LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027         ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029         SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 */
00031 
00032 #ifndef IMU_TOOLS_COMPLEMENTARY_FILTER_H
00033 #define IMU_TOOLS_COMPLEMENTARY_FILTER_H
00034 
00035 namespace imu_tools {
00036 
00037 class ComplementaryFilter
00038 {
00039   public:
00040     ComplementaryFilter();    
00041     virtual ~ComplementaryFilter();
00042 
00043     bool setGainAcc(double gain);
00044     bool setGainMag(double gain);
00045     double getGainAcc() const;
00046     double getGainMag() const;
00047 
00048     bool setBiasAlpha(double bias_alpha);
00049     double getBiasAlpha() const;
00050 
00051     // When the filter is in the steady state, bias estimation will occur (if the
00052     // parameter is enabled).
00053     bool getSteadyState() const;
00054 
00055     void setDoBiasEstimation(bool do_bias_estimation);
00056     bool getDoBiasEstimation() const;
00057 
00058     void setDoAdaptiveGain(bool do_adaptive_gain);
00059     bool getDoAdaptiveGain() const;
00060   
00061     double getAngularVelocityBiasX() const;
00062     double getAngularVelocityBiasY() const;
00063     double getAngularVelocityBiasZ() const;
00064 
00065     // Set the orientation, as a Hamilton Quaternion, of the body frame wrt the
00066     // fixed frame.
00067     void setOrientation(double q0, double q1, double q2, double q3);
00068 
00069     // Get the orientation, as a Hamilton Quaternion, of the body frame wrt the
00070     // fixed frame.
00071     void getOrientation(double& q0, double& q1, double& q2, double& q3) const;
00072 
00073     // Update from accelerometer and gyroscope data.
00074     // [ax, ay, az]: Normalized gravity vector.
00075     // [wx, wy, wz]: Angular veloctiy, in rad / s.
00076     // dt: time delta, in seconds.
00077     void update(double ax, double ay, double az, 
00078                 double wx, double wy, double wz,
00079                 double dt);
00080 
00081     // Update from accelerometer, gyroscope, and magnetometer data.
00082     // [ax, ay, az]: Normalized gravity vector.
00083     // [wx, wy, wz]: Angular veloctiy, in rad / s.
00084     // [mx, my, mz]: Magnetic field, units irrelevant.
00085     // dt: time delta, in seconds.
00086     void update(double ax, double ay, double az, 
00087                 double wx, double wy, double wz,
00088                 double mx, double my, double mz,
00089                 double dt);
00090 
00091   private:
00092     static const double kGravity;
00093     static const double gamma_;
00094     // Bias estimation steady state thresholds
00095     static const double kAngularVelocityThreshold;
00096     static const double kAccelerationThreshold;
00097     static const double kDeltaAngularVelocityThreshold;
00098 
00099     // Gain parameter for the complementary filter, belongs in [0, 1].
00100     double gain_acc_;
00101     double gain_mag_;
00102 
00103     // Bias estimation gain parameter, belongs in [0, 1].
00104     double bias_alpha_;
00105 
00106     // Parameter whether to do bias estimation or not.
00107     bool do_bias_estimation_;
00108     
00109     // Parameter whether to do adaptive gain or not.
00110     bool do_adaptive_gain_;
00111 
00112     bool initialized_;
00113     bool steady_state_;
00114 
00115     // The orientation as a Hamilton quaternion (q0 is the scalar). Represents
00116     // the orientation of the fixed frame wrt the body frame.
00117     double q0_, q1_, q2_, q3_; 
00118 
00119     // Bias in angular velocities;
00120     double wx_prev_, wy_prev_, wz_prev_;
00121 
00122     // Bias in angular velocities;
00123     double wx_bias_, wy_bias_, wz_bias_;
00124 
00125     void updateBiases(double ax, double ay, double az, 
00126                       double wx, double wy, double wz);
00127 
00128     bool checkState(double ax, double ay, double az, 
00129                     double wx, double wy, double wz) const;
00130 
00131     void getPrediction(
00132         double wx, double wy, double wz, double dt, 
00133         double& q0_pred, double& q1_pred, double& q2_pred, double& q3_pred) const;
00134    
00135     void getMeasurement(
00136         double ax, double ay, double az, 
00137         double& q0_meas, double& q1_meas, double& q2_meas, double& q3_meas);
00138 
00139     void getMeasurement(
00140         double ax, double ay, double az, 
00141         double mx, double my, double mz,  
00142         double& q0_meas, double& q1_meas, double& q2_meas, double& q3_meas);
00143 
00144     void getAccCorrection(
00145         double ax, double ay, double az,
00146         double p0, double p1, double p2, double p3,
00147         double& dq0, double& dq1, double& dq2, double& dq3);
00148    
00149     void getMagCorrection(
00150         double mx, double my, double mz,
00151         double p0, double p1, double p2, double p3,
00152         double& dq0, double& dq1, double& dq2, double& dq3); 
00153     
00154     double getAdaptiveGain(double alpha, double ax, double ay, double az);                   
00155 };
00156 
00157 // Utility math functions:
00158 
00159 void normalizeVector(double& x, double& y, double& z);
00160 
00161 void normalizeQuaternion(double& q0, double& q1, double& q2, double& q3);
00162 
00163 void scaleQuaternion(double gain,
00164                      double& dq0, double& dq1, double& dq2, double& dq3); 
00165 
00166 void invertQuaternion(
00167     double q0, double q1, double q2, double q3,
00168     double& q0_inv, double& q1_inv, double& q2_inv, double& q3_inv);
00169 
00170 void quaternionMultiplication(double p0, double p1, double p2, double p3,
00171                               double q0, double q1, double q2, double q3,
00172                               double& r0, double& r1, double& r2, double& r3);
00173 
00174 void rotateVectorByQuaternion(double x, double y, double z,
00175                               double q0, double q1, double q2, double q3,
00176                               double& vx, double& vy, double& vz);
00177 
00178 }  // namespace imu
00179 
00180 #endif  // IMU_TOOLS_COMPLEMENTARY_FILTER_H


imu_complementary_filter
Author(s): Roberto G. Valenti
autogenerated on Sat Jun 8 2019 18:39:06