Adafruit 10-DOF - Calibration and Filtering

I thought I might just upload some code since I took along time to figure this one out. I acquired a 10-DOF IMU, which has a L3GD20H, LSM303 and BMP180, from Adafruit which does not have support anymore and I desperately wanted to make a compass. So I found this post:

With this I managed to properly calibrate my IMU. However, I wasn’t happy with the heading indicator. So I saw that Paul Stoffregen has made a Mahony and Madgwick filter. What I realized though is that it is not compatible with all IMU’s, so with some tweaks I managed to make it compatible with the 10-DOF.

So in case anyone is interested, here it is.

Please note: I am just a beginner and I just want to help anyone that has the same headache as I had trying to figure this one out. Also, if there is anyway to make it even better, let me know ;).

10DOF_Filter.ino (2.7 KB)

Lines like the following invert the handedness of the measurements, which if not correct, leads to incorrect or nonsensical orientation angles. Since the LSM303DLHC raw data are right handed to begin with, why do you do this?

If you have a good reason, you should explain why in the program comments.

ax = -1*ax_cal/100.00;

it is not compatible with all IMU's

Not true. You simply have to present IMU data that are properly scaled and in a suitable, correctly aligned right handed coordinate system. Both the Madgwick and Mahony filters normalize the magnetometer and accelerometer data, so the overall scale factors (that is, the units) in those cases are irrelevant. The gyro rate data MUST be in radians/second.

To make a tilt compensated compass, Pololu's vector code is much simpler, faster and more accurate than either the Madgwick or Mahony filter.

Hi jremington, thanks for looking over the code!

Not true. You simply have to present IMU data that are properly scaled and in a suitable, correctly aligned right handed coordinate system.

I meant the module with that specific library. I think the library was intended for the Arduino 101. I suppose the CurieIMU library already has corrected values for the filter. However, I am not too sure.

Since the LSM303DLHC raw data are right handed to begin with, why do you do this?

Whilst testing everything, I realized that a positive pitch showed negative values, so I corrected this by inverting the axis. This also included the gyros with its rotations.

...leads to incorrect or nonsensical orientation angles.

This did happen but the tests look good with the inverts.

The gyro rate data MUST be in radians/second.

The Madgwick and Mahony library converts the deg in rad. So, you can either delete this conversion in the library or convert the gyroscope to degrees per second.

To make a tilt compensated compass, Pololu's vector code is much simpler, faster and more accurate than either the Madgwick or Mahony filter.

What kind off bothers me is that, how can a compass that uses only the magnetometer (okay - tilt compensated) be better than a magnetometer fused with the gyroscope? Also, I am using the Teensy 4.1.

p.s. I also tested this with a BNO080. It works fine aswell.

a positive pitch showed negative values

Much better just to change the sign of the pitch. There is no universal agreement on what constitutes a positive rotation angle. On the other hand, there IS a universal agreement on what constitutes a right handed coordinate system, and changing the sign of the raw data on one axis leads to a left handed coordinate system.

how can a compass that uses only the magnetometer (okay - tilt compensated) be better than a magnetometer fused with the gyroscope

The gyro does nothing but add noise, when the sensor is used as a compass. The gyro is useful primarily for correcting the accelerometer measurements (that is, the estimated direction of "down") when the sensor is rotating.

Incidentally, some time ago I posted a Mahony filter for the LSM303/L3DG20 sensor combination on Github, which includes code for sensor calibration and a tilt compensated compass. I much prefer the simplicity and increased accuracy of the compass code.

Hi jremington, Thanks for the link, it looks great! I wish I found this before. I'll take some time to look through the code and also give it a shot.

The gyro does nothing but add noise, when the sensor is used as a compass. The gyro is useful primarily for correcting the accelerometer measurements (that is, the estimated direction of "down") when the sensor is rotating.

I agree, but magnetometers are influenced by external factors. However, I think for my purposes, I won't have any external factor issues. Also, my purpose would not have fast and sudden movements.

magnetometers are influenced by external factors

Which can only be corrected by calibration in place, certainly not by the gyro.

I have now made a test in a train ride comparing the Madgwick filter from the tilt-compensated heading and also took the longs and lats from the iPhone GPS and converted it to heading.

The best test is to compare the headings from your device with those obtained using a high quality magnetic compass, for reasonably large number of orientations around a complete 360 degree circle.

I admit that I did not think of using a high quality magnetometer. I will see if I can borrow one.

The idea of this test was to compare the 2 methods. The train ride had accelerations and tilts in all directions. I just zoomed into a portion of the results. The results from the other times look similar.

My opinion is that, while looking at this result, your assumption is correct. The tilt-compensation has a better result.

Therefore I would like to ask:

  1. In your opinion, would you say that this experiment is sufficient to give this conclusion?
  2. Is this due to the gyro drifting?

Since the gyro makes no contribution toward absolute heading, it can never help, and the iron containing components and power wiring of the train will distort the Earth's magnetic field.

Did you calibrate the sensor in the train?

No, I did not calibrate it in the train. I did an initial calibration at home. My 10-DOF module is soldered with other components on a Eurocard circuit board in a large casing. That's why i did not feel that it was necessary to re-calibrate the sensor.

the iron containing components and power wiring of the train will distort the Earth's magnetic field.

I did not think about the distortion factors that the train can cause. So yeah, I should have re-calibrated it.
However, I sat on the upper deck and the results show that the headings aren't too bad. I also forgot to correct for the Magnetic Declination of 3°E we have here. But I can do that in MATLAB.

I made sure that box could not move while in the train and the x-axis was always facing the direction the train was heading.

I will do another test with my car.

Here is the sort of testing I had in mind: Adafruit customer service forums • View topic - BNO055 Possible use in +/- 1.0 deg pointing system.

I will do another test with my car.

Auto bodies severely distort the Earth's magnetic field. That is why they can be so easily detected by pickup loops in the pavement.

Here is the sort of testing I had in mind: Adafruit customer service forums • View topic - BNO055 Possible use in +/- 1.0 deg pointing system.

I will then try to do a test like that.

Auto bodies severely distort the Earth's magnetic field.

That's surprising. A lot of general aviation aircraft have a "Glass Cockpit" which means that they have a digital heading indicator. e.g. Garmin G1000, Dynon Skyview, Aspen Evolution 1000, etc... I know that a car is not the same as an airplane, but I assume that if auto bodies cause severe distortions, then an Aircraft should too.

My issue is that, For my Bachelor Thesis, I need a "relatively good" heading indicator for data logging on a GA aircraft (That's only a "small" part of the project). This will only be used for data research and will not be observed while flying the A/C, so safety is not an issue here. I could also do the test on an Aircraft, but with Covid, it has become very difficult for me to rent an A/C.

Which can only be corrected by calibration in place, certainly not by the gyro.

Last time you mentioned this. If I did an initial calibration of the magnetometer in the vehicle, should that be sufficient?

In general, magnetometers (and ordinary magnetic needle compasses) must be calibrated in the environment where they will be used.

So, of course your aircraft magnetometers and compasses will have offset adjustments. Here is an overview of FAA requirements in the U.S.

Thank you. I have roughly read through the important bits. That's something I can definitely add to the thesis.

From what I have understood:

  1. I could calibrate the magnetometer using the elliptical fitting method at "roughly" where the box will be installed.
  2. Install the Box.
  3. Then the best idea would be to follow one of the compass swing methods, namely I prefer the one with the compass correction worksheet.
  4. Do a test with the electrical system on and do a test with them off.
  5. I would then compare this to see if the results are the same (maybe there won't be any distortion from the electrical system)
  6. Implement this in the program.