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

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

use magneto

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