The scetch example is supposed to show the degrees from 0-180 and -180-0. The compass seems to work fine when it is heading to the rest points of the horizon (south,east,west) but when it reaches near North (close to 0 degrees) the compass goes crazy.

some of the values im getting from the compass near 0 degrees:

-63
0
0
0
-60
11
-53
-49
0
-60
8
0
0
11
0
-49
0
0
0
-49

The compass works from -180 degrees to -50 aproximately and then if goes crazy with the above values.
It then gives stable values of the heading after +50 degrees.

What can be wrong? Another guy who used the same compass and code said it worked with no problems.
Could electric devices in the room affect the compass? Speakers, tv etc. and if so why the problem occurs near 0 degrees?

Post Edited (blankk) : 10/13/2009 10:29:43 PM GMT

Comments

1) We can't help you with your Arduino code. Parallax provides support for the use of their products with their own microcontrollers. Although Parallax makes their products so they should work with other microcontrollers, each microcontroller and its software is unique. What applies to one may not apply to another and there are too many different microcontrollers to support them all.

2) There could be something wrong with the Arduino code given that you get problems near a quadrant change (near 0 degrees).

3) The compass is easily affected (like any compass) by nearby magnetic devices like speakers and TVs which all have very strong magnets in them.

4) If you post your source code as a message attachment·and a schematic or detailed description of how you have things connected, there may be someone with some Arduino experience who may be able to help.· You need to be patient and keep in mind that you may get better help using some of the Arduino support forums.

Another guy from the arduino forums, using an arduino board and the same compass said the code worked fine from 0-180 and -180 to 0 degrees.
Could this really be a code issue? The problem occurs only near "North". In every other quadrant (90, 180, -90) the compass works fine.
Could the compass be affected from the electric devices in a distance of 6-7 meters? I've tested it in my balcony and got the same results. Still if theres some kind of magnetic interference how come the compass works in every other quadrant except "North". isn't that odd?

From what i've read in the documentation --> http://www.parallax.com/Portals/0/Downloads/docs/prod/compshop/HM55BModDocs.pdf
theres some kind of calibration that need to be done for the compass in order to work correctly. The calibration will be done by running 2 programs that come with the compass.
The problem is that the code is writen in the language for the parallax microcontrollers and i need to convert it to c++ for my arduinoBT board. Thats where i'll need your help guys. I'm not familiar with the basic stamp language, but from a first look it doesnt seem hard to understand. I'll come back later when i'll have some questions. I first need to understand how the calibration must be done.

The "calibration" process is used to improve the accuracy. It's not necessary for the compass to work correctly.

The compass produces an analog voltage proportional to the magnetic field strength along the N-S and E-W axis. This is measured by an 11-bit analog to digital converter and supplied to the microcontroller. To get an angle, the microcontroller has to compute the arctangent of the N-S and E-W values. The Stamps are limited in how well they can do this and the "calibration" is intended to improve it. I assume that the Arduino routines are more accurate than the Stamp's and the calibration isn't necessary.

Near North, the E-W (x) axis should see a field near zero and the N-S (y) axis should see a field near positive maximum (+1023). The angle would be atan(-y/x). If this isn't computed correctly, it would be very unstable around x = 0 showing large changes with any small field strength variation, exactly what you're seeing.

As Mike points out... "It's not necessary for the compass to work correctly" if the Compass is not calibrated.

The Compass, whether read by a Stamp or another micro, will exhibit non-linearities in the magnetic field as it's rotated. This can be due to geography or variations in the sensor itself. The calibration routine attempts to correct this error through linear interpolation.

Thanks for making it clear Mike.
I'm using the compass as the project I work on is an autonomous robot guiding it self using the parallax GPS module, so I need to use the heading given by the compass in order to guide my vehicle through the waypoints. Accuracy is vital to me.

I got these values posted above yesterday, with my compass mounted on my vehicle including lots of wires on a breadboard, motors, battery which all might affect it. I tested the compass today with 30cms distance from my vehicle and the gap of the values got smaller. Yesterday the values were stable till -50 then after a small rotation i got the values on my first post. Then the next stable value was around 30. Today the compass gave me stable values from -180 to -10 then they got unstable around 0 and the next stable value was +10.

I'll do some more tests tomorrow with bigger distance between the compass and the devices that could generate magnetic fields. I'll take a closer look at the code too, to see if all these things that you Mike say are done properly.

my compass still has this variation near North (0 degrees). I really need to fix this error
heres the c++ code i use to get my values, which does all the calculations Mike pointed out, if anyone can help me out. Im in a dead end

Your compass will always have a variation around North. It's inherent to compasses that measure the magnetic field strength along two orthogonal axes and convert that to a compass direction. You can improve it by averaging a series of readings or detecting the case where one axis is near maximum absolute value and averaging the other axis or detecting that the other axis is unstable (changing sign often).

What happens if you take a rare earth magnet and rotate it about 5 inches away from the compass? Does it still show problems near what it's reporting as North? The problem I don't think is in X transitioning on Zero near North, because it is the Y that actually transitions at North and South. The X axis will transition at East and West.

That said, so what's this mean? I think that the atan2 c++ function that you are using is just fine. The big thing that happens near North is the Deg transition from 0 to 360 deg this "wrap around" can cause big headaches in software if not handled properly. Another thing is that the RAW X and Y values that are returned are relatively small. Even though the HM55B Compass is 11 bit, there is plenty of overhead for detecting stronger magnetic fields. With the Earth's magnetic force I get +/- XY numbers with a magnitude of about 50. With a rare earth magnet about 5 inches away those numbers increase to about 100.

Since the numbers are relatively small, it doesn't take much for the atan2 function to have large swings as a result. Especially when converting RADians to DEGegree your scaling any error up by 57.295...

A couple of things that you could try.

Mike mentions averaging, this is good, but do this on the RAW X and Y values. You might also try scaling these numbers up so that the atan2 function has a larger number to deal with.

Another thought... some atan functions don't handle negative numbers well, you could try detecting the sign first and then applying the absolute values to the atan2 function so that it only deals with positive numbers. Then based on the sign you can reconstruct the quadrant the angle needs to be in.

It's been months and I've been trying to solve the rest problems i was facing with my project. Here I am, stuck with the compass again.
I have a totaly different problem now. The digital compass has big offset in every angle. I've printed the full circle of the documentation file, found the north with a common magnetic compass and wrote down the readings from HM55B. I've attached a text file with those readings.

I've tried to calibrate my compass by adding the offset i measured in every one of the 16 segments. I actually took the readings with the compass and the microcontroller off my vehicle. I measured again the readings and the calibration seemed ok. Then I mounted the micro and the compass on my vehicle and puff... there went my calibration. I know this can be reasonable but I have placed the compass well away from the motors, cables and enything that could create a magnetic field on my vehicle. I also don't see any reaction in the magnetic compass when im placing it next to the digital compass while it is mounted on the vehicle. here's my vehicle: http://img.photobucket.com/albums/v415/techblank/Image013.jpg

What could be wrong? I've read a 5years old thread here in the forums, with a guy that had the same problem and eventually gave up on the HM55B. Could the compass be permanently damaged or something?

The HM55B module can't fail with the kind of fixed offset that your data shows. It's just not built that way. The actual compass chip would have to be mounted 30 degrees off in the module or the module would have to be mounted 30 degrees off axis. There must be something wrong with your routines.

A device like the HM55B generally either works as expected or doesn't work at all. From a hardware standpoint, what you're reporting doesn't make sense. Again, most likely there is something wrong with your code.

If I were trying to troubleshoot something like this, I'd want to write a program to list the raw data (x and y) at maybe 32 or 64 positions around the compass. I would write a new program from scratch to do so. I would manually check the listed data against what I'd expect to see. If I had another microcontroller (like a Parallax Stamp), I might try with that one as well.

What happens if you take a rare earth magnet and rotate it about 5 inches away from the compass? Does it still show problems near what it's reporting as North? The problem I don't think is in X transitioning on Zero near North, because it is the Y that actually transitions at North and South. The X axis will transition at East and West.

That said, so what's this mean? I think that the atan2 c++ function that you are using is just fine. The big thing that happens near North is the Deg transition from 0 to 360 deg this "wrap around" can cause big headaches in software if not handled properly. Another thing is that the RAW X and Y values that are returned are relatively small. Even though the HM55B Compass is 11 bit, there is plenty of overhead for detecting stronger magnetic fields. With the Earth's magnetic force I get +/- XY numbers with a magnitude of about 50. With a rare earth magnet about 5 inches away those numbers increase to about 100.

Same happens with my compass. When I place the magnetic next to the digital one, the numbers increase about 50-60 degrees.

I've checked the code again... I think its all done properly and its the exact same code i've used months back when I was reading the maximum of y-axis +1023. The only difference is that I use another vehicle now.

Could the compass be permanently damaged?

Chris Savage (Parallax) said...
Don,

Please note that putting a strong magnetic field near the compass would likely permanently damage it (Affect it's accuracy in a BIG way)! =(