SparkFun_9DOF_SensorStick

Dependencies:   FatFileSystem mbed

Fork of 9DOF_SensorStick by hige dura

Files at this revision

API Documentation at this revision

Comitter:
higedura
Date:
Wed Jan 25 05:25:29 2012 +0000
Parent:
1:29713f02de29
Child:
3:5b192b38b3bb
Commit message:

Changed in this revision

HMC5843.cpp Show diff for this revision Revisions of this file
HMC5843.h Show diff for this revision Revisions of this file
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HMC5843.cpp	Sat Nov 26 15:01:52 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,174 +0,0 @@
-/**
- * @author Jose R. Padron
- * @author Used HMC6352 library  developed by Aaron Berk as template
- * @section LICENSE
- *
- * Copyright (c) 2010 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * Honeywell HMC5843digital compass.
- *
- * Datasheet:
- *
- * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
- */
-
-/**
- * Includes
- */
-#include "HMC5843.h"
-
-HMC5843::HMC5843(PinName sda, PinName scl) {
-
-    i2c_ = new I2C(sda, scl);
-    //100KHz, as specified by the datasheet.
-    i2c_->frequency(100000);
-
-
-}
-
-
-void HMC5843::write(int address, int data) {
-   
-    char tx[2];
-   
-    tx[0]=address;
-    tx[1]=data;
-
-    i2c_->write(HMC5843_I2C_WRITE,tx,2);
-   
-    wait_ms(100);
-
-}
-
-
-void HMC5843::setSleepMode() {
-    
-    write(HMC5843_MODE, HMC5843_SLEEP);
-}
-
-void HMC5843::setDefault(void) {
-   
-   write(HMC5843_CONFIG_A,HMC5843_10HZ_NORMAL);
-   write(HMC5843_CONFIG_B,HMC5843_1_0GA);
-   write(HMC5843_MODE,HMC5843_CONTINUOUS);
-   wait_ms(100);
-}
-
-
-void HMC5843::getAddress(char *buffer) {
-    
-   char rx[3];
-   char tx[1];
-   tx[0]=HMC5843_IDENT_A;
-    
-       
-    i2c_->write(HMC5843_I2C_WRITE, tx,1);
-   
-    wait_ms(1);
-    
-    i2c_->read(HMC5843_I2C_READ,rx,3);
-    
-    buffer[0]=rx[0];
-    buffer[1]=rx[1];
-    buffer[2]=rx[2];
-}
-
-
-
-void HMC5843::setOpMode(int mode, int ConfigA, int ConfigB) {
-    
-    
-    write(HMC5843_CONFIG_A,ConfigA);
-    write(HMC5843_CONFIG_B,ConfigB);
-    write(HMC5843_MODE,mode);
-    
-
-}
-
-
-
-
-void HMC5843::readData(int* getMag) {
-
-  
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5843_X_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    getMag[0]= (int)rx[0]<<8|(int)rx[1];
-
-     
-    tx[0]=HMC5843_Y_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    getMag[1]= (int)rx[0]<<8|(int)rx[1];
-     
-    tx[0]=HMC5843_Z_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    getMag[2]= (int)rx[0]<<8|(int)rx[1];
-    
-}
-
-int HMC5843::getMx() {
-
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5843_X_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
-
-}
-
-int HMC5843::getMy() {
-
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5843_Y_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
- 
-}
-
-
-int HMC5843::getMz(){
-
-    char tx[1];
-    char rx[2];
-    
-    
-    tx[0]=HMC5843_Z_MSB;
-    i2c_->write(HMC5843_I2C_READ,tx,1);
-    i2c_->read(HMC5843_I2C_READ,rx,2);
-    return ((int)rx[0]<<8|(int)rx[1]);
- 
-}
\ No newline at end of file
--- a/HMC5843.h	Sat Nov 26 15:01:52 2011 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,219 +0,0 @@
-/**
- * @author Uwe Gartmann
- * @author Used HMC5843 library developed by Jose R. Padron and Aaron Berk as template
- *
- * @section LICENSE
- *
- * Copyright (c) 2010 ARM Limited
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @section DESCRIPTION
- *
- * Honeywell HMC5843 digital compass.
- *
- * Datasheet:
- *
- * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5843.pdf
- */
-
-#ifndef HMC5843_H
-#define HMC5843_H
-
-/**
- * Includes
- */
-#include "mbed.h"
-
-/**
- * Defines
- */
-#define HMC5843_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
-#define HMC5843_I2C_WRITE   0x3C 
-#define HMC5843_I2C_READ    0x3D 
-
-//Values Config A
-#define HMC5843_0_5HZ_NORMAL         0x00
-#define HMC5843_0_5HZ_POSITIVE       0x01
-#define HMC5843_0_5HZ_NEGATIVE       0x02
-
-#define HMC5843_1HZ_NORMAL           0x04
-#define HMC5843_1HZ_POSITIVE         0x05
-#define HMC5843_1HZ_NEGATIVE         0x06
-
-#define HMC5843_2HZ_NORMAL           0x08
-#define HMC5843_2HZ_POSITIVE         0x09
-#define HMC5843_2HZ_NEGATIVE         0x0A
-
-#define HMC5843_5HZ_NORMAL           0x0C
-#define HMC5843_5HZ_POSITIVE         0x0D
-#define HMC5843_5HZ_NEGATIVE         0x0E
-
-#define HMC5843_10HZ_NORMAL           0x10
-#define HMC5843_10HZ_POSITIVE         0x11
-#define HMC5843_10HZ_NEGATIVE         0x12
-
-#define HMC5843_20HZ_NORMAL           0x14
-#define HMC5843_20HZ_POSITIVE         0x15
-#define HMC5843_20HZ_NEGATIVE         0x16
-
-#define HMC5843_50HZ_NORMAL           0x18
-#define HMC5843_50HZ_POSITIVE         0x19
-#define HMC5843_50HZ_NEGATIVE         0x1A
-
-//Values Config B
-#define HMC5843_0_7GA         0x00
-#define HMC5843_1_0GA         0x20
-#define HMC5843_1_5GA         0x40
-#define HMC5843_2_0GA         0x60
-#define HMC5843_3_2GA         0x80
-#define HMC5843_3_8GA         0xA0
-#define HMC5843_4_5GA         0xC0
-#define HMC5843_6_5GA         0xE0
-
-//Values MODE
-#define HMC5843_CONTINUOUS   0x00
-#define HMC5843_SINGLE         0x01
-#define HMC5843_IDLE         0x02
-#define HMC5843_SLEEP         0x03
-
-
-
-#define HMC5843_CONFIG_A     0x00
-#define HMC5843_CONFIG_B     0x01
-#define HMC5843_MODE         0x02
-#define HMC5843_X_MSB        0x03
-#define HMC5843_X_LSB        0x04
-#define HMC5843_Y_MSB        0x05
-#define HMC5843_Y_LSB        0x06
-#define HMC5843_Z_MSB        0x07
-#define HMC5843_Z_LSB        0x08
-#define HMC5843_STATUS       0x09
-#define HMC5843_IDENT_A      0x0A
-#define HMC5843_IDENT_B      0x0B
-#define HMC5843_IDENT_C      0x0C
-
-
-
-/**
- * Honeywell HMC5843 digital compass.
- */
-class HMC5843 {
-
-public:
-
-    /**
-     * Constructor.
-     *
-     * @param sda mbed pin to use for SDA line of I2C interface.
-     * @param scl mbed pin to use for SCL line of I2C interface.
-     */
-    HMC5843(PinName sda, PinName scl);
-
-        
-     /**
-     * Enter into sleep mode.
-     *
-     */
-    void setSleepMode();
-    
-       
-     /**
-     * Set Device in Default Mode.
-     * HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
-     */
-    void setDefault();
-    
-       
-    /**
-     * Read the memory location on the device which contains the address.
-     *
-     * @param Pointer to a buffer to hold the address value
-     * Expected     H, 4 and 3.
-     */
-    void getAddress(char * address);
-
-
-    
-    /**
-     * Set the operation mode.
-     *
-     * @param mode 0x00 -> Continuous
-     *             0x01 -> Single
-     *             0x02 -> Idle
-     * @param ConfigA values
-    * @param ConfigB values
-     */
-    void setOpMode(int mode, int ConfigA, int ConfigB);
-    
-     /**
-     * Write to  on the device.
-     *
-     * @param address Address to write to.
-     * @param data Data to write.
-     */
-    
-    void write(int address, int data);
-
-     /**
-     * Get the output of all three axes.
-     *
-     * @param Pointer to a buffer to hold the magnetics value for the
-     *        x-axis, y-axis and z-axis [in that order].
-     */
-    void readData(int* getMag);
-    
-    /**
-     * Get the output of X axis.
-     *
-     * @return x-axis magnetic value
-     */
-    int getMx();
-    
-    /**
-     * Get the output of Y axis.
-     *
-     * @return y-axis magnetic value
-     */
-    int getMy();
-    
-    /**
-     * Get the output of Z axis.
-     *
-     * @return z-axis magnetic value
-     */
-    int getMz();
-   
-    
-    /**
-     * Get the current operation mode.
-     *
-     * @return Status register values
-     */
-    int getStatus(void);
-
-  
-
-    I2C* i2c_;
-
-   
-
-};
-
-#endif /* HMC5843_H */
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HMC5883L.cpp	Wed Jan 25 05:25:29 2012 +0000
@@ -0,0 +1,174 @@
+/**
+ * @author Jose R. Padron
+ * @author Used HMC6352 library  developed by Aaron Berk as template
+ * @section LICENSE
+ *
+ * Copyright (c) 2010 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Honeywell HMC5883Ldigital compass.
+ *
+ * Datasheet:
+ *
+ * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf
+ */
+
+/**
+ * Includes
+ */
+#include "HMC5883L.h"
+
+HMC5883L::HMC5883L(PinName sda, PinName scl) {
+
+    i2c_ = new I2C(sda, scl);
+    //100KHz, as specified by the datasheet.
+    i2c_->frequency(100000);
+
+
+}
+
+
+void HMC5883L::write(int address, int data) {
+   
+    char tx[2];
+   
+    tx[0]=address;
+    tx[1]=data;
+
+    i2c_->write(HMC5883L_I2C_WRITE,tx,2);
+   
+    wait_ms(100);
+
+}
+
+
+void HMC5883L::setSleepMode() {
+    
+    write(HMC5883L_MODE, HMC5883L_SLEEP);
+}
+
+void HMC5883L::setDefault(void) {
+   
+   write(HMC5883L_CONFIG_A,HMC5883L_10HZ_NORMAL);
+   write(HMC5883L_CONFIG_B,HMC5883L_1_0GA);
+   write(HMC5883L_MODE,HMC5883L_CONTINUOUS);
+   wait_ms(100);
+}
+
+
+void HMC5883L::getAddress(char *buffer) {
+    
+   char rx[3];
+   char tx[1];
+   tx[0]=HMC5883L_IDENT_A;
+    
+       
+    i2c_->write(HMC5883L_I2C_WRITE, tx,1);
+   
+    wait_ms(1);
+    
+    i2c_->read(HMC5883L_I2C_READ,rx,3);
+    
+    buffer[0]=rx[0];
+    buffer[1]=rx[1];
+    buffer[2]=rx[2];
+}
+
+
+
+void HMC5883L::setOpMode(int mode, int ConfigA, int ConfigB) {
+    
+    
+    write(HMC5883L_CONFIG_A,ConfigA);
+    write(HMC5883L_CONFIG_B,ConfigB);
+    write(HMC5883L_MODE,mode);
+    
+
+}
+
+
+
+
+void HMC5883L::readData(int* getMag) {
+
+  
+    char tx[1];
+    char rx[2];
+    
+    
+    tx[0]=HMC5883L_X_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    getMag[0]= (int)rx[0]<<8|(int)rx[1];
+
+     
+    tx[0]=HMC5883L_Y_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    getMag[1]= (int)rx[0]<<8|(int)rx[1];
+     
+    tx[0]=HMC5883L_Z_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    getMag[2]= (int)rx[0]<<8|(int)rx[1];
+    
+}
+
+int HMC5883L::getMx() {
+
+    char tx[1];
+    char rx[2];
+    
+    
+    tx[0]=HMC5883L_X_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    return ((int)rx[0]<<8|(int)rx[1]);
+
+}
+
+int HMC5883L::getMy() {
+
+    char tx[1];
+    char rx[2];
+    
+    
+    tx[0]=HMC5883L_Y_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    return ((int)rx[0]<<8|(int)rx[1]);
+ 
+}
+
+
+int HMC5883L::getMz(){
+
+    char tx[1];
+    char rx[2];
+    
+    
+    tx[0]=HMC5883L_Z_MSB;
+    i2c_->write(HMC5883L_I2C_READ,tx,1);
+    i2c_->read(HMC5883L_I2C_READ,rx,2);
+    return ((int)rx[0]<<8|(int)rx[1]);
+ 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HMC5883L.h	Wed Jan 25 05:25:29 2012 +0000
@@ -0,0 +1,219 @@
+/**
+ * @author Uwe Gartmann
+ * @author Used HMC5883L library developed by Jose R. Padron and Aaron Berk as template
+ *
+ * @section LICENSE
+ *
+ * Copyright (c) 2010 ARM Limited
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Honeywell HMC5883L digital compass.
+ *
+ * Datasheet:
+ *
+ * http://www.ssec.honeywell.com/magnetic/datasheets/HMC5883L.pdf
+ */
+
+#ifndef HMC5883L_H
+#define HMC5883L_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Defines
+ */
+#define HMC5883L_I2C_ADDRESS 0x1E //7-bit address. 0x3C write, 0x3D read.
+#define HMC5883L_I2C_WRITE   0x3C 
+#define HMC5883L_I2C_READ    0x3D 
+
+//Values Config A
+#define HMC5883L_0_5HZ_NORMAL         0x00
+#define HMC5883L_0_5HZ_POSITIVE       0x01
+#define HMC5883L_0_5HZ_NEGATIVE       0x02
+
+#define HMC5883L_1HZ_NORMAL           0x04
+#define HMC5883L_1HZ_POSITIVE         0x05
+#define HMC5883L_1HZ_NEGATIVE         0x06
+
+#define HMC5883L_2HZ_NORMAL           0x08
+#define HMC5883L_2HZ_POSITIVE         0x09
+#define HMC5883L_2HZ_NEGATIVE         0x0A
+
+#define HMC5883L_5HZ_NORMAL           0x0C
+#define HMC5883L_5HZ_POSITIVE         0x0D
+#define HMC5883L_5HZ_NEGATIVE         0x0E
+
+#define HMC5883L_10HZ_NORMAL           0x10
+#define HMC5883L_10HZ_POSITIVE         0x11
+#define HMC5883L_10HZ_NEGATIVE         0x12
+
+#define HMC5883L_20HZ_NORMAL           0x14
+#define HMC5883L_20HZ_POSITIVE         0x15
+#define HMC5883L_20HZ_NEGATIVE         0x16
+
+#define HMC5883L_50HZ_NORMAL           0x18
+#define HMC5883L_50HZ_POSITIVE         0x19
+#define HMC5883L_50HZ_NEGATIVE         0x1A
+
+//Values Config B
+#define HMC5883L_0_7GA         0x00
+#define HMC5883L_1_0GA         0x20
+#define HMC5883L_1_5GA         0x40
+#define HMC5883L_2_0GA         0x60
+#define HMC5883L_3_2GA         0x80
+#define HMC5883L_3_8GA         0xA0
+#define HMC5883L_4_5GA         0xC0
+#define HMC5883L_6_5GA         0xE0
+
+//Values MODE
+#define HMC5883L_CONTINUOUS   0x00
+#define HMC5883L_SINGLE         0x01
+#define HMC5883L_IDLE         0x02
+#define HMC5883L_SLEEP         0x03
+
+
+
+#define HMC5883L_CONFIG_A     0x00
+#define HMC5883L_CONFIG_B     0x01
+#define HMC5883L_MODE         0x02
+#define HMC5883L_X_MSB        0x03
+#define HMC5883L_X_LSB        0x04
+#define HMC5883L_Z_MSB        0x05
+#define HMC5883L_Z_LSB        0x06
+#define HMC5883L_Y_MSB        0x07
+#define HMC5883L_Y_LSB        0x08
+#define HMC5883L_STATUS       0x09
+#define HMC5883L_IDENT_A      0x0A
+#define HMC5883L_IDENT_B      0x0B
+#define HMC5883L_IDENT_C      0x0C
+
+
+
+/**
+ * Honeywell HMC5883L digital compass.
+ */
+class HMC5883L {
+
+public:
+
+    /**
+     * Constructor.
+     *
+     * @param sda mbed pin to use for SDA line of I2C interface.
+     * @param scl mbed pin to use for SCL line of I2C interface.
+     */
+    HMC5883L(PinName sda, PinName scl);
+
+        
+     /**
+     * Enter into sleep mode.
+     *
+     */
+    void setSleepMode();
+    
+       
+     /**
+     * Set Device in Default Mode.
+     * HMC5883L_CONTINUOUS, HMC5883L_10HZ_NORMAL HMC5883L_1_0GA
+     */
+    void setDefault();
+    
+       
+    /**
+     * Read the memory location on the device which contains the address.
+     *
+     * @param Pointer to a buffer to hold the address value
+     * Expected     H, 4 and 3.
+     */
+    void getAddress(char * address);
+
+
+    
+    /**
+     * Set the operation mode.
+     *
+     * @param mode 0x00 -> Continuous
+     *             0x01 -> Single
+     *             0x02 -> Idle
+     * @param ConfigA values
+    * @param ConfigB values
+     */
+    void setOpMode(int mode, int ConfigA, int ConfigB);
+    
+     /**
+     * Write to  on the device.
+     *
+     * @param address Address to write to.
+     * @param data Data to write.
+     */
+    
+    void write(int address, int data);
+
+     /**
+     * Get the output of all three axes.
+     *
+     * @param Pointer to a buffer to hold the magnetics value for the
+     *        x-axis, y-axis and z-axis [in that order].
+     */
+    void readData(int* getMag);
+    
+    /**
+     * Get the output of X axis.
+     *
+     * @return x-axis magnetic value
+     */
+    int getMx();
+    
+    /**
+     * Get the output of Y axis.
+     *
+     * @return y-axis magnetic value
+     */
+    int getMy();
+    
+    /**
+     * Get the output of Z axis.
+     *
+     * @return z-axis magnetic value
+     */
+    int getMz();
+   
+    
+    /**
+     * Get the current operation mode.
+     *
+     * @return Status register values
+     */
+    int getStatus(void);
+
+  
+
+    I2C* i2c_;
+
+   
+
+};
+
+#endif /* HMC5883L_H */
\ No newline at end of file
--- a/main.cpp	Sat Nov 26 15:01:52 2011 +0000
+++ b/main.cpp	Wed Jan 25 05:25:29 2012 +0000
@@ -1,106 +1,99 @@
 #include "ADXL345_I2C.h"
 #include "ITG3200.h"
-#include "HMC5843.h"
+#include "HMC5883L.h"
 
 ADXL345_I2C accelerometer(p9, p10);
 ITG3200 gyro(p9, p10);
-HMC5843 compass(p9, p10);
+HMC5883L compass(p9, p10);
 Serial pc(USBTX, USBRX);
 
+#define N 3
+
 int main(){ 
+    
+    int i   =  0;
     float dt = 0.1;
-    float t  = 0.0;    
-    pc.baud(115200);
-    int    bitAcc[3]    =   {0, 0, 0};
-    int    getMag[3];
-    double Acc   [3]    =   {0, 0, 0};
-    double Gyro0 [3]    =   {0, 0, 0};
-    double Gyro1 [3]    =   {0, 0, 0};
-    double Gyro2 [3]    =   {0, 0, 0};
-    double Ang0  [3]    =   {0, 0, 0};
-    double Ang1  [3]    =   {0, 0, 0};
-    double Ang2  [3]    =   {0, 0, 0};
-    int    Mag1  [3]    =   {0, 0, 0};
+    float t  = 0;    
+    pc.baud(9600);
+    int    bitAcc[N]    =   {0};    // Buffer of the accelerometer
+    int    getMag[N]    =   {0};    // Buffer of the compass
+    double Acc  [N]    =   {0};
+    double Gyro [N]    =   {0};
+    int    Mag  [N]    =   {0};
     
-    // *** Part of the accelerometer ***
-    // These are here to test whether any of the initialization fails. It will print the failure
+    // *** Setting up accelerometer ***
+    // These are here to test whether any of the initialization fails. It will print the failure.
     if (accelerometer.setPowerControl(0x00)){
-        pc.printf("didn't intitialize power control\n"); 
-        return 0;  }
+        pc.printf("didn't intitialize power control\n\r"); 
+        return 0;
+    }
     // Full resolution, +/-16g, 4mg/LSB.
     wait(.001);
      
     if(accelerometer.setDataFormatControl(0x0B)){
-        pc.printf("didn't set data format\n");
+        pc.printf("didn't set data format\n\r");
         return 0;  }
     wait(.001);
      
     // 3.2kHz data rate.
     if(accelerometer.setDataRate(ADXL345_3200HZ)){
-        pc.printf("didn't set data rate\n");
-        return 0;    }
+        pc.printf("didn't set data rate\n\r");
+        return 0;
+    }
     wait(.001);
       
     if(accelerometer.setPowerControl(MeasurementMode)) {
-        pc.printf("didn't set the power control to measurement\n"); 
-        return 0;   } 
-    // *** Part of the accelerometer ***
+        pc.printf("didn't set the power control to measurement\n\r"); 
+        return 0;
+    } 
+    // ***  Setting up accelerometer ***
     
+    // ***  Setting up gyro ***
     gyro.setLpBandwidth(LPFBW_42HZ);
-    
-    //Continuous mode, , 10Hz measurement rate.
-    // HMC5843_CONTINUOUS, HMC5843_10HZ_NORMAL HMC5843_1_0GA
+
+    // ***  Setting up compass ***
     compass.setDefault();
     
-    //Wait some time(Need at least 5ms)
+    // Wait some time for all sensors (Need at least 5ms)
     wait(0.1);
     
-    pc.printf("Starting ADXL345, ITG3200 and HMC5843 test...\n");
-    pc.printf("  Time     AccX     AccY     AccZ    GyroX    GyroY    GyroZ   MagX   MagY   MagZ\n");
-    //pc.printf("  Time    GyroX    GyroY    GyroZ   AngleX   AngleY   AngleZ\n");
+    pc.printf("\n\rStarting ADXL345, ITG3200 and HMC5883L test...\n\r");
+    pc.printf("   Time     AccX     AccY     AccZ    GyroX    GyroY    GyroZ   MagX   MagY   MagZ\n\r");
                 
     while(1){
+        // Updating accelerometer and compass
         accelerometer.getOutput(bitAcc);
         compass.readData(getMag);
-        // Transfering units (Acc[g], Gyro[deg/s], Compass[?])
-        // Calibration YAL 9DOF Acc:X+2, Y-12, Z+44
-        Acc[0]   =  ((int16_t)bitAcc[0]+2)*0.004;
-        Acc[1]   =  ((int16_t)bitAcc[1]-12)*0.004;
-        Acc[2]   =  ((int16_t)bitAcc[2]+44)*0.004;
-        // Calibration YAL 9DOF Gyro:X+26, Y-45, Z+34 
-        Gyro2[0] =  (gyro.getGyroX()+26)/14.375;
-        Gyro2[1] =  (gyro.getGyroY()-45)/14.375;
-        Gyro2[2] =  (gyro.getGyroZ()+34)/14.375;
+        
+        // Transfering units (Acc[g], Gyro[deg/s], Compass[Ga])
+        // Calibration YAL 9DOF Acc:X+6, Y-12, Z+44
+        // Calibration green Acc:X+1, Y-18, Z+45
+        Acc[0]  =  ((int16_t)bitAcc[0])*0.004;
+        Acc[1]  =  ((int16_t)bitAcc[1])*0.004;
+        Acc[2]  =  ((int16_t)bitAcc[2])*0.004;
+        // Calibration YAL 9DOF Gyro:X+28, Y-45, Z+34 
+        // Calibration green Gyro:X+135, Y-12, Z-53 
+        Gyro[0] =  (gyro.getGyroX())/14.375;
+        Gyro[1] =  (gyro.getGyroY())/14.375;
+        Gyro[2] =  (gyro.getGyroZ())/14.375;
         // Calibration YAL 9DOF Compass:X, Y, Z
-        Mag1[0]  =  (int16_t)getMag[0];
-        Mag1[1]  =  (int16_t)getMag[1];
-        Mag1[2]  =  (int16_t)getMag[2];
+        Mag[0]  =  (int16_t)getMag[0];
+        Mag[1]  =  (int16_t)getMag[1];
+        Mag[2]  =  (int16_t)getMag[2];
+        
+        // Low pass filter for acc
+        //for ( int i=0;i<N;i++ ){
+        //    if( -0.05<Acc[i] && Acc[i]<0.05 ){    Acc[i]=0;  }
+        //}
         
         // Low pass filter for gyro
-        //if( -1.0<Gyro2[0] && Gyro2[0]<1.0 ){    Gyro2[0]=0;  }
-        //if( -1.0<Gyro2[1] && Gyro2[1]<1.0 ){    Gyro2[1]=0;  }
-        //if( -1.0<Gyro2[2] && Gyro2[2]<1.0 ){    Gyro2[2]=0;  }
-        
-        // Trapezoidal integration
-        //Ang1[0] = Ang0[0]+(Gyro0[0]+Gyro1[0])*dt/2;
-        //Ang1[1] = Ang0[1]+(Gyro0[1]+Gyro1[1])*dt/2;
-        //Ang1[2] = Ang0[2]+(Gyro0[2]+Gyro1[2])*dt/2;
-        
-        // Simpson integration
-        // Ang1[0] = Ang0[0]+(Gyro0[0]+4*Gyro1[0]+Gyro2[0])*dt/6;
-        // Ang1[1] = Ang0[1]+(Gyro0[1]+4*Gyro1[1]+Gyro2[1])*dt/6;
-        // Ang1[2] = Ang0[2]+(Gyro0[2]+4*Gyro1[2]+Gyro2[2])*dt/6;
-        
-        pc.printf("%6.1f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f, %5d, %5d, %5d\n\r", t, Acc[0], Acc[1], Acc[2], Gyro1[0], Gyro1[1], Gyro1[2], Mag1[0], Mag1[1], Mag1[2]);
-        //pc.printf("%6.1f, %7.1f, %7.1f, %7.1f, %7.1f, %7.1f, %7.1f\n\r", t, Gyro1[0], Gyro1[1], Gyro1[2], Ang1[0], Ang1[1], Ang1[2]);
-        //pc.printf("%6.1f, %6i, %6i, %6i, %7.2f, %7.2f, %7.2f\n\r", t, gyro.getGyroX()+32, gyro.getGyroY()-27, gyro.getGyroZ()-17, Ang1[0], Ang1[1], Ang1[2]);
-        //pc.printf("%6.1f, %6i, %6i, %6i, %7.3f, %7.3f, %7.3f\n\r", t, (int16_t)bitAcc[0], (int16_t)bitAcc[1], (int16_t)bitAcc[2], Acc[0], Acc[1], Acc[2]);
-        Gyro1[0] = Gyro2[0];  Gyro1[1] = Gyro2[1];  Gyro1[2] = Gyro2[2];
-        Gyro0[0] = Gyro1[0];  Gyro0[1] = Gyro1[1];  Gyro0[2] = Gyro1[2];
-        Ang0[0]  = Ang1[0];   Ang0[1]  = Ang1[1];   Ang0[2]  = Ang1[2];
+        //for ( int i=0;i<N;i++ ){
+        //    if( -1.0<Gyro[i] && Gyro[i]<1.0 ){    Gyro[i]=0;  }
+        //}
+
+        pc.printf("%7.2f, %7.3f, %7.3f, %7.3f, %7.1f, %7.1f, %7.1f, %5d, %5d, %5d\n\r", t, Acc[0], Acc[1], Acc[2], Gyro[0], Gyro[1], Gyro[2], Mag[0], Mag[1], Mag[2]);
         
         t += dt;
         wait(dt);
     }
- 
-}
+ }