- My account
Stabilizing compass with the accelerometer
Sun, 2012-04-01 20:47
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.
Tue, 2013-01-08 22:27#1
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.
Tue, 2013-01-08 22:15#2
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.
Wed, 2013-01-09 06:28#3
Worked for me as well
All worked just fine for me in my Mac.
Tue, 2013-01-08 15:03#4
stabilizedcompass.zip - empty when uncompressed
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
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.