dpyeates / react-native-attitude

Provides Attitude (Roll, Pitch & Heading) in degrees for both iOS and Android.
MIT License
12 stars 6 forks source link

Heading for setRotation('right') not correct #3

Open nkulic opened 3 years ago

nkulic commented 3 years ago

When rotating device to landscape (right) side it seems that heading value is not correct. It appears that it jumps by ~20 degrees when in landscape right position, while landscape left position is correct.

@dpyeates Can you check if you have same results on your side?

I have tried Attitude.reset() on rotation change but it didn't changed my results.


  const handleOrientation = (orientation: string) => {
    if (orientation === 'LANDSCAPE-LEFT') {
      Attitude.setRotation('left');
      Attitude.reset();
    } else if (orientation === 'LANDSCAPE-RIGHT') {
      Attitude.setRotation('right');
      Attitude.reset();
    } else {
      Attitude.setRotation('none');
      Attitude.reset();
    }
  };
dpyeates commented 3 years ago

You don't need to call reset in this instance - reset is used after a call to zero.

I've just checked on my Samsung S8 test device and it seems to be working.

Can you try calibrating your compass for me? Basically to do this you need to hold the device and move it around in a figure of 8 for about a minute.

Then give it another try please?

nkulic commented 3 years ago

@dpyeates I have done more testing and it appears it does work. But sometimes it acts weird, perhaps because i'm connected to pc with usb and it probably conflicts with other magnets nearby. I will test it outside soon as well. It works slightly different than google maps i've noticed (not an issue for me), for example when you're in portrait mode, with google maps you're able to rotate forward or backwards by 360° and it would still point to same direction. Same if you're in landscape. While with attitude library heading gets messed up when device is in portrait and gets to a point where it's rotated by +90° forward/backward

Also not an issue but I wonder can you detect accuracy of sensors in order to be able to preform calibration?

dpyeates commented 3 years ago

I've just pushed quite a considerable update. Please try version 2.1.0

nkulic commented 3 years ago

I've just pushed quite a considerable update. Please try version 2.1.0

In a new version I have noticed when you hold device in portrait or landscape mode. and if you tilt it forward or backwards by for example 40°, heading would jump by 20° and then return to correct degree. It would be nice if it is consistent, perhaps a low pass filter could help there? I'm just guessing because I do not know exactly how google maps heading beam works.

(also when device is rotated more than 90° forward or backwards, heading still gets messed up)

dpyeates commented 3 years ago

OK, try version 2.1.1 :-) I have tweaked how heading is calculated on Android.

Google maps automatically rotates the display and adjusts heading accordingly. I have deliberately not done this as for my application it needs to stay in the same orientation.

nkulic commented 3 years ago

@dpyeates Sorry for a late reply. I have been working on android and it appeared to work fine no matter which orientation device is positioned (left ,right, portrait). And having Attitude.setRotation does not change anything in android. But that does not matter because heading works well for android. But recently I started working on IOS, and i've noticed Attitude.setRotation(); does not change heading value, but it does change roll value as expected:

Could this be changed so that heading value changed based on device orientation position? Or it can be made same as on android, whenever phone is rotated to left, right or portrait position, heading value remains the same.

ORIENTATION     LANDSCAPE-LEFT
HEADING         283.2732849121094
HEADING         281.37896728515625

ORIENTATION     PORTRAIT
HEADING         2.1712067127227783
HEADING         4.7780680656433105

ORIENTATION     LANDSCAPE-RIGHT
HEADING         89.44868469238281
HEADING         90.5740966796875

----------------------------------------

ORIENTATION     LANDSCAPE-LEFT
ROLL            1.1514811544565
ROLL            2.37896728515625

ORIENTATION     PORTRAIT
ROLL            -1.1712067127227783
ROLL            -4.7780680656433105

ORIENTATION     LANDSCAPE-RIGHT
ROLL            0.44868469238281
ROLL            4.5740966796875

Code:

  const handleOrientation = (orientation: string) => {
    console.log(orientation);
    if (orientation === 'LANDSCAPE-LEFT') {
      Attitude.setRotation('left');
    } else if (orientation === 'LANDSCAPE-RIGHT') {
      Attitude.setRotation('right');
    } else {
      Attitude.setRotation('none');
    }
  };

  useEffect(() => {
    Orientation.addDeviceOrientationListener(handleOrientation);

    return () => {
      Orientation.removeOrientationListener(handleOrientation);
    };
  }, []);

   useFocusEffect(
    useCallback(() => {
      (async function () {
        setIsSupported(await Attitude.isSupported());
      })();

      if (isSupported) {
        Attitude.watch((payload) => {
          console.log(payload.heading);
        });
      }

      return () => {
        Attitude.stopObserving();
      };
    }, [isSupported]),
  );