[24] | 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 |
|
---|
| 6 | // created: 2014/04/28
|
---|
| 7 | // filename: AhrsComplementaryFilter.cpp
|
---|
| 8 | //
|
---|
| 9 | // authors: Augustin Manecy (RT-MaG Toolbox author, augustin.manecy@gmail.com)
|
---|
| 10 | // API changes by Guillaume Sanahuja to fit the Flair framework
|
---|
| 11 | //
|
---|
| 12 | // version: $Id: $
|
---|
| 13 | //
|
---|
| 14 | // purpose: Class defining an Ahrs complementary filter
|
---|
| 15 | //
|
---|
| 16 | //
|
---|
| 17 | /*********************************************************************/
|
---|
| 18 |
|
---|
| 19 | #include "AhrsComplementaryFilter.h"
|
---|
| 20 | #include <Imu.h>
|
---|
| 21 | #include <DoubleSpinBox.h>
|
---|
| 22 | #include <GridLayout.h>
|
---|
| 23 | #include <ImuData.h>
|
---|
| 24 | #include <AhrsData.h>
|
---|
| 25 | #include <math.h>
|
---|
| 26 |
|
---|
| 27 | #define G 9.81
|
---|
| 28 |
|
---|
| 29 | using std::string;
|
---|
| 30 | using namespace flair::core;
|
---|
| 31 | using namespace flair::gui;
|
---|
| 32 | using namespace flair::sensor;
|
---|
| 33 |
|
---|
| 34 | namespace flair { namespace filter {
|
---|
| 35 |
|
---|
| 36 | AhrsComplementaryFilter::AhrsComplementaryFilter(const Imu* parent,string name): isInit(false), Ahrs(parent,name) {
|
---|
| 37 |
|
---|
| 38 | QHat.q0=1;
|
---|
| 39 | QHat.q1=0;
|
---|
| 40 | QHat.q2=0;
|
---|
| 41 | QHat.q3=0;
|
---|
| 42 | BHat.x=0;
|
---|
| 43 | BHat.y=0;
|
---|
| 44 | BHat.z=0;
|
---|
| 45 |
|
---|
| 46 | ka[0]=new DoubleSpinBox(parent->GetLayout()->NewRow(),"ka[0]:",0.,10,0.1,2,0.5);
|
---|
| 47 | ka[1]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"ka[1]:",0.,10,0.1,2,0.5);
|
---|
| 48 | ka[2]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"ka[2]:",0.,10.,0.1,2,0.);
|
---|
| 49 |
|
---|
| 50 | kb[0]=new DoubleSpinBox(parent->GetLayout()->NewRow(),"kb[0]:",0.,10,0.1,2,0.01);
|
---|
| 51 | kb[1]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"kb[1]:",0.,10,0.1,2,0.01);
|
---|
| 52 | kb[2]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"kb[2]:",0.,10,0.1,2,0.01);
|
---|
[157] | 53 |
|
---|
| 54 | SetIsReady(true);
|
---|
[24] | 55 | }
|
---|
| 56 |
|
---|
| 57 | AhrsComplementaryFilter::~AhrsComplementaryFilter() {
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void AhrsComplementaryFilter::UpdateFrom(const io_data *data) {
|
---|
| 62 | ImuData *input=(ImuData*)data;
|
---|
| 63 | float delta_t;
|
---|
| 64 | AhrsData* ahrsData;
|
---|
| 65 | GetDatas(&ahrsData);
|
---|
[167] | 66 | Vector3Df rawAcc,rawMag,rawGyr;
|
---|
[24] | 67 | input->GetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
|
---|
| 68 |
|
---|
| 69 | delta_t=(float)(data->DataTime()-previous_time)/1000000000.;
|
---|
| 70 | previous_time=data->DataTime();
|
---|
| 71 |
|
---|
[167] | 72 | Vector3Df aBar,aHat,aTilde;
|
---|
| 73 | Vector3Df mBar,mHat,mTilde;
|
---|
| 74 | Vector3Df alpha,dBHat,omegaHat;
|
---|
[24] | 75 | Quaternion dQHat;
|
---|
| 76 |
|
---|
| 77 | //float ka[3]={0.5,0.5,0};
|
---|
| 78 | //float kb[3]={0.01,0.01,0.01};
|
---|
| 79 | float k_m[3]= {0,0,0};
|
---|
| 80 |
|
---|
| 81 | if(isInit==true) {
|
---|
| 82 | // CORRECTION FORM ACCELEROMETER
|
---|
| 83 | aBar = rawAcc;
|
---|
| 84 |
|
---|
| 85 | // estimation of IMU vector using QHat (estimated quaternion): aHat = Inv(QHat) * g
|
---|
| 86 | /*
|
---|
| 87 | Inv(QHat) = [q0 -q1 -q2 -q3]'
|
---|
| 88 | Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [0]
|
---|
| 89 | [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [0]
|
---|
| 90 | [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [g]
|
---|
| 91 | */
|
---|
| 92 | aHat.x = -2*G*(QHat.q1*QHat.q3 - QHat.q0*QHat.q2);
|
---|
| 93 | aHat.y = -2*G*(QHat.q2*QHat.q3 + QHat.q0*QHat.q1);
|
---|
| 94 | aHat.z = -G*(1-2.0*(QHat.q1*QHat.q1 + QHat.q2*QHat.q2));
|
---|
| 95 |
|
---|
| 96 | // cross(aHat, aBar)
|
---|
| 97 | aTilde=CrossProduct(aHat, aBar);
|
---|
| 98 |
|
---|
| 99 | // CORRECTION FROM FICTIOUS MAGNETOMETER (to avoid drift of yaw)
|
---|
| 100 | // estimation of IMU vector using QHat (estimated quaternion): mHat = Inv(QHat) * m_ref = Inv(QHat) * [1.0, 0.0, 0.0]'
|
---|
| 101 | /*
|
---|
| 102 | Inv(QHat) = [q0 -q1 -q2 -q3]'
|
---|
| 103 | Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [1]
|
---|
| 104 | [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [0]
|
---|
| 105 | [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [0]
|
---|
| 106 | */
|
---|
| 107 | mBar.x=1;
|
---|
| 108 | mBar.y=0;
|
---|
| 109 | mBar.z=0;
|
---|
| 110 |
|
---|
| 111 | mHat.x = (1-2.0*(QHat.q2*QHat.q2 + QHat.q3*QHat.q3));
|
---|
| 112 | mHat.y = 2.0*(QHat.q1*QHat.q2 - QHat.q0*QHat.q3);
|
---|
| 113 | mHat.z = 2.0*(QHat.q1*QHat.q3 + QHat.q0*QHat.q2);
|
---|
| 114 |
|
---|
| 115 | // compute the error between mHat and mTilde
|
---|
| 116 | mTilde=CrossProduct(mHat, mBar);
|
---|
| 117 |
|
---|
| 118 | // Compute the debiased rotation speed
|
---|
| 119 | omegaHat = rawGyr - BHat;
|
---|
| 120 |
|
---|
| 121 | // calculate the correction to apply to the quaternion
|
---|
| 122 | alpha.x = (ka[0]->Value()*aTilde.x)/(G*G) + (k_m[0]*mTilde.x);
|
---|
| 123 | alpha.y = (ka[1]->Value()*aTilde.y)/(G*G) + (k_m[1]*mTilde.y);
|
---|
| 124 | alpha.z = (ka[2]->Value()*aTilde.z)/(G*G) + (k_m[2]*mTilde.z);
|
---|
| 125 |
|
---|
| 126 | // Bias derivative
|
---|
| 127 | dBHat.x = kb[0]->Value() * alpha.x;
|
---|
| 128 | dBHat.y = kb[1]->Value() * alpha.y;
|
---|
| 129 | dBHat.z = kb[2]->Value() * alpha.z;
|
---|
| 130 |
|
---|
| 131 | // Bias integration
|
---|
| 132 | BHat = BHat+dBHat*delta_t;
|
---|
| 133 |
|
---|
| 134 | // Quaternion derivative: dQHat = 0.5*(QHat*Q_corr)
|
---|
| 135 | // Q_corr: Corrected pure rotation quaternion for integration
|
---|
| 136 | dQHat=QHat.GetDerivative(omegaHat - alpha);
|
---|
| 137 |
|
---|
| 138 | // Quaternion integration
|
---|
| 139 | QHat = QHat +dQHat*delta_t; // delta_t: sampling period [s]
|
---|
| 140 |
|
---|
| 141 | QHat.Normalize();
|
---|
| 142 |
|
---|
| 143 | ahrsData->SetQuaternionAndAngularRates(QHat,rawGyr - BHat);
|
---|
| 144 |
|
---|
| 145 | } else {
|
---|
| 146 | isInit=true;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | ahrsData->SetDataTime(data->DataTime());
|
---|
| 150 | ProcessUpdate(ahrsData);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | } // end namespace filter
|
---|
| 154 | } // end namespace flair
|
---|