m

Fork of HMC5883L by Tyler Weaver

Files at this revision

API Documentation at this revision

Comitter:
tylerjw
Date:
Wed Oct 31 05:06:34 2012 +0000
Parent:
0:8b84d61cee94
Child:
2:8eb755577f83
Commit message:
0.2 XY heading

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
--- a/HMC5883L.cpp	Wed Oct 31 04:35:08 2012 +0000
+++ b/HMC5883L.cpp	Wed Oct 31 05:06:34 2012 +0000
@@ -127,4 +127,21 @@
     
     for(int i = 0; i < 3; i++) // fill the output variables
         output[i] = int16_t(((unsigned char)data[i*2] << 8) | (unsigned char)data[i*2+1]);
+}
+
+double HMC5883L::getHeadingXY()
+{
+    int raw_data[3];
+    getXYZ(raw_data);
+    double heading = atan2(raw_data[1], raw_data[0]); // heading = arctan(Y/X)
+    
+    // TODO: declenation angle compensation
+    
+    if(heading < 0.0) // fix sign
+        heading += 2PI;
+        
+    if(heading > 2PI) // fix overflow
+        heading -= 2PI;
+        
+    return heading;
 }
\ No newline at end of file
--- a/HMC5883L.h	Wed Oct 31 04:35:08 2012 +0000
+++ b/HMC5883L.h	Wed Oct 31 05:06:34 2012 +0000
@@ -74,6 +74,15 @@
 #define STATUS_LOCK         0x02
 #define STATUS_READY        0x01
 
+// Utility
+#ifndef M_PI
+#define M_PI 3.1415926535897932384626433832795
+#endif
+
+#define 2PI         (2*M_PI)
+#define RAD_TO_DEG  (180.0/M_PI)
+#define DEG_TO_RAD  (M_PI/180.0)
+
 /**
  * The HMC5883L 3-Axis Digital Compass IC
  */
@@ -84,16 +93,13 @@
 
     /**
      * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left).
-     *
-     * You don't need to manually set or clear the LSB when calling I2C::read() or I2C::write(),
-     * the library takes care of it.  We just always clear the LSB.
      */
     static const int I2C_ADDRESS = 0x3D;
 
     /**
      * Constructor.
      *
-     * Sets FS_SEL to 0x03 for proper opertaion.
+     * Calls init function
      *
      * @param sda - mbed pin to use for the SDA I2C line.
      * @param scl - mbed pin to use for the SCL I2C line.
@@ -102,6 +108,8 @@
 
     /**
     * Constructor that accepts external i2c interface object.
+    * 
+    * Calls init function
     *
     * @param i2c The I2C interface object to use.
     */
@@ -159,14 +167,7 @@
     /**
     * Funciton for setting the mode register
     * 
-    * |= 7 |= 6 |= 5 |= 4 |= 3 |= 2 |= 1 |= 0 |
-    * | (0)| (0)| (0)| (0)| (0)| (0)|MD1 | MD0|
-    *
-    * |=MD1 |=MD0 |=Operating Mode |=Constant |
-    * | 0 | 0 | Continuous-Measurement Mode | CONTINUOUS_MODE |
-    * | 0 | 1 | Single-Measument Mode | SINGLE_MODE |
-    * | 1 | 0 | Idle Mode | IDLE_MODE |
-    * | 1 | 1 | Idle Mode | IDLE_MODE |
+    * Constants: CONTINUOUS_MODE, SINGLE_MODE, IDLE_MODE
     * 
     * When you send a the Single-Measurement Mode instruction to the mode register
     * a single measurement is made, the RDY bit is set in the status register,
@@ -200,12 +201,37 @@
     /**
     * Function for retrieving the contents of status register
     * 
-    * |= 7 |= 6 |= 5 |= 4 |= 3 |= 2 |= 1 |= 0 |
-    * | (0)| (0)| (0)| (0)| (0)| (0)|LOCK | RDY|
+    * Bit1: LOCK, Bit0: RDY
     *
     * @returns status register
     */
     char getStatus();
+    
+    /**
+    * Function for getting radian heading using 2-dimensional calculation.
+    * 
+    * 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
+    */
+    double getHeadingXY();
+    
+    /**
+    * Function for getting degree heading using 2-dimensional calculation.
+    * 
+    * 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
+    */
+    double getHeadingXYDeg() {
+        return (getHeadingXY() * RAD_TO_DEG);
+    }
 
 private: