Tilt-compensated compass - at my wits' end

I'm a bit at my wits' end here - I'm trying to build a tilt compensated compass for my autonomous sailboat (ardusailor!). I'm using an InvenSense MPU9150. Originally, I used the built-in fusion support on the sensor to get a quaternion, pull the yaw/pitch/roll angles from that, and then use this formula to do the tilt compensation:

float heading = atan2(-(mz * s_phi - my * c_phi), mx * c_theta + my * s_theta * s_phi + mz * s_theta * c_phi);

where the various s_angle is sin(angle) and c_angle is cos(angle). That didn't work. I tried using a vector-based approach stolen from here. That didn't work. Then, I took away the tilt compensation, and just did an uncompensated atan2(Yh,Xh), and that produced very strange result as well.

Basically, as I rotate the sensor about the z axis, the value rotates between 70 and -10 degrees, completing a full circle (i.e. as i make a 360 degree rotation, it starts at 70, gets to -10, and then back up to 70). 70 is at about 0* magnetic, 10 is at about 180, 0 is at about 70-80.

I see the same behavior from an HMC5883L magnetometer chip as well. The thing is, looking at raw values, I get magnetic values that seem fine, and hard and soft iron offsets are in place:

top row is corrected for offsets (using an ellipsoid fit method), bottom is raw. The numbers may look skewed, but they aren't - the scales aren't all the same. Graphs are, in order, x:y, y:z, x:z

What could this be?

The problem you have run into, is the calibration of the magnetic field sensor axes.

If you consider all of the possible orientations of the device, the set of possible magnetic field vectors should appear like a sphere, where the vector has constant magnitude of 600 uT or whatever it has at your location.

They don't.

It is possible, by experiment, to determine an offset and scaling factor for each axis of the magnetic sensor device, to bring the vector output of the device to a more or less uniform magnitude, before you combine it with the orientation vector to obtain the trace of the magnetic field vector in this horizontal place, which represents the conventional magnetic heading. The actual magnetic field vector has a large vertical component, which is usually ignored for conventional wayfinding purposes.

An offset and scale multiplier for each axis of the magnetic sensor is usually sufficient to make the device usable. There may be other irregularities in the sensor response, which can be worked around with some more complication.

top row is corrected for offsets (using an ellipsoid fit method), bottom is raw.

I am not understanding what you mean here. The top row look more elliptical ? They should look more circular?

The description you have, of rotating the device about the z axis, is a bit unclear.

Do you realise that the actual magnetic field vector is at an angle, spring up out of the ground at a steep diagonal ?

If I put my magnetic sensor flat on the table and rotate it horizontally in a circle through 360, degrees, and look at the 3-d direction of the magnetic field vector, that is what I would expect to see. The magnetic field vector shooting up out of the table at a 65% angle from the horizontal and aiming for the magnetic north, but upwards.

If I draw a line on the tabletop which is normal to both the vertical line and the magnetic field vector ( this line will point east and west ), and then draw a circle in the plane formed by the magnetic field vector and the east-west line on the table, and then consider all the points on the edge of that ciricle, and then consider the inclination of each of those points with respect to the centre of the desk, then those inclination angles are going to vary between +65 degrees when I consider the point on the circle aligned with the north, round to -25 degrees when I consider the point on the circle aligned with the south.

This is what you are seeing.

From the accelerometer, you can get a vector which points either up or down. You don't need "fusion" for this, but you do need to distinguish the gravitational acceleration from actual ( movement ) acceleration.

If you calculate the cross product of the up or down vector, and the magnetic field vector, this vector will always point horizontally, and either due east or west magnetic , regardless of the orientation of your device. You can then take the cross product of this vector with the up or down vector to determine the horizontal direction of due north or south. You don't need to determine the actual orientation of your device to do this.

Yeah, as I think about it, it actually makes sense - thanks. I know the magnetic vector is at some angle based on my distance from the north pole, just didn't put 2 and 2 together.

Yes, the top row in the chart is offset and scaled by a value obtained from Yury Petrov's ellipsoid fit method. It looks stretched out because matlab is scaling the axes weirdly.

The very simple vector based approach described by Pololu here Pololu - LSM303DLH 3D Compass and Accelerometer Carrier with Voltage Regulators works well with my DLH303 sensor, after careful calibration following the procedure described here: Sailboat Instruments: Improved magnetometer calibration (Part 1)

Probably the best overview of tilt compensation in general is this application note from Freescale http://cache.freescale.com/files/sensors/doc/app_note/AN4248.pdf

If you calculate the cross product of the up or down vector, and the magnetic field vector, this vector will always point horizontally, and either due east or west magnetic , regardless of the orientation of your device.

This method won't work at the locations where the magnetic field vector points straight up or down. The vector cross product will be zero. That's either the north magnetic pole or the north geomagnetic pole - I forget which. They are both in artic canada and I have no plans to take my robots there.

If you want to be pedantic, no compass is useful in a location where the magnetic field is perpendicular to the Earth's surface. But you would most likely know where you are.

i think if my 3' sail boat is at a spot where the magnetic field is parallel to G, i've got bigger issues to worry about :slight_smile:

I hear that at the North geomagnetic pole, in the summer the mosquitoes are truly awful.