Code: Select all
#if defined(HMC5883)
magCal[ROLL] = 1160.0 / abs(magADC[ROLL]);
magCal[PITCH] = 1160.0 / abs(magADC[PITCH]);
magCal[YAW] = 1080.0 / abs(magADC[YAW]);
magADC for the different axis should here deliver values about 700. The actual values were about 1100. Therefore the following line is not processed correctly, since the measure 1100 are correct for the default configuration gain of 1.3 Ga:
i2c_writeReg(MAG_ADDRESS ,0x01 ,0x60 ); //Configuration Register B -- 011 00000 configuration gain 2.5Ga
I now introduced a 50 ms delay after that command, then I could read the expected values (I don't know, why this has to be put in, and why the delay must be such big)
Code: Select all
..
i2c_writeReg(MAG_ADDRESS ,0x01 ,0x60 ); //Configuration Register B -- 011 00000 configuration gain 2.5Ga
delay(50);
// force positiveBias
i2c_writeReg(MAG_ADDRESS ,0x00 ,0x71 ); //Configuration Register A -- 0 11 100 01 num samples: 8 ; output rate: 15Hz ; positive bias
..
and here:
Code: Select all
...
i2c_writeReg(MAG_ADDRESS ,0x01 ,0x20 ); //Configuration Register B -- 001 00000 configuration gain 1.3Ga
delay(50);
i2c_writeReg(MAG_ADDRESS ,0x00 ,0x70 ); //Configuration Register A -- 0 11 100 00 num samples: 8 ; output rate: 15Hz ; normal measurement mode
i2c_writeReg(MAG_ADDRESS ,0x02 ,0x00 ); //Mode register -- 000000 00 continuous Conversion Mode
...
This code works for me.
There is another not so fine code here:
magCal[ROLL] = 1160.0 / abs(magADC[ROLL]);
magCal[PITCH] = 1160.0 / abs(magADC[PITCH]);
magCal[YAW] = 1080.0 / abs(magADC[YAW]);
The different factor of 1080 has to be applied to the physical axis of the chip, not to the logical axis of the copter (define MAG_ORIENTATION has bee applied already). I didn't find a easy solution for this, but I think it's not so relevant, since it only leads to a problem when the chip is mounted vertically
