Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QT Compass/Magnetometer/Accelerometer
Forum Updated to NodeBB v4.3 + New Features

QT Compass/Magnetometer/Accelerometer

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 3 Posters 679 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BTSTOnline
    wrote on last edited by
    #1

    Ok... LG V50 running Android 9.

    Been working on an app that needs use of the compass.

    First issue was that the compass was always returning bad readings at a very high rate (around 100hz). I set up a timer to grab the readings every 300-500ms.. which helped stabilize the readings somewhat... and then added in code to force the returned values to a 360 degree range... no problems... -ish.

    Now... Why does the compass readings float so much. I'm getting values ranging +/- 30 degrees randomly when the phone is sitting still on my desk (away from other magnetic devices).

    I then moved to try using the magnetometer and accelerometer to get a good bearing... again, that works OK, except for the same issue... Still getting +/- 30 degrees, randomly.

    Ideas?

    M 1 Reply Last reply
    0
    • B BTSTOnline

      Ok... LG V50 running Android 9.

      Been working on an app that needs use of the compass.

      First issue was that the compass was always returning bad readings at a very high rate (around 100hz). I set up a timer to grab the readings every 300-500ms.. which helped stabilize the readings somewhat... and then added in code to force the returned values to a 360 degree range... no problems... -ish.

      Now... Why does the compass readings float so much. I'm getting values ranging +/- 30 degrees randomly when the phone is sitting still on my desk (away from other magnetic devices).

      I then moved to try using the magnetometer and accelerometer to get a good bearing... again, that works OK, except for the same issue... Still getting +/- 30 degrees, randomly.

      Ideas?

      M Offline
      M Offline
      mvuori
      wrote on last edited by
      #2

      Just one idea: Qt seems to have broken sensor related things in 5.12.4, and I wouldn't be surprised if they had broken the compass too. So if you use later Qt, I'd suggest trying version 5.12.3.

      (Showing code is also always a good idea, as everybody makes silly mistakes they don't see themselves.)

      1 Reply Last reply
      2
      • B Offline
        B Offline
        BTSTOnline
        wrote on last edited by
        #3

        Compiling with 5.12.3 instead of 5.12.4 helps...
        Marking this as Solved.

        1 Reply Last reply
        1
        • B Offline
          B Offline
          BTSTOnline
          wrote on last edited by BTSTOnline
          #4

          Reopened this...
          My Compass code:

          mapCompass = new QCompass(this);
          mapCompass->setDataRate(100);
          connect(mapCompass,SIGNAL(readingChanged()), this, SLOT(compassReadingChanged()));
          

          And the slot that processes the output:

          void Functionsqt::compassReadingChanged() {
              QCompassReading *myReading = mapCompass->reading();
              double myAngle = myReading->azimuth();
              if( myAngle < 0.0 ) {
                 myAngle = myAngle + 360.0;
              }
          
             azArray.append(myAngle);
          
              if( azArray.count() == 100 ) {
                  float tAz = 0;
                  for( int i = 0; i< 100; i++ ) {
                      tAz = tAz+azArray[i];
                  }
                  tAz = tAz / 100.0;
                  myCurrentAz = QString::number( tAz, 'f', 1).toFloat();
          
                  qDebug() << "new AZ: " << tAz;
          
                  azArray.clear();
              }
              myCurrentCal = QString::number( myReading->calibrationLevel(), 'f', 1).toFloat();
          }
          

          And the output from the qDebug():

          new AZ:  221.512
          new AZ:  220.078
          new AZ:  221.048
          new AZ:  227.811
          new AZ:  223.733
          new AZ:  196.193
          new AZ:  192.071
          new AZ:  213.953
          new AZ:  208.428
          new AZ:  208.155
          new AZ:  208.688
          new AZ:  208.456
          new AZ:  209.089
          new AZ:  210.445
          new AZ:  210.164
          new AZ:  210.134
          new AZ:  209.201
          new AZ:  208.077
          new AZ:  208.99
          new AZ:  209.358
          new AZ:  210.152
          new AZ:  209.229
          new AZ:  209.664
          new AZ:  221.767
          new AZ:  221.634
          

          Trying to 'smooth' the data via averaging... Problem is, with or without the averaging, the 'floating' of the data happens... and this is with the phone laying down, not moving. As you can see, this dataset ranges from 221 to 192...

          I've also tried various data rates (1hz, 10hz, 50hz... ) with the same results.

          I get the same results anywhere: plugged into my laptop, out in the yard, in the car, etc. Always 'floating' 30+ degrees.

          Any ideas why?

          --Sam

          Pablo J. RoginaP 1 Reply Last reply
          0
          • B BTSTOnline

            Reopened this...
            My Compass code:

            mapCompass = new QCompass(this);
            mapCompass->setDataRate(100);
            connect(mapCompass,SIGNAL(readingChanged()), this, SLOT(compassReadingChanged()));
            

            And the slot that processes the output:

            void Functionsqt::compassReadingChanged() {
                QCompassReading *myReading = mapCompass->reading();
                double myAngle = myReading->azimuth();
                if( myAngle < 0.0 ) {
                   myAngle = myAngle + 360.0;
                }
            
               azArray.append(myAngle);
            
                if( azArray.count() == 100 ) {
                    float tAz = 0;
                    for( int i = 0; i< 100; i++ ) {
                        tAz = tAz+azArray[i];
                    }
                    tAz = tAz / 100.0;
                    myCurrentAz = QString::number( tAz, 'f', 1).toFloat();
            
                    qDebug() << "new AZ: " << tAz;
            
                    azArray.clear();
                }
                myCurrentCal = QString::number( myReading->calibrationLevel(), 'f', 1).toFloat();
            }
            

            And the output from the qDebug():

            new AZ:  221.512
            new AZ:  220.078
            new AZ:  221.048
            new AZ:  227.811
            new AZ:  223.733
            new AZ:  196.193
            new AZ:  192.071
            new AZ:  213.953
            new AZ:  208.428
            new AZ:  208.155
            new AZ:  208.688
            new AZ:  208.456
            new AZ:  209.089
            new AZ:  210.445
            new AZ:  210.164
            new AZ:  210.134
            new AZ:  209.201
            new AZ:  208.077
            new AZ:  208.99
            new AZ:  209.358
            new AZ:  210.152
            new AZ:  209.229
            new AZ:  209.664
            new AZ:  221.767
            new AZ:  221.634
            

            Trying to 'smooth' the data via averaging... Problem is, with or without the averaging, the 'floating' of the data happens... and this is with the phone laying down, not moving. As you can see, this dataset ranges from 221 to 192...

            I've also tried various data rates (1hz, 10hz, 50hz... ) with the same results.

            I get the same results anywhere: plugged into my laptop, out in the yard, in the car, etc. Always 'floating' 30+ degrees.

            Any ideas why?

            --Sam

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @BTSTOnline said in QT Compass/Magnetometer/Accelerometer:

            Any ideas why?

            Could it be the sensor sensitivity or precision and not Qt framework?
            Is it possible you run another application to validate you don't have such "floating" range, in order to rule out hardware or software?

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BTSTOnline
              wrote on last edited by
              #6

              Yes, I have tried other compass apps on my 'Droid and none exhibit this behavior.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved