Madgwick sensor fusion with Sense Rev2 - heading straying to random values, bmi270/magnetometer axes wrong?

I'm using the built-in BMI270 accelerometer+gyroscope and BMM150 magnetometer on the Rev 2 version of the Nano 33 BLE Sense, as a 9 axis IMU, using the MadgwickAHRS library.

Firstly, it seems like the BMI270 chip is on a different axis than printed on the board - the X and Y axes are switched. I've gotten around this by just using different inputs - I swapped the X and Y axes and then rotated the input 90 degrees around the Z axis, by using -gx, gy, gz, -ax, ay, az instead of gx, gy, gz, ax, ay, az. When plugging only the accelerometer and gyroscope values into the Madgwick sensor fusion algorithm, the output is reasonable and fairly accurate.

However, using the magnetometer (which has been calibrated) breaks these readings - the heading constantly changes, slowly straying toward arbitrary values, usually between 180 and 270. This is greatly exaggerated by changing the Arduino's pitch or roll away from 0.

I'm using filter.update(-gx, gy, gz, -ax, ay, az, mx, my, mz);, where "filter" is the Madgwick object. The pitch and roll function somewhat well, though I suspect this is just because the accelerometer + gyroscope overpower the magnetometer's input. (directions defined by: if the arduino is a thin, long plane, I'm piloting it from the top of the processor)

I've tried several different configurations of the magnetometer's input, but none of these have given me reasonable output:

// -my mx mz: drifts
// my -mx mz: drifts
// mx my mz: drifts sometimes
// -mx my mz: seems slightly better? but screws up some headings and when z involved
// -mx my -mz: doesn't fix the above issues
// -mx -my -mz: vaguely ok ish but drifting exaggerated by tilting down

Does the magnetometer just use weird axes that I haven't tested for yet? I'll try using a magnet to test the different axes, and if I find anything notable I'll comment back on this thread.

filter.update(-gx, -gy, -gz, ax, ay, az, my, -mx, -mz);
Don't know why. Don't know how.

It is absolutely critical that the gyro, accelerometer and magnetometer all have right handed coordinate systems in the same axial orientation.

On many IMUs, the gyro/acc have one system and the magnetometer has another. The ICM-20948 is one such example (see below). For that IMU, the correct call for either the Mahony or Madgwick filter in the code I use looks like this:

    // reconcile magnetometer and accelerometer axes. X axis points magnetic North for yaw = 0

    Mxyz[1] = -Mxyz[1]; //reflect Y and Z
    Mxyz[2] = -Mxyz[2]; //must be done after offsets & scales applied to raw data
    now = micros();
    deltat = (now - last) * 1.0e-6; //seconds since last update
    last = now;
    MahonyQuaternionUpdate(Axyz[0], Axyz[1], Axyz[2], Gxyz[0], Gxyz[1], Gxyz[2],
                           Mxyz[0], Mxyz[1], Mxyz[2], deltat);

Capture

Check the data sheet for your sensors to determine the proper alignment. It seems unlikely that changing the signs of the gyro contribution, as shown in the line of code you posted, is correct.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.