-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compass.ino
73 lines (52 loc) · 2.42 KB
/
Compass.ino
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
/*
MinIMU-9-Arduino-AHRS
Pololu MinIMU-9 + Arduino AHRS (Attitude and Heading Reference System)
Copyright (c) 2011 Pololu Corporation.
http://www.pololu.com/
MinIMU-9-Arduino-AHRS is based on sf9domahrs by Doug Weibel and Jose Julio:
http://code.google.com/p/sf9domahrs/
sf9domahrs is based on ArduIMU v1.5 by Jordi Munoz and William Premerlani, Jose
Julio and Doug Weibel:
http://code.google.com/p/ardu-imu/
MinIMU-9-Arduino-AHRS is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option)
any later version.
MinIMU-9-Arduino-AHRS 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 Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License along
with MinIMU-9-Arduino-AHRS. If not, see <http://www.gnu.org/licenses/>.
*/
#if Compass == 0
//LSM303 compass;
void Compass_Heading() // 2/24/24: use depends on whether the LSMLib is at 0 or 1. if LSMLib = 1, this is not used.
{
float MAG_X;
float MAG_Y;
float cos_roll;
float sin_roll;
float cos_pitch;
float sin_pitch;
cos_roll = cos(roll);
sin_roll = sin(roll);
cos_pitch = cos(pitch);
sin_pitch = sin(pitch);
//added by chatgpt to make tilt compensation work regardless of imu orientation. Not sure it's adding much because the IMU still only works when face-down
// if (c_magnetom_z < 0) {
// if (abs(roll) < PI / 2.0) {
//magnetom_x = -magnetom_x;
// magnetom_y = -magnetom_y; }
// adjust for LSM303 compass axis offsets/sensitivity differences by scaling to +/-0.5 range
c_magnetom_x = (float)(magnetom_x - SENSOR_SIGN[6]*M_X_MIN) / (M_X_MAX - M_X_MIN) - SENSOR_SIGN[6]*0.5;
c_magnetom_y = (float)(magnetom_y - SENSOR_SIGN[7]*M_Y_MIN) / (M_Y_MAX - M_Y_MIN) - SENSOR_SIGN[7]*0.5;
c_magnetom_z = (float)(magnetom_z - SENSOR_SIGN[8]*M_Z_MIN) / (M_Z_MAX - M_Z_MIN) - SENSOR_SIGN[8]*0.5;
// Tilt compensated Magnetic filed X:
MAG_X = c_magnetom_x*cos_pitch + c_magnetom_y*sin_roll*sin_pitch + c_magnetom_z*cos_roll*sin_pitch;
// Tilt compensated Magnetic filed Y:
MAG_Y = c_magnetom_y*cos_roll - c_magnetom_z*sin_roll;
// Tilt-Compensated Magnetic Heading
MAG_Heading = atan2(-MAG_Y,MAG_X);
}
#endif