external mag
Dependents: FreeIMU_external_magnetometer
Fork of HMC58X3 by
HMC58X3.cpp@1:72ecf7399250, 2013-06-24 (annotated)
- Committer:
- pommzorz
- Date:
- Mon Jun 24 19:44:55 2013 +0000
- Revision:
- 1:72ecf7399250
- Parent:
- 0:d84e1c7e4e86
- Child:
- 2:c5ac16c88514
Minor adjustments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pommzorz | 0:d84e1c7e4e86 | 1 | /* |
pommzorz | 0:d84e1c7e4e86 | 2 | HMC58X3.cpp - Interface a Honeywell HMC58X3 or HMC5883L magnetometer to an mbed via i2c |
pommzorz | 0:d84e1c7e4e86 | 3 | Copyright (C) 2011 Fabio Varesano <fvaresano@yahoo.it> |
pommzorz | 0:d84e1c7e4e86 | 4 | ported for mbed by Aloïs Wolff (wolffalois@gmail.com) |
pommzorz | 0:d84e1c7e4e86 | 5 | |
pommzorz | 0:d84e1c7e4e86 | 6 | Based on: |
pommzorz | 0:d84e1c7e4e86 | 7 | http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274748346 |
pommzorz | 0:d84e1c7e4e86 | 8 | Modification/extension of the following by E.J.Muller |
pommzorz | 0:d84e1c7e4e86 | 9 | http://eclecti.cc/hardware/hmc5843-magnetometer-library-for-arduino |
pommzorz | 0:d84e1c7e4e86 | 10 | Copyright (c) 2009 Nirav Patel, |
pommzorz | 0:d84e1c7e4e86 | 11 | |
pommzorz | 0:d84e1c7e4e86 | 12 | The above were based on: |
pommzorz | 0:d84e1c7e4e86 | 13 | http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC58X3-v11.c |
pommzorz | 0:d84e1c7e4e86 | 14 | http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf |
pommzorz | 0:d84e1c7e4e86 | 15 | |
pommzorz | 0:d84e1c7e4e86 | 16 | |
pommzorz | 0:d84e1c7e4e86 | 17 | This program is free software: you can redistribute it and/or modify |
pommzorz | 0:d84e1c7e4e86 | 18 | it under the terms of the version 3 GNU General Public License as |
pommzorz | 0:d84e1c7e4e86 | 19 | published by the Free Software Foundation. |
pommzorz | 0:d84e1c7e4e86 | 20 | |
pommzorz | 0:d84e1c7e4e86 | 21 | This program is distributed in the hope that it will be useful, |
pommzorz | 0:d84e1c7e4e86 | 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
pommzorz | 0:d84e1c7e4e86 | 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
pommzorz | 0:d84e1c7e4e86 | 24 | GNU General Public License for more details. |
pommzorz | 0:d84e1c7e4e86 | 25 | |
pommzorz | 0:d84e1c7e4e86 | 26 | You should have received a copy of the GNU General Public License |
pommzorz | 0:d84e1c7e4e86 | 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
pommzorz | 0:d84e1c7e4e86 | 28 | |
pommzorz | 0:d84e1c7e4e86 | 29 | */ |
pommzorz | 0:d84e1c7e4e86 | 30 | |
pommzorz | 0:d84e1c7e4e86 | 31 | //#define DEBUG (1) |
pommzorz | 0:d84e1c7e4e86 | 32 | |
pommzorz | 0:d84e1c7e4e86 | 33 | #include "mbed.h" |
pommzorz | 0:d84e1c7e4e86 | 34 | #include "HMC58X3.h" |
pommzorz | 0:d84e1c7e4e86 | 35 | #include <new> |
pommzorz | 0:d84e1c7e4e86 | 36 | //#include <DebugUtils.h> |
pommzorz | 0:d84e1c7e4e86 | 37 | #define DEBUG_PRINT |
pommzorz | 0:d84e1c7e4e86 | 38 | |
pommzorz | 0:d84e1c7e4e86 | 39 | |
pommzorz | 0:d84e1c7e4e86 | 40 | |
pommzorz | 0:d84e1c7e4e86 | 41 | |
pommzorz | 0:d84e1c7e4e86 | 42 | /*! |
pommzorz | 0:d84e1c7e4e86 | 43 | Counts/milli-gauss per gain for the self test bias current. |
pommzorz | 0:d84e1c7e4e86 | 44 | */ |
pommzorz | 0:d84e1c7e4e86 | 45 | #if defined(ISHMC5843) |
pommzorz | 0:d84e1c7e4e86 | 46 | const int counts_per_milligauss[8]= { |
pommzorz | 0:d84e1c7e4e86 | 47 | 1620, |
pommzorz | 0:d84e1c7e4e86 | 48 | 1300, |
pommzorz | 0:d84e1c7e4e86 | 49 | 970, |
pommzorz | 0:d84e1c7e4e86 | 50 | 780, |
pommzorz | 0:d84e1c7e4e86 | 51 | 530, |
pommzorz | 0:d84e1c7e4e86 | 52 | 460, |
pommzorz | 0:d84e1c7e4e86 | 53 | 390, |
pommzorz | 0:d84e1c7e4e86 | 54 | 280 |
pommzorz | 0:d84e1c7e4e86 | 55 | }; |
pommzorz | 0:d84e1c7e4e86 | 56 | #else // HMC5883L |
pommzorz | 0:d84e1c7e4e86 | 57 | const int counts_per_milligauss[8]= { |
pommzorz | 0:d84e1c7e4e86 | 58 | 1370, |
pommzorz | 0:d84e1c7e4e86 | 59 | 1090, |
pommzorz | 0:d84e1c7e4e86 | 60 | 820, |
pommzorz | 0:d84e1c7e4e86 | 61 | 660, |
pommzorz | 0:d84e1c7e4e86 | 62 | 440, |
pommzorz | 0:d84e1c7e4e86 | 63 | 390, |
pommzorz | 0:d84e1c7e4e86 | 64 | 330, |
pommzorz | 0:d84e1c7e4e86 | 65 | 230 |
pommzorz | 0:d84e1c7e4e86 | 66 | }; |
pommzorz | 0:d84e1c7e4e86 | 67 | #endif |
pommzorz | 0:d84e1c7e4e86 | 68 | |
pommzorz | 0:d84e1c7e4e86 | 69 | |
pommzorz | 0:d84e1c7e4e86 | 70 | |
pommzorz | 0:d84e1c7e4e86 | 71 | /* PUBLIC METHODS */ |
pommzorz | 0:d84e1c7e4e86 | 72 | |
pommzorz | 1:72ecf7399250 | 73 | //HMC58X3::HMC58X3(PinName sda, PinName scl): i2c(sda, scl) |
pommzorz | 1:72ecf7399250 | 74 | HMC58X3::HMC58X3(I2C i2c_):i2c(i2c_) |
pommzorz | 0:d84e1c7e4e86 | 75 | { |
pommzorz | 1:72ecf7399250 | 76 | //this->i2c= i2c_; |
pommzorz | 0:d84e1c7e4e86 | 77 | x_scale=1.0F; |
pommzorz | 0:d84e1c7e4e86 | 78 | y_scale=1.0F; |
pommzorz | 0:d84e1c7e4e86 | 79 | z_scale=1.0F; |
pommzorz | 0:d84e1c7e4e86 | 80 | |
pommzorz | 0:d84e1c7e4e86 | 81 | } |
pommzorz | 0:d84e1c7e4e86 | 82 | |
pommzorz | 0:d84e1c7e4e86 | 83 | |
pommzorz | 0:d84e1c7e4e86 | 84 | void HMC58X3::init(bool setmode) |
pommzorz | 0:d84e1c7e4e86 | 85 | { |
pommzorz | 0:d84e1c7e4e86 | 86 | // note that we don't initialize Wire here. |
pommzorz | 0:d84e1c7e4e86 | 87 | // You'll have to do that in setup() in your Arduino program |
pommzorz | 0:d84e1c7e4e86 | 88 | wait_ms(5); // you need to wait at least 5ms after power on to initialize |
pommzorz | 0:d84e1c7e4e86 | 89 | if (setmode) { |
pommzorz | 0:d84e1c7e4e86 | 90 | setMode(0); |
pommzorz | 0:d84e1c7e4e86 | 91 | } |
pommzorz | 0:d84e1c7e4e86 | 92 | |
pommzorz | 0:d84e1c7e4e86 | 93 | |
pommzorz | 0:d84e1c7e4e86 | 94 | |
pommzorz | 0:d84e1c7e4e86 | 95 | writeReg(HMC58X3_R_CONFA, 0x70); // 8 samples averaged, 75Hz frequency, no artificial bias. |
pommzorz | 0:d84e1c7e4e86 | 96 | writeReg(HMC58X3_R_CONFB, 0xA0); |
pommzorz | 0:d84e1c7e4e86 | 97 | writeReg(HMC58X3_R_MODE, 0x00); |
pommzorz | 0:d84e1c7e4e86 | 98 | } |
pommzorz | 0:d84e1c7e4e86 | 99 | |
pommzorz | 0:d84e1c7e4e86 | 100 | |
pommzorz | 0:d84e1c7e4e86 | 101 | void HMC58X3::setMode(unsigned char mode) |
pommzorz | 0:d84e1c7e4e86 | 102 | { |
pommzorz | 0:d84e1c7e4e86 | 103 | if (mode > 2) { |
pommzorz | 0:d84e1c7e4e86 | 104 | return; |
pommzorz | 0:d84e1c7e4e86 | 105 | } |
pommzorz | 0:d84e1c7e4e86 | 106 | |
pommzorz | 0:d84e1c7e4e86 | 107 | writeReg(HMC58X3_R_MODE, mode); |
pommzorz | 0:d84e1c7e4e86 | 108 | wait_ms(100); |
pommzorz | 0:d84e1c7e4e86 | 109 | } |
pommzorz | 0:d84e1c7e4e86 | 110 | |
pommzorz | 0:d84e1c7e4e86 | 111 | /* |
pommzorz | 0:d84e1c7e4e86 | 112 | Calibrate which has a few weaknesses. |
pommzorz | 0:d84e1c7e4e86 | 113 | 1. Uses wrong gain for first reading. |
pommzorz | 0:d84e1c7e4e86 | 114 | 2. Uses max instead of max of average when normalizing the axis to one another. |
pommzorz | 0:d84e1c7e4e86 | 115 | 3. Doesn't use neg bias. (possible improvement in measurement). |
pommzorz | 0:d84e1c7e4e86 | 116 | */ |
pommzorz | 0:d84e1c7e4e86 | 117 | void HMC58X3::calibrate(unsigned char gain) |
pommzorz | 0:d84e1c7e4e86 | 118 | { |
pommzorz | 0:d84e1c7e4e86 | 119 | x_scale=1; // get actual values |
pommzorz | 0:d84e1c7e4e86 | 120 | y_scale=1; |
pommzorz | 0:d84e1c7e4e86 | 121 | z_scale=1; |
pommzorz | 0:d84e1c7e4e86 | 122 | writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias |
pommzorz | 0:d84e1c7e4e86 | 123 | setGain(gain); |
pommzorz | 0:d84e1c7e4e86 | 124 | float x, y, z, mx=0, my=0, mz=0, t=10; |
pommzorz | 0:d84e1c7e4e86 | 125 | |
pommzorz | 0:d84e1c7e4e86 | 126 | for (int i=0; i<(int)t; i++) { |
pommzorz | 0:d84e1c7e4e86 | 127 | setMode(1); |
pommzorz | 0:d84e1c7e4e86 | 128 | getValues(&x,&y,&z); |
pommzorz | 0:d84e1c7e4e86 | 129 | if (x>mx) mx=x; |
pommzorz | 0:d84e1c7e4e86 | 130 | if (y>my) my=y; |
pommzorz | 0:d84e1c7e4e86 | 131 | if (z>mz) mz=z; |
pommzorz | 0:d84e1c7e4e86 | 132 | } |
pommzorz | 0:d84e1c7e4e86 | 133 | |
pommzorz | 0:d84e1c7e4e86 | 134 | float max=0; |
pommzorz | 0:d84e1c7e4e86 | 135 | if (mx>max) max=mx; |
pommzorz | 0:d84e1c7e4e86 | 136 | if (my>max) max=my; |
pommzorz | 0:d84e1c7e4e86 | 137 | if (mz>max) max=mz; |
pommzorz | 0:d84e1c7e4e86 | 138 | x_max=mx; |
pommzorz | 0:d84e1c7e4e86 | 139 | y_max=my; |
pommzorz | 0:d84e1c7e4e86 | 140 | z_max=mz; |
pommzorz | 0:d84e1c7e4e86 | 141 | x_scale=max/mx; // calc scales |
pommzorz | 0:d84e1c7e4e86 | 142 | y_scale=max/my; |
pommzorz | 0:d84e1c7e4e86 | 143 | z_scale=max/mz; |
pommzorz | 0:d84e1c7e4e86 | 144 | |
pommzorz | 0:d84e1c7e4e86 | 145 | writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default |
pommzorz | 0:d84e1c7e4e86 | 146 | } // calibrate(). |
pommzorz | 0:d84e1c7e4e86 | 147 | |
pommzorz | 0:d84e1c7e4e86 | 148 | /*! |
pommzorz | 0:d84e1c7e4e86 | 149 | \brief Calibrate using the self test operation. |
pommzorz | 0:d84e1c7e4e86 | 150 | |
pommzorz | 0:d84e1c7e4e86 | 151 | Average the values using bias mode to obtain the scale factors. |
pommzorz | 0:d84e1c7e4e86 | 152 | |
pommzorz | 0:d84e1c7e4e86 | 153 | \param gain [in] Gain setting for the sensor. See data sheet. |
pommzorz | 0:d84e1c7e4e86 | 154 | \param n_samples [in] Number of samples to average together while applying the positive and negative bias. |
pommzorz | 0:d84e1c7e4e86 | 155 | \return Returns false if any of the following occurs: |
pommzorz | 0:d84e1c7e4e86 | 156 | # Invalid input parameters. (gain>7 or n_samples=0). |
pommzorz | 0:d84e1c7e4e86 | 157 | # Id registers are wrong for the compiled device. Unfortunately, we can't distinguish between HMC5843 and HMC5883L. |
pommzorz | 0:d84e1c7e4e86 | 158 | # Calibration saturates during the positive or negative bias on any of the readings. |
pommzorz | 0:d84e1c7e4e86 | 159 | # Readings are outside of the expected range for bias current. |
pommzorz | 0:d84e1c7e4e86 | 160 | */ |
pommzorz | 0:d84e1c7e4e86 | 161 | bool HMC58X3::calibrate(unsigned char gain,unsigned int n_samples) |
pommzorz | 0:d84e1c7e4e86 | 162 | { |
pommzorz | 0:d84e1c7e4e86 | 163 | int xyz[3]; // 16 bit integer values for each axis. |
pommzorz | 0:d84e1c7e4e86 | 164 | long int xyz_total[3]= {0,0,0}; // 32 bit totals so they won't overflow. |
pommzorz | 0:d84e1c7e4e86 | 165 | bool bret=true; // Function return value. Will return false if the wrong identifier is returned, saturation is detected or response is out of range to self test bias. |
pommzorz | 0:d84e1c7e4e86 | 166 | char id[3]; // Three identification registers should return 'H43'. |
pommzorz | 0:d84e1c7e4e86 | 167 | long int low_limit, high_limit; |
pommzorz | 0:d84e1c7e4e86 | 168 | /* |
pommzorz | 0:d84e1c7e4e86 | 169 | Make sure we are talking to the correct device. |
pommzorz | 0:d84e1c7e4e86 | 170 | Hard to believe Honeywell didn't change the identifier. |
pommzorz | 0:d84e1c7e4e86 | 171 | */ |
pommzorz | 0:d84e1c7e4e86 | 172 | if ((8>gain) && (0<n_samples)) { // Notice this allows gain setting of 7 which the data sheet warns against. |
pommzorz | 0:d84e1c7e4e86 | 173 | getID(id); |
pommzorz | 0:d84e1c7e4e86 | 174 | if (('H' == id[0]) && ('4' == id[1]) && ('3' == id[2])) { |
pommzorz | 0:d84e1c7e4e86 | 175 | /* |
pommzorz | 0:d84e1c7e4e86 | 176 | Use the positive bias current to impose a known field on each axis. |
pommzorz | 0:d84e1c7e4e86 | 177 | This field depends on the device and the axis. |
pommzorz | 0:d84e1c7e4e86 | 178 | */ |
pommzorz | 0:d84e1c7e4e86 | 179 | writeReg(HMC58X3_R_CONFA, 0x010 + HMC_POS_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to pos bias |
pommzorz | 0:d84e1c7e4e86 | 180 | /* |
pommzorz | 0:d84e1c7e4e86 | 181 | Note that the very first measurement after a gain change maintains the same gain as the previous setting. |
pommzorz | 0:d84e1c7e4e86 | 182 | The new gain setting is effective from the second measurement and on. |
pommzorz | 0:d84e1c7e4e86 | 183 | */ |
pommzorz | 0:d84e1c7e4e86 | 184 | setGain(gain); |
pommzorz | 0:d84e1c7e4e86 | 185 | setMode(1); // Change to single measurement mode. |
pommzorz | 0:d84e1c7e4e86 | 186 | getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values and ignore since this reading may use previous gain. |
pommzorz | 0:d84e1c7e4e86 | 187 | |
pommzorz | 0:d84e1c7e4e86 | 188 | for (unsigned int i=0; i<n_samples; i++) { |
pommzorz | 0:d84e1c7e4e86 | 189 | setMode(1); |
pommzorz | 0:d84e1c7e4e86 | 190 | getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed. |
pommzorz | 0:d84e1c7e4e86 | 191 | /* |
pommzorz | 0:d84e1c7e4e86 | 192 | Since the measurements are noisy, they should be averaged rather than taking the max. |
pommzorz | 0:d84e1c7e4e86 | 193 | */ |
pommzorz | 0:d84e1c7e4e86 | 194 | xyz_total[0]+=xyz[0]; |
pommzorz | 0:d84e1c7e4e86 | 195 | xyz_total[1]+=xyz[1]; |
pommzorz | 0:d84e1c7e4e86 | 196 | xyz_total[2]+=xyz[2]; |
pommzorz | 0:d84e1c7e4e86 | 197 | /* |
pommzorz | 0:d84e1c7e4e86 | 198 | Detect saturation. |
pommzorz | 0:d84e1c7e4e86 | 199 | */ |
pommzorz | 0:d84e1c7e4e86 | 200 | if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) { |
pommzorz | 0:d84e1c7e4e86 | 201 | DEBUG_PRINT("HMC58x3 Self test saturated. Increase range."); |
pommzorz | 0:d84e1c7e4e86 | 202 | bret=false; |
pommzorz | 0:d84e1c7e4e86 | 203 | break; // Breaks out of the for loop. No sense in continuing if we saturated. |
pommzorz | 0:d84e1c7e4e86 | 204 | } |
pommzorz | 0:d84e1c7e4e86 | 205 | } |
pommzorz | 0:d84e1c7e4e86 | 206 | /* |
pommzorz | 0:d84e1c7e4e86 | 207 | Apply the negative bias. (Same gain) |
pommzorz | 0:d84e1c7e4e86 | 208 | */ |
pommzorz | 0:d84e1c7e4e86 | 209 | writeReg(HMC58X3_R_CONFA, 0x010 + HMC_NEG_BIAS); // Reg A DOR=0x010 + MS1,MS0 set to negative bias. |
pommzorz | 0:d84e1c7e4e86 | 210 | for (unsigned int i=0; i<n_samples; i++) { |
pommzorz | 0:d84e1c7e4e86 | 211 | setMode(1); |
pommzorz | 0:d84e1c7e4e86 | 212 | getRaw(&xyz[0],&xyz[1],&xyz[2]); // Get the raw values in case the scales have already been changed. |
pommzorz | 0:d84e1c7e4e86 | 213 | /* |
pommzorz | 0:d84e1c7e4e86 | 214 | Since the measurements are noisy, they should be averaged. |
pommzorz | 0:d84e1c7e4e86 | 215 | */ |
pommzorz | 0:d84e1c7e4e86 | 216 | xyz_total[0]-=xyz[0]; |
pommzorz | 0:d84e1c7e4e86 | 217 | xyz_total[1]-=xyz[1]; |
pommzorz | 0:d84e1c7e4e86 | 218 | xyz_total[2]-=xyz[2]; |
pommzorz | 0:d84e1c7e4e86 | 219 | /* |
pommzorz | 0:d84e1c7e4e86 | 220 | Detect saturation. |
pommzorz | 0:d84e1c7e4e86 | 221 | */ |
pommzorz | 0:d84e1c7e4e86 | 222 | if (-(1<<12) >= min(xyz[0],min(xyz[1],xyz[2]))) { |
pommzorz | 0:d84e1c7e4e86 | 223 | DEBUG_PRINT("HMC58x3 Self test saturated. Increase range."); |
pommzorz | 0:d84e1c7e4e86 | 224 | bret=false; |
pommzorz | 0:d84e1c7e4e86 | 225 | break; // Breaks out of the for loop. No sense in continuing if we saturated. |
pommzorz | 0:d84e1c7e4e86 | 226 | } |
pommzorz | 0:d84e1c7e4e86 | 227 | } |
pommzorz | 0:d84e1c7e4e86 | 228 | /* |
pommzorz | 0:d84e1c7e4e86 | 229 | Compare the values against the expected self test bias gauss. |
pommzorz | 0:d84e1c7e4e86 | 230 | Notice, the same limits are applied to all axis. |
pommzorz | 0:d84e1c7e4e86 | 231 | */ |
pommzorz | 0:d84e1c7e4e86 | 232 | low_limit =SELF_TEST_LOW_LIMIT *counts_per_milligauss[gain]*2*n_samples; |
pommzorz | 0:d84e1c7e4e86 | 233 | high_limit=SELF_TEST_HIGH_LIMIT*counts_per_milligauss[gain]*2*n_samples; |
pommzorz | 0:d84e1c7e4e86 | 234 | |
pommzorz | 0:d84e1c7e4e86 | 235 | if ((true==bret) && |
pommzorz | 0:d84e1c7e4e86 | 236 | (low_limit <= xyz_total[0]) && (high_limit >= xyz_total[0]) && |
pommzorz | 0:d84e1c7e4e86 | 237 | (low_limit <= xyz_total[1]) && (high_limit >= xyz_total[1]) && |
pommzorz | 0:d84e1c7e4e86 | 238 | (low_limit <= xyz_total[2]) && (high_limit >= xyz_total[2]) ) { |
pommzorz | 0:d84e1c7e4e86 | 239 | /* |
pommzorz | 0:d84e1c7e4e86 | 240 | Successful calibration. |
pommzorz | 0:d84e1c7e4e86 | 241 | Normalize the scale factors so all axis return the same range of values for the bias field. |
pommzorz | 0:d84e1c7e4e86 | 242 | Factor of 2 is from summation of total of n_samples from both positive and negative bias. |
pommzorz | 0:d84e1c7e4e86 | 243 | */ |
pommzorz | 0:d84e1c7e4e86 | 244 | x_scale=(counts_per_milligauss[gain]*(HMC58X3_X_SELF_TEST_GAUSS*2))/(xyz_total[0]/n_samples); |
pommzorz | 0:d84e1c7e4e86 | 245 | y_scale=(counts_per_milligauss[gain]*(HMC58X3_Y_SELF_TEST_GAUSS*2))/(xyz_total[1]/n_samples); |
pommzorz | 0:d84e1c7e4e86 | 246 | z_scale=(counts_per_milligauss[gain]*(HMC58X3_Z_SELF_TEST_GAUSS*2))/(xyz_total[2]/n_samples); |
pommzorz | 0:d84e1c7e4e86 | 247 | } else { |
pommzorz | 0:d84e1c7e4e86 | 248 | DEBUG_PRINT("HMC58x3 Self test out of range."); |
pommzorz | 0:d84e1c7e4e86 | 249 | bret=false; |
pommzorz | 0:d84e1c7e4e86 | 250 | } |
pommzorz | 0:d84e1c7e4e86 | 251 | writeReg(HMC58X3_R_CONFA, 0x010); // set RegA/DOR back to default. |
pommzorz | 0:d84e1c7e4e86 | 252 | } else { |
pommzorz | 0:d84e1c7e4e86 | 253 | #if defined(ISHMC5843) |
pommzorz | 0:d84e1c7e4e86 | 254 | DEBUG_PRINT("HMC5843 failed id check."); |
pommzorz | 0:d84e1c7e4e86 | 255 | #else |
pommzorz | 0:d84e1c7e4e86 | 256 | DEBUG_PRINT("HMC5883L failed id check."); |
pommzorz | 0:d84e1c7e4e86 | 257 | #endif |
pommzorz | 0:d84e1c7e4e86 | 258 | bret=false; |
pommzorz | 0:d84e1c7e4e86 | 259 | } |
pommzorz | 0:d84e1c7e4e86 | 260 | } else { |
pommzorz | 0:d84e1c7e4e86 | 261 | /* |
pommzorz | 0:d84e1c7e4e86 | 262 | Bad input parameters. |
pommzorz | 0:d84e1c7e4e86 | 263 | */ |
pommzorz | 0:d84e1c7e4e86 | 264 | DEBUG_PRINT("HMC58x3 Bad parameters."); |
pommzorz | 0:d84e1c7e4e86 | 265 | bret=false; |
pommzorz | 0:d84e1c7e4e86 | 266 | } |
pommzorz | 0:d84e1c7e4e86 | 267 | return(bret); |
pommzorz | 0:d84e1c7e4e86 | 268 | } // calibrate(). |
pommzorz | 0:d84e1c7e4e86 | 269 | |
pommzorz | 0:d84e1c7e4e86 | 270 | // set data output rate |
pommzorz | 0:d84e1c7e4e86 | 271 | // 0-6, 4 default, normal operation assumed |
pommzorz | 0:d84e1c7e4e86 | 272 | void HMC58X3::setDOR(unsigned char DOR) |
pommzorz | 0:d84e1c7e4e86 | 273 | { |
pommzorz | 0:d84e1c7e4e86 | 274 | if (DOR>6) return; |
pommzorz | 0:d84e1c7e4e86 | 275 | writeReg(HMC58X3_R_CONFA,DOR<<2); |
pommzorz | 0:d84e1c7e4e86 | 276 | } |
pommzorz | 0:d84e1c7e4e86 | 277 | |
pommzorz | 0:d84e1c7e4e86 | 278 | |
pommzorz | 0:d84e1c7e4e86 | 279 | void HMC58X3::setGain(unsigned char gain) |
pommzorz | 0:d84e1c7e4e86 | 280 | { |
pommzorz | 0:d84e1c7e4e86 | 281 | // 0-7, 1 default |
pommzorz | 0:d84e1c7e4e86 | 282 | if (gain > 7) return; |
pommzorz | 0:d84e1c7e4e86 | 283 | writeReg(HMC58X3_R_CONFB, gain << 5); |
pommzorz | 0:d84e1c7e4e86 | 284 | } |
pommzorz | 0:d84e1c7e4e86 | 285 | |
pommzorz | 0:d84e1c7e4e86 | 286 | |
pommzorz | 0:d84e1c7e4e86 | 287 | void HMC58X3::writeReg(unsigned char reg, unsigned char val) |
pommzorz | 0:d84e1c7e4e86 | 288 | { |
pommzorz | 0:d84e1c7e4e86 | 289 | i2c.start(); |
pommzorz | 0:d84e1c7e4e86 | 290 | i2c.write(reg); // send register address |
pommzorz | 0:d84e1c7e4e86 | 291 | i2c.write(val); // send value to write |
pommzorz | 0:d84e1c7e4e86 | 292 | i2c.stop(); //end transmission |
pommzorz | 0:d84e1c7e4e86 | 293 | } |
pommzorz | 0:d84e1c7e4e86 | 294 | |
pommzorz | 0:d84e1c7e4e86 | 295 | |
pommzorz | 0:d84e1c7e4e86 | 296 | void HMC58X3::getValues(int *x,int *y,int *z) |
pommzorz | 0:d84e1c7e4e86 | 297 | { |
pommzorz | 0:d84e1c7e4e86 | 298 | float fx,fy,fz; |
pommzorz | 0:d84e1c7e4e86 | 299 | getValues(&fx,&fy,&fz); |
pommzorz | 0:d84e1c7e4e86 | 300 | *x= (int) (fx + 0.5); |
pommzorz | 0:d84e1c7e4e86 | 301 | *y= (int) (fy + 0.5); |
pommzorz | 0:d84e1c7e4e86 | 302 | *z= (int) (fz + 0.5); |
pommzorz | 0:d84e1c7e4e86 | 303 | } |
pommzorz | 0:d84e1c7e4e86 | 304 | |
pommzorz | 0:d84e1c7e4e86 | 305 | |
pommzorz | 0:d84e1c7e4e86 | 306 | void HMC58X3::getValues(float *x,float *y,float *z) |
pommzorz | 0:d84e1c7e4e86 | 307 | { |
pommzorz | 0:d84e1c7e4e86 | 308 | int xr,yr,zr; |
pommzorz | 0:d84e1c7e4e86 | 309 | |
pommzorz | 0:d84e1c7e4e86 | 310 | getRaw(&xr, &yr, &zr); |
pommzorz | 0:d84e1c7e4e86 | 311 | *x= ((float) xr) / x_scale; |
pommzorz | 0:d84e1c7e4e86 | 312 | *y = ((float) yr) / y_scale; |
pommzorz | 0:d84e1c7e4e86 | 313 | *z = ((float) zr) / z_scale; |
pommzorz | 0:d84e1c7e4e86 | 314 | } |
pommzorz | 0:d84e1c7e4e86 | 315 | |
pommzorz | 0:d84e1c7e4e86 | 316 | |
pommzorz | 0:d84e1c7e4e86 | 317 | void HMC58X3::getRaw(int *x,int *y,int *z) |
pommzorz | 0:d84e1c7e4e86 | 318 | { |
pommzorz | 0:d84e1c7e4e86 | 319 | |
pommzorz | 0:d84e1c7e4e86 | 320 | char cmd[2]; |
pommzorz | 0:d84e1c7e4e86 | 321 | char data[6]; |
pommzorz | 0:d84e1c7e4e86 | 322 | cmd[0] = 0x03; |
pommzorz | 0:d84e1c7e4e86 | 323 | |
pommzorz | 0:d84e1c7e4e86 | 324 | i2c.write(I2C_ADDRESS, cmd, 1, true); // set the pointer to the start of x |
pommzorz | 0:d84e1c7e4e86 | 325 | i2c.read(I2C_ADDRESS, data, 6, false); |
pommzorz | 0:d84e1c7e4e86 | 326 | |
pommzorz | 0:d84e1c7e4e86 | 327 | // read out the 3 values, 2 bytes each. |
pommzorz | 0:d84e1c7e4e86 | 328 | *x = int16_t(((unsigned char)data[0] << 8) | (unsigned char)data[1]); |
pommzorz | 0:d84e1c7e4e86 | 329 | #ifdef ISHMC5843 |
pommzorz | 0:d84e1c7e4e86 | 330 | *y = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]); |
pommzorz | 0:d84e1c7e4e86 | 331 | *z = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]); |
pommzorz | 0:d84e1c7e4e86 | 332 | #else // the Z registers comes before the Y registers in the HMC5883L |
pommzorz | 0:d84e1c7e4e86 | 333 | *z = int16_t(((unsigned char)data[1*2] << 8) | (unsigned char)data[1*2+1]); |
pommzorz | 0:d84e1c7e4e86 | 334 | *y = int16_t(((unsigned char)data[2*2] << 8) | (unsigned char)data[2*2+1]); |
pommzorz | 0:d84e1c7e4e86 | 335 | #endif |
pommzorz | 0:d84e1c7e4e86 | 336 | // the HMC58X3 will automatically wrap around on the next request |
pommzorz | 0:d84e1c7e4e86 | 337 | |
pommzorz | 0:d84e1c7e4e86 | 338 | |
pommzorz | 0:d84e1c7e4e86 | 339 | } |
pommzorz | 0:d84e1c7e4e86 | 340 | |
pommzorz | 0:d84e1c7e4e86 | 341 | |
pommzorz | 0:d84e1c7e4e86 | 342 | void HMC58X3::getValues(float *xyz) |
pommzorz | 0:d84e1c7e4e86 | 343 | { |
pommzorz | 0:d84e1c7e4e86 | 344 | getValues(&xyz[0], &xyz[1], &xyz[2]); |
pommzorz | 0:d84e1c7e4e86 | 345 | } |
pommzorz | 0:d84e1c7e4e86 | 346 | |
pommzorz | 0:d84e1c7e4e86 | 347 | /*! |
pommzorz | 0:d84e1c7e4e86 | 348 | \brief Retrieve the value of the three ID registers. |
pommzorz | 0:d84e1c7e4e86 | 349 | |
pommzorz | 0:d84e1c7e4e86 | 350 | Note: Both the HMC5843 and HMC5883L have the same 'H43' identification register values. (Looks like someone at Honeywell screwed up.) |
pommzorz | 0:d84e1c7e4e86 | 351 | |
pommzorz | 0:d84e1c7e4e86 | 352 | \param id [out] Returns the three id register values. |
pommzorz | 0:d84e1c7e4e86 | 353 | */ |
pommzorz | 0:d84e1c7e4e86 | 354 | void HMC58X3::getID(char id[3]) |
pommzorz | 0:d84e1c7e4e86 | 355 | { |
pommzorz | 0:d84e1c7e4e86 | 356 | i2c.start(); |
pommzorz | 0:d84e1c7e4e86 | 357 | i2c.write(HMC58X3_R_IDA); // Will start reading registers starting from Identification Register A. |
pommzorz | 0:d84e1c7e4e86 | 358 | |
pommzorz | 0:d84e1c7e4e86 | 359 | id[0] = i2c.read(0); |
pommzorz | 0:d84e1c7e4e86 | 360 | id[1] = i2c.read(0); |
pommzorz | 0:d84e1c7e4e86 | 361 | id[2] = i2c.read(0); |
pommzorz | 0:d84e1c7e4e86 | 362 | |
pommzorz | 0:d84e1c7e4e86 | 363 | i2c.stop(); |
pommzorz | 0:d84e1c7e4e86 | 364 | } // getID(). |
pommzorz | 0:d84e1c7e4e86 | 365 | |
pommzorz | 0:d84e1c7e4e86 | 366 | int HMC58X3::min (int a, int b) |
pommzorz | 0:d84e1c7e4e86 | 367 | { |
pommzorz | 0:d84e1c7e4e86 | 368 | return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2) |
pommzorz | 0:d84e1c7e4e86 | 369 | } |