forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompass.h
220 lines (177 loc) · 8.13 KB
/
Compass.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
AeroQuad v2.5 Beta 1 - July 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
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 class updated by jihlein
class Compass {
public:
float magMax[3];
float magMin[3];
float magCalibration[3];
float magScale[3];
float magOffset[3];
float hdgX;
float hdgY;
int compassAddress;
float measuredMagX;
float measuredMagY;
float measuredMagZ;
Compass(void) {}
const float getHdgXY(byte axis) {
if (axis == XAXIS) return hdgX;
if (axis == YAXIS) return hdgY;
}
const int getRawData(byte axis) {
if (axis == XAXIS) return measuredMagX;
if (axis == YAXIS) return measuredMagY;
if (axis == ZAXIS) return measuredMagZ;
}
void setMagCal(byte axis, float maxValue, float minValue) {
magMax[axis] = maxValue;
magMin[axis] = minValue;
// Assume max/min is scaled to +1 and -1
// y2 = 1, x2 = max; y1 = -1, x1 = min
// m = (y2 - y1) / (x2 - x1)
// m = 2 / (max - min)
magScale[axis] = 2.0 / (magMax[axis] - magMin[axis]);
// b = y1 - mx1; b = -1 - (m * min)
magOffset[axis] = -(magScale[axis] * magMin[axis]) - 1;
}
const float getMagMax(byte axis) {
return magMax[axis];
}
const float getMagMin(byte axis) {
return magMin[axis];
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Magnetometer (HMC5843)
////////////////////////////////////////////////////////////////////////////////
class Magnetometer_HMC5843 : public Compass {
private:
float cosRoll;
float sinRoll;
float cosPitch;
float sinPitch;
public:
Magnetometer_HMC5843() : Compass() {
compassAddress = 0x1E;
}
////////////////////////////////////////////////////////////////////////////////
// Initialize AeroQuad Mega v2.0 Magnetometer
////////////////////////////////////////////////////////////////////////////////
void initialize(void) {
byte numAttempts = 0;
bool success = false;
delay(10); // Power up delay **
magCalibration[XAXIS] = 1.0;
magCalibration[YAXIS] = 1.0;
magCalibration[ZAXIS] = 1.0;
while (success == false && numAttempts < 5 ) {
numAttempts++;
updateRegisterI2C(compassAddress, 0x00, 0x11); // Set positive bias configuration for sensor calibraiton
delay(50);
updateRegisterI2C(compassAddress, 0x01, 0x20); // Set +/- 1G gain
delay(10);
updateRegisterI2C(compassAddress, 0x02, 0x01); // Perform single conversion
delay(10);
measure(0.0, 0.0); // Read calibration data
delay(10);
if ( fabs(measuredMagX) > 500.0 && fabs(measuredMagX) < 1000.0 \
&& fabs(measuredMagY) > 500.0 && fabs(measuredMagY) < 1000.0 \
&& fabs(measuredMagZ) > 500.0 && fabs(measuredMagZ) < 1000.0) {
magCalibration[XAXIS] = fabs(715.0 / measuredMagX);
magCalibration[YAXIS] = fabs(715.0 / measuredMagY);
magCalibration[ZAXIS] = fabs(715.0 / measuredMagZ);
success = true;
}
updateRegisterI2C(compassAddress, 0x00, 0x10); // Set 10hz update rate and normal operaiton
delay(50);
updateRegisterI2C(compassAddress, 0x02, 0x00); // Continuous Update mode
delay(50); // Mode change delay (1/Update Rate) **
}
measure(0.0, 0.0); // Assume 1st measurement at 0 degrees roll and 0 degrees pitch
}
////////////////////////////////////////////////////////////////////////////////
// Measure AeroQuad Mega v2.0 Magnetometer
////////////////////////////////////////////////////////////////////////////////
void measure(float roll, float pitch) {
float magX;
float magY;
float tmp;
sendByteI2C(compassAddress, 0x03);
Wire.requestFrom(compassAddress, 6);
measuredMagX = ((Wire.receive() << 8) | Wire.receive()) * magCalibration[XAXIS];
measuredMagY = -((Wire.receive() << 8) | Wire.receive()) * magCalibration[YAXIS];
measuredMagZ = -((Wire.receive() << 8) | Wire.receive()) * magCalibration[ZAXIS];
Wire.endTransmission();
cosRoll = cos(roll);
sinRoll = sin(roll);
cosPitch = cos(pitch);
sinPitch = sin(pitch);
magX = ((float)measuredMagX * magScale[XAXIS] + magOffset[XAXIS]) * cosPitch + \
((float)measuredMagY * magScale[YAXIS] + magOffset[YAXIS]) * sinRoll * sinPitch + \
((float)measuredMagZ * magScale[ZAXIS] + magOffset[ZAXIS]) * cosRoll * sinPitch;
magY = ((float)measuredMagY * magScale[YAXIS] + magOffset[YAXIS]) * cosRoll - \
((float)measuredMagZ * magScale[ZAXIS] + magOffset[ZAXIS]) * sinRoll;
tmp = sqrt(magX * magX + magY * magY);
hdgX = magX / tmp;
hdgY = -magY / tmp;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ***********************************************************************
// ************************* CHR6DM Subclass *****************************
// ***********************************************************************
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
class Compass_CHR6DM : public Compass {
public:
Compass_CHR6DM() : Compass() {}
void initialize(void) {}
const int getRawData(byte) {}
void measure(float roll, float pitch) {
heading = chr6dm.data.yaw; //this hardly needs any filtering :)
// Change from +/-180 to 0-360
if (heading < 0) absoluteHeading = 360 + heading;
else absoluteHeading = heading;
}
};
class Compass_CHR6DM_Fake : public Compass {
public:
Compass_CHR6DM_Fake() : Compass() {}
void initialize(void) {}
const int getRawData(byte) {}
void measure(float roll, float pitch) {
heading = 0;
// Change from +/-180 to 0-360
if (heading < 0) absoluteHeading = 360 + heading;
else absoluteHeading = heading;
}
};
#endif