Calculating a compass angle

compasselectromagnetismsensor

I am getting data from compass( hmC5883L) and trying to detect angle.

Well, i dont really care about offsets, i just want to check that if its flat on the table and i make a 90 degrees round, i will get numbers x-> 90+x

Instead for 90 degrees turn i get values like that :
(starting from 13 degrees)

13->180, jump right to 0, 0->180, 180->13

code to manipulate data :

                float gScale = .92;  // Scale factor for +1.3Ga setting

                float adjx = x - xOffset; //set to 0 for testing
                float adjy = y - yOffset;//set to 0 for testing

                xs = adjx * gScale; 
                ys = adjy * gScale;
                zs = z * gScale;    

                float heading = atan2(ys, xs);
                heading += declination / 1000; // Declination for geo area

                   if (heading < 0);
                     heading += 2*PI;

                   if (heading > 2*PI)
                     heading -= 2*PI;

                  float angle = heading * 180/M_PI;

What am i doing wrong ?

Best Answer

            float gScale = .92;  // Scale factor for +1.3Ga setting

            float adjx = x - xOffset; //set to 0 for testing
            float adjy = y - yOffset;//set to 0 for testing

            xs = adjx * gScale; 
            ys = adjy * gScale;
            zs = z * gScale;    

            float heading = atan2(ys, xs);
           if (heading < 0)
            heading += 2*PI; // translate result atan2() from [-PI,PI] to [0, 2*PI]  

            heading += declination / 1000; // Declination for geo area
            if (heading < 0) heading += 2*PI;;

            if (heading > 2*PI) heading -= 2*PI;

            float angle = heading * 180/PI;
Related Topic