Fork from Tyler Weaver. Slightly changed to put into fast I2C mode.

Fork of HMC5883L by Tyler Weaver

Files at this revision

API Documentation at this revision

Comitter:
pHysiX
Date:
Fri May 02 17:01:15 2014 +0000
Parent:
4:bc4e1201e092
Commit message:
Fixed and tidied code. Need to add in features from Teensy/Tilty library

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 5104b90e3f0f HMC5883L.cpp
--- a/HMC5883L.cpp	Tue Nov 06 17:35:51 2012 +0000
+++ b/HMC5883L.cpp	Fri May 02 17:01:15 2014 +0000
@@ -36,7 +36,7 @@
 {
     // Placement new to avoid additional heap memory allocation.
     new(i2cRaw) I2C(sda, scl);
-    
+    i2c_.frequency(400000);
     init();
 }
 
@@ -51,7 +51,7 @@
 {
     // init - configure your setup here
     setConfigurationA(AVG8_SAMPLES | OUTPUT_RATE_15); // 8 sample average, 15Hz, normal mode
-    setConfigurationB(0x20); // default 
+    setConfigurationB(0x20); // default // TILTY USES 0x00
     setMode(CONTINUOUS_MODE); // continuous sample mode
 }
 
@@ -129,6 +129,29 @@
         output[i] = int16_t(((unsigned char)data[i*2] << 8) | (unsigned char)data[i*2+1]);
 }
 
+/**
+*@param[in] x X magnetometer value.
+*@param[in] y Y magnetometer value.
+*@param[in] z Z magnetometer value.
+*@param[in] pitch  Pitch angle in degrees.
+*@param[in] roll Y Roll angle in degrees.
+*/
+float HMC5883L::getTiltCompensatedHeading(int x, int y, int z, float pitch, float roll)
+{
+    /*
+    float pitch_rad = pitch * (M_PI / 180);
+    float roll_rad = roll * (M_PI / 180);
+    
+    int xH = x * cos(pitch_rad) + z * sin(pitch_rad);
+    int yH = x * sin(roll_rad) * sin(pitch_rad) + y * cos(roll_rad) - z * sin(roll_rad) * cos(pitch_rad);
+    
+    float temp = atan2(yH, xH);//(atan2(yH, xH) + M_PI) * 180/M_PI;
+    
+    return temp;
+    */
+    return 0.0;
+}
+
 double HMC5883L::getHeadingXY()
 {
     int16_t raw_data[3];
@@ -144,4 +167,20 @@
         heading -= PI2;
         
     return heading;
+}
+
+/** \brief undocumented function
+
+    \param[in] param description of parameter
+    \param[out] param description of parameter  
+
+    \return return description of return value
+**/
+bool HMC5883L::getDataReady()
+{
+    char cmd[2];
+    cmd[0] = 0x09; // status register
+    i2c_.write(I2C_ADDRESS, cmd, 1, true);
+    i2c_.read(I2C_ADDRESS, &cmd[1], 1, false);
+    return cmd[1] == 0x17;
 }
\ No newline at end of file
diff -r bc4e1201e092 -r 5104b90e3f0f HMC5883L.h
--- a/HMC5883L.h	Tue Nov 06 17:35:51 2012 +0000
+++ b/HMC5883L.h	Fri May 02 17:01:15 2014 +0000
@@ -219,6 +219,8 @@
     */
     double getHeadingXY();
     
+    float getTiltCompensatedHeading(int x, int y, int z, float pitch, float roll);
+    
     /**
     * Function for getting degree heading using 2-dimensional calculation.
     * 
@@ -233,6 +235,8 @@
         return (getHeadingXY() * RAD_TO_DEG);
     }
 
+    bool getDataReady();
+    
 private:
 
     I2C &i2c_;