Stabilizing compass with the accelerometer

6 replies [Last post]
paller's picture
Offline
Joined: 11 Apr 2010
Points: 195
0
0

Let's explore sensor fusion possibilities of the compass and the accelerometer a bit further. In the previous post we used the compass to calculate the gravity vector when the accelerometer is subject to motion acceleration too. Now let's support the compass with the accelerometer and create a stabilized compass.

As we discussed before, the Earth's magnetic field is a 3D vector which has two components: the horizontal element that points to toward the magnetic North (this is what we use for compass) and the magnetic inclination that has variable degree but points mostly down on the Northern Hemisphere. Now if the magnetic sensor is not parallel to the Earth's surface, the z component (pointing downward) of the Earth's magnetic vector is projected into the x and y axes of the phone's magnetic sensor causing the compass to rotate when the device is tilted. Try for example this great and popular compass application. Point the compass toward the North then tilt the device left and right. You will see that the compass rotates even though the longer axis of the device (the y axis in Android API) still points toward the North Pole.

This is a major problem in normal compasses that have only magnetic sensor, that's why complex mechanical systems are used to keep the compass horizontal e.g. on ships. Fortunately our Android phones have accelerometer beside the magnetic sensor and we can use that accelerometer to figure out of the device's tilt. Note that this time we assume that we don't have motion acceleration present. If we have and the phone is also equipped with gyroscope then we can additionally compensate the motion acceleration with the gyroscope as we discussed previously. This time, however, we play it simple and we assume that the accelerometer measures only the gravity acceleration and points toward the center of the Earth.

The example program is attached to the post. You have to be logged in to Sfonge site to access it.

The application is a variation of our previous example where we used the compass to compensate the motion acceleration. The compass calibration logic is the same. After the calibration the data is visualized differently. The red arrow points toward the North as calculated from the x and y components of the magnetic sensor and that's what most of the compass applications out there measures. The white arrow shows the North by the tilt-compensated compass. As the magnetic sensor and the accelerometer measures in the same coordinate system (the device's coordinate system), both vectors are subject to the same roll and pitch. The tilt-compensation algorithm calculates the rotation axis and angle between the gravity vector as measured by the accelerometer and the z (downward) axis. Then it rotates the magnetic vector measured by the magnetic sensor by the same axis and angle.

 

Attached file: 
vrman369's picture
Offline
Joined: 13 Mar 2014
Points: 6
UnZip on Windows 7

I likewise was unable to unzip with Explorer on Windows 7. I was able to successfully unzip the same file on Windows 7 with 7-zip 9.20.

tanery's picture
Offline
Joined: 7 Jan 2013
Points: 8
Unzip Problem was window's 7 unzip

I successfully unzipped the file on my MAC and Linux machines. For some reason the windows 7 and 8 unzip (from explorer right-click menu) failed say file had problems.

Thanks for the quick response.

Tom

tanery's picture
Offline
Joined: 7 Jan 2013
Points: 8
yes it is very wierd

Yes, I am logged onto sfonge before I down load.

Tried again and it's still empty. I'm using the standard windows-7 explorer "unzip" from the right click menu extract all. When I do I get the following popup window:

Title: Compressed (zipped) Folders Error
Windows cannot compltee the extraction
The Compressed (zipped) Folder 'C:Users\Tom|...\stabilizedcompass.zip' is invalid.

I did try to unzip it on my Linux development system and was successful. So this has to be a problem with the windows unzip implementation.

Saila Laitinen's picture
Offline
Joined: 11 Apr 2010
Points: 106
Worked for me as well

All worked just fine for me in my Mac.

tanery's picture
Offline
Joined: 7 Jan 2013
Points: 8
stabilizedcompass.zip - empty when uncompressed

Gabor,

Thank you for excellent series of articles. I'm trying to follow them through, building the examples so I understand the nuances you discuss. However upon downloading the stabilizedcompass.zip file

link: http://www.sfonge.com/sites/default/files/stabilizedcompass.zip

I see a size of 62.8KB, but when opened the zip is empty.

I'd really appreciate it if you could re-post the sample.

Tom

paller's picture
Offline
Joined: 11 Apr 2010
Points: 195
Weird, it works for me either

Weird, it works for me either using the link at the end of the post or your link. Were you logged in to the Sfonge site when you tried to download?