Compass: Initialisation criteria incorrect
Currently the HMC5883 compass initialisation enforces a different self test response criteria compared to what the sensor spec allows for. This means that the initialisation may fail when the sensor is perfectly in spec and pass when the sensor is out of spec. Currently the x axes is treated differently from the y and z axis with the allowable range of 589-1084. For the y and z axis the allowable range is 548-1019. The HMC5883 spec sheet gives the same limits for all three axes as:
If Gain = 3, self test limits are: Low Limit = 243 * 660/390 = 411 High Limit = 575 * 660/390 = 973
I have encountered one example of an 'in spec' sensor failing to initialise here: http://goo.gl/1MmwMF
In addition, the expected values for the self test used in the code seem to be incorrect for the y axis. Currently we have: expected_x=766 expected_yz=713 Per the spec sheet, both the x AND y axis experience the same 1.16 gauss increase in mag field during the test, where as the z axis is 1.08 gauss. This means that we should have: expected_xy=766 (note: x and y the same) expected_z=713 (only z different) This is currently affecting the y axis calibration resulting in the y axis correction being some 7% out.
Suggested alterations to libraries/AP_Compass/AP_Compass_HMC5843.cpp:
// Public Methods //////////////////////////////////////////////////////////////
bool
AP_Compass_HMC5843::init()
{
int numAttempts = 0, good_count = 0;
bool success = false;
uint8_t calibration_gain = 0x20;
/* jimovonz:
uint16_t expected_x = 715;
uint16_t expected_yz = 715;
*/
uint16_t expected_xy = 715;
uint16_t expected_z = 715;
uint16_t test_limit_low = 550; // Default value based on previous calc of 715/1.3
uint16_t test_limit_high = 1021; // Default value based on previous calc of 715/0.7
// jimovonz finish
float gain_multiple = 1.0;
hal.scheduler->delay(10);
_i2c_sem = hal.i2c->get_semaphore();
if (!_i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
hal.scheduler->panic(PSTR("Failed to get HMC5843 semaphore"));
}
// determine if we are using 5843 or 5883L
_base_config = 0;
if (!write_register(ConfigRegA, SampleAveraging_8<<5 | DataOutputRate_75HZ<<2 | NormalOperation) ||
!read_register(ConfigRegA, &_base_config)) {
healthy = false;
_i2c_sem->give();
return false;
}
if ( _base_config == (SampleAveraging_8<<5 | DataOutputRate_75HZ<<2 | NormalOperation)) {
// a 5883L supports the sample averaging config
product_id = AP_COMPASS_TYPE_HMC5883L;
calibration_gain = 0x60;
/* jimovonz:
expected_x = 766;
expected_yz = 713;
*/
expected_xy = 766;
expected_z = 713;
test_limit_low=411; // Based on HMC5883 spec sheet with gain=3 (RegB=0x60)
test_limit_high=973; // Based on HMC5883 spec sheet with gain=3 (RegB=0x60)
// jimovonz finish
gain_multiple = 660.0 / 1090; // adjustment for runtime vs calibration gain
} else if (_base_config == (NormalOperation | DataOutputRate_75HZ<<2)) {
product_id = AP_COMPASS_TYPE_HMC5843;
} else {
// not behaving like either supported compass type
_i2c_sem->give();
return false;
}
calibration[0] = 0;
calibration[1] = 0;
calibration[2] = 0;
while ( success == 0 && numAttempts < 20 && good_count < 5)
{
// record number of attempts at initialisation
numAttempts++;
// force positiveBias (compass should return 715 for all channels)
if (!write_register(ConfigRegA, PositiveBiasConfig))
continue; // compass not responding on the bus
hal.scheduler->delay(50);
// set gains
if (!write_register(ConfigRegB, calibration_gain) ||
!write_register(ModeRegister, SingleConversion))
continue;
// read values from the compass
hal.scheduler->delay(50);
if (!read_raw())
continue; // we didn't read valid values
hal.scheduler->delay(10);
float cal[3];
/* jimovonz:
cal[0] = fabsf(expected_x / (float)_mag_x);
cal[1] = fabsf(expected_yz / (float)_mag_y);
cal[2] = fabsf(expected_yz / (float)_mag_z);
//moved into if statement below
if (cal[0] > 0.7f && cal[0] < 1.3f &&
cal[1] > 0.7f && cal[1] < 1.3f &&
cal[2] > 0.7f && cal[2] < 1.3f) {
*/
if (abs(_mag_x) > test_limit_low && abs(_mag_x) < test_limit_high &&
abs(_mag_y) > test_limit_low && abs(_mag_y) < test_limit_high &&
abs(_mag_z) > test_limit_low && abs(_mag_z) < test_limit_high) {
cal[0] = fabsf(expected_xy / (float)_mag_x);
cal[1] = fabsf(expected_xy / (float)_mag_y);
cal[2] = fabsf(expected_z / (float)_mag_z);
// jimovonz finish
good_count++;
calibration[0] += cal[0];
calibration[1] += cal[1];
calibration[2] += cal[2];
}
#if 0
/* useful for debugging */
hal.console->printf_P(PSTR("MagX: %d MagY: %d MagZ: %d\n"), (int)_mag_x, (int)_mag_y, (int)_mag_z);
hal.console->printf_P(PSTR("CalX: %.2f CalY: %.2f CalZ: %.2f\n"), cal[0], cal[1], cal[2]);
#endif
}
if (good_count >= 5) {
calibration[0] = calibration[0] * gain_multiple / good_count;
calibration[1] = calibration[1] * gain_multiple / good_count;
calibration[2] = calibration[2] * gain_multiple / good_count;
success = true;
} else {
/* best guess */
calibration[0] = 1.0;
calibration[1] = 1.0;
calibration[2] = 1.0;
}
// leave test mode
if (!re_initialise()) {
_i2c_sem->give();
return false;
}
_i2c_sem->give();
_initialised = true;
// perform an initial read
healthy = true;
read();
return success;
}
Have the same problem occasionally with v3.1rc5, new compass is not here yet to test.
HMC5883L Specsheet: http://www51.honeywell.com/aero/common/documents/myaerospacecatalog-documents/Defense_Brochures-documents/HMC5883L_3-Axis_Digital_Compass_IC.pdf
Page 2:
Characteristics Conditions Min Typical Max Units Self Test X & Y Axes
Z Axis±1.16
±1.08gauss X & Y & Z Axes (GN=5) +ve Bias
X & Y & Z Axes (GN=5) -ve Bias-575 243 LSB
Page 13:
Configuration Register B
The configuration register B for setting the device gain. CRB0 through CRB7 indicate bit locations, with CRB denoting the bits that are in the configuration register. CRB7 denotes the first bit of the data stream. The number in parenthesis indicates the default value of that bit. CRB default is 0x20.
CRB7 CRB6 CRB5 CRB4 CRB3 CRB2 CRB1 CRB0 GN2 (0) GN1 (0) GN0 (1) (0) (0) (0) (0) (0)
Note: the gain used for the compass initialisation in AP_Compass_HMC5843.cpp is: calibration_gain = 0x60;
0x60 = 01100000 011 is a GN of 3 giving us a gain of 660 (from the table on the same page)
Page 19:
Below is an example of how to adjust the “positive self” test limits basing on the gain setting:
- If Gain = 6, self test limits are: Low Limit = 243 * 330/390 = 206 High Limit = 575 * 330/390 = 487
- If Gain = 7, self test limits are: Low Limit = 243 * 230/390 = 143 High Limit = 575 * 230/390 = 339
Following the example above, the self test limits for Gain=3 is: Low Limit = 243 * 660/390 = 411 High Limit = 575 * 660/390 = 973
We're pretty sure that the datasheet is incorrect. Tridge did a bunch of testing when he original modified the expected output from the self-test to be 713 for the y axis and I did some more testing on two different APM2s and found that the current code produces a slightly more consistent mag field length.
I've checked in a change to relax the limits: https://github.com/diydrones/ardupilot/commit/84fdff4cd691c36f0196f80b893d03206dc64c95
I'm just waiting for any feedback from people before I close this issue and pull this commit into the ArduCopter-3.1 branch ahead of the AC3.1 official release.
ok, no comments and I think the code is correct and the datasheet incorrect so i'm closing this. I'll monitor for replies in case someone thinks I'm wrong, thanks!
I eventually received my second compass, installed it and have not had the problem with v3.1rc7. With the errant compass I had to change the one value to 1.35f otherwise 9/10 times the compass would not initialize. No error showed up but the copters heading was always 0 deg on startup and Loiter was terrible.
I was hoping James would respond by no sign of him.
Sorry to come in so late but I have been too busy to keep my eye on these matters. There seem to be two issues here, the first was the self-test response and the limits we impose and the second is the treatment of the x, y and z axes in the calibration. Randy I duplicated your test and came up with similar results. My suggested alteration to bring the calibration in line with the spec sheet gave an 11% variation in the magnitude of the mag field compared to only 5% with the standard code on my spare APM2.5. I am a little sceptical of declaring the manufacturers spec sheet incorrect based on such a small (albeit consistent!) sample but am quite happy to defer to your experience in this matter. With regard to the self test limits, I see no valid reason to impose any other limits than the ones expressly given in the spec sheet especially when Randy has already declared that the current limits are 'arbitrary'. If we test according to the spec sheet, at least folk would have grounds to return any new chip that failed.
Yes, it's surprising but it seems that the datasheet is incorrect. I'd have a hard time making a change that goes against the testing results we've done so I'm leaving the expected values for the y axis as they are in the code at the moment. I've increased the arbitrary limits so that should resolve the one or two people who were hitting the problem.