Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Madgwick Algorithm With Magnetometer: Issues in True North Detection #495

Open
gafa7758 opened this issue Dec 20, 2024 · 5 comments
Open

Comments

@gafa7758
Copy link

Hello,

I am currently calculating quaternions using the Madgwick algorithm with magnetometer data. The calculation works well, as the quaternion values change correctly when the sensor is rotated. I have verified the results using a quaternion calculator and visualized them with a 3D cube model.

However, I am encountering an issue with the qz value (yaw/heading).
When the sensor is powered on while lying flat, with the magnetometer's X-axis pointing north, the initial quaternion is (1, 0, 0, 0). If I power off the sensor, rotate it 90 degrees about the Z-axis (so the magnetometer's X-axis points west), and power it back on, the quaternion remains (1, 0, 0, 0).

This behavior seems incorrect since the magnetometer readings have to change after the rotation. I also tested rotating the sensor so that the magnetometer's X-axis points south or east, but the quaternion still does not change (still 1, 0, 0,0) when the program is restarted.

Could I have missed something that prevents the quaternion's Z-axis rotation from updating properly, even after changing the sensor's position and restarting the system (turning off the sensor, adjusting its position, and turning it back on)?

Note: Restarting means turning off the sensor, adjusting its position, and then powering it back on.

Thank you for your attention.

@kriswiner
Copy link
Owner

kriswiner commented Dec 20, 2024 via email

@gafa7758
Copy link
Author

gafa7758 commented Dec 20, 2024

How are you calibrating the three sensors?

On Fri, Dec 20, 2024 at 12:15 AM gafa7758 @.> wrote: Hello, I am currently calculating quaternions using the Madgwick algorithm with magnetometer data. The calculation works well, as the quaternion values change correctly when the sensor is rotated. I have verified the results using a quaternion calculator and visualized them with a 3D cube model. However, I am encountering an issue with the qz value (yaw/heading). When the sensor is powered on while lying flat, with the magnetometer's X-axis pointing north, the initial quaternion is (1, 0, 0, 0). If I power off the sensor, rotate it 90 degrees about the Z-axis (so the magnetometer's X-axis points west), and power it back on, the quaternion remains (1, 0, 0, 0). This behavior seems incorrect since the magnetometer readings have to change after the rotation. I also tested rotating the sensor so that the magnetometer's X-axis points south or east, but the quaternion still does not change (still 1, 0, 0,0) when the program is restarted. Could I have missed something that prevents the quaternion's Z-axis rotation from updating properly, even after changing the sensor's position and restarting the system (turning off the sensor, adjusting its position, and turning it back on)? Note: Restarting means turning off the sensor, adjusting its position, and then powering it back on. Thank you for your attention. — Reply to this email directly, view it on GitHub <#495>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKQXRA777DBNYNECP232GPGYXAVCNFSM6AAAAABT6UDX5OVHI2DSMVQWIX3LMV43ASLTON2WKOZSG42TEMJYHE4TOMI . You are receiving this because you are subscribed to this thread.Message ID: @.>

First of all, I would like to thank you, for the respon Sir
For calibration, I used bias error correction for the gyroscope and soft iron and hard iron calibration for the magnetometer. I did not calibrate the accelerometer because I believe its values are already good enough. Below are the equations I used in the code for calibration.

  1. Gyroscope Calibration

     //Finding the total value with a certain number of samples.
     avr_gyroX = avr_gyroX + gyroX;
     avr_gyroY = avr_gyroY + gyroY;
     avr_gyroZ = avr_gyroZ + gyroZ;
     
     //Finding the offset value by dividing the total value by the total number of 
     //samples
     offset_gyroX = avr_gyroX/sample;
     offset_gyroY = avr_gyroY/sample;
     offset_gyroZ = avr_gyroZ/sample;
    

    Result:

       Sample: 752
    
       Average X: -227.23
       Average Y: 825.67
       Average Z: 825.24
    
       Offset X: -0.30
       Offset Y: 1.10
       Offset Z: 1.10
    
       Gyro X Calibrated: 0.03
       Gyro Y Calibrated: -0.00
       Gyro Z Calibrated: -0.03
    
  2. Magnetometer Calibration

    //hard iron
     offset_x = (max_magX + min_magX)/2;
     offset_y = (max_magY + min_magY)/2;
     offset_z = (max_magZ + min_magZ)/2;
    
     //soft iron
     avg_delta_x = (max_magX - min_magX)/2;
     avg_delta_y = (max_magY - min_magY)/2;
     avg_delta_z = (max_magZ - min_magZ)/2;
    
     avg_delta = (avg_delta_x + avg_delta_y + avg_delta_z)/3;
    
     //Scale
     scale_x = avg_delta/avg_delta_x;
     scale_y = avg_delta/avg_delta_y;
     scale_z = avg_delta/avg_delta_z;
    

    Result:

     avg_delta: 46.44
    
     Max X: 89.72	Min X: 6.30
     Max Y: 41.71	Min Y: -42.46
     Max Z: -63.17	Min Z: -174.19
    
     Offset X: 48.01
     Offset Y: -0.38
     Offset Z: -118.68
    
     Scale X: 1.11
     Scale Y: 1.10
     Scale Z: 0.84
    
  3. Example of accelerometer value that i think it's already proper value, so i decide didn't calibrate it.
    ax: -0.01, ay: 0.04, az: 0.97 g
    ax: -0.02, ay: 0.03, az: 0.98 g
    ax: 0.01, ay:0.04, az: 0.98 g

So, the calibration I did is like that, do you think it’s correct or not? Here, I am using the 9DoF Razor IMU sensor from SparkFun to get raw data from each sensor, and it is done in real-time.
I have also uploaded the code and a short video related to the simulation that might help in understanding the issue I am facing. If you are willing to visit it, you can open my GitHub link: https://github.com/gafa7758/Quaternion_heading_issue.git

Thank you

@kriswiner
Copy link
Owner

kriswiner commented Dec 20, 2024 via email

@gafa7758
Copy link
Author

Hi Sir, thank you. My quaternion values are now changing correctly when I face the sensor towards the south, west, or east (it no longer stays at 1,0,0,0 continuously when program is first run) after you suggested using the Mahony algorithm. It is quite stable for flat positions and rotations around the Z-axis. However, when I make sequential changes to the pitch, roll, and yaw values, the values sometimes continue to change even after I stop moving the sensor. Additionally, could you provide me with references to learn more in-depth about the Mahony algorithm? I am using the algorithm from this code: https://github.com/PaulStoffregen/MahonyAHRS/blob/master/src/MahonyAHRS.cpp

As for the magnetometer calibration, I have followed the steps described in the article you shared, and it matches what I have been doing. I have also tried recalibrating multiple times, but the result remains the same (the quaternion values always start at 1,0,0,0 when the program is first run). Is there any other way to calibrate the magnetometer? Thank you

@kriswiner
Copy link
Owner

kriswiner commented Dec 24, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants