• 0

CMPS2 compass module with Arduino - Heading steps accuracy



I'm using the CMPS2 compass module with an Arduino Uno and the example code from https://reference.digilentinc.com/reference/pmod/pmodcmps2/start#example_projects to get a heading from the compass module, but it seems to be only jumping between specific values and never taking the values in between. For example, running the program while keeping the module horizontal and slowly spinnng it 360° around the z-axis gives an output like this:

Heading = 231.18°    
Heading = 231.19°    
Heading = 186.09°    
Heading = 141.09°    
Heading = 186.12°    
Heading = 231.10°    
Heading = 231.09°    
Heading = 231.08°    
Heading = 231.09°    
Heading = 231.03°    
Heading = 230.96°    
Heading = 321.25°    
Heading = 321.15°    
Heading = 321.04°    
Heading = 51.20°    
Heading = 51.06°    
Heading = 51.06°    
Heading = 50.96°    
Heading = 50.91°    
Heading = 96.12°    
Heading = 96.10°    
Heading = 51.00°    
Heading = 141.17°    
Heading = 141.11°    
Heading = 141.12°    
Heading = 141.04°    
Heading = 141.04°    
Heading = 186.11°    
Heading = 141.01°    
Heading = 231.16°    
Heading = 231.16°    
Heading = 231.16°    

Basically, plotting the heading as a function of time doesn't give a continuos plot no matter how slowly the module rotates.

I have tried to check for disturbances from metal objects or electronic devices using another compass, so I'm pretty sure that's not the problem.

I don't need to get down to the 1° accuracy promised by the maker (5° is probably enough for my project), but these giant leaps from 320° to 50° won't work.

All help is appreciated!

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 1

Hello @Erosand,

I do not know when I will have time to fix the library (or I guess the single file) but I identified a few weird things that are likely resulting in problems.

  • The CMPS2_init() sets the global maximum variable to -32768, and the global minimum variable to 32767. This mismatch causes problems later. Global midpoint is set to 0.
  • The CMPS2_read_XYZ() inside of the CMPS2_getHeading() then checks to see if the measured X and Y data (after already converting it into mG, so they aren't even the same units) is greater than the global maximum variable and the less than the global minimum variable. Because of bullet point 1, this (mostly likely) sets the global max and min values to be exactly the same as the measurement value. The global midpoint is set to the average of these two values, so exactly the same value.
  • In the CMPS2_getHeading() a Reset function is performed (to get an opposite phase shift I guess) after bullet point 2 and then CMPS_read_XYZ is performed again. However, because the global max and min values have not been reset, the new measurement data is getting compared to the exact same data. I guess in theory if you continually repeated this process as the module was rotated around, you would get the max and min values for the X and Y axis, but this isn't done in the code. So the max or minimum may have changed slightly, but not both, and the global midpoint is the bifurcation of this potential small change. I might be misunderstanding the code though.
  • In the CMPS2_getHeading() after bullet point 3, it says offset is eliminated between the two measurements, but all it does is assign the calculated offset to a temporary variable, component[i].
  • In the CMPS2_getHeading() after bullet point 4, it then uses these calculated offsets, rather than actual values, to compare to the global midpoint, in order to determine what quadrant and thus what equation should be used to determine the corresponding degree heading.

I'll ask around to see if somebody will be able to address some of these changes.


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now