Compensating accelerometer with the compass - the limitations

9 replies [Last post]
paller's picture
Offline
Joined: 11 Apr 2010
Points: 189
+1
0

In the previous parts of our sensor processing series, we have seen how to use the gyroscope to separate the motion and the gravity acceleration components measured by the accelerometer. But gyroscope-equipped phones are rare. Can we have a "poor man's gyroscope" that is more widely deployed in today's devices?

Let's reiterate the problem. We have an accelerometer that is exposed to two main kinds of accelerations: gravity and motion acceleration. Gravity acceleration is always present. Sadly, its direction is not constant but changes with the direction of the device. If the device is subject to motion acceleration (e.g. the user is walking), motion acceleration is added to the gravity component. Motion acceleration vector changes dynamically due to the movement phases. If we have to assume that motion acceleration is present but we don't know the gravity acceleration vector, separating the gravity and motion acceleration components is impossible. We need another sensor therefore and we made a good use of the gyroscope so far.

The other evident sensor that can be used to determine the Earth's coordinate system relative to the device coordinate system is the compass or magnetic sensor. Compass is attractive because it is widely available in today's devices. Unfortunately it has certain drawbacks that make it more inconvenient to support the accelerometer than gyroscope. In this part I will go through the characteristics of the compass to set the expectations before we start to discuss, how to use it together with the accelerometer.

First the basics. The compass measures the magnetic field the device is exposed to. This magnetic field has many components. We are most interested in the Earth's magnetic field but obviously there are other disturbances, e.g. metal objects, magnets, magnetic fields generated by electric devices. The Earth's magnetic field is not trivial either. Contrary to a belief I hear often, the Earth's magnetic field points mostly downward, toward the center of the Earth. The effect is called magnetic inclination and it means that the Earth's magnetic field has a varying degree relative to the Earth's surface. Where I live, the inclination is  about 70 degrees. The component that compasses measure to find the "North" (more exactly: magnetic North) is just the x and y components of the magnetic field which are smaller than the z component.

External magnetic fields can make measuring the Earth's magnetic field virtually impossible. The graph below shows an extreme case when I started compass sampling and walked into an underground train that eventually left the station.

The x axis is the sample count (proportional to time), the y axis is the length of the magnetic field vector measured by the compass. You can see that around 1900 sample count the metal body of the train starts to show its effect. Then around 2400 the electrical traction motors of the train start to operate which generates at its peak 6 times larger magnetic field than the Earth's magnetic field. This magnetic field also varies in time as the power of the motors fall after the train is accelerated to its cruising speed. First rule therefore is to avoid large electric devices, particularly those that vary their power in time.

The compass also has characteristics that make life harder. The two diagrams below show two measurements. In each measurement the device was rotated around its y axis a full circle (see the SensorEvent documentation about device axes in Android) then it was rotated 90 degrees around its x axis and rotated fully around the y axis again. The two measurements were different only in their location. Both were performed in average rooms, relatively far from metal objects (as much as it is possible in average rooms and buildings).

We expect to see neat circles centered around the origo. What we see is that the circles are not centered around the origo but have characteristic offsets. Worse, those offsets are not the same even though the measurements were done with exactly the same device.


In order to support the accelerometer with the compass, we have to swallow the following two limitations.

  • No magnetic fields around that vary in time (typically electric devices, trains, etc.)
  • The compass has to be calibrated at each location (by rotating the device around). This pretty much excludes measurements on the move (e.g. walking).

If these limitations are acceptable, however, then we have a reference sensor that can replace the accelerometer while the device is subject to motion acceleration and we can extract motion acceleration. We can implement those Wii-like games even if there is no gyroscope in the phone.

I tell you how in the next part.

andrew8yang's picture
Offline
Joined: 14 Aug 2012
Points: 10
how you draw the 2D graphics

hello , gaborpaller

there was a question which I want to ask and which is puzzling me ?

the 2D graphics , you use sage to draw it , right ? if it is , could you shared this script to us , i want to show it on my computer and see more what's the sage do. looking forward to your reply, thanks very much.

paller's picture
Offline
Joined: 11 Apr 2010
Points: 189
Hi, Andrew, I shared a number

Hi, Andrew,
I shared a number of Sage scripts in these article series, e.g. there is one attached to this article. Relevant excerpts from the script:

import sage.plot.line
...

def plot2lists(list1,list2):
tuple1=zip(range(len(list1)),list1)
tuple2=zip(range(len(list2)),list2)
p1=sage.plot.line.line(tuple1,rgbcolor=(1,0,0))
p2=sage.plot.line.line(tuple2,rgbcolor=(0,0,1))
return p1+p2
This draws data series contained in list1 and list2 in the same coordinate system.

andrew8yang's picture
Offline
Joined: 14 Aug 2012
Points: 10
Hi Gabor there was some problem

Hi , Gabor

i use your script , but it's not work fine , i know it is my problem . i use it like this

def process_capture():
loadphonedata("capture.csv")
plot2lists(accelvector , accelvector) //the list is same , just for test
return

according to the error log , it is happened in line p1=sage.plot.line.line(tuple1,rgbcolor=(1,0,0)),
i think the tuple1 is not right for the sage.plot.line.line function. c why the tuple1 is not right ? have you been met it before? could you give me some advise please?
thanks a lot.

536 #Add the options specified by @options to the signature of the wrapped

/home/freecomm/sage/sage-5.2-linux-32bit-ubuntu_12.04_lts-i686-Linux/local/lib/python2.7/site-packages/sage/plot/line.pyc in line2d(points, **options)
442 if points == []:
443 return Graphics()
--> 444 xdata, ydata = xydata_from_point_list(points)
445 g = Graphics()
446 g._set_extra_kwds(Graphics._extract_kwds_for_show(options))

/home/freecomm/sage/sage-5.2-linux-32bit-ubuntu_12.04_lts-i686-Linux/local/lib/python2.7/site-packages/sage/plot/plot.pyc in xydata_from_point_list(points)
560
561 xdata = [float(z[0]) for z in points]
--> 562 ydata = [float(z[1]) for z in points]
563
564 return xdata, ydata

andrew8yang's picture
Offline
Joined: 14 Aug 2012
Points: 10
good news

hi Gabor
i have shown the 2D graphics right. that's made me so excited . haha , but i also want to know if you have some special way use the function you write in script? such as the plot2lists(list1 , list2) function. thanks.

paller's picture
Offline
Joined: 11 Apr 2010
Points: 189
I don't fully understand your

I don't fully understand your question. plot2list is an utility function that came handy when examining the algorithm's performance, that's why I left it in the script. It takes two list arguments and creates a 2D graph showing the number series of both lists. Does this answer your question?

andrew8yang's picture
Offline
Joined: 14 Aug 2012
Points: 10
yes , thanks for your help.

yes , thanks for your help.

andrew8yang's picture
Offline
Joined: 14 Aug 2012
Points: 10
thanks to you shared , so

thanks to you shared , so kind of you.

kukulu's picture
Offline
Joined: 2 Mar 2012
Points: 8
Great article

Can't wait for your next article about the implementation details :) btw if you need some example environment for your demonstration you could integrate your implementation details in an Augmented reality framework like DroidAR (for Android). I am working with it and the only thing you would have to do is to write your own sensor action (http://code.google.com/p/droidar/source/browse/trunk/droidar/DroidAR/src... and http://code.google.com/p/droidar/source/browse/trunk/droidar/DroidAR/src... are example actions for sensor processing)

paller's picture
Offline
Joined: 11 Apr 2010
Points: 189
AR

I watched the DroidAR video and it seems to me that it uses the sensor only for gravity vector measurements. Is that correct? What movements are you interested in?
The next post is still going to be somewhat theoretical, I plan to contribute the example application in the post after.