[There is now a better method for removing offsets in flight. See this discussion. - WJP]

The picture shows a UAV DevBoard connected to Jordi's magnetometer breakout board.

First, since my discussions tend to get long winded, if you want to "cut to the chase", you might want to read this.

I recently integrated Jordi's HMC5843 magnetometer breakout board with the UAV DevBoard (UDB).

Jordi's board is really great, having the onboard bidirectional level shifters and builtin pullup resistors made it possible to simply connect Jordi's board directly to the I2C pins in the programming connector. Thank you very much, Jordi.

So, heli and quadcopter pilots who are using the UDB would do well to buy Jordi's board, my plan is to integrate it into the heli firmware that is being developed for the UDB. There is now a magnetometer demo and instructions for the UDB.

But that is all besides the point of this discussion....

Along the way to implementing the magnetometer demo, I realized the hardest part was going to be determining the offsets of the magnetometer. The integration of the magnetometer itself into the DCM algorithm was trivial, but what to do about the offsets?

The HMC5843 has a self calibration feature, so you can determine the gains just fine. But the first magnetometer I bought had rather large offsets. So I bought 3 more (1 more from Jordi, and 2 more from Sparkfun). It turned out that all 4 units had some offsets. Even if they did not have offsets, it would still be possible for stray magnetic fields in the aircraft to create offsets.

So, some sort of procedure is needed to null the offsets.

I thought of several manual ways to do that, and I read several postings on the subject....

Then I wondered if there might be some way to use the direction cosine matrix to automatically compute the offsets in flight. And there is....even for dynamically changing offsets, such as from power leads to the motors...

The basic idea is the following...

When the aircraft is rotating, the offset fields rotate with the aircraft, while the earth's magnetic field is stationary. So it should be possible to use the direction cosines to separate the two fields.

The basis of the idea is the same as flipping the magnetometer exactly 180 degrees along one of the axis and averaging the two readings. The result is the offset.

The idea can be extended to rotations other than 180 degrees, even small rotations, by using the direction cosine matrix and some vector algebra.

The resulting theory and implementation is described here, and it has been tested. It works rather well.

It was interesting to watch the computations of the offsets during the tests. A few random turns along a couple of the axes was all that was needed to compute the offsets. So, you can either null the offsets prior to takeoff by rotating your aircraft a bit, or simply let it happen automatically during the first few turns of the flight.

It was also interesting to see what would happen if I deliberately manually set the offsets to the wrong values, they would quickly reset to the correct values after a few turns.

The bottom line is that now both the calibration and the nulling of offsets can be done completely automatically, without any manual operations or entry of measurements. It is possible to extend any DCM-based IMU, such as the ArduIMU, to use this method to simplify the integration of a magnetometer.

Best regards,

Views: 8485

Reply to This

Replies to This Discussion

Ok let me see if I do understand it right.
I dont need the MOSFETs to change the voltage level on the SDA and SCK pins right?

I only need to use pull up resistors of 5KR to the 3.3V line such as in the figure above.

Then the SDA and SCL pins would be connected to the dsPIC30F pins that will use the 3.3V as logic level 1 am I right?
Yes , I am using the same level converter for my data radio with similar problem.
here is the power supply that can be used
I use this one to supply 3.3v for the data radio
I plan to use the same setup for HMC 5843 too

Thank you very much, Morli.


Whether you need the level shifters or not depends on what you are connecting the SparkFun board to. If you are connecting it to the UAV DevBoard, you do not need the level shifters, because the SDA and SCL lines on the dsPIC30F4011 will work at 3.3 volts just fine.

If you are connecting to something else, it depends on whether or not it will work at 3.3 volts. If it will, then everything is fine. If not, then you will need the level shifters.

In any case, be sure to tie the pullup resistors to 3.3 volts if you are going to use the SparkFun board.

Best regards,
My pleasure Bill.
@ Carlito
yes that is correct to my knowledge . I haven't tested it yet as magnetometer is still on the way.
I will use the level shifter and 3.3v PS as mentioned earlier , I will keep you informed when it happens :-), meanwhile if you get across then pls keep us informed of the progress too. thanks
Hello Bill,

Thanks very much for the answer, I am going to connect the sparkfun magnetometer to the UDB with the electronic modifications u said and try the magnetometer test code.

This code is going to be very usefull for me cause I plan to use another PIC30F4011 in parallel to control the airspeed of the plane using the MPXV5004DP differential pressure sensor with a pitot tube and a current sensor.

The airspeed control will be performed using both pressure sensor and current sensor, with the airspeed feedback generating a setpoint for the current control.

This second controller will be a slave of the UDB2 so they can share those new sensors data.

The reason I am adding a second controller is my airframe sensibility for airspeed changes. They make it very hard to tune the controller gains once the servo outputs may be amplified for low speeds and attenuated for high speeds.

I didn't test the wind estimator algorithm in flight yet but using the GPS feedback for the speed control showed unsatisfactory responses as well as the altitude hold that I also plan to use a second barometric sensor in the future as altitude feedback.

Anyway this I2C code will open several possibilities to connect other sensors to the UDB.

When I have some documents about the airspeed control I'll post it.

Thanks again.
No, thank you Carlito
Interesting that you are using second dsPic, Looking forward to your feed backs
You have to be careful calculating the pull-up resistor. Whether you operate at 400 or 100kHz, you have to keep in mind how many devices are attached to the I2C bus and look up their capacitance in the data-sheet.

Then for the clock rising from low to high, you have Rp*(C1 + C2 + ... + Cn) = 1 / 2pi * Clock Frequency.

Normally with 2 or 3 devices, 5K should be fine, but just keep in mind.

Hi Bill,

Why didn't you use the set/reset straps of the magnetometer. You could easily find the offset voltage by a simple algebraic sum of two samples?
Hi Bill,

In your equation (8) you have an "exact formula" for the offset in the plane perpendicular to the rotation
axis. You use instead the variable vector: the numerator of that equation. In equation (11) you update
the offset estimation by adding that vector multiplied by the ratio of the sampling time to the time constant.
I wish to know what is the reason of that?

Hi David,

There are two separate parameters that need to be estimated for each axis of the magnetometer. One of them is the gain for that axis. The other is the offset.

In other words, the transfer function for each axis is:

output_reading = input_magnetic_field*gain + offset

For a given reading, we have output_reading. However, input_magnetic_field, gain, and offset are unknown.

The straps are used to add or subtract a known increment of magnetic field to the unknown total field:

output_reading1 = input_magnetic_field*gain + offset
output_reading2 = (input_magnetic_field+strap_field)*gain + offset

Therefore, gain = (output_reading2 - output_reading1)/strap_field

The magnetometer documentation was clear on how the straps are used to determine the gains using the above equations, but was very vague about the offsets.

The calibration is done automatically by the magnetometer. It uses the straps to add or subtract a magnetic field to the ambient magnetic field, and computes the gain from the change in the measurement.

The ambient magnetic field is unknown, so as far as I can see there is no way to use the straps to cancel it out to discover the offset. If you average the two readings with and without the strap current, you cannot get the offset from it, because the average will include the earth's magnetic field. If you subtract the two readings (and that is what the magnetometer does to calibrate itself) both the offset and the earth's magnetic field cancel out, and you are left with the gain.

So, I don't think it is possible to use the straps to determine the offset. But I am not 100% sure of that, because I could not find any information about computing offsets in the documentation for the HMC5843. If you are aware of documentation relating to computing offsets in the HMC5843, I would be very interested in reading it. I would appreciate that very much.

Best regards,
Hi Dave,

Equation 8 is exact, but there are three problems with it.

1. It only gives us the component of the magnetic offset vector that is perpendicular to the axis of rotation, so it does not tell us anything about the component that is parallel to the axis of rotation.

2. If the rotation is small, you get into a numerical problem of dividing zero by zero, and you will get a spurious result.

3. There is noise in the measurements.

We solve all three problems by using equation 11 instead of equation 8.

Problems 1 and 3 are solved, because equation 11 is a filtering equation that combines many data points.

Problem 2 is solved by recognizing that we can apply weights to the data points. In particular, the smart thing to do is to multiply each data point by a weight that just happens to be the denominator of equation 8. That accomplishes two things. It simplifies the implementation, preventing any possibility of division by zero. Also, it gives greater weight to those data points that involve a larger rotation. And that is good, because the accuracy of those points is greater than that of the points with a smaller rotation.

Best regards,
Hi Bill,
Please see the document : AN212 "Hnadling Sensor Bridge Offset" page. 5

The method is simple. Suppose that the ambient magnetic field is -127mG.

Now if you apply a set pulse to S/R strap you will have a reading of: -127mG + offset

Now if you apply a reset pulse to the S/R strap the reading will be :+127mG + offset

As you see the ambient magnetic field has changed sign after the reset. This a property of AMR sensors.

Therefore offset = ( reading1 + reading2 ) / 2 .

Best regards

Reply to Discussion


© 2019   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service