-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIMU.cpp
76 lines (64 loc) · 2.28 KB
/
IMU.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
IMU.cpp - Library for combining my accel and gyro data into one nice set of headings/angles
Created by Myles Grant <[email protected]>
See also: https://github.com/grantmd/QuadCopter
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//
// This is a Complementary Filter!
// http://web.mit.edu/scolton/www/filter.pdf
//
// Maybe in the future it should be a Kalman filter:
// http://tom.pycke.be/mav/71/kalman-filtering-of-imu-data
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248889032/35
//
#include "WProgram.h"
#include "IMU.h"
IMU::IMU(){
for (byte axis = XAXIS; axis <= ZAXIS; axis++) {
data[axis] = 0.0;
}
_a = 0.96;
_b = 1 - _a;
_dtGyro = 0.1; // Update rate of gyros: 1kHz (in millis)
}
// Update the filter based on most recent values
// dT is in milliseconds
// g* is gyro rotation rate in degrees/s
// a* is current angle in degrees
// heading is magnetometer heading in degrees
void IMU::update(int dT, float gx, float gy, float gz, float ax, float ay, float az, float heading){
updateAxis(ROLL, dT, gx, ax);
updateAxis(PITCH, dT, gy, ay);
updateAxis(YAW, dT, gz, heading);
}
// Get filtered roll angle
// Positive left, negative right
float IMU::getRoll(){
return data[ROLL];
}
// Get filtered pitch angle
// Positive forward, negative backward
float IMU::getPitch(){
return data[PITCH];
}
// Our absolute compass direction in degrees
float IMU::getHeading(){
return data[YAW];
}
// Update an axis using the complementary filter
// dT is in millis
// gyro is gyro rotation rate in degrees/s
// accel is current angle in degrees
void IMU::updateAxis(byte axis, int dT, float gyro, float accel){
data[axis] = (_a * (data[axis] + (gyro * dT * _dtGyro))) + (_b * accel);
}