Hi,

I was wondering if anyone knew how to calibrate the compass when used with the ArduIMU. I saw Bill Premerlani's code for the UAV DevBoard, it is very interesting and it would be really cool to calibrate the compass in flight by sending a signal, but it's not quite the same with the ArduIMU in the arduino code that is on google code I think.
Any idea?

Thank you in advance,

Christophe

Views: 790

Reply to This

Replies to This Discussion

Christphe,

I've implemented the HMC 5843 scale factor adjustment and bias adjusments to the Sparkfun Razor IMU code. Should port to the arduIMU fairly easily. I just need to figure out how to post the code.....

John
Didn't even see that that "Upload Files" link on the bottom of the page, will upload the code when I get home this evening.

John
Hi John,

Thank you very much, I can't wait to see this :) I think it won't be too hard to move the code from the Razor 9DoF to the ArduIMU.
I've been reading some articles on compass calibration it seemed a bit complex to code!

Thanks again,

Christophe
Here's my code. I changed the formatting somewhat so I could read it easier. The matrix multiply routine and matrix storage method has been changed too. This was to accomodate matrix mulitplys in various dimensions. This also forced changes in the vector library.

Not the cleanest code, I've been away from C programming for a while, but I believe it's working as intended.
Attachments:
Hi John,

Thank you, I'll take a look at your code today :)

Christophe
John,

I'm looking at your code and I have a few questions:

- Are you calibrating the magnetometer bias at each start of the IMU? It seems so, I thought this only had to be done from time to time. Can you tell me why it is done everytime? What does it do?

- Do you have to calculate the offset in the loop everytime too?

So from what I understand, you are using the DCM to calibrate the Hard Iron at each loop using the magoffsetcal function.
The thing I don't understand is: aren't we suppose to turn the IMU 360° and calculate the min and max in order to correct the Soft Iron? Is the Soft Iron corrected in your code then? If yes, how?

Thank you for your help, I really appreciate it!

Christophe
Christophe,

When the magnetomter is initialized, it is run thru a self test as desribed in the HMC5843 datasheet. This should output a specific set of numbers. The difference between these readings and their theoretical value (715 the way I set up the magnetometer) is the scale factor that must be applied to every set of magnetometer readings. This is done once at the beginning of each power up sequence.

The bias calculation is done every frame. I can't recall all the terms at the moment, but I believe this is referred to as the soft iron correction. The reason for doing this every frame is that the magnetic environment can vary with the varying loads on the motor/battery system, for example. This alleviates the need to do the mag cal swing you referred too.

If I understand everything correctly, this is the whole point of the algorithm described in Bill P's post., and what I tried to implement in this code.

John
John,

From what I understand from Bill P's post, this method should have a good heading even when the motors turn, but when I test this by putting a magnet near the IMU the magnetometer and the yaw both drift a lot! It does this when I put the magnet less than a second next to the IMU or when I leave it for a few seconds next to the IMU. This isn't good because I need the heading or the yaw to be correct at all times, so I'll try to program the IMU so that everytime the magnetometer goes crazy because of the motors (or the magnet for my tests) the DCM doesn't correct the yaw with the magnetometer, even if it implies a little drift for some seconds. When the magnetometer comes back to normal (when I stop playing with the magnet) then the yaw drift is corrected with the magnetometer again.
so when the magnet is near, I only rely on the gyros, when the magnet is far I correct the gyros with the magnetometer.

I'll keep you informed of my researched.

thank you very much for your help :)


Christophe
Hi Christophe,

The algorithm that I posted for removing offsets works best if the offsets do not change in time. In that case, the offsets can be removed automatically in flight. There is a short "learning period". Each time the airframe rotates in any direction, some of the offset is removed. Eventually, all of the offset will be totally removed. But, during the learning period, there will be an error that will gradually reduce. The amount of error, and the time it takes to remove it, will depend on how large the offsets were in the first place.

So, in one of the situations described in this thread where a very large magnet was brought near the magnetometer, it would take several turns to compensate, because the offsets would be very large.

Similarly, if the magnetometer were near some current carrying wires, there would be temporary shifts when the current came on, and when it went off.

So, the best way to use the algorithm is to also use some engineering common sense when deciding where to mount the magnetometer. Don't mount it close to strong magnetic fields on purpose. Keep it far enough away so that the residual offsets seen by the magnetometer can be removed after a turn or two.

In my situation, the offsets seen at the magnetometer were around 10 to 20% of the earth's magnetic field, and were removed in a couple of turns.

Keep in mind that the technique that I posted can only compensate for magnetic fields that are fixed in the aircraft frame of reference. There is not much you can do about magnetic interference that is fixed in the earth frame of reference.

Best regards,
Bill
John and Christophe,

One more thought on the subject of magnetometer calibration...the algorithm that I published is for offset removal only, it is not for soft-iron compensation. If you are using the algorithm for offset removal, it will not compensate for soft-iron. The two effects are two different things.

When I got my first set of readings from my magnetometer, I realized there was an offset, even if I kept the magnetometer away from all sources of magnetic interference. It became apparent that even in free space, the output of each axis of the magnetometer was related to the magnetic field by a gain and an offset. In other words:

output = gain*input + offset

I am not sure where the offset came from, maybe from the electronics, but it was definitely there. There was a different offset for each axis. And the offsets were different for the three magnetometers that I purchased. One of them was much worse than the other 2, with offsets on the order of 20%.

So, I was looking for a simple process for computing the gains and offsets for the 3 axes of the magnetometer.

The gain part was easy, since the magnetometer will make the measurements you need to compute the gain.

But what about the offsets? I could not ignore them. Even if I was careful to mount the magnetometer away from magnetic interference, the offsets would remain.

So, I came up with an algorithm for computing the offsets on the fly.

But I should caution you, you still need to keep the magnetometer away from "soft-iron".

The reason is, the effect of nearby iron is not the same as an offset. The amount that the iron is magnetized depends on its shape and orientation. So, you will wind up with an interfering magnetic field that is not constant, it will vary with the orientation of your aircraft.

So, the bottom line is that the offset-removal algorithm takes care of magnetometer offsets only. It does not take care of soft-iron, so you still need to be careful to keep the magnetometer away from soft-iron, otherwise you will need a different calibration procedure than the one that I described.

Best regards,
Bill

Digging this up in a search for info on soft-iron magnetometer distortion.

I gather that it is theoretically possible to calibrate for soft iron distortion... it's just not easy to do from what I can tell. Per Freescale app note. If only I were more math-savvy maybe I'd have figured this out by now. :D

John and Christophe,

One more comment about the algorithm that I described:

It relies on the assumption that the gyros, the magnetometer, and the magnetic interference physically move together in lock step. That way the algorithm can distinguish between the earth's magnetic field and the magnetic interference. From the point of view of the moving frame (the aircraft) the earth's magnetic field will rotate, while the interference will be stationary.

There is a filter built into the algorithm, so the cancellation is gradual. Cancellation continues until the total magnetic field is seen to rotate in way that is consistent with the gyro information.

If you tried to run the algorithm in a situation in which the three elements were not all attached to the same physical frame of reference, it would not work. So, for example, it would not work if you held a large magnet in your hand near the magnetometer, and rotated the magnetometer. In that situation, the magnet would be equivalent to an error in the earth's magnetic field.

If you mounted a large magnet on the aircraft, the algorithm would eventually cancel it.

Best regards,
Bill

RSS

Contests

Season Two of the Trust Time Trial (T3) Contest has now begun. The third round was a reliablilty/aerial photography round for both planes and copters, which is now closed. Stay tuned for the next round, beginning soon.

A list of all T3 contests is here

 

© 2012   Created by Chris Anderson.

Badges  |  Report an Issue  |  Terms of Service