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

Last change on this file since 223 was 223, checked in by Sanahuja Guillaume, 4 years ago

add delta time to io_data

File size: 5.8 KB
Line 
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 <Vector3DSpinBox.h>
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): Ahrs(parent,name) {
38
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;
46
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.);
50
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);
59
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
63 SetIsReady(true);
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);
75 Vector3Df rawAcc,rawMag,rawGyr;
76 input->GetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
77
78 delta_t=(float)(data->DataDeltaTime())/1000000000.;
79
80 Vector3Df aBar,aHat,aTilde;
81 Vector3Df mBar,mHat,mTilde;
82 Vector3Df alpha,dBHat,omegaHat,magRef;
83 Quaternion dQHat;
84
85 //float ka[3]={0.5,0.5,0};
86 //float kb[3]={0.01,0.01,0.01};
87 //float k_m[3]= {0,0,0};
88 magRef=this->magRef->Value();
89
90 if(delta_t!=0) {
91 // CORRECTION FROM ACCELEROMETER
92 aBar = rawAcc;
93
94 // estimation of IMU vector using QHat (estimated quaternion): aHat = Inv(QHat) * g
95 /*
96 Inv(QHat) = [q0 -q1 -q2 -q3]'
97 Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [0]
98 [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [0]
99 [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [g]
100 */
101 aHat.x = -2*G*(QHat.q1*QHat.q3 - QHat.q0*QHat.q2);
102 aHat.y = -2*G*(QHat.q2*QHat.q3 + QHat.q0*QHat.q1);
103 aHat.z = -G*(1-2.0*(QHat.q1*QHat.q1 + QHat.q2*QHat.q2));
104
105 // cross(aHat, aBar)
106 aTilde=CrossProduct(aHat, aBar);
107
108
109 // CORRECTION FROM MAGNETOMETER
110 // estimation of IMU vector using QHat (estimated quaternion): mHat = Inv(QHat) * m_ref = Inv(QHat) * magRef
111 /*
112 Inv(QHat) = [q0 -q1 -q2 -q3]'
113 Inv(QHat)*g = [1-2(q2^2+q3^2) 2(q1q2+q0q3) 2(q1q3-q0q2)] [magRef.x]
114 [ 2(q1q2-q0q3) 1-2(q1^2+q3^2)) 2(q2q3+q0q1)] * [magRef.x]
115 [ 2(q1q3+q0q2) 2(q2q3-q0q1) 1-2(q1^2+q2^2)] [magRef.x]
116 */
117 mBar=rawMag;
118
119 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;
120 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;
121 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;
122
123
124 // compute the error between mHat and mTilde
125 mTilde=CrossProduct(mHat, mBar);
126
127 // Compute the debiased rotation speed
128 omegaHat = rawGyr - BHat;
129
130 // calculate the correction to apply to the quaternion
131 alpha.x = (ka[0]->Value()*aTilde.x)/(G*G) + (km[0]->Value()*mTilde.x)/(magRef.GetNorm()*magRef.GetNorm());
132 alpha.y = (ka[1]->Value()*aTilde.y)/(G*G) + (km[1]->Value()*mTilde.y)/(magRef.GetNorm()*magRef.GetNorm());
133 alpha.z = (ka[2]->Value()*aTilde.z)/(G*G) + (km[2]->Value()*mTilde.z)/(magRef.GetNorm()*magRef.GetNorm());
134
135 // Bias derivative
136 dBHat.x = kb[0]->Value() * alpha.x;
137 dBHat.y = kb[1]->Value() * alpha.y;
138 dBHat.z = kb[2]->Value() * alpha.z;
139
140 // Bias integration
141 BHat = BHat+dBHat*delta_t;
142
143 // Quaternion derivative: dQHat = 0.5*(QHat*Q_corr)
144 // Q_corr: Corrected pure rotation quaternion for integration
145 dQHat=QHat.GetDerivative(omegaHat - alpha);
146
147 // Quaternion integration
148 QHat = QHat +dQHat*delta_t; // delta_t: sampling period [s]
149
150 QHat.Normalize();
151
152 ahrsData->SetQuaternionAndAngularRates(QHat,rawGyr - BHat);
153
154 }
155
156 ahrsData->SetDataTime(data->DataTime());
157 ProcessUpdate(ahrsData);
158}
159
160} // end namespace filter
161} // end namespace flair
Note: See TracBrowser for help on using the repository browser.