AeroQuad/AeroQuad — pull request #228 by eelsirhc (2012-05-25 20:21:45+00:00)
Title: Compass scaling

Hi,

These changesets deal with multiplicative scaling of the magnetometer used in the compass. The problem I'm trying to solve is that the ratio of hdgX and hdgY are used to determine the Yaw based on orientation from magnetic north (stored in trueNorthHeading I think). But if the scaling of the magnetometer axes are not the same then the ratios get screwed up by not rescaling the observed values to the same range before using them in heading calculation.

For example. A perfect magnetometer, corrected for the offset bias in each axes, will predict a 45 degree heading if the size of the hdgX and hdgY vectors are the same (i.e. atan2(1,1)). But if one of the mag axes has a much reduced range (say due to metal obstruction, bad sensors) then the 45 degree angle is no longer on the 1:1 line of the unscaled mag.

When I use the Configurator software I get pretty similar ranges from the HMC5883L, but I don't think even in controlled conditions the full range of each mag axis was the same, so there will be a small error even after the offsets are removed.

My fix for this is to include the magScale values in the magnetometer reading just as they are done for accelerometer. In the changesets I've included the magScale parameters are all initialized to 1.0 (biases to 0.0 as before) and addresses in the EEPROM are allocated for the new values (requires a reset of the EEPROM data and recalibration).

I've also added in a new function called getMagnetometerData (an analogue of getMagnetometerRawData, but using the 'measuredMag ' value instead). I've also defined a new user define 'useMeasuredMag', which if enabled will output the measured (scaled + offset) mag fields to Serial instead of the Raw values. The only place this doesn't/ shouldn't happen is the case 'j' option that is used to read the raw sensor data to measure the magnetometer offsets.

I haven't added in code that would allow you to change the magScale values yet. I think these should be added to the same case used to set the Biases (case 'M'), but I if this is being used by the Configurator to set the offset values, adding extra fields would cause errors in that program. I would also support using another case option for the Scale fields but I couldn't find many free options.

Chris.