Compass give me crazy data, is calibration needed or it's the sensor broken?

15,397

Solution 1

I've worked quite a lot with these electronic compasses on mobile phones and its quite possible that there is nothing wrong with your code or sensor.

Instead it could very well be a problem with your environment. There are magnetic fields interfering with the earth's magnetic fields all the time. From electrical equipment interference to the metal structure holding up a building. At the end of the day a compass is just a magnet. If you stand beside a large lump of metal the compass will be attracted to it and point to it rather than the magnetic north pole.

Try this: Install GPS status then turn off all filtering (settings... gps & sensors...sensor filtering... no filtering). Do the calibration (figure of 8 wavy stuff) and then move the phone around your desk.. near monitors, cables, etc. You'll see it go crazy. The information is completely unreliable. I found in the past that moving the phone a few inches to the right completely changed its reading. The same happens with a real compass. Strictly speaking there is no "problem". The device's compass is assigning itself with the strongest magnetic field. Even the magnetic content of nearby rocks can interfere with the compass.

As a further test I've just placed a real (orienteering) compass over my phone which has a compass app installed. The real compass is now pointing everywhere but magnetic North. The two devices are interfering with each other.

So my advice is.. go somewhere in the open, like a park or field, away from any potential interference and power lines, (if you have one bring a real compass to check that the GPS status app is pointing the right way), and see if your compass works as you'd expect.

Extra: The answer from @resus is also important when calibrating. Rotate the phone a few times in each axis. Looks silly but it does calibrate it properly.

Extra 2: Would it be possible/practical to use the compass bearing of your GPS? It would require that the device be moving (walking speed should be fine) but you would not need to worry about any interference. It should give an accurate reading provided your GPS signal is good.

Extra 3: Another thought just occurred to me.. You could try apply a low pass filter to the sensor. This means that the sudden changes in the sensor reading are filtered out .. have a look at this answer. And if that doesn't do a good job there are lots of algorithms on the web for you to choose from.

Solution 2

If you definitely haven't changed anything in your code, and it still works fine on other devices, it would suggest a problem with that particular device.

While your app is running (i.e. the compass is in use), you should be able to wave it in a figure of 8 in order to automatically recalibrate the compass. You should also make sure you aren't standing next to any large lumps of metal etc. that might interfere with readings.

You can override the onAccuracyChanged() method of SensorEventListener to flash up a message to the user when the compass requires recalibration (probably when accuracy drops to SENSOR_STATUS_ACCURACY_LOW).

In my experience of playing with the compass on android phones, they can be pretty unreliable...

Solution 3

If your application work on another tablet and other compass application do not work on your device, this is probably due to a bad calibration.

As said in the post above, to make the calibration, wave your device in a figure of 8. I just want to add that you should do it for EACH axis. This should fix your problem.

Solution 4

If it is not a calibration error, as some people have already answered, it is possible that the compass had gone through a magnetic field and now it is desmagnetized, so it is not working properly.

Where do you usually keep the tablet? Could it be that it was near big servers or magnets? You should check the compass just in case, talk to to android's tech support.

Hope it helps.

Solution 5

Nowadays I have the luck of developing a robot with an HMC-5983 and an MPU-6050, which can be calibrated by using its libraries with Arduino.

That code is compatible/portable to other uController but for not also so easy for smartphones, I guess that the offsets needed for calibrating the compass, the gyro and the accelerometer are inside some internals of Android, not available in the SDK.

I answered before thinking that maybe calibration was only for some devices, but realized that must be as I said before. So, if playing with robots its possible, I mean it's also easy, but when using an smartphone maybe some custom firmware as CyanogenMod would bring the possibility of investigating the way of setting that offsets, but more important to run some program ported from sketch (following its concept only) to get them first ...

So, good luck ! What is also true, is that in both devices (smartphone and my robot) it's need to move them for them to get working well, as I showed you in the video of latest answer, also helpful on robots.

Good luck and a lot of fun with those things, are very powerful.

Share:
15,397
vgonisanz
Author by

vgonisanz

Victor Goñi received his M.S. degree in Telecommunication Engeneering from the Public University of Navarra, Pamplona, Spain, in 2010. He spent an internship course at the Monterrey Institute of Technology and Higher Education, Mexico, during 2009, as a complement of his Engineer degree. He wrote his final Master Thesis at Vicomtech-IK4, San Sebastian, Spain, under the title of "Peripheral Evaluation for Virtual Environment Control", which received an A grade. Since then, he is working in the 3D Animation and Interactive Virtual Environments Department of Vicomtech-IK4 as a research assistant. He has developed a deep interest in science, programming and scientific research, especially in algorithm efficiency, real-time complex calculation and optimization. He is also interested in graphic engines and sensor data filtering and processing for electronic devices. He is specialized in code developement on mobile platforms. Also he is interested in resurrect tiranosaurios rex. My goal to achieve is to be a developer/architect that writes C++ like musicians compose music and songs.

Updated on June 10, 2022

Comments

  • vgonisanz
    vgonisanz about 2 years

    I'm working with android sensor data. My application use

    SensorManager.getRotationMatrixFromVector(
                mRotationMatrix , event.values);
    

    and it has been working well until this morning, when the rotation matrix started to send a lot of noise data (Change N to W in a second).

    It's not a problem with my code, because on friday was working and no changes have been done. I have used a compass app from the market, and the compass is giving random data.

    I have tested my app on another tablet, and it is working well.

    Does someone know why is this happening? A problem with the sensor? Does it need a calibration?