Compass Display and FXOS8700CQ

Dear All,

I am strugling with my christmas project.

Arduino with I2C FXOS8700CQ Sensor and 1.3" OLED Display with U8G2 library.
I can calibrate it, show flat magnetical heading.

But I cannot tilt compensate the compass heading with the accelerometer data.

The project is put as an attachment.

I was trying to use tilt compensation method as shown here

Any ideas?

Compass.zip (8.04 KB)

Fox82:
But I cannot tilt compensate the compass heading with the accelerometer data.

How do you know?

The code in the Instructable will need to be modified substantially for your sensor, as the sensor axes are aligned differently.

Did you make those modifications correctly?

Ok thanks. I will revise that.

Done!

First I had to calibrate the Magnetical Z axis. This I had forgott.

Then I could use this code:

     // ---------- 3D Compensated Compass Heading -----------------------


      accRaw[0] = FXO.accelData.x;
      accRaw[1] = FXO.accelData.y;
      accRaw[2] = FXO.accelData.z;

      magRaw[0] = -FXO.magData.x;
      magRaw[1] = FXO.magData.y;
      magRaw[2] = FXO.magData.z;


      headingraw =  atan2(magRaw[1], magRaw[0]) * RadToDeg;

        
        xM = magRaw[0];
        yM = magRaw[1];
        zM = magRaw[2];
        xG = accRaw[0];
        yG = accRaw[1];
        zG = accRaw[2];
      

      pitch = atan2(xG, sqrt(yG * yG + zG * zG));
      roll = atan2(yG, zG);
      roll_print = roll * RadToDeg;
      pitch_print = pitch * RadToDeg;


      xM2 = xM * cos(pitch) + zM * sin(pitch);
      yM2 = xM * sin(roll) * sin(pitch) + yM * cos(roll) - zM * sin(roll) * cos(pitch);
      compHeading = atan2(yM2, xM2) * RadToDeg;



      compHeading = compHeading + 90  ;   //Korrektur weil Sensor 90° auf der Platine eigebaut ist.
      if (compHeading <= 0) {//If the heading is negative (ranges from -1 to -180)
        compHeading = compHeading * -1; //Negate the heading
      }
      else if (compHeading > 0) {
        compHeading = 360 - compHeading;
      }

Good progress! For best results you need to calibrate all magnetometer axes.

Excellent tutorial here: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers