
Originally Posted by
vladestCh
first of all, magnetometer as well as compass have to be calibrated, otherwise it produces white noise
second, I think for such thing we need gyroscope instead of magnetometer
Your advice helped at least some steps forward. Calibration changed the x, y, z -values to make at least some sense. I used this program (partly copied from these pages):
Code:
from sensor import *
import e32, time
class DemoApp():
def __init__(self):
self.sobj = MagnetometerXYZAxisData()
self.sobj.set_callback(data_callback=self.my_callback)
self.counter = 0
self.xs = 0
self.ys = 0
self.zs = 0
def my_callback(self):
if self.counter % 10 == 0:
print "X:%s, Y:%s, Z:%s, c:%s" % (self.sobj.x,
self.sobj.y, self.sobj.z, self.sobj.calib_level)
#print "Timestamp:", self.sobj.timestamp # works
self.counter = self.counter + 1
self.xs += self.sobj.x
self.ys += self.sobj.y
self.zs += self.sobj.z
def run(self):
self.sobj.start_listening()
if __name__ == '__main__':
d = DemoApp()
d.run()
e32.ao_sleep(30) # 30 seconds
d.sobj.stop_listening()
print d.xs / d.counter, d.ys / d.counter, d.zs / d.counter
print "Normal exit"
During calibration the phone was rotated e.g. along a Moebius strip. The output showed how the variable calib_level changed from 0 to 1 to 2 and finally to 3, within
some seconds. Thus the calibration was much simpler than I thought
.
After calibration I restarted the program so that the phone was still in a constant orientation and position during the program run. I took note about the averages of x, y, and z.
I restarted again, now z-axis of the phone exactly in the opposite direction compared to the previous test run. One would suppose that now the z-axis would be about the same magnitude as earlier but with the opposite sign. The test gave about the expected answers, but the the variation of the numbers was fairly large, ten or even tens of percent. The uncalibrated values were total garbage.
In repeated tests the calib_level went back to 0 after 2-4 tests typically. (during one test the phone was still). This makes the sensor unusable in my intended bike application: it is not practical to start waving the phone in every minute! Any ideas?
By the way, the gyroscope idea might be good. I did not know that there are already Nokia phones with a gyroscope.