external mag

Dependents:   FreeIMU_external_magnetometer

Fork of HMC58X3 by Yifei Teng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC58X3.h Source File

HMC58X3.h

Go to the documentation of this file.
00001 /**
00002 @file HMC58X3.h - Interface a Honeywell HMC58X3 magnetometer to an Arduino via i2c.
00003 
00004 @author Fabio Varesano <fvaresano@yahoo.it>
00005 Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it>
00006 ported for Mbed by Aloïs Wolff
00007 
00008 Based on:
00009 http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346
00010   Modification/extension of the following by E.J.Muller
00011 http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino
00012   Copyright (c) 2009 Nirav Patel,
00013 
00014 The above were based on:
00015 http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c
00016 http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
00017 
00018 This program is free software: you can redistribute it and/or modify
00019 it under the terms of the version 3 GNU General Public License as
00020 published by the Free Software Foundation.
00021 
00022 This program is distributed in the hope that it will be useful,
00023 but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 GNU General Public License for more details.
00026 
00027 You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028 */
00029 
00030 //#define ISHMC5843 (1) // Uncomment this following line if you are using this library with the HMC5843.
00031 #include "Global.h"
00032 
00033 
00034 #ifndef HMC58X3_h
00035 #define HMC58X3_h
00036 
00037 #ifndef I2C_SDA
00038     #define I2C_SDA p28
00039     #define I2C_SCL p27
00040 #endif
00041 
00042 #define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library
00043 #define HMC_POS_BIAS 1
00044 #define HMC_NEG_BIAS 2
00045 
00046 // HMC58X3 register map. For details see HMC58X3 datasheet
00047 #define HMC58X3_R_CONFA 0
00048 #define HMC58X3_R_CONFB 1
00049 #define HMC58X3_R_MODE 2
00050 #define HMC58X3_R_XM 3
00051 #define HMC58X3_R_XL 4
00052 
00053 #ifdef ISHMC5843
00054 #define HMC58X3_R_YM (5)    //!< Register address for YM.
00055 #define HMC58X3_R_YL (6)    //!< Register address for YL.
00056 #define HMC58X3_R_ZM (7)    //!< Register address for ZM.
00057 #define HMC58X3_R_ZL (8)    //!< Register address for ZL.
00058 
00059 #define HMC58X3_X_SELF_TEST_GAUSS (+0.55)                       //!< X axis level when bias current is applied.
00060 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00061 #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00062 
00063 /*
00064     This is my best guess at the LOW, HIGH limit.  The data sheet does not have these values.
00065 */
00066 #define SELF_TEST_LOW_LIMIT  (HMC58X3_X_SELF_TEST_GAUSS*0.53)   //!< Low limit 53% of expected value.
00067 #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36)   //!< High limit 136% of expected values.
00068 #else // HMC5883L
00069 #define HMC58X3_R_YM (7)  //!< Register address for YM.
00070 #define HMC58X3_R_YL (8)  //!< Register address for YL.
00071 #define HMC58X3_R_ZM (5)  //!< Register address for ZM.
00072 #define HMC58X3_R_ZL (6)  //!< Register address for ZL.
00073 
00074 #define HMC58X3_X_SELF_TEST_GAUSS (+1.16)                       //!< X axis level when bias current is applied.
00075 #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.
00076 #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08)                       //!< Y axis level when bias current is applied.
00077 
00078 #define SELF_TEST_LOW_LIMIT  (243.0/390.0)   //!< Low limit when gain is 5.
00079 #define SELF_TEST_HIGH_LIMIT (575.0/390.0)   //!< High limit when gain is 5.
00080 #endif
00081 
00082 #define HMC58X3_R_STATUS 9
00083 //#define HMC58X3_R_IDA 10
00084 #define HMC58X3_R_IDB 11
00085 #define HMC58X3_R_IDC 12
00086 
00087 class HMC58X3
00088 {
00089 
00090 
00091 public:
00092     HMC58X3();
00093     void init(bool setmode);
00094     void init(int address, bool setmode);
00095     void getValues(int16_t *x,int16_t *y,int16_t *z);
00096     void getValues(float *x,float *y,float *z);
00097     void getValues(float *xyz);
00098     void getRaw(int16_t *x,int16_t *y,int16_t *z);
00099     void getRaw(int16_t *xyz);
00100     void calibrate(unsigned char gain);     // Original calibrate with a few weaknesses.
00101     bool calibrate(unsigned char gain,unsigned int n_samples);
00102     void setMode(unsigned char mode);
00103     void setDOR(unsigned char DOR);
00104     void setGain(unsigned char gain);
00105     void getID(char id[3]);
00106     
00107     int16_t cache_x, cache_y, cache_z;
00108     
00109     static void samplingthread_stub(void const *p);
00110     
00111     void samplingthread();
00112     void start_sampling();
00113 
00114     static const int I2C_ADDRESS = 0x3D;
00115     char HMC58X3_R_IDA;
00116     MODI2C i2c;
00117     
00118     Thread _thread;
00119     
00120     Semaphore sem;
00121 
00122 private:
00123     void writeReg(unsigned char reg, unsigned char val);
00124     float x_scale,y_scale,z_scale,x_max,y_max,z_max;
00125     int min(int a, int b);
00126 };
00127 
00128 #endif // HMC58X3_h