xioTechnologies / Fusion

MIT License
928 stars 230 forks source link

Magnetic calibration model does not match MATLAB and others #103

Closed Overff closed 9 months ago

Overff commented 1 year ago

Hello. I spent a lot of time to setup and run up that code. Unfortunately with no success. My goal is to calculate heading by using MAG and ACC. I used FusionCalibrationMagnetic() in that purpose.

In order to calibrate MAG and ACC i tied to use two different ways: 1. MagMaster app and 2. Magneto. These applications generate transformation matrix (soft iron) and bias (hard iron offset). Then I used outputs from these applications in your code (check out Annex1) So I assume that FusionCalibrationMagnetic() does not work properly with your calibration model (mc = Smu - h). Fortunately, if use regular model (mc = S(mu - h)) it works better but actually not stable.

mc is the calibrated magnetometer measurement and return value mu is the uncalibrated magnetometer measurement and uncalibrated argument S is the soft iron matrix and softIronMatrix argument h is the hard iron offset vector and hardIronOffset argument

So, I assumed that code does not work properly (include yaw, roll, pitch). Visualization by using processing confirm my assumption. My setup: teensy board and different IMUs(9250, Gy86, Gy91 and etc)

It would be great to see/read applicable instruction how to it use.

Annex1 (example of my code with soft and hard iron):

//Accelerometer transformation matrix  
    const FusionMatrix accelerometerMisalignment = {0.997900, -0.013244, -0.000926, -0.013244, 0.998071, -0.002810, -0.000926, -0.002810, 0.972633};
    const FusionVector accelerometerSensitivity = {1.0, 1.0, 1.0};
    const FusionVector accelerometerOffset = {-0.001079, 0.009427, -0.020567}; //Bias

//Magnetometer transformation matrix    
    const FusionMatrix softIronMatrix = {0.076813, 0.000005, -0.000037, 0.000005, 0.073755, -0.002360, -0.000037, -0.002360, 0.084221};
    const FusionVector hardIronOffset = {64.808407, 277.759814, -536.878619}; //Bias
xioTechnologies commented 1 year ago

The function FusionCalibrationMagnetic implements the expression mc = Smu - h, as described in the README. This implementation is as follows (see line 37).

static inline FusionVector FusionCalibrationMagnetic(const FusionVector uncalibrated, const FusionMatrix softIronMatrix, const FusionVector hardIronOffset) {
    return FusionVectorSubtract(FusionMatrixMultiplyVector(softIronMatrix, uncalibrated), hardIronOffset);
}

If you believe that the code does not work then please demonstrate this in a way that others can replaciate.

PetSven commented 10 months ago

I am also wondering if FusionCalibrationMagnetic is wrong. Matlab uses the equation: IMG_1677 Vectornav uses the equation: IMG_1678

xioTechnologies commented 10 months ago

Both $m = Su - h$ and $m = S(u - h)$ are valid but $m = S(u - h)$ does seem to be in more widespread use. The choice of $m = Su - h$ in Fusion is not arbitrary. Decoupling $S$ and $h$ provides several practical advantages.

It is also my understanding that $m = Su - h$ better represents the physical phenomena concerned. For example, if we were to start with a calibrated magnetometer providing its measurement $m$, and then attach a magnet which had an intensity of $n$ at the location of the magnetometer, then we would have introduced a new hard-iron offset and the measurement $m$ would become $m + n$. If we wanted to update our calibration to correct for this then we would replace $h$ with $h + n$. In this case, $h$ directly represents the physical hard-iron offset.

If we were to instead use $m = S(u - h)$ then the correction would require we replace $h$ with $h + S^{-1}n$. In this case, $h$ does not directly represent the physical hard-iron offset.

That said, conventions are important. If there is a general consensus for $m = S(u - h)$ then it may render arguments for the alternative moot. I would be interested to hear other's thoughts on this.

xioTechnologies commented 9 months ago

The magnetic calibration model has been updated to $m=S(u-h)$ in the latest release, v1.2.0.

Overff commented 6 months ago

Thy for update!

AndreyDiDev commented 1 month ago

@Overff how did you get the accelerometer transformation matrices? I couldn't find MagMaster, but both seem for the Magnetometer ?