-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathQMC5883LCompass.cpp
468 lines (358 loc) · 10.4 KB
/
QMC5883LCompass.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
===============================================================================================================
QMC5883LCompass.h
Library for using QMC5583L series chip boards as a compass.
Learn more at [https://github.com/mprograms/QMC5883LCompass]
Supports:
- Getting values of XYZ axis.
- Calculating Azimuth.
- Getting 16 point Azimuth bearing direction (0 - 15).
- Getting 16 point Azimuth bearing Names (N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW)
- Smoothing of XYZ readings via rolling averaging and min / max removal.
- Optional chipset modes (see below)
===============================================================================================================
v1.0 - June 13, 2019
Written by MPrograms
Github: [https://github.com/mprograms/]
Release under the GNU General Public License v3
[https://www.gnu.org/licenses/gpl-3.0.en.html]
===============================================================================================================
FROM QST QMC5883L Datasheet [https://nettigo.pl/attachments/440]
-----------------------------------------------
MODE CONTROL (MODE)
Standby 0x00
Continuous 0x01
OUTPUT DATA RATE (ODR)
10Hz 0x00
50Hz 0x04
100Hz 0x08
200Hz 0x0C
FULL SCALE (RNG)
2G 0x00
8G 0x10
OVER SAMPLE RATIO (OSR)
512 0x00
256 0x40
128 0x80
64 0xC0
*/
#include "Arduino.h"
#include "QMC5883LCompass.h"
#include <Wire.h>
QMC5883LCompass::QMC5883LCompass() {
}
/**
INIT
Initialize Chip - This needs to be called in the sketch setup() function.
@since v0.1;
**/
void QMC5883LCompass::init(){
Wire.begin();
_writeReg(0x0B,0x01);
setMode(0x01,0x0C,0x10,0X00);
}
/**
SET ADDRESS
Set the I2C Address of the chip. This needs to be called in the sketch setup() function.
@since v0.1;
**/
// Set I2C Address if different then default.
void QMC5883LCompass::setADDR(byte b){
_ADDR = b;
}
/**
REGISTER
Write the register to the chip.
@since v0.1;
**/
// Write register values to chip
void QMC5883LCompass::_writeReg(byte r, byte v){
Wire.beginTransmission(_ADDR);
Wire.write(r);
Wire.write(v);
Wire.endTransmission();
}
/**
CHIP MODE
Set the chip mode.
@since v0.1;
**/
// Set chip mode
void QMC5883LCompass::setMode(byte mode, byte odr, byte rng, byte osr){
_writeReg(0x09,mode|odr|rng|osr);
}
/**
* Define the magnetic declination for accurate degrees.
* https://www.magnetic-declination.com/
*
* @example
* For: Londrina, PR, Brazil at date 2022-12-05
* The magnetic declination is: -19º 43'
*
* then: setMagneticDeclination(-19, 43);
*/
void QMC5883LCompass::setMagneticDeclination(int degrees, uint8_t minutes) {
_magneticDeclinationDegrees = degrees + minutes / 60;
}
/**
RESET
Reset the chip.
@since v0.1;
**/
// Reset the chip
void QMC5883LCompass::setReset(){
_writeReg(0x0A,0x80);
}
// 1 = Basic 2 = Advanced
void QMC5883LCompass::setSmoothing(byte steps, bool adv){
_smoothUse = true;
_smoothSteps = ( steps > 10) ? 10 : steps;
_smoothAdvanced = (adv == true) ? true : false;
}
void QMC5883LCompass::calibrate() {
clearCalibration();
long calibrationData[3][2] = {{65000, -65000}, {65000, -65000}, {65000, -65000}};
long x = calibrationData[0][0] = calibrationData[0][1] = getX();
long y = calibrationData[1][0] = calibrationData[1][1] = getY();
long z = calibrationData[2][0] = calibrationData[2][1] = getZ();
unsigned long startTime = millis();
while((millis() - startTime) < 10000) {
read();
x = getX();
y = getY();
z = getZ();
if(x < calibrationData[0][0]) {
calibrationData[0][0] = x;
}
if(x > calibrationData[0][1]) {
calibrationData[0][1] = x;
}
if(y < calibrationData[1][0]) {
calibrationData[1][0] = y;
}
if(y > calibrationData[1][1]) {
calibrationData[1][1] = y;
}
if(z < calibrationData[2][0]) {
calibrationData[2][0] = z;
}
if(z > calibrationData[2][1]) {
calibrationData[2][1] = z;
}
}
setCalibration(
calibrationData[0][0],
calibrationData[0][1],
calibrationData[1][0],
calibrationData[1][1],
calibrationData[2][0],
calibrationData[2][1]
);
}
/**
SET CALIBRATION
Set calibration values for more accurate readings
@author Claus Näveke - TheNitek [https://github.com/TheNitek]
@since v1.1.0
@deprecated Instead of setCalibration, use the calibration offset and scale methods.
**/
void QMC5883LCompass::setCalibration(int x_min, int x_max, int y_min, int y_max, int z_min, int z_max){
setCalibrationOffsets(
(x_min + x_max)/2,
(y_min + y_max)/2,
(z_min + z_max)/2
);
float x_avg_delta = (x_max - x_min)/2;
float y_avg_delta = (y_max - y_min)/2;
float z_avg_delta = (z_max - z_min)/2;
float avg_delta = (x_avg_delta + y_avg_delta + z_avg_delta) / 3;
setCalibrationScales(
avg_delta / x_avg_delta,
avg_delta / y_avg_delta,
avg_delta / z_avg_delta
);
}
void QMC5883LCompass::setCalibrationOffsets(float x_offset, float y_offset, float z_offset) {
_offset[0] = x_offset;
_offset[1] = y_offset;
_offset[2] = z_offset;
}
void QMC5883LCompass::setCalibrationScales(float x_scale, float y_scale, float z_scale) {
_scale[0] = x_scale;
_scale[1] = y_scale;
_scale[2] = z_scale;
}
float QMC5883LCompass::getCalibrationOffset(uint8_t index) {
return _offset[index];
}
float QMC5883LCompass::getCalibrationScale(uint8_t index) {
return _scale[index];
}
void QMC5883LCompass::clearCalibration(){
setCalibrationOffsets(0., 0., 0.);
setCalibrationScales(1., 1., 1.);
}
/**
READ
Read the XYZ axis and save the values in an array.
@since v0.1;
**/
void QMC5883LCompass::read(){
Wire.beginTransmission(_ADDR);
Wire.write(0x00);
int err = Wire.endTransmission();
if (!err) {
Wire.requestFrom(_ADDR, (byte)6);
_vRaw[0] = (int)(int16_t)(Wire.read() | Wire.read() << 8);
_vRaw[1] = (int)(int16_t)(Wire.read() | Wire.read() << 8);
_vRaw[2] = (int)(int16_t)(Wire.read() | Wire.read() << 8);
_applyCalibration();
if ( _smoothUse ) {
_smoothing();
}
//byte overflow = Wire.read() & 0x02;
//return overflow << 2;
}
}
/**
APPLY CALIBRATION
This function uses the calibration data provided via @see setCalibration() to calculate more
accurate readings
@author Claus Näveke - TheNitek [https://github.com/TheNitek]
Based on this awesome article:
https://appelsiini.net/2018/calibrate-magnetometer/
@since v1.1.0
**/
void QMC5883LCompass::_applyCalibration(){
_vCalibrated[0] = (_vRaw[0] - _offset[0]) * _scale[0];
_vCalibrated[1] = (_vRaw[1] - _offset[1]) * _scale[1];
_vCalibrated[2] = (_vRaw[2] - _offset[2]) * _scale[2];
}
/**
SMOOTH OUTPUT
This function smooths the output for the XYZ axis. Depending on the options set in
@see setSmoothing(), we can run multiple methods of smoothing the sensor readings.
First we store (n) samples of sensor readings for each axis and store them in a rolling array.
As each new sensor reading comes in we replace it with a new reading. Then we average the total
of all (n) readings.
Advanced Smoothing
If you turn advanced smoothing on, we will select the min and max values from our array
of (n) samples. We then subtract both the min and max from the total and average the total of all
(n - 2) readings.
NOTE: This function does several calculations and can cause your sketch to run slower.
@since v0.3;
**/
void QMC5883LCompass::_smoothing(){
byte max = 0;
byte min = 0;
if ( _vScan > _smoothSteps - 1 ) { _vScan = 0; }
for ( int i = 0; i < 3; i++ ) {
if ( _vTotals[i] != 0 ) {
_vTotals[i] = _vTotals[i] - _vHistory[_vScan][i];
}
_vHistory[_vScan][i] = _vCalibrated[i];
_vTotals[i] = _vTotals[i] + _vHistory[_vScan][i];
if ( _smoothAdvanced ) {
max = 0;
for (int j = 0; j < _smoothSteps - 1; j++) {
max = ( _vHistory[j][i] > _vHistory[max][i] ) ? j : max;
}
min = 0;
for (int k = 0; k < _smoothSteps - 1; k++) {
min = ( _vHistory[k][i] < _vHistory[min][i] ) ? k : min;
}
_vSmooth[i] = ( _vTotals[i] - (_vHistory[max][i] + _vHistory[min][i]) ) / (_smoothSteps - 2);
} else {
_vSmooth[i] = _vTotals[i] / _smoothSteps;
}
}
_vScan++;
}
/**
GET X AXIS
Read the X axis
@since v0.1;
@return int x axis
**/
int QMC5883LCompass::getX(){
return _get(0);
}
/**
GET Y AXIS
Read the Y axis
@since v0.1;
@return int y axis
**/
int QMC5883LCompass::getY(){
return _get(1);
}
/**
GET Z AXIS
Read the Z axis
@since v0.1;
@return int z axis
**/
int QMC5883LCompass::getZ(){
return _get(2);
}
/**
GET SENSOR AXIS READING
Get the smoothed, calibration, or raw data from a given sensor axis
@since v1.1.0
@return int sensor axis value
**/
int QMC5883LCompass::_get(int i){
if ( _smoothUse )
return _vSmooth[i];
return _vCalibrated[i];
}
/**
GET AZIMUTH
Calculate the azimuth (in degrees);
Correct the value with magnetic declination if defined.
@since v0.1;
@return int azimuth
**/
int QMC5883LCompass::getAzimuth(){
float heading = atan2( getY(), getX() ) * 180.0 / PI;
heading += _magneticDeclinationDegrees;
return (int)heading % 360;
}
/**
GET BEARING
Divide the 360 degree circle into 16 equal parts and then return the a value of 0-15
based on where the azimuth is currently pointing.
@since v1.2.1 - function takes into account negative azimuth values. Credit: https://github.com/prospark
@since v1.0.1 - function now requires azimuth parameter.
@since v0.2.0 - initial creation
@return byte direction of bearing
*/
byte QMC5883LCompass::getBearing(int azimuth){
unsigned long a = ( azimuth > -0.5 ) ? azimuth / 22.5 : (azimuth+360)/22.5;
unsigned long r = a - (int)a;
byte sexdec = 0;
sexdec = ( r >= .5 ) ? ceil(a) : floor(a);
return sexdec;
}
/**
This will take the location of the azimuth as calculated in getBearing() and then
produce an array of chars as a text representation of the direction.
NOTE: This function does not return anything since it is not possible to return an array.
Values must be passed by reference back to your sketch.
Example:
( if direction is in 1 / NNE)
char myArray[3];
compass.getDirection(myArray, azimuth);
Serial.print(myArray[0]); // N
Serial.print(myArray[1]); // N
Serial.print(myArray[2]); // E
@see getBearing();
@since v1.0.1 - function now requires azimuth parameter.
@since v0.2.0 - initial creation
*/
void QMC5883LCompass::getDirection(char* myArray, int azimuth){
int d = getBearing(azimuth);
myArray[0] = _bearings[d][0];
myArray[1] = _bearings[d][1];
myArray[2] = _bearings[d][2];
}