thomaspasser / q-mekf

C++ implementation of the Quaternion Multiplicative Extended Kalman Filter (Q-MEKF).
MIT License
6 stars 0 forks source link

Axis orientation #2

Open mgrouch opened 2 months ago

mgrouch commented 2 months ago

It's not clear how X Y Z axis are oriented. Usually IMU returns accel Z in fractions of G from 0 to 1. In example it is negative 1. It gets really confusing how to use it with real IMUs.

Some Arduino example for esp32 would help.

I'm playing with it on m5stack MPU6886 with

https://github.com/thecountoftuscany/PyTeapot-Quaternion-Euler-cube-rotation

I have Mahony example for it which works fine, however this filter always produces rotations around some axis. They do not look like Z drift in lack of magnetometer.

Thanks

mgrouch commented 2 months ago

Do I need to swap X and Y and negate Z for angular velocity and acceleration?

Thanks

thomaspasser commented 2 months ago

Hello, thanks for you question.

Have you initialized the filter with the initialize_from_acc_mag method? It properly initializes the two reference vectors, v1ref and v2ref as the gravity and magnetic reference, respectively.

The filter provides a pose in the NED (north, east, down) reference frame. The initialization method calculates the reference vectors in the NED frame. In the NED frame, an accelerometer at standstill (and with no rotation) on the ground will measure [0,0, -1 g], since it measures proper acceleration, and relative to free-fall the object is accelerating upwards at 1g.

If your accelerometer, gyroscope and magnetometer measurements are all in the same body frame, you should be able to provide them directly to all the filter methods. The units of the accelerometer (g or m/s^2) and magnetometer does not matter, as long as you are consistent. However, your gyroscope input has to be in rad/s.

If you are unable to make it work, feel free to share the code and data and I will gladly help you out.

mgrouch commented 2 months ago

I was able to make it work in my project. however I got better results with just Mahony filter. It worked better for my goals. Somehow this filter introduced bigger bias on accelerometer side while calculating vertical acceleration and it was impacting the convergence speed of my frequency estimation.

Anyway the code of my project is here. This filter can be enabled there too (but it’s disabled by default). I might use it in other projects when I start building a compass and use magnetometer too.

https://github.com/bareboat-necessities/bbn-wave-period-esp32

Thanks!

thomaspasser commented 1 month ago

Good to hear that you got it working.

Your issue sounds like something that could be solved by tuning the filter parameters, did you attempt to do so? The Mahony filter is often prefered because it is easier to tune (has fewer parameters), but it should be inferior to a well-tuned Kalman Filter.

Specifically, there are the parameters to the constructor of the class (sigma_a, sigma_g, Pq0, Pb0).

There are also some parameters that I have not had the time to expose, that is the terms 1e-12 in QuaternionMEKF::initialize_Q. These affect the covariance of the random walk model for the gyroscope bias.