HMC5883L Digital Compass Library
Fork of HMC5883L by
Diff: HMC5883L.cpp
- Revision:
- 3:f5f6aaf24be0
- Parent:
- 2:bbc9ad18fd3e
--- a/HMC5883L.cpp Wed Aug 05 13:16:12 2015 +0000 +++ b/HMC5883L.cpp Sun Jun 04 06:19:54 2017 +0000 @@ -103,6 +103,7 @@ void HMC5883L::init() { + i2c.frequency(400000); // use fast (400 kHz) I2C writeByte(HMC5883L_ADDRESS, CONFIG_A, 0x78); // Number of samples averaged: 8, Data output rate: 75 Hz writeByte(HMC5883L_ADDRESS, MODE, 0x00); // Continuous-Measurement Mode setMagGain(MagGain_13); @@ -121,10 +122,11 @@ dest[2] = (int16_t)(((int16_t)rawData[2]<<8) | rawData[3]); // MAG_ZOUT dest[1] = (int16_t)(((int16_t)rawData[4]<<8) | rawData[5]); // MAG_YOUT + float mRes = 0.73; /* Convert raw data to magnetic field values in microtesla */ - dest[0] = dest[0] / Gauss_LSB_XY * GAUSS_TO_MICROTESLA; - dest[1] = dest[1] / Gauss_LSB_XY * GAUSS_TO_MICROTESLA; - dest[2] = dest[2] / Gauss_LSB_Z * GAUSS_TO_MICROTESLA; + dest[0] = dest[0] * mRes; // / Gauss_LSB_XY * GAUSS_TO_MICROTESLA; + dest[1] = dest[1] * mRes; // / Gauss_LSB_XY * GAUSS_TO_MICROTESLA; + dest[2] = dest[2] * mRes; // / Gauss_LSB_Z * GAUSS_TO_MICROTESLA; } double HMC5883L::getHeading() @@ -138,7 +140,7 @@ // After calculating heading declination angle should be added to heading which is the error of the magnetic field in specific location. // declinationAngle can be found here http://www.magnetic-declination.com/ // For Ankara (my location) declinationAngle is ~5.5 degrees (0.096 radians) - float declinationAngle = 0.096; + float declinationAngle = 0.022; heading += declinationAngle; // Correct for when signs are reversed.