plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

MagnetometerEvent to compass values

Open JoergPf opened this issue 3 years ago • 2 comments

Hi, I'm desperately searching for an example on how to get the 0-360 compass values out of the MagnetometerEvent. Any hint is very appreciated. Thanks Joerg

JoergPf avatar Jun 10 '22 16:06 JoergPf

We can do this-

magnetometerEvents.listen((MagnetometerEvent event) 
{
      double D = 360 - atan2(event.x, event.y) * (180 / pi);
      if (D < 0) {
        D += 360;
      }
      print(D);
    });
  }

For reference - https://arduino.stackexchange.com/questions/18625/converting-three-axis-magnetometer-to-degrees

You can also use this package - https://pub.dev/packages/flutter_compass

Mukund-Tandon avatar Jun 17 '22 02:06 Mukund-Tandon

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Sep 16 '22 00:09 github-actions[bot]

The code snippet in https://github.com/fluttercommunity/plus_plugins/issues/903#issuecomment-1158424761 is wrong. I've tried it and it returned 400+ degrees.

  • Why?: atan2(event.x, event.y) * (180 / pi) could return negative numbers, and 360 - negative_number is more than 360. For example, if -45 degrees is returned, 360--45 is 405.

I would highly recommend using https://pub.dev/packages/flutter_compass or writing your own plugin to use the OS APIs to get the data you need directly, instead of using the magnetometer readings (unit: micro Teslas), to allow you to use the operating-system calculated heading, which could use multiple sensors (sensor fusion) to give improved accuracy based on the hardware, as well as compensate for the location (the magnetometer only allows you to calculate magnetic north, not true north).

ben-xD avatar Oct 25 '22 09:10 ben-xD

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Jan 24 '23 00:01 github-actions[bot]