source: flair-src/trunk/lib/FlairFilter/src/AhrsComplementaryFilter.cpp@ 214

Last change on this file since 214 was 198, checked in by Sanahuja Guillaume, 7 years ago

modifs imu

File size: 5.9 KB
RevLine 
[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>
[198]22#include <Vector3DSpinBox.h>
[24]23#include <GridLayout.h>
24#include <ImuData.h>
25#include <AhrsData.h>
26#include <math.h>
27
28#define G 9.81
29
30using std::string;
31using namespace flair::core;
32using namespace flair::gui;
33using namespace flair::sensor;
34
35namespace flair { namespace filter {
36
37AhrsComplementaryFilter::AhrsComplementaryFilter(const Imu* parent,string name): isInit(false), Ahrs(parent,name) {
38
[198]39 QHat.q0=1;
40 QHat.q1=0;
41 QHat.q2=0;
42 QHat.q3=0;
43 BHat.x=0;
44 BHat.y=0;
45 BHat.z=0;
[24]46
[198]47 ka[0]=new DoubleSpinBox(parent->GetLayout()->NewRow(),"ka[0]:",0.,10,0.1,2,0.5);
48 ka[1]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"ka[1]:",0.,10,0.1,2,0.5);
49 ka[2]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"ka[2]:",0.,10.,0.1,2,0.);
[24]50
[198]51 kb[0]=new DoubleSpinBox(parent->GetLayout()->NewRow(),"kb[0]:",0.,10,0.1,2,0.01);
52 kb[1]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"kb[1]:",0.,10,0.1,2,0.01);
53 kb[2]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"kb[2]:",0.,10,0.1,2,0.01);
54
55 //put km to [0,0,0] to disable magnetometers
56 km[0]=new DoubleSpinBox(parent->GetLayout()->NewRow(),"km[0]:",0.,50,0.1,2,0.01);
57 km[1]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"km[1]:",0.,50,0.1,2,0.01);
58 km[2]=new DoubleSpinBox(parent->GetLayout()->LastRowLastCol(),"km[2]:",0.,50,0.1,2,0.01);
[157]59
[198]60 magRef=new Vector3DSpinBox(parent->GetLayout()->NewRow(),"ref magnetic field (uT)",
61 0, 100, 1,
62 3,Vector3Df(20.5324,.2367,43.6682)); //Compiegne, France
[157]63 SetIsReady(true);
[24]64}
65
66AhrsComplementaryFilter::~AhrsComplementaryFilter() {
67
68}
69
70void AhrsComplementaryFilter::UpdateFrom(const io_data *data) {
71 ImuData *input=(ImuData*)data;
72 float delta_t;
73 AhrsData* ahrsData;
74 GetDatas(&ahrsData);
[167]75 Vector3Df rawAcc,rawMag,rawGyr;
[24]76 input->GetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
77
78 delta_t=(float)(data->DataTime()-previous_time)/1000000000.;
79 previous_time=data->DataTime();
80
[167]81 Vector3Df aBar,aHat,aTilde;
82 Vector3Df mBar,mHat,mTilde;
[197]83 Vector3Df alpha,dBHat,omegaHat,magRef;
[24]84 Quaternion dQHat;
85
86 //float ka[3]={0.5,0.5,0};
87 //float kb[3]={0.01,0.01,0.01};
[197]88 //float k_m[3]= {0,0,0};
[198]89 magRef=this->magRef->Value();
[24]90
91 if(isInit==true) {
[197]92 // CORRECTION FROM ACCELEROMETER
[24]93 aBar = rawAcc;
94
95 // estimation of IMU vector using QHat (estimated quaternion): aHat = Inv(QHat) * g
96 /*
97 Inv(QHat) = [q0 -q1 -q2 -q3]'
98 Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [0]
99 [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [0]
100 [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [g]
101 */
102 aHat.x = -2*G*(QHat.q1*QHat.q3 - QHat.q0*QHat.q2);
103 aHat.y = -2*G*(QHat.q2*QHat.q3 + QHat.q0*QHat.q1);
104 aHat.z = -G*(1-2.0*(QHat.q1*QHat.q1 + QHat.q2*QHat.q2));
105
106 // cross(aHat, aBar)
107 aTilde=CrossProduct(aHat, aBar);
108
[197]109
110 // CORRECTION FROM MAGNETOMETER
111 // estimation of IMU vector using QHat (estimated quaternion): mHat = Inv(QHat) * m_ref = Inv(QHat) * magRef
[24]112 /*
113 Inv(QHat) = [q0 -q1 -q2 -q3]'
[197]114 Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [magRef.x]
115 [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [magRef.x]
116 [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [magRef.x]
[24]117 */
[197]118 mBar=rawMag;
119
120 mHat.x = (1-2.0*(QHat.q2*QHat.q2 + QHat.q3*QHat.q3))*magRef.x+2.0*(QHat.q1*QHat.q2+QHat.q0*QHat.q3)*magRef.y+2.0*(QHat.q1*QHat.q3-QHat.q0*QHat.q2)*magRef.z;
121 mHat.y = 2.0*(QHat.q1*QHat.q2 - QHat.q0*QHat.q3)*magRef.x+(1-2.0*(QHat.q1*QHat.q1 + QHat.q3*QHat.q3))*magRef.y+2.0*(QHat.q2*QHat.q3 + QHat.q0*QHat.q1)*magRef.z;
122 mHat.z = 2.0*(QHat.q1*QHat.q3+QHat.q0*QHat.q2)*magRef.x+2.0*(QHat.q2*QHat.q3 - QHat.q0*QHat.q1)*magRef.y+(1-2.0*(QHat.q1*QHat.q1 + QHat.q2*QHat.q2))*magRef.z;
123
124
[24]125 // compute the error between mHat and mTilde
126 mTilde=CrossProduct(mHat, mBar);
127
128 // Compute the debiased rotation speed
129 omegaHat = rawGyr - BHat;
130
131 // calculate the correction to apply to the quaternion
[197]132 alpha.x = (ka[0]->Value()*aTilde.x)/(G*G) + (km[0]->Value()*mTilde.x)/(magRef.GetNorm()*magRef.GetNorm());
133 alpha.y = (ka[1]->Value()*aTilde.y)/(G*G) + (km[1]->Value()*mTilde.y)/(magRef.GetNorm()*magRef.GetNorm());
134 alpha.z = (ka[2]->Value()*aTilde.z)/(G*G) + (km[2]->Value()*mTilde.z)/(magRef.GetNorm()*magRef.GetNorm());
[24]135
136 // Bias derivative
137 dBHat.x = kb[0]->Value() * alpha.x;
138 dBHat.y = kb[1]->Value() * alpha.y;
139 dBHat.z = kb[2]->Value() * alpha.z;
140
141 // Bias integration
142 BHat = BHat+dBHat*delta_t;
143
144 // Quaternion derivative: dQHat = 0.5*(QHat*Q_corr)
145 // Q_corr: Corrected pure rotation quaternion for integration
146 dQHat=QHat.GetDerivative(omegaHat - alpha);
147
148 // Quaternion integration
149 QHat = QHat +dQHat*delta_t; // delta_t: sampling period [s]
150
151 QHat.Normalize();
152
153 ahrsData->SetQuaternionAndAngularRates(QHat,rawGyr - BHat);
154
155 } else {
156 isInit=true;
157 }
158
159 ahrsData->SetDataTime(data->DataTime());
160 ProcessUpdate(ahrsData);
161}
162
163} // end namespace filter
164} // end namespace flair
Note: See TracBrowser for help on using the repository browser.