HMC5883 Digital compass Library

Dependents:   sensor Cansat_program Cansat_program2 compass_cal ... more

Fork of HMC5883L by Tyler Weaver

Files at this revision

API Documentation at this revision

Comitter:
xeta05
Date:
Tue Dec 10 10:58:28 2013 +0000
Parent:
4:bc4e1201e092
Commit message:
*Corrected the XZY order instead of the previous XYZ to match the datasheet.; * Added Declination compensation by a define

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 bc4e1201e092 -r c9ce1eeaf001 HMC5883L.cpp
--- a/HMC5883L.cpp	Tue Nov 06 17:35:51 2012 +0000
+++ b/HMC5883L.cpp	Tue Dec 10 10:58:28 2013 +0000
@@ -1,6 +1,6 @@
 /*
  * @file HMC5883L.cpp
- * @author Tyler Weaver
+ * @author Oskar Lopez de Gamboa
  *
  * @section LICENSE
  *
@@ -22,11 +22,11 @@
  * @section DESCRIPTION
  *
  * HMC5883L 3-Axis Digital Compas IC
- * For use with the Sparkfun 9 Degrees of Freedom - Sensor Stick
+ * The library done by Tyler Weaver with:
  *
- * Datasheet:
- *
- * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf
+ *  *Corrected the XZY order instead of the previous XYZ to match the datasheet.
+ *  *Added Declination compensation by a define
+ * 
  */
 
 #include "HMC5883L.h"
@@ -51,8 +51,9 @@
 {
     // init - configure your setup here
     setConfigurationA(AVG8_SAMPLES | OUTPUT_RATE_15); // 8 sample average, 15Hz, normal mode
-    setConfigurationB(0x20); // default 
+    setConfigurationB(0x20); // default gain
     setMode(CONTINUOUS_MODE); // continuous sample mode
+    wait(0.006);//wait 6ms as told in the datasheet
 }
 
 void HMC5883L::setConfigurationA(char config)
@@ -133,9 +134,11 @@
 {
     int16_t raw_data[3];
     getXYZ(raw_data);
-    double heading = atan2(static_cast<double>(raw_data[1]), static_cast<double>(raw_data[0])); // heading = arctan(Y/X)
+    //The  HMC5883L gives X Z Y order
+    double heading = atan2(static_cast<double>(raw_data[2]), static_cast<double>(raw_data[0])); // heading = arctan(Y/X)
     
-    // TODO: declenation angle compensation
+    
+    heading += DECLINATION_ANGLE;
     
     if(heading < 0.0) // fix sign
         heading += PI2;
diff -r bc4e1201e092 -r c9ce1eeaf001 HMC5883L.h
--- a/HMC5883L.h	Tue Nov 06 17:35:51 2012 +0000
+++ b/HMC5883L.h	Tue Dec 10 10:58:28 2013 +0000
@@ -1,6 +1,6 @@
 /*
  * @file HMC5883L.h
- * @author Tyler Weaver
+ * @author Oskar Lopez de Gamboa
  *
  * @section LICENSE
  *
@@ -22,11 +22,12 @@
  * @section DESCRIPTION
  *
  * HMC5883L 3-Axis Digital Compas IC
- * For use with the Sparkfun 9 Degrees of Freedom - Sensor Stick
+ * The library done by Tyler Weaver with:
  *
- * Datasheet:
+ *  *Corrected the XZY order instead of the previous XYZ to match the datasheet.
+ *  *Added Declination compensation by a define
+ * 
  *
- * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf
  */
 
 #ifndef HMC5883L_H
@@ -83,6 +84,17 @@
 #define RAD_TO_DEG  (180.0/M_PI)
 #define DEG_TO_RAD  (M_PI/180.0)
 
+// Once you have your heading, you must then add your 'Declination Angle', 
+// which is  the 'Error' of the magnetic field in your location.
+// Find yours here: http://www.magnetic-declination.com/
+// Mine is:  -1° 13' WEST which is -1.2167 Degrees, or (which we need) 
+// 0,021234839232597676519238237683278  radians, I will use 0.02123
+// If you cannot find your Declination, put 0, your compass will be slightly off.
+
+#define  DECLINATION_ANGLE -0.02123
+//#define  DECLINATION_ANGLE 0
+
+
 /**
  * The HMC5883L 3-Axis Digital Compass IC
  */
@@ -193,8 +205,9 @@
     
     /**
     * Function for retriaval of the raw data
+    * Caution!!  the HMC5883L gives you the data in XZY order
     *
-    * @param output buffer that is atleast 3 in length
+    * @param output buffer that is at least 3 in length
     */
     void getXYZ(int16_t raw[3]);
     
@@ -213,7 +226,7 @@
     * Compass must be held flat and away from an magnetic field generating
     * devices such as cell phones and speakers.
     *
-    * TODO: declenation angle compensation
+    * 
     * 
     * @returns heading in radians
     */
@@ -225,7 +238,7 @@
     * Compass must be held flat and away from an magnetic field generating
     * devices such as cell phones and speakers.
     *
-    * TODO: declenation angle compensation
+    * 
     * 
     * @returns heading in degrees
     */