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

Fix wrong axis alignment #370

Closed
wants to merge 1 commit into from
Closed

Fix wrong axis alignment #370

wants to merge 1 commit into from

Conversation

maisonsmd
Copy link

magnetic sensor has Z axis inverted

@robertoostenveld
Copy link

I can confirm this is a problem, passing the inverted value of mz solves it.

@kriswiner
Copy link
Owner

kriswiner commented May 12, 2019 via email

@robertoostenveld
Copy link

hmm, I am currently not totally sure about my prior conformation. I noticed that - after making the change - the AHRS worked fine (from the top of my head it was NWU, which is a right-handed system just like NED and ENU), but that convergence was very slow. I now wonder whether that might be due to a remaining axis misalignment.

The MPU9250 is according to figure 4 and 5 in http://www.invensense.com/wp-content/uploads/2015/02/PS-MPU-9250A-01-v1.1.pdf

Screen Shot 2019-05-13 at 14 36 12

which for the break-out-board version from e.g. Ebay means

Screen Shot 2019-05-13 at 14 36 50

So keeping (x,y,z) for accelerometer and gyro as they are, and for the magnetometer taking (y,x,-z) seems correct. It also maintains the magnetometer axes in a right-handed coordinate system, whereas the original code changes from a right-handed to a left-handed coordinate system.

I will check it further and report back.

@maisonsmd
Copy link
Author

maisonsmd commented May 13, 2019

Hi @robertoostenveld !
I'm currently using the same board as yours.
Did you calib the accel and magetic sensor?
This is my magetic raw data without calibration, as you can see there a huge bias in each axis:
image
And after calibrating (scale & bias):
image
After calibrating accel and magnetic, I got the right angle.
So I'm pretty sure that inverting magnetic Z resolves problem.

The slow convergence is because Kp, Beta is not suitable for your sensor, I had to hand-changing them to find the best fit.
I also had to multiply gyroscope values to some constant (maybe its sensivity is not so correct).
image

After all that, I've got fast response, correct value.
The result is pretty same as this:
https://youtu.be/BXsGWoOMtmU

Hope this help!

@maisonsmd
Copy link
Author

@robertoostenveld Thinking about slow convergence, try setting gyro scale to highest (2000 deg/sec), because when you set it low (eg: 250deg/sec), it cannot measure fast rotation.

@kriswiner
Copy link
Owner

kriswiner commented May 13, 2019 via email

@maisonsmd
Copy link
Author

maisonsmd commented May 14, 2019

@kriswiner Update: I had to multiply gyro because I didn't calculate the resolution properly. Now I checked and it doesn't need to multiply anymore.
In fact, I don't need to change any factor (kp, beta) above and it reponses fast!

@robertoostenveld
Copy link

Just for reference, this pull request relates to issue #368 and pull request #369 is a duplicate of this one.

@robertoostenveld
Copy link

I have tested some variants of the sketch more.

If I change in my code the line that corresponds to this

MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, mz);

into

MahonyQuaternionUpdate(ax, ay, az, gx*PI/180.0f, gy*PI/180.0f, gz*PI/180.0f, my, mx, -mz); 

then the yaw, roll and pitch behave as I would expect them. Also they converge smoothly. The problematic convergence that I reported at the start of this comment was not a problem any more when I reverted to the default GScale, Ascale and Mmode values. So in summary, in my previous experimenting, I changed the scales and the minus-mz in one go, solving one, but introducing an unrelated issue. The suggested -mz still seems a good idea to me.

@kriswiner
Copy link
Owner

kriswiner commented May 14, 2019 via email

@robertoostenveld
Copy link

with testing sketches I mean checking with some additional serial console output that the orientations and signs of values for each of the nine axes is consistent with those specified in the invensense data sheet, prior to feeding them into the sensor fusion algorithm. I confirmed that they are consistent.

And it is indeed as you describe, the magnetometer values should be represented in a right-hand coordinate system which is consistent with that of the accelerometer and gyroscope.

The code in this line feeds the magnetometer values in the order (my, mx, mz) to the MahonyQuaternionUpdate function. Flipping mx and my in the input values, but keeping mz as it is, results in a left handed coordinate system. The proposed input arguments (my, mx, -mz) are in a right handed system and aligned with (ax, ay, az) and (gx, gy, gz).

Do with it what you wish.

@kriswiner
Copy link
Owner

kriswiner commented May 14, 2019 via email

@maisonsmd maisonsmd closed this May 13, 2024
@maisonsmd
Copy link
Author

Closing due to no update for the past 5 years.

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

Successfully merging this pull request may close these issues.

3 participants