wollewald / MPU9250_WE

An Arduino library for the 9-axis accelerometer, gyroscope and magnetometer MPU9250 and MPU6500. It contains many example sketches make it easy to use.
https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1
MIT License
56 stars 26 forks source link

Feature: tilt adjusted compass output #3

Closed tmcadam closed 2 years ago

tmcadam commented 2 years ago

Thank you for this great library. It is the only one I have found for the mpu9250 that outputs pitch and roll correctly with my sensor.

I see you have magnetometer x,y,z output. I was wondering if a function can be added to output a tilt adjusted compass heading from these values?

Thanks

ivicajan commented 2 years ago

It should be easy, based on available info you should add something like this after you read pitch, roll and mag values. You'll need to define Declination for your location, snip of the code based on the available example at this github: ..... pitch = myMPU9250.getPitch(); roll = myMPU9250.getRoll();

float Xh = magValue.xcos(pitch/180Pi) + magValue.ysin(roll/180Pi)sin(pitch/180Pi) - \ magValue.zcos(roll/180Pi)sin(pitch/180Pi); float Yh = magValue.ycos(roll/180Pi) + magValue.zsin(roll/180Pi); Xh = Xh 0.9 + magValue.x 0.1; Yh = Yh 0.9 + magValue.y 0.1; float Heading = atan2(Xh, Yh) * 180 / Pi; Heading += Declination ;

if (Heading > 360.0) Heading -= 360.0; if (Heading < 0.0) Heading += 360.0; // ----- Allow for under/overflow if (Heading < 0) Heading += 360 ; if (Heading >= 360) Heading -= 360 ;

Serial.println(Heading);

wollewald commented 2 years ago

Hi @tmcadam , @ivicajan ,

to be very honest I have a knowledge gap in this matter. I don't know what the equations above mean. I know that other libraries connect the different sensor values using quaternions, like here:

https://github.com/hideakitai/MPU9250/blob/master/MPU9250/QuaternionFilter.h

Is this the kind of thing you want? Not sure when I will find the time to dig into this. I haven't found an easy explanation yet (maybe because it is not easy?) Without a complete understandig it will be difficult to implement this.

Always happy about contributors....

wollewald commented 2 years ago

@tmcadam , @ivicajan I am closing this issue now. At least for the moment I do not have the capacity to implement this. If it's so easy as ivicajan says, the I am happy if someone wants to become a contributor!