/** Power on and prepare for general usage. * This will prepare the magnetometer with default settings, ready for single- * use mode (very low power requirements). Default settings include 8-sample * averaging, 15 Hz data output rate, normal measurement bias, a,d 1090 gain (in * terms of LSB/Gauss). Be sure to adjust any settings you need specifically * after initialization, especially the gain settings if you happen to be seeing * a lot of -4096 values (see the datasheet for mor information). */ void HMC5883L::initialize() { // write CONFIG_A register setSampleAveraging(HMC5883L_AVERAGING_8); setMeasurementBias(HMC5883L_BIAS_NORMAL); setDataRate(HMC5883L_RATE_15); // write CONFIG_B register setGain(HMC5883L_GAIN_1090); // write MODE register setMode(HMC5883L_MODE_SINGLE); }
bool HMC5883L::calibrate(int8_t testGain) { // Keep the current status ... uint8_t previousGain = getGain(); // Set the gain if (testGain < 0) { testGain = gain; } setGain(testGain); // To check the HMC5883L for proper operation, a self test // feature in incorporated in which the sensor offset straps // are excited to create a nominal field strength (bias field) // to be measured. To implement self test, the least significant // bits (MS1 and MS0) of configuration register A are changed // from 00 to 01 (positive bias) or 10 (negetive bias) setMeasurementBias(HMC5883L_BIAS_POSITIVE); // Then, by placing the mode register into single-measurement mode ... setMode(HMC5883L_MODE_SINGLE); // Two data acquisition cycles will be made on each magnetic vector. // The first acquisition will be a set pulse followed shortly by // measurement data of the external field. The second acquisition // will have the offset strap excited (about 10 mA) in the positive // bias mode for X, Y, and Z axes to create about a ±1.1 gauss self // test field plus the external field. // The first acquisition values will be subtracted from the // second acquisition, and the net measurement will be placed into // the data output registers. int16_t x,y,z; getRawHeading(&x,&y,&z); // In the event the ADC reading overflows or underflows for the // given channel, or if there is a math overflow during the bias // measurement, this data register will contain the value -4096. // This register value will clear when after the next valid // measurement is made. if (min(x,min(y,z)) == -4096) { scaleFactors[testGain][0] = 1.0f; scaleFactors[testGain][1] = 1.0f; scaleFactors[testGain][2] = 1.0f; return false; } getRawHeading(&x,&y,&z); if (min(x,min(y,z)) == -4096) { scaleFactors[testGain][0] = 1.0f; scaleFactors[testGain][1] = 1.0f; scaleFactors[testGain][2] = 1.0f; return false; } // Since placing device in positive bias mode // (or alternatively negative bias mode) applies // a known artificial field on all three axes, // the resulting ADC measurements in data output // registers can be used to scale the sensors. float xExpectedSelfTestValue = HMC5883L_SELF_TEST_X_AXIS_ABSOLUTE_GAUSS * HMC5883L_LSB_PER_GAUS[testGain]; float yExpectedSelfTestValue = HMC5883L_SELF_TEST_Y_AXIS_ABSOLUTE_GAUSS * HMC5883L_LSB_PER_GAUS[testGain]; float zExpectedSelfTestValue = HMC5883L_SELF_TEST_Z_AXIS_ABSOLUTE_GAUSS * HMC5883L_LSB_PER_GAUS[testGain]; scaleFactors[testGain][0] = xExpectedSelfTestValue/x; scaleFactors[testGain][1] = yExpectedSelfTestValue/y; scaleFactors[testGain][2] = zExpectedSelfTestValue/z; setGain(previousGain); setMeasurementBias(HMC5883L_BIAS_NORMAL); return true; }