HMC5883L Digital Compass Library

Fork of HMC5883L by Baser Kandehir

Files at this revision

API Documentation at this revision

Comitter:
roger_wee
Date:
Sun Jun 04 06:19:54 2017 +0000
Parent:
2:bbc9ad18fd3e
Commit message:
changed magnetometer values from microtesla to milligauss; passed through madgwick's filter to compute yaw, pitch, & roll

Changed in this revision

HMC5883L.cpp Show annotated file Show diff for this revision Revisions of this file
HMC5883L.h Show annotated file Show diff for this revision Revisions of this file
diff -r bbc9ad18fd3e -r f5f6aaf24be0 HMC5883L.cpp
--- a/HMC5883L.cpp	Wed Aug 05 13:16:12 2015 +0000
+++ b/HMC5883L.cpp	Sun Jun 04 06:19:54 2017 +0000
@@ -103,6 +103,7 @@
 
 void HMC5883L::init()
 {
+    i2c.frequency(400000);  // use fast (400 kHz) I2C
     writeByte(HMC5883L_ADDRESS, CONFIG_A, 0x78);  // Number of samples averaged: 8, Data output rate: 75 Hz
     writeByte(HMC5883L_ADDRESS, MODE,     0x00);  // Continuous-Measurement Mode
     setMagGain(MagGain_13);
@@ -121,10 +122,11 @@
     dest[2] = (int16_t)(((int16_t)rawData[2]<<8) | rawData[3]);  // MAG_ZOUT
     dest[1] = (int16_t)(((int16_t)rawData[4]<<8) | rawData[5]);  // MAG_YOUT 
     
+     float mRes = 0.73;
     /* Convert raw data to magnetic field values in microtesla */
-     dest[0] = dest[0] / Gauss_LSB_XY * GAUSS_TO_MICROTESLA;
-     dest[1] = dest[1] / Gauss_LSB_XY * GAUSS_TO_MICROTESLA;
-     dest[2] = dest[2] / Gauss_LSB_Z  * GAUSS_TO_MICROTESLA;
+     dest[0] = dest[0] * mRes; // / Gauss_LSB_XY * GAUSS_TO_MICROTESLA;
+     dest[1] = dest[1] * mRes; // / Gauss_LSB_XY * GAUSS_TO_MICROTESLA;
+     dest[2] = dest[2] * mRes; // / Gauss_LSB_Z  * GAUSS_TO_MICROTESLA;
 }
 
 double HMC5883L::getHeading()
@@ -138,7 +140,7 @@
     // After calculating heading declination angle should be added to heading which is the error of the magnetic field in specific location.
     // declinationAngle can be found here http://www.magnetic-declination.com/
     // For Ankara (my location) declinationAngle is ~5.5 degrees (0.096 radians)
-    float declinationAngle = 0.096;
+    float declinationAngle = 0.022;
     heading += declinationAngle;
     
     // Correct for when signs are reversed.
diff -r bbc9ad18fd3e -r f5f6aaf24be0 HMC5883L.h
--- a/HMC5883L.h	Wed Aug 05 13:16:12 2015 +0000
+++ b/HMC5883L.h	Sun Jun 04 06:19:54 2017 +0000
@@ -28,7 +28,6 @@
 
 #include "mbed.h"
 #include "math.h"
-#include "ledControl.h"
 
 #define PI 3.14159265359 
 #define GAUSS_TO_MICROTESLA 100