kriswiner / MPU9250

Arduino sketches for MPU9250 9DoF with AHRS sensor fusion
1.02k stars 469 forks source link

Magnetometer and Madgwick or Mahony does not work in case of tilting #180

Closed mindchanger82 closed 6 years ago

mindchanger82 commented 6 years ago

Dear Chris. First of all thanks a lot for your great work for all in this esp and 9250 things. Great Job.

I'm facing a probem right now. I successfully get the imu to run with your example codes but it seems, that the filter functions have no influence to the mag data when i turn the device over some axes. EG I mountet the imu to a robotic lawn mower. Everytime the mower moves over a small hill it moves to somewhere but not to the should direction. I tested it on a breakout board inside with another imu and esp32 device with the same result. Here is my function to print out the heading degrees. I placed it in the loop, directly under the serial prints of mx,my,mz ` float heading = atan2(my, mx); float headingDegrees=0; float declinationAngle = 30.45/1000; heading += declinationAngle;

if(heading < 0)
  heading += 2*PI;

if(heading > 2*PI)
  heading -= 2*PI;

// Convert radians to degrees for readability.
//float headingDegrees = 360-(heading * 180/M_PI);
headingDegrees = heading * 180/M_PI;
headingDegrees = 360.00 - headingDegrees;
Serial.print("Heading = "); Serial.print( (int)headingDegrees ); Serial.println(" Degrees!");`

So everytime i softly move the device (yaw or pitch) the mag data changes a lot to unsensful values.

Have you got any idea? Is my code wrong for this case?

PS: I used your latest "MPU9250_MS5637_AHRS_t3.ino" and removed all the display stuff.

Thanks in advance and with best regards

Marius

kriswiner commented 6 years ago

"float heading = atan2(my, mx);", funny!

Why aren't you using the sensor fusion to get heading?

And did you calibrate your sensors to remove offset bias?

On Tue, Aug 29, 2017 at 10:01 PM, mindchanger82 notifications@github.com wrote:

Dear Chris. First of all thanks a lot for your great work for all in this esp and 9250 things. Great Job.

I'm facing a probem right now. I successfully get the imu to run with your example codes but it seems, that the filter functions have no influence to the mag data when i turn the device over some axes. EG I mountet the imu to a robotic lawn mower. Everytime the mower moves over a small hill it moves to somewhere but not to the should direction. I tested it on a breakout board inside with another imu and esp32 device with the same result. Here is my function to print out the heading degrees. I placed it in the loop, directly under the serial prints of mx,my,mz ` float heading = atan2(my, mx); float headingDegrees=0; float declinationAngle = 30.45/1000; heading += declinationAngle;

if(heading < 0) heading += 2*PI;

if(heading > 2PI) heading -= 2PI;

// Convert radians to degrees for readability. //float headingDegrees = 360-(heading 180/M_PI); headingDegrees = heading 180/M_PI; headingDegrees = 360.00 - headingDegrees; Serial.print("Heading = "); Serial.print( (int)headingDegrees ); Serial.println(" Degrees!");`

So everytime i softly move the device (yaw or pitch) the mag data changes a lot to unsensful values.

Have you got any idea? Is my code wrong for this case?

PS: I used your latest "MPU9250_MS5637_AHRS_t3.ino" and removed all the display stuff.

Thanks in advance and with best regards

Marius

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/180, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qsTdzx9ugFPTv7JusWDFKHDWRUjZks5sdOyNgaJpZM4PG3oJ .

mindchanger82 commented 6 years ago

Hi Chris.

Thanks for your reply. That is exactly my problem. Either I don't know how to use the sensor fusion, that's my goal to achive, nor I don't exactly know how to calibrate the sensors. I just used your mag calibration with figure 8 waving.

TediumRemedy commented 6 years ago

@mindchanger82 the idea is to feed accelerometer, magnetometer and gyroscope measurements into the madgwickQuaternionUpdate, which, when run, fills out the q[0]...q[3] array (quaternion), which you then can convert into 3 angles - heading (yaw), tilt around 1 axis (pitch), tilt around another axis (roll) using simple formulas https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles madgwickQuaternionUpdate is the sensor fusion algorithm which turns raw sensor measurements into more or less reliable estimate of the Euler angles.

mindchanger82 commented 6 years ago

Dear TediumRemedy. That means: /from wiki/ // yaw (z-axis rotation) double siny = +2.0 (q.w() q.z() + q.x() q.y()); double cosy = +1.0 - 2.0 (q.y() q.y() + q.z() q.z());
yaw = atan2(siny, cosy);

but from where do I know which arry pos (q[0] or q[xyz] stands for q.w or q.z ? Sorry but I'm not really seeing through this.

TediumRemedy commented 6 years ago

@mindchanger82 you can use pieces of code from: https://github.com/kriswiner/MPU9250/blob/master/MPU9250_MS5637_AHRS_t3.ino

// yaw = atan2f(2.0f (q[1] q[2] + q[0] q[3]), q[0] q[0] + q[1] q[1] - q[2] q[2] - q[3] q[3]);
// pitch = -asinf(2.0f
(q[1] q[3] - q[0] q[2])); // roll = atan2f(2.0f (q[0] q[1] + q[2] q[3]), q[0] q[0] - q[1] q[1] - q[2] q[2] + q[3] q[3]); // pitch = 180.0f / PI; // yaw = 180.0f / PI; // yaw += 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 // if(yaw < 0) yaw += 360.0f; // Ensure yaw stays between 0 and 360 // roll = 180.0f / PI;

and you get yaw/pitch/roll in degrees (can be negative).

The original code uses another conversion, apparently to save Arduino time on mathematical operations:

a12 = 2.0f (q[1] q[2] + q[0] q[3]); a22 = q[0] q[0] + q[1] q[1] - q[2] q[2] - q[3] q[3]; a31 = 2.0f (q[0] q[1] + q[2] q[3]); a32 = 2.0f (q[1] q[3] - q[0] q[2]); a33 = q[0] q[0] - q[1] q[1] - q[2] q[2] + q[3] q[3]; pitch = -asinf(a32); roll = atan2f(a31, a33); yaw = atan2f(a12, a22); pitch = 180.0f / PI; yaw = 180.0f / PI; yaw += 13.8f; // Declination at Danville, California is 13 degrees 48 minutes and 47 seconds on 2014-04-04 if(yaw < 0) yaw += 360.0f; // Ensure yaw stays between 0 and 360 roll = 180.0f / PI; lin_ax = ax + a31; lin_ay = ay + a32; lin_az = az - a33;

mindchanger82 commented 6 years ago

woaaaaah. Thank you so much.

I used, as you said, the following lines: yaw = atan2f(2.0f * (q[1] * q[2] + q[0] * q[3]), q[0] * q[0] + q[1] * q[1] - q[2] * q[2] - q[3] * q[3]); yaw *= 180.0f / PI; yaw += 1.44f; // Declination at Düsseldorf Germany if(yaw < 0) yaw += 360.0f; // Ensure yaw stays between 0 and 360 The data is now very stable, but false :-) haha, real south is 279 degrees with the code... mhhhhhh where is the error.

TediumRemedy commented 6 years ago

That's a good question. You could "calibrate" it by just adding a number of degrees to yaw that make yaw 0º at the direction where a real compass points to north, instead of adding the declination to yaw. I am getting a wrong geographical heading value too (I don't need absolute heading though). Maybe Kris might know why this is happening?

kriswiner commented 6 years ago

The other formulation is to calculate members of the rotation matrix from which gravity and linear acceleration are easily estimated.

On Wed, Aug 30, 2017 at 9:08 AM, mindchanger82 notifications@github.com wrote:

woaaaaah. Thank you so much.

I used, as you said, the following lines: yaw = atan2f(2.0f (q[1] q[2] + q[0] q[3]), q[0] q[0] + q[1] * q[1]

  • q[2] q[2] - q[3] q[3]); yaw *= 180.0f / PI; yaw += 1.44f; // Declination at Düsseldorf Germany if(yaw < 0) yaw += 360.0f; // Ensure yaw stays between 0 and 360 The data is now very stable, but false :-) haha, real south is 279 degrees with the code... mhhhhhh where is the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/180#issuecomment-326040117, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qp8cIZQWR3UblP6lHi2xVg7qk2tSks5sdYkRgaJpZM4PG3oJ .

kriswiner commented 6 years ago

The Madgwick filter is NED, so choose which accel axis you want for North, and then, make sure you feed in sensor data into the Madwick filter accordingly. I usually choose accel x as North and also negate the accel data, this gives me a true North reading along the accel x-axis modulo the declination.

My colleague says Madgwick is ENU, so maybe this is why negating the accel data works better.

But the point is, it needs to be consistent.

On Wed, Aug 30, 2017 at 9:21 AM, Kris Winer tleracorp@gmail.com wrote:

The other formulation is to calculate members of the rotation matrix from which gravity and linear acceleration are easily estimated.

On Wed, Aug 30, 2017 at 9:08 AM, mindchanger82 notifications@github.com wrote:

woaaaaah. Thank you so much.

I used, as you said, the following lines: yaw = atan2f(2.0f (q[1] q[2] + q[0] q[3]), q[0] q[0] + q[1] q[1] - q[2] q[2] - q[3] q[3]); yaw = 180.0f / PI; yaw += 1.44f; // Declination at Düsseldorf Germany if(yaw < 0) yaw += 360.0f; // Ensure yaw stays between 0 and 360 The data is now very stable, but false :-) haha, real south is 279 degrees with the code... mhhhhhh where is the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/180#issuecomment-326040117, or mute the thread https://github.com/notifications/unsubscribe-auth/AGY1qp8cIZQWR3UblP6lHi2xVg7qk2tSks5sdYkRgaJpZM4PG3oJ .

TediumRemedy commented 6 years ago

Thanks Kris

mindchanger82 commented 6 years ago

Thanks Kris and TediumRemedy. Everything works fine now.

henzim commented 6 years ago

@mindchanger82 glad you got your setup working! I am also faced with "yaw" problems. Could you please have a look at [https://github.com/kriswiner/MPU9250/issues/182](my issue) and confirm you don't have this problem in your setup. In brief, the problem is that the yaw angle "misses" a couple of degrees when rotating the setup, e.g., I rotate the MPU by 90° and the yaw changes by 80°... Thanks alot!

mindchanger82 commented 6 years ago

Dear henzim. Sorry for my late reply. I'm on a biz trip and I'll be back on sunday. I will try to find out if I have the same prob too. Thanks

henzim commented 6 years ago

Thanks!

mindchanger82 commented 6 years ago

Hi Henzim. Sorry for my late replay. I tested it the last weekend and and I don't get these kind of errors. But keep in mind that I use a esp32 and it seems to be a "bit faster" than the arduino mini pro.

Best regards

henzim commented 6 years ago

Hey, no problem, thanks a lot for testing!It really seems to depend a lot on the CPU performance.Although I am still not quite convinced... I did try sending the values to the PC and doing the integration there but the results were still far from perfect.Now I'll try my luck with this powerful beauty: https://www.tindie.com/products/onehorse/nrf52832-development-board/

-------- Ursprüngliche Nachricht -------- Von: mindchanger82 notifications@github.com Datum: 17.10.17 06:26 (GMT+01:00) An: kriswiner/MPU9250 MPU9250@noreply.github.com Cc: henzim henrik@zimcs.de, Comment comment@noreply.github.com Betreff: Re: [kriswiner/MPU9250] Magnetometer and Madgwick or Mahony does not   work in case of tilting (#180)

Hi Henzim. Sorry for my late replay.

I tested it the last weekend and and I don't get these kind of errors. But keep in mind that I use a esp32 and it seems to be a "bit faster" than the arduino mini pro. Best regards

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/kriswiner/MPU9250","title":"kriswiner/MPU9250","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/kriswiner/MPU9250"}},"updates":{"snippets":[{"icon":"PERSON","message":"@mindchanger82 in #180: Hi Henzim. Sorry for my late replay.\r\nI tested it the last weekend and and I don't get these kind of errors. But keep in mind that I use a esp32 and it seems to be a \"bit faster\" than the arduino mini pro.\r\n\r\nBest regards"}],"action":{"name":"View Issue","url":"https://github.com/kriswiner/MPU9250/issues/180#issuecomment-337114326"}}}

kriswiner commented 1 year ago

What MCU are you using?

On Tue, Nov 1, 2022 at 6:30 AM Kaijudo2 @.***> wrote:

I am having issue with 'TwoWire i2c'. Program says "no matching function for call to 'TwoWire::TwoWire()'". Would really appreciate if anyone could help me!

— Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/180#issuecomment-1298513476, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKVJDGECTRPOMO7YHH3WGELP7ANCNFSM4DY3PIEQ . You are receiving this because you were mentioned.Message ID: @.***>

Kaijudo2 commented 1 year ago

I am using adafruit esp32 feather

What MCU are you using? On Tue, Nov 1, 2022 at 6:30 AM Kaijudo2 @.> wrote: I am having issue with 'TwoWire i2c'. Program says "no matching function for call to 'TwoWire::TwoWire()'". Would really appreciate if anyone could help me! — Reply to this email directly, view it on GitHub <#180 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKVJDGECTRPOMO7YHH3WGELP7ANCNFSM4DY3PIEQ . You are receiving this because you were mentioned.Message ID: @.>

kriswiner commented 1 year ago

See https://github.com/kriswiner/ESP32

On Tue, Nov 1, 2022 at 9:37 AM Kaijudo2 @.***> wrote:

I am using adafruit esp32 feather

What MCU are you using? … <#m-2263717153512973863> On Tue, Nov 1, 2022 at 6:30 AM Kaijudo2 @.> wrote: I am having issue with 'TwoWire i2c'. Program says "no matching function for call to 'TwoWire::TwoWire()'". Would really appreciate if anyone could help me! — Reply to this email directly, view it on GitHub <#180 (comment) https://github.com/kriswiner/MPU9250/issues/180#issuecomment-1298513476>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKVJDGECTRPOMO7YHH3WGELP7ANCNFSM4DY3PIEQ https://github.com/notifications/unsubscribe-auth/ABTDLKVJDGECTRPOMO7YHH3WGELP7ANCNFSM4DY3PIEQ . You are receiving this because you were mentioned.Message ID: @.>

— Reply to this email directly, view it on GitHub https://github.com/kriswiner/MPU9250/issues/180#issuecomment-1298809067, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTDLKTUKIB7AN7UYPTDGA3WGFBOHANCNFSM4DY3PIEQ . You are receiving this because you were mentioned.Message ID: @.***>