1
\$\begingroup\$

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 ?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ you have to remember that the compass is possibly drifting constantly, and you haven't shown us where you get the data in - which could be wrong too \$\endgroup\$ – KyranF Dec 1 '15 at 18:15
  • \$\begingroup\$ thanks, what do you mean where did i get the data in ? i am using Wire library with arduino and read the data as usual, i think the technical details are not relevant because i do get everything to work, only calculate wrong. if you look at it, it seems that my angles are going 0-180 always, and not 0-360, although i do zeroing it every 2pai \$\endgroup\$ – Curnelious Dec 1 '15 at 18:17
  • \$\begingroup\$ Look for atan2() it gives [-pi,pi] \$\endgroup\$ – Marko Buršič Dec 1 '15 at 19:35
  • 1
    \$\begingroup\$ PI and M_PI why are you using both? \$\endgroup\$ – Jasen Dec 1 '15 at 19:56
  • 1
    \$\begingroup\$ @MarkoBuršič he is using Atan2 \$\endgroup\$ – KyranF Dec 1 '15 at 20:13
1
\$\begingroup\$
            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;
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Unless you actually need the magnetic field strength in mG, you can omit gScale and the multiplication. Mathematically, the heading will be the same. \$\endgroup\$ – Edward Dec 4 '15 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.